Skip to content

Commit fbad17f

Browse files
authored
Merge pull request #231 from hydephp/refactor-navlinks
Refactor navigation link components
2 parents a942a62 + ed3740a commit fbad17f

File tree

8 files changed

+111
-17
lines changed

8 files changed

+111
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<a aria-label="Home page link" href="{{ $navigation->getHomeLink($currentPage) }}" class="font-bold px-4">
2-
{{ config('hyde.name', 'HydePHP') }}
1+
<a href="{{ $navigation->getHomeLink() }}" class="font-bold px-4" aria-label="Home page">
2+
{{ config('hyde.name', 'HydePHP') }}
33
</a>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<a href="{{ $item->resolveLink($currentPage) }}" {{$item->isCurrent($page) ? 'aria-current="page"' : '' }}
2-
@class(['block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100'
3-
, 'border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent'=>$item->isCurrent($page)
4-
])>
5-
{{ $item->title }}
6-
</a>
1+
<a href="{{ $item }}" {!! $item->isCurrent() ? 'aria-current="page"' : '' !!} @class([
2+
'block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100',
3+
'border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent' => $item->isCurrent($page)
4+
])>{{ $item->title }}</a>

packages/framework/src/Models/NavItem.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Hyde\Framework\Contracts\PageContract;
66
use Hyde\Framework\Contracts\RouteContract;
7+
use Hyde\Framework\Hyde;
78

89
/**
910
* Abstraction for a navigation menu item.
@@ -89,8 +90,12 @@ public function __toString(): string
8990
/**
9091
* Check if the NavItem instance is the current page.
9192
*/
92-
public function isCurrent(PageContract $current): bool
93+
public function isCurrent(?PageContract $current = null): bool
9394
{
95+
if ($current === null) {
96+
$current = Hyde::currentRoute()->getSourceModel();
97+
}
98+
9499
if (! isset($this->route)) {
95100
return ($current->getRoute()->getRouteKey() === $this->href)
96101
|| ($current->getRoute()->getRouteKey().'.html' === $this->href);

packages/framework/src/Models/NavigationMenu.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hyde\Framework\Models;
44

55
use Hyde\Framework\Contracts\RouteContract;
6+
use Hyde\Framework\Hyde;
67
use Hyde\Framework\Router;
78
use Illuminate\Support\Collection;
89

@@ -20,11 +21,14 @@ public function __construct()
2021
$this->items = new Collection();
2122
}
2223

23-
public static function create(RouteContract $currentRoute): static
24+
public static function create(?RouteContract $currentRoute = null): static
2425
{
25-
return (new self())->setCurrentRoute($currentRoute)->generate()->filter()->sort();
26+
return (new self())->setCurrentRoute($currentRoute ?? Hyde::currentRoute())->generate()->filter()->sort();
2627
}
2728

29+
/**
30+
* @deprecated v0.50.0 - Automatically inferred from the view.
31+
*/
2832
public function setCurrentRoute(RouteContract $currentRoute): self
2933
{
3034
$this->currentRoute = $currentRoute;
@@ -67,7 +71,7 @@ public function sort(): self
6771
return $this;
6872
}
6973

70-
/** @internal */
74+
/** @deprecated v0.50.x - use Route::home() instead */
7175
public function getHomeLink(): string
7276
{
7377
return Route::get('index');

packages/framework/tests/Feature/DarkmodeFeatureTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Hyde\Framework\Helpers\Features;
66
use Hyde\Framework\Models\Pages\DocumentationPage;
7-
use Hyde\Framework\Models\Pages\MarkdownPage;
87
use Hyde\Testing\TestCase;
98
use Illuminate\Support\Facades\Config;
109

@@ -18,7 +17,8 @@ protected function setUp(): void
1817
{
1918
parent::setUp();
2019

21-
view()->share('page', new MarkdownPage([], ''));
20+
$this->mockRoute();
21+
$this->mockPage();
2222
}
2323

2424
public function test_has_darkmode()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Testing\Unit\Views;
4+
5+
use Hyde\Testing\TestCase;
6+
7+
/**
8+
* @see resources/views/components/navigation/navigation-brand.blade.php
9+
*/
10+
class NavigationBrandViewTest extends TestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
$this->mockRoute();
16+
$this->mockPage();
17+
}
18+
19+
protected function render(): string
20+
{
21+
return view('hyde::components.navigation.navigation-brand', [
22+
'navigation' => \Hyde\Framework\Models\NavigationMenu::create(),
23+
])->render();
24+
}
25+
26+
public function test_component_links_to_home_route()
27+
{
28+
$this->assertStringContainsString('href="index.html"', $this->render());
29+
}
30+
31+
public function test_component_uses_site_name()
32+
{
33+
$this->assertStringContainsString('HydePHP', $this->render());
34+
config(['hyde.name' => 'foo']);
35+
$this->assertStringContainsString('foo', $this->render());
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Testing\Unit\Views;
4+
5+
use Hyde\Framework\Models\NavItem;
6+
use Hyde\Framework\Models\Route;
7+
use Hyde\Testing\TestCase;
8+
9+
/**
10+
* @see resources/views/components/navigation/navigation-link.blade.php
11+
*/
12+
class NavigationLinkViewTest extends TestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
$this->mockRoute();
18+
$this->mockPage();
19+
}
20+
21+
protected function render(?NavItem $item = null): string
22+
{
23+
return view('hyde::components.navigation.navigation-link', [
24+
'item' => $item ?? NavItem::toLink('foo.html', 'Foo'),
25+
])->render();
26+
}
27+
28+
public function test_component_links_to_route_destination()
29+
{
30+
$this->assertStringContainsString('href="foo.html"', $this->render());
31+
}
32+
33+
public function test_component_uses_title()
34+
{
35+
$this->assertStringContainsString('Foo', $this->render());
36+
}
37+
38+
public function test_component_is_current_when_current_route_matches()
39+
{
40+
$this->mockRoute(Route::get('index'));
41+
$this->assertStringContainsString('current', $this->render(NavItem::toRoute(Route::get('index'), 'Home')));
42+
}
43+
44+
public function test_component_has_aria_current_when_current_route_matches()
45+
{
46+
$this->mockRoute(Route::get('index'));
47+
$this->assertStringContainsString('aria-current="page"', $this->render(NavItem::toRoute(Route::get('index'), 'Home')));
48+
}
49+
}

tests/TestCase.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyde\Testing;
44

5+
use Hyde\Framework\Contracts\PageContract;
56
use Hyde\Framework\Hyde;
67
use Hyde\Framework\Models\Pages\MarkdownPage;
78
use Hyde\Framework\Models\Route;
@@ -67,9 +68,9 @@ protected function mockRoute(?Route $route = null)
6768
}
6869

6970
/** @internal */
70-
protected function mockPage()
71+
protected function mockPage(?PageContract $page = null, ?string $currentPage = null)
7172
{
72-
view()->share('page', new MarkdownPage());
73-
view()->share('currentPage', 'PHPUnit');
73+
view()->share('page', $page ?? new MarkdownPage());
74+
view()->share('currentPage', $currentPage ?? 'PHPUnit');
7475
}
7576
}

0 commit comments

Comments
 (0)