Skip to content

Commit

Permalink
Remove breadcrumb leaf
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Sep 6, 2024
1 parent 2e8eb77 commit 769b8ec
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
9 changes: 8 additions & 1 deletion app/Domain/Breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ class Breadcrumb
{
public function __construct(
public string $name,
public string $url)
public string $url,
public bool $leaf,
)
{
}

public function leaf(): self
{
return new Breadcrumb($this->name, $this->url, true);
}
}
24 changes: 22 additions & 2 deletions app/Services/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function pushMany(array $breadcrumbs): void

public function push(string $name, string $url): void
{
$this->breadcrumbs[] = new Breadcrumb($name, $url);
$this->breadcrumbs[] = new Breadcrumb($name, $url, false);
}

public function render(): ?View
Expand All @@ -30,8 +30,28 @@ public function render(): ?View
return view('components/breadcrumb', [
'root_name' => config('app.name'),
'root_href' => route('home'),
'breadcrumbs' => $this->breadcrumbs,
'breadcrumbs' => $this->breadcrumbsWithLeaf(),
'schema_breadcrumb' => new Seo\Schema(new BreadcrumbList($this->breadcrumbs)),
]);
}

private function breadcrumbsWithLeaf(): array
{
return $this->lastBreadcrumbIsLeaf($this->breadcrumbs);
}

private function lastBreadcrumbIsLeaf(array $breadcrumbs): array
{
return [
...\array_slice($breadcrumbs, 0, -1),
$this->last($breadcrumbs)->leaf(),
];
}

private function last(array $breadcrumbs): Breadcrumb
{
/** @var Breadcrumb $last */
$last = \end($breadcrumbs);
return $last;
}
}
31 changes: 22 additions & 9 deletions resources/sass/core/_breadcrumbs.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
.breadcrumb-item {
a, span {
color: $gray;
}
@import "../helpers/variables";

ul.breadcrumb {
font-size: 0.95em;

.breadcrumb-item {
a, span {
color: $gray;
}

+ .breadcrumb-item {
&::before {
content: "»";
}
}

+ .breadcrumb-item {
&::before {
content: "»";
.leaf {
display: none;
}
}

&:last-child:not(:first-child) {
a, span {
&#breadcrumb-fixed,
.footer-top &,
{
.leaf {
display: inline;
font-weight: bold;
}
}
Expand Down
6 changes: 3 additions & 3 deletions resources/views/components/breadcrumb.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="container-3xl">
<ul class="breadcrumb">
<ul class="breadcrumb pb-0">
<li class="breadcrumb-item">
<a href="{{ root_href }}" class="d-none d-sm-inline-block">
{{ root_name }}
Expand All @@ -11,8 +11,8 @@

{% for breadcrumb in breadcrumbs %}
<li class="breadcrumb-item">
{% if loop.last %}
<span>{{ breadcrumb.name }}</span>
{% if breadcrumb.leaf %}
<span class="leaf">{{ breadcrumb.name }}</span>
{% else %}
<a href="{{ breadcrumb.url }}">
{{ breadcrumb.name }}
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Breadcrumbs/Fixture/BreadcrumbView.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Tests\Unit\Breadcrumbs\Fixture;

use Neon\Test\BaseFixture\Selector\Selector;
use Tests\Unit\BaseFixture\View;
use Tests\Unit\BaseFixture\View\ViewDom;

Expand Down Expand Up @@ -43,8 +44,9 @@ function hrefAttribute(\DOMElement $breadcrumb): ?string
function breadcrumbsContainerVisible(string $uri): bool
{
$dom = new ViewDom($this->htmlView($uri));
$selector = new Selector('div', 'div', 'ul.breadcrumb');
/** @var \DOMElement $breadcrumb */
foreach ($dom->elements(xPath:"//div/div/ul[@class='breadcrumb']") as $container) {
foreach ($dom->elements($selector->xPath()) as $container) {
return true;
}
return false;
Expand Down

0 comments on commit 769b8ec

Please sign in to comment.