Skip to content

Commit

Permalink
Breaking: Rename NavItem method isCurrent to isActive
Browse files Browse the repository at this point in the history
While the method is implemented by using the `currentRoute` helper, the actual end usage is to compare active state, so it makes more sense for this to use that in the name.

   - `isActive()` is more common and may be more intuitive to other developers, as it explicitly suggests that the method is checking if the item is active.
   - `isCurrent()` is also clear, but it might be slightly less common in this context. It still conveys the intended meaning effectively.
  • Loading branch information
caendesilva committed Mar 1, 2024
1 parent 0d74b95 commit 64c3e0e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@props(['grouped' => false])
<li @class(['sidebar-item -ml-4 pl-4', $grouped
? 'active -ml-8 pl-8 bg-black/5 dark:bg-black/10'
: 'active bg-black/5 dark:bg-black/10' => $item->isCurrent()
: 'active bg-black/5 dark:bg-black/10' => $item->isActive()
]) role="listitem">
@if($item->isCurrent())
@if($item->isActive())
<a href="{{ $item->getRoute() }}" aria-current="true" @class([$grouped
? '-ml-8 pl-4 py-1 px-2 block text-indigo-600 dark:text-indigo-400 dark:font-medium border-l-[0.325rem] border-indigo-500 transition-colors duration-300 ease-in-out hover:bg-black/10'
: '-ml-4 p-2 block hover:bg-black/5 dark:hover:bg-black/10 text-indigo-600 dark:text-indigo-400 dark:font-medium border-l-[0.325rem] border-indigo-500 transition-colors duration-300 ease-in-out'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<a href="{{ $item }}" {!! $item->isCurrent() ? 'aria-current="page"' : '' !!} @class([
<a href="{{ $item }}" {!! $item->isActive() ? '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()
'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->isActive()
])>{{ $item->getLabel() }}</a>
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function hasChildren(): bool
/**
* Check if the NavItem instance is the current page being rendered.
*/
public function isCurrent(): bool
public function isActive(): bool
{
return Hyde::currentRoute()->getLink() === $this->route->getLink();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @see \Hyde\Framework\Testing\Unit\NavItemTest
*/
class NavItemIsCurrentHelperTest extends UnitTestCase
class NavItemIsActiveHelperTest extends UnitTestCase
{
public static function setUpBeforeClass(): void
{
Expand All @@ -34,205 +34,205 @@ protected function tearDown(): void
public function testIsCurrent()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('bar'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('bar'))->isActive());
}

public function testIsCurrentWhenCurrent()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo'))->isCurrent());
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo'))->isActive());
}

public function testIsCurrentUsingCurrentRoute()
{
$this->mockRenderData($this->makeRoute('index'));
$this->assertTrue(NavItem::forRoute(Routes::get('index'))->isCurrent());
$this->assertTrue(NavItem::forRoute(Routes::get('index'))->isActive());
}

public function testIsCurrentUsingCurrentLink()
{
$this->mockRenderData($this->makeRoute('index'));
$this->assertTrue(NavItem::forLink('index.html', 'Home')->isCurrent());
$this->assertTrue(NavItem::forLink('index.html', 'Home')->isActive());
}

public function testIsCurrentWhenNotCurrent()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('bar'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('bar'))->isActive());
}

public function testIsCurrentUsingNotCurrentRoute()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forRoute(Routes::get('index'))->isCurrent());
$this->assertFalse(NavItem::forRoute(Routes::get('index'))->isActive());
}

public function testIsCurrentUsingNotCurrentLink()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forLink('index.html', 'Home')->isCurrent());
$this->assertFalse(NavItem::forLink('index.html', 'Home')->isActive());
}

public function testIsCurrentWithNestedCurrentPage()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('bar'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('bar'))->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPage()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo/bar'))->isCurrent());
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo/bar'))->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenNested()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo/bar'))->isCurrent());
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo/bar'))->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenNested()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo/baz'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo/baz'))->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryNested()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo/bar/baz'))->isCurrent());
$this->assertTrue(NavItem::forRoute($this->makeRoute('foo/bar/baz'))->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenVeryNested()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo/baz/bar'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo/baz/bar'))->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryDifferingNested()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo/bar/baz'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo/bar/baz'))->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryDifferingNestedInverse()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo'))->isCurrent());
$this->assertFalse(NavItem::forRoute($this->makeRoute('foo'))->isActive());
}

public function testIsCurrentUsingCurrentLinkWithNestedCurrentPage()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/bar.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar.html', 'foo')->isActive());
}

public function testIsCurrentUsingNotCurrentLinkWithNestedCurrentPage()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo.html', 'foo')->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageAndSubjectPage()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/bar.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar.html', 'foo')->isActive());
}

public function testIsCurrentWhenNotCurrentWithNestedCurrentPageAndSubjectPage()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/baz.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/baz.html', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenNestedUsingLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/bar.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar.html', 'foo')->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenNestedUsingLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/baz.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/baz.html', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryNestedUsingLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('foo/bar/baz.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar/baz.html', 'foo')->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenVeryNestedUsingLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('foo/baz/bar.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/baz/bar.html', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryDifferingNestedUsingLinkItem()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forLink('foo/bar/baz.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar/baz.html', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryDifferingNestedInverseUsingLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('foo.html', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo.html', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenNestedUsingPrettyLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/bar', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar', 'foo')->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenNestedUsingPrettyLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('foo/baz', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/baz', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryNestedUsingPrettyLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('foo/bar/baz', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar/baz', 'foo')->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenVeryNestedUsingPrettyLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('foo/baz/bar', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/baz/bar', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryDifferingNestedUsingPrettyLinkItem()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forLink('foo/bar/baz', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo/bar/baz', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenVeryDifferingNestedInverseUsingPrettyLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('foo', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('foo', 'foo')->isActive());
}

public function testIsCurrentWithAbsoluteLink()
{
$this->mockRenderData($this->makeRoute('foo'));
$this->assertFalse(NavItem::forLink('/foo', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('/foo', 'foo')->isActive());
}

public function testIsCurrentWithNestedCurrentPageWhenNestedUsingAbsoluteLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar'));
$this->assertFalse(NavItem::forLink('/foo/bar', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('/foo/bar', 'foo')->isActive());
}

public function testIsCurrentWhenCurrentWithNestedCurrentPageWhenNestedUsingAbsoluteLinkItem()
{
$this->mockRenderData($this->makeRoute('foo/bar/baz'));
$this->assertFalse(NavItem::forLink('/foo/bar/baz', 'foo')->isCurrent());
$this->assertFalse(NavItem::forLink('/foo/bar/baz', 'foo')->isActive());
}

protected function mockRenderData(Route $route): void
Expand Down
10 changes: 5 additions & 5 deletions packages/framework/tests/Unit/NavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @covers \Hyde\Framework\Features\Navigation\NavItem
*
* @see \Hyde\Framework\Testing\Unit\NavItemIsCurrentHelperTest
* @see \Hyde\Framework\Testing\Unit\NavItemIsActiveHelperTest
*/
class NavItemTest extends UnitTestCase
{
Expand Down Expand Up @@ -309,8 +309,8 @@ public function testIsCurrent()
'getRoute' => new Route(new InMemoryPage('foo')),
'getRouteKey' => 'foo',
]));
$this->assertTrue(NavItem::forRoute(new Route(new InMemoryPage('foo')))->isCurrent());
$this->assertFalse(NavItem::forRoute(new Route(new InMemoryPage('bar')))->isCurrent());
$this->assertTrue(NavItem::forRoute(new Route(new InMemoryPage('foo')))->isActive());
$this->assertFalse(NavItem::forRoute(new Route(new InMemoryPage('bar')))->isActive());
}

public function testIsCurrentWithExternalRoute()
Expand All @@ -319,8 +319,8 @@ public function testIsCurrentWithExternalRoute()
'getRoute' => new Route(new InMemoryPage('foo')),
'getRouteKey' => 'foo',
]));
$this->assertFalse(NavItem::forLink('foo', 'bar')->isCurrent());
$this->assertFalse(NavItem::forLink('https://example.com', 'bar')->isCurrent());
$this->assertFalse(NavItem::forLink('foo', 'bar')->isActive());
$this->assertFalse(NavItem::forLink('https://example.com', 'bar')->isActive());
}

public function testGetGroupWithNoGroup()
Expand Down

0 comments on commit 64c3e0e

Please sign in to comment.