Skip to content

Commit

Permalink
feat: [BREAKING CHANGES] Now working with Livewire v2 too (#8)
Browse files Browse the repository at this point in the history
* fix: Separate files according to namespace and version

* feat: test-route check on links in blade file

* feat: test-route check on sub-menu links
  • Loading branch information
achyutkneupane authored Dec 24, 2023
1 parent ec14218 commit a738763
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 80 deletions.
7 changes: 4 additions & 3 deletions config/dashboard.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php


$stages = [];

return [
/**
* Your product has logo or not
Expand Down Expand Up @@ -103,18 +100,22 @@
'title' => 'Link1',
'route' => 'link1',
'icon' => "M575.8 255.5c0 18-15 32.1-32 32.1h-32l.7 160.2c0 2.7-.2 5.4-.5 8.1V472c0 22.1-17.9 40-40 40H456c-1.1 0-2.2 0-3.3-.1c-1.4 .1-2.8 .1-4.2 .1H416 392c-22.1 0-40-17.9-40-40V448 384c0-17.7-14.3-32-32-32H256c-17.7 0-32 14.3-32 32v64 24c0 22.1-17.9 40-40 40H160 128.1c-1.5 0-3-.1-4.5-.2c-1.2 .1-2.4 .2-3.6 .2H104c-22.1 0-40-17.9-40-40V360c0-.9 0-1.9 .1-2.8V287.6H32c-18 0-32-14-32-32.1c0-9 3-17 10-24L266.4 8c7-7 15-8 22-8s15 2 21 7L564.8 231.5c8 7 12 15 11 24z",
'test-route' => true
], [
'title' => 'Parent Link',
'icon' => "M272 384c9.6-31.9 29.5-59.1 49.2-86.2l0 0c5.2-7.1 10.4-14.2 15.4-21.4c19.8-28.5 31.4-63 31.4-100.3C368 78.8 289.2 0 192 0S16 78.8 16 176c0 37.3 11.6 71.9 31.4 100.3c5 7.2 10.2 14.3 15.4 21.4l0 0c19.8 27.1 39.7 54.4 49.2 86.2H272zM192 512c44.2 0 80-35.8 80-80V416H112v16c0 44.2 35.8 80 80 80zM112 176c0 8.8-7.2 16-16 16s-16-7.2-16-16c0-61.9 50.1-112 112-112c8.8 0 16 7.2 16 16s-7.2 16-16 16c-44.2 0-80 35.8-80 80z",
'test-route' => true,
'submenu' => [
[
'title' => 'Sub link1',
'route' => 'parent.sub1',
'icon' => "M40 48C26.7 48 16 58.7 16 72v48c0 13.3 10.7 24 24 24H88c13.3 0 24-10.7 24-24V72c0-13.3-10.7-24-24-24H40zM192 64c-17.7 0-32 14.3-32 32s14.3 32 32 32H480c17.7 0 32-14.3 32-32s-14.3-32-32-32H192zm0 160c-17.7 0-32 14.3-32 32s14.3 32 32 32H480c17.7 0 32-14.3 32-32s-14.3-32-32-32H192zm0 160c-17.7 0-32 14.3-32 32s14.3 32 32 32H480c17.7 0 32-14.3 32-32s-14.3-32-32-32H192zM16 232v48c0 13.3 10.7 24 24 24H88c13.3 0 24-10.7 24-24V232c0-13.3-10.7-24-24-24H40c-13.3 0-24 10.7-24 24zM40 368c-13.3 0-24 10.7-24 24v48c0 13.3 10.7 24 24 24H88c13.3 0 24-10.7 24-24V392c0-13.3-10.7-24-24-24H40z",
'test-route' => true
], [
'title' => 'Sub link2',
'route' => 'parent.sub2',
'icon' => "M256 80c0-17.7-14.3-32-32-32s-32 14.3-32 32V224H48c-17.7 0-32 14.3-32 32s14.3 32 32 32H192V432c0 17.7 14.3 32 32 32s32-14.3 32-32V288H400c17.7 0 32-14.3 32-32s-14.3-32-32-32H256V80z",
'test-route' => true
]
],
'noMenuRoutes' => [
Expand Down
24 changes: 17 additions & 7 deletions src/DashboardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function boot()
* @method loadViewsFrom
*/
if (method_exists($this, 'loadViewsFrom')) {
$this->loadViewsFrom(__DIR__.'/../views', 'dashboard');
$this->loadViewsFrom(__DIR__.'/views', 'dashboard');
}

/**
* Load routes from the package
*
* @method loadRoutesFrom
*/
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
$this->loadRoutesFrom(__DIR__.'/routes/web.php');

/**
* Publish the config and view files
Expand All @@ -46,16 +46,26 @@ public function boot()
$livewire_version = explode('.', $livewire_version)[0];
$livewire_version = (int)substr($livewire_version, 1);

$path = $livewire_version >= 3 ? 'components/' : '';
$viewPath = $livewire_version >= 3 ? 'components/' : '';
$compPath = $livewire_version >= 3 ? '' : 'Http/';

if($livewire_version >= 3) {
$this->publishes([
__DIR__.'/LiveWire/Sidebar.php' => app_path('Livewire/Components/Sidebar.php'),
__DIR__.'/LiveWire/Navbar.php' => app_path('Livewire/Components/Navbar.php'),
]);
} else {
$this->publishes([
__DIR__.'/LiveWire/Sidebarv2.php' => app_path('Http/Livewire/Components/Sidebar.php'),
__DIR__.'/LiveWire/Navbarv2.php' => app_path('Http/Livewire/Components/Navbar.php'),
]);
}

$this->publishes([
__DIR__.'/../config/dashboard.php' => config_path('dashboard.php'),
__DIR__.'/views/sidebar.blade.php' => resource_path('views/livewire/components/sidebar.blade.php'),
__DIR__.'/views/navbar.blade.php' => resource_path('views/livewire/components/navbar.blade.php'),
__DIR__.'/views/layout.blade.php' => resource_path('views/'.$path.'layouts/app.blade.php'),

__DIR__.'/Livewire/Sidebar.php' => app_path('Livewire/Components/Sidebar.php'),
__DIR__.'/Livewire/Navbar.php' => app_path('Livewire/Components/Navbar.php'),
__DIR__.'/views/layout.blade.php' => resource_path('views/'.$viewPath.'layouts/app.blade.php'),

__DIR__.'/sass/_variables.scss' => resource_path('sass/_variables.scss'),
__DIR__.'/sass/app.scss' => resource_path('sass/app.scss'),
Expand Down
18 changes: 18 additions & 0 deletions src/Livewire/Navbarv2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Livewire\Components;

use Livewire\Component;

class Navbar extends Component
{
public $title;
public function mount($title)
{
$this->title = $title;
}
public function render()
{
return view('livewire.components.navbar');
}
}
13 changes: 13 additions & 0 deletions src/Livewire/Sidebarv2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Livewire\Components;

use Livewire\Component;

class Sidebar extends Component
{
public function render()
{
return view('livewire.components.sidebar');
}
}
159 changes: 89 additions & 70 deletions src/views/sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
$slug = str_replace(' ', '-', $item['title']);
$guards = [];
$hasNoTestRoute = false;
if(array_key_exists('guard', $item)) {
$guards[] = $item['guard'];
}
Expand All @@ -49,86 +50,104 @@
if(array_key_exists('guard', $subItem)) {
$guards[] = $subItem['guard'];
}
if(!array_key_exists('test-route', $subItem) || (array_key_exists('test-route', $subItem) && $subItem['test-route'] === true)) {
$hasNoTestRoute = true;
}
}
}
@endphp
@if((array_key_exists('guard', $item) && auth()->user()->canAny($guards)) || !array_key_exists('guard', $item))
<li class="nav-item py-1">
@if(!array_key_exists('route', $item))
<a class="nav-link @if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) active @endif p-1 px-3 pe-auto user-select-none"
aria-current="page" data-bs-toggle="collapse" href="#{{ $slug }}Collapse" role="button"
aria-expanded="@if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) true @else false @endif"
aria-controls="#{{ $slug }}Collapse">
@else
<a href="{{ route(config('dashboard.prefix').$item['route']) }}"
class="nav-link @if(request()->routeIs(config('dashboard.prefix').$item['route'])) active @endif"
aria-current="page">
@endif

<div class="d-flex align-items-center gap-2">
@if(config('dashboard.icons') == 'svg')
@if(array_key_exists('route', $item))
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600"
fill="@if(request()->routeIs(config('dashboard.prefix').$item['route'])) #252834 @else #ffffff @endif"
style="width: 30px; height: 30px;">
@else
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600"
fill="@if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) #252834 @else white @endif"
style="width: 25px; height: 25px; padding:1px;" class="">
@endif
@php
$canGoFurther = true;
if(array_key_exists('test-route', $item) && !(!array_key_exists('test-route', $item) && config('dashboard.enableTestRoutes') === true) && $item['test-route'] === true && !(array_key_exists('submenu', $item) && !$hasNoTestRoute)) {
$canGoFurther = false;
}
@endphp

<path d="{{ $item['icon'] }}"/>
@if($canGoFurther)
@if((array_key_exists('guard', $item) && auth()->user()->canAny($guards)) || !array_key_exists('guard', $item))
<li class="nav-item py-1">
@if(!array_key_exists('route', $item))
<a class="nav-link @if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) active @endif p-1 px-3 pe-auto user-select-none"
aria-current="page" data-bs-toggle="collapse" href="#{{ $slug }}Collapse" role="button"
aria-expanded="@if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) true @else false @endif"
aria-controls="#{{ $slug }}Collapse">
@else
<a href="{{ route(config('dashboard.prefix').$item['route']) }}"
class="nav-link @if(request()->routeIs(config('dashboard.prefix').$item['route'])) active @endif"
aria-current="page">
@endif

@if(array_key_exists('route', $item))
</svg>
<div class="d-flex align-items-center gap-2">
@if(config('dashboard.icons') == 'svg')
@if(array_key_exists('route', $item))
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600"
fill="@if(request()->routeIs(config('dashboard.prefix').$item['route'])) #252834 @else #ffffff @endif"
style="width: 30px; height: 30px;">
@else
</svg>
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 600 600"
fill="@if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) #252834 @else white @endif"
style="width: 25px; height: 25px; padding:1px;"
class="">
@endif

<path d="{{ $item['icon'] }}"/>

@if(array_key_exists('route', $item))
</svg>
@else
</svg>
@endif
@elseif(config('dashboard.icons') == 'font-awesome')
<i class="fas fa-{{ $item['icon'] }} fa-fw"></i>
@endif
@elseif(config('dashboard.icons') == 'font-awesome')
<i class="fas fa-{{ $item['icon'] }} fa-fw"></i>
@endif
<span class="routeText">{{ $item['title'] }}</span>
</div>
<span class="routeText">{{ $item['title'] }}</span>
</div>

@if(array_key_exists('route', $item))
</a>
@else
</a>
@endif
@if(array_key_exists('route', $item))
</a>
@else
</a>
@endif

@if(array_key_exists('submenu', $item))
<div
class="collapse @if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) show @endif"
id="{{ $slug }}Collapse">
<ul class="d-flex flex-column align-items-end mt-2 pb-0 nav nav-pills">
@foreach($item['submenu'] as $index => $subItem)
@if((array_key_exists('guard', $subItem) && auth()->user()->can($subItem['guard'])) || !array_key_exists('guard', $subItem))
<li class="nav-item" style="width: 90%;">
<a href="{{ route(config('dashboard.prefix').$subItem['route']) }}"
class="nav-link @if(request()->routeIs(config('dashboard.prefix').$subItem['route'])) active @endif p-1 px-2"
aria-current="page">
<div class="d-flex align-items-center gap-2">
@if(config('dashboard.icons') == 'svg')
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 750 750"
fill="@if(request()->routeIs($subItem['route'])) #252834 @else #ffffff @endif"
style="width: 30px; height: 30px;">
<path d="{{ $subItem['icon'] }}"/>
</svg>
@elseif(config('dashboard.icons') == 'font-awesome')
<i class="fas fa-{{ $subItem['icon'] }} fa-fw"></i>
@endif
<span class="routeText">{{ $subItem['title'] }}</span>
</div>
</a>
</li>
@endif
@endforeach
</ul>
</div>
@endif
</li>
<hr class="my-1"/>
@endcan
@if(array_key_exists('submenu', $item))
<div
class="collapse @if(in_array(Route::currentRouteName(), $subRoutes) || in_array(Route::currentRouteName(), $noMenuRoutes)) show @endif"
id="{{ $slug }}Collapse">
<ul class="d-flex flex-column align-items-end mt-2 pb-0 nav nav-pills">
@foreach($item['submenu'] as $index => $subItem)
@if(!array_key_exists('test-route', $subItem) || (array_key_exists('test-route', $subItem) && config('dashboard.enableTestRoutes') === true) || $subItem['test-route'] === false)
@if((array_key_exists('guard', $subItem) && auth()->user()->can($subItem['guard'])) || !array_key_exists('guard', $subItem))
<li class="nav-item" style="width: 90%;">
<a href="{{ route(config('dashboard.prefix').$subItem['route']) }}"
class="nav-link @if(request()->routeIs(config('dashboard.prefix').$subItem['route'])) active @endif p-1 px-2"
aria-current="page">
<div class="d-flex align-items-center gap-2">
@if(config('dashboard.icons') == 'svg')
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 750 750"
fill="@if(request()->routeIs($subItem['route'])) #252834 @else #ffffff @endif"
style="width: 30px; height: 30px;">
<path d="{{ $subItem['icon'] }}"/>
</svg>
@elseif(config('dashboard.icons') == 'font-awesome')
<i class="fas fa-{{ $subItem['icon'] }} fa-fw"></i>
@endif
<span class="routeText">{{ $subItem['title'] }}</span>
</div>
</a>
</li>
@endif
@endif
@endforeach
</ul>
</div>
@endif
</li>
<hr class="my-1"/>
@endcan
@endif
@endforeach
</ul>
<ul class="nav nav-pills flex-column mb-0">
Expand Down

0 comments on commit a738763

Please sign in to comment.