Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Navigation components for improved route handling #40

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions resources/views/components/desktop/desktop-navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<x-slot name="content" x-show="open">
@foreach($item['links'] as $link)
@php
// Check if params exist, otherwise fallback to route
$url = isset($link['params'])
? route($link['route'], $link['params'])
: route($link['route']);
Expand All @@ -40,9 +39,15 @@
$url = isset($link['params'])
? route($link['route'], $link['params'])
: route($link['route']);

$currentPath = '/'.request()->path();
$linkPath = parse_url($url, PHP_URL_PATH) ?? '';
@endphp
<x-navigation::dropdown-link :href="$url"
:active="request()->routeIs($link['route'])">

<x-navigation::dropdown-link
:href="$url"
:active="$currentPath === $linkPath"
>
{{ __($link['name']) }}
</x-navigation::dropdown-link>
@endforeach
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/dropdown-link.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@props(['active'])
@props(['active' => false])

@php
$classes = ($active ?? false)
Expand Down
56 changes: 45 additions & 11 deletions resources/views/components/mobile/mobile-dropdown.blade.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
@props(['active', 'name', 'links', 'bgClass', 'route', 'params' => [], 'active' => null])
@props([
'active' => false,
'name',
'links',
'bgClass',
'route',
'params' => []
])

@php
$classes = ($active)
? "block w-full ps-3 pe-4 py-2 text-start text-base font-medium text-gray-900 hover:text-gray-700 hover:border-prime-400 focus:outline-none focus:text-gray-800 focus:bg-gray-50 hover:bg-slate-200 focus:border-prime-300 transition duration-150 ease-in-out border-l-4 bg-slate-200 border-prime-400"
: "block w-full ps-3 pe-4 py-2 text-start text-base font-medium text-gray-900 hover:text-gray-700 hover:border-prime-400 focus:outline-none focus:text-gray-800 focus:bg-gray-50 hover:bg-slate-200 focus:border-gray-400 transition duration-150 ease-in-out border-l-4 $bgClass";
$dropdownActive = false;

foreach ($links as $link) {
$url = isset($link['params'])
? route($link['route'], $link['params'])
: route($link['route']);

$parsedPath = trim(parse_url($url, PHP_URL_PATH) ?? '', '/');

if (request()->is($parsedPath) || request()->is($parsedPath . '/*')) {
$dropdownActive = true;
break;
}
}

$active = $active || $dropdownActive;

$classes = $active
? "block w-full ps-3 pe-4 py-2 text-start text-base font-medium text-gray-900
hover:text-gray-700 hover:border-prime-400 focus:outline-none focus:text-gray-800
focus:bg-gray-50 hover:bg-slate-200 focus:border-prime-300 transition duration-150
ease-in-out border-l-4 bg-slate-200 border-prime-400"
: "block w-full ps-3 pe-4 py-2 text-start text-base font-medium text-gray-900
hover:text-gray-700 hover:border-prime-400 focus:outline-none focus:text-gray-800
focus:bg-gray-50 hover:bg-slate-200 focus:border-gray-400 transition duration-150
ease-in-out border-l-4 $bgClass";
@endphp

<div x-data="{ dropdownOpen: false }" class="relative w-full">
<a @click="dropdownOpen = !dropdownOpen" @click.outside="dropdownOpen = false; manualToggle = false"
class="w-full text-left flex items-center justify-start cursor-pointer">
<a
@click="dropdownOpen = !dropdownOpen"
@click.outside="dropdownOpen = false;"
class="w-full text-left flex items-center justify-start cursor-pointer"
>
<button {{ $attributes->merge(['class' => $classes]) }}>
{{ $name }}
<x-navigation::dropdown-icon/>
<x-navigation::dropdown-icon />
</button>
</a>

<div x-show="dropdownOpen" x-transition class="w-full lg:mt-2">
@foreach($links as $link)
@php
// If 'params' exist, pass them to route()
$url = isset($link['params'])
? route($link['route'], $link['params'])
: route($link['route']);

$parsedPath = trim(parse_url($url, PHP_URL_PATH) ?? '', '/');
$childActive = request()->is($parsedPath) || request()->is($parsedPath . '/*');
@endphp

<x-navigation::dropdown-link
:href="$url"
:active="request()->routeIs($link['route'])"
:active="$childActive"
>
{{ $link['name'] }}
{{ __($link['name']) }}
</x-navigation::dropdown-link>
@endforeach

</div>
</div>
57 changes: 50 additions & 7 deletions resources/views/components/mobile/mobile-navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,73 @@

<nav>
@foreach(Navigation::getCombinedNavigationItems() as $index => $item)
{{-- If it's a single link --}}

@if ($item['type'] === 'link')
@php
$linkUrl = isset($item['params'])
? route($item['route'], $item['params'])
: route($item['route']);

$parsedPath = trim(parse_url($linkUrl, PHP_URL_PATH) ?? '', '/');

$active = request()->is($parsedPath) || request()->is($parsedPath . '/*');
@endphp

<x-navigation::mobile.mobile-navigation-link
:href="route($item['route'])"
:active="request()->routeIs($item['route'])"
:href="$linkUrl"
:active="$active"
:bgClass="$bgClass($index)"
>
{{ __($item['name']) }}
</x-navigation::mobile.mobile-navigation-link>

{{-- If it's a standard dropdown (with "links") --}}
@elseif($item['type'] === 'dropdown' && array_key_exists('links', $item))
@elseif ($item['type'] === 'dropdown' && array_key_exists('links', $item))
@php
$dropdownActive = false;

foreach ($item['links'] as $link) {
$linkUrl = isset($link['params'])
? route($link['route'], $link['params'])
: route($link['route']);

$parsedPath = trim(parse_url($linkUrl, PHP_URL_PATH) ?? '', '/');

if (request()->is($parsedPath) || request()->is($parsedPath . '/*')) {
$dropdownActive = true;
break;
}
}
@endphp

<x-navigation::mobile.mobile-dropdown
:name="$item['name']"
:links="$item['links']"
:active="Navigation::isDropdownRouteActive($item['links'])"
:active="$dropdownActive"
:bgClass="$bgClass($index)"
/>

{{-- If it's a blog dropdown AND "enabled" is true --}}
@elseif($item['type'] === 'dropdown-blog' && ($item['enabled'] ?? false) && array_key_exists('links', $item))
@php
$dropdownActive = false;

foreach ($item['links'] as $link) {
$linkUrl = isset($link['params'])
? route($link['route'], $link['params'])
: route($link['route']);

$parsedPath = trim(parse_url($linkUrl, PHP_URL_PATH) ?? '', '/');

if (request()->is($parsedPath) || request()->is($parsedPath . '/*')) {
$dropdownActive = true;
break;
}
}
@endphp

<x-navigation::mobile.mobile-dropdown
:name="$item['name']"
:links="$item['links']"
:active="$dropdownActive"
:bgClass="$bgClass($index)"
/>
@endif
Expand Down
33 changes: 9 additions & 24 deletions src/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fuelviews\Navigation;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Navigation
{
Expand All @@ -21,24 +22,24 @@ public function getNavigationItems(): Collection
if (empty($item['links'])) {
$item['links'] = [
[
'name' => 'Blog',
'name' => Str::Title(config('sabhero-blog.dropdown.name')),
'position' => 0,
'route' => 'sabblog.post.index',
'route' => 'sabhero-blog.post.index',
],
[
'name' => 'Categories',
'position' => 2,
'route' => 'sabblog.category.all',
'route' => 'sabhero-blog.category.all',
],
[
'name' => 'Tags',
'position' => 3,
'route' => 'sabblog.tag.all',
'route' => 'sabhero-blog.tag.all',
],
[
'name' => 'Authors',
'position' => 4,
'route' => 'sabblog.author.all',
'route' => 'sabhero-blog.author.all',
],
];
}
Expand Down Expand Up @@ -66,15 +67,15 @@ public function getDynamicNavigationItems(): Collection
foreach ($states as $state) {
$dynamicLinks->push([
'name' => $state->name,
'route' => 'sabblog.post.metro.state.index',
'route' => 'sabhero-blog.post.metro.state.index',
'params' => ['state' => $state->slug.'#'.$state->slug],
'type' => 'state',
]);

foreach ($state->children as $city) {
$dynamicLinks->push([
'name' => $city->name,
'route' => 'sabblog.post.metro.city.index',
'route' => 'sabhero-blog.post.metro.state.city.index',
'params' => [
'state' => $state->slug,
'city' => $city->slug.'#'.$city->slug,
Expand All @@ -91,53 +92,37 @@ public function getCombinedNavigationItems(): \Illuminate\Support\Collection
{
$staticItems = $this->getNavigationItems();

// 1) Locate the 'blog-dropdown'
$blogDropdownKey = $staticItems->search(
fn ($item) => $item['type'] === 'dropdown-blog'
);

if ($blogDropdownKey !== false) {
// 2) Grab the existing blog-dropdown and dynamic links
$blogDropdown = $staticItems[$blogDropdownKey];
$existingLinks = collect($blogDropdown['links']); // existing sub-links
$existingLinks = collect($blogDropdown['links']);
$dynamicLinks = $this->getDynamicNavigationItems();

// Separate states & cities
$states = $dynamicLinks->filter(fn ($link) => $link['type'] === 'state');
$cities = $dynamicLinks->filter(fn ($link) => $link['type'] === 'city');

// 3) Partition the existing links by position=0
// "position=0" is presumably your "Blog" link
[$posZero, $others] = $existingLinks->partition(fn ($link) => ($link['position'] ?? null) === 0);

// 4) Sort $others by ascending position (e.g. 1,2,3,...).
// If some items don't have a position, they go last.
$othersSorted = $others->sortBy(fn ($link) => $link['position'] ?? 999999);

// 5) Rebuild the links:
// - All position=0 items
// - Then all states
// - Then all cities
// - Then everything else
$mergedLinks = $posZero
->concat($states)
->concat($cities)
->concat($othersSorted)
->values();

// 6) Update the 'blog-dropdown' with the new links array
$blogDropdown['links'] = $mergedLinks->map(function ($link) {
// Remove 'type' if you like or keep it
unset($link['type']);

return $link;
})->toArray();

// Put it back into the static items
$staticItems[$blogDropdownKey] = $blogDropdown;
}

// Finally, sort top-level items by their 'position' if needed
return $staticItems->sortBy('position')->values();
}

Expand Down