Skip to content

Commit

Permalink
Merge pull request #231 from hydephp/refactor-navlinks
Browse files Browse the repository at this point in the history
Refactor navigation link components
  • Loading branch information
caendesilva authored Jul 16, 2022
2 parents a942a62 + ed3740a commit fbad17f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<a aria-label="Home page link" href="{{ $navigation->getHomeLink($currentPage) }}" class="font-bold px-4">
{{ config('hyde.name', 'HydePHP') }}
<a href="{{ $navigation->getHomeLink() }}" class="font-bold px-4" aria-label="Home page">
{{ config('hyde.name', 'HydePHP') }}
</a>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<a href="{{ $item->resolveLink($currentPage) }}" {{$item->isCurrent($page) ? 'aria-current="page"' : '' }}
@class(['block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100'
, '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)
])>
{{ $item->title }}
</a>
<a href="{{ $item }}" {!! $item->isCurrent() ? 'aria-current="page"' : '' !!} @class([
'block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100',
'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)
])>{{ $item->title }}</a>
7 changes: 6 additions & 1 deletion packages/framework/src/Models/NavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hyde\Framework\Contracts\PageContract;
use Hyde\Framework\Contracts\RouteContract;
use Hyde\Framework\Hyde;

/**
* Abstraction for a navigation menu item.
Expand Down Expand Up @@ -89,8 +90,12 @@ public function __toString(): string
/**
* Check if the NavItem instance is the current page.
*/
public function isCurrent(PageContract $current): bool
public function isCurrent(?PageContract $current = null): bool
{
if ($current === null) {
$current = Hyde::currentRoute()->getSourceModel();
}

if (! isset($this->route)) {
return ($current->getRoute()->getRouteKey() === $this->href)
|| ($current->getRoute()->getRouteKey().'.html' === $this->href);
Expand Down
10 changes: 7 additions & 3 deletions packages/framework/src/Models/NavigationMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Hyde\Framework\Models;

use Hyde\Framework\Contracts\RouteContract;
use Hyde\Framework\Hyde;
use Hyde\Framework\Router;
use Illuminate\Support\Collection;

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

public static function create(RouteContract $currentRoute): static
public static function create(?RouteContract $currentRoute = null): static
{
return (new self())->setCurrentRoute($currentRoute)->generate()->filter()->sort();
return (new self())->setCurrentRoute($currentRoute ?? Hyde::currentRoute())->generate()->filter()->sort();
}

/**
* @deprecated v0.50.0 - Automatically inferred from the view.
*/
public function setCurrentRoute(RouteContract $currentRoute): self
{
$this->currentRoute = $currentRoute;
Expand Down Expand Up @@ -67,7 +71,7 @@ public function sort(): self
return $this;
}

/** @internal */
/** @deprecated v0.50.x - use Route::home() instead */
public function getHomeLink(): string
{
return Route::get('index');
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/DarkmodeFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Config;

Expand All @@ -18,7 +17,8 @@ protected function setUp(): void
{
parent::setUp();

view()->share('page', new MarkdownPage([], ''));
$this->mockRoute();
$this->mockPage();
}

public function test_has_darkmode()
Expand Down
37 changes: 37 additions & 0 deletions packages/framework/tests/Unit/Views/NavigationBrandViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Hyde\Framework\Testing\Unit\Views;

use Hyde\Testing\TestCase;

/**
* @see resources/views/components/navigation/navigation-brand.blade.php
*/
class NavigationBrandViewTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->mockRoute();
$this->mockPage();
}

protected function render(): string
{
return view('hyde::components.navigation.navigation-brand', [
'navigation' => \Hyde\Framework\Models\NavigationMenu::create(),
])->render();
}

public function test_component_links_to_home_route()
{
$this->assertStringContainsString('href="index.html"', $this->render());
}

public function test_component_uses_site_name()
{
$this->assertStringContainsString('HydePHP', $this->render());
config(['hyde.name' => 'foo']);
$this->assertStringContainsString('foo', $this->render());
}
}
49 changes: 49 additions & 0 deletions packages/framework/tests/Unit/Views/NavigationLinkViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Hyde\Framework\Testing\Unit\Views;

use Hyde\Framework\Models\NavItem;
use Hyde\Framework\Models\Route;
use Hyde\Testing\TestCase;

/**
* @see resources/views/components/navigation/navigation-link.blade.php
*/
class NavigationLinkViewTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->mockRoute();
$this->mockPage();
}

protected function render(?NavItem $item = null): string
{
return view('hyde::components.navigation.navigation-link', [
'item' => $item ?? NavItem::toLink('foo.html', 'Foo'),
])->render();
}

public function test_component_links_to_route_destination()
{
$this->assertStringContainsString('href="foo.html"', $this->render());
}

public function test_component_uses_title()
{
$this->assertStringContainsString('Foo', $this->render());
}

public function test_component_is_current_when_current_route_matches()
{
$this->mockRoute(Route::get('index'));
$this->assertStringContainsString('current', $this->render(NavItem::toRoute(Route::get('index'), 'Home')));
}

public function test_component_has_aria_current_when_current_route_matches()
{
$this->mockRoute(Route::get('index'));
$this->assertStringContainsString('aria-current="page"', $this->render(NavItem::toRoute(Route::get('index'), 'Home')));
}
}
7 changes: 4 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Testing;

use Hyde\Framework\Contracts\PageContract;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Route;
Expand Down Expand Up @@ -67,9 +68,9 @@ protected function mockRoute(?Route $route = null)
}

/** @internal */
protected function mockPage()
protected function mockPage(?PageContract $page = null, ?string $currentPage = null)
{
view()->share('page', new MarkdownPage());
view()->share('currentPage', 'PHPUnit');
view()->share('page', $page ?? new MarkdownPage());
view()->share('currentPage', $currentPage ?? 'PHPUnit');
}
}

0 comments on commit fbad17f

Please sign in to comment.