Skip to content

Commit a357555

Browse files
authored
Merge pull request #1587 from hydephp/refactor-sidebar-data-handling
[2.x] Refactor sidebar data handling
2 parents 5676725 + 8d28069 commit a357555

17 files changed

+207
-50
lines changed

_ide_helper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
/** @var string $routeKey The route key for the page being compiled/previewed */
2323
$routeKey = \Hyde\Support\Facades\Render::getRouteKey();
2424

25+
// Variables available only to some page types
26+
27+
/** @var \Hyde\Framework\Features\Navigation\DocumentationSidebar $sidebar */
28+
$sidebar = app('navigation.sidebar');
29+
2530
// Facades (aliased in app/config.php)
2631

2732
/** @mixin \Hyde\Foundation\HydeKernel */

config/docs.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030
// When using a grouped sidebar, should the groups be collapsible?
3131
'collapsible' => true,
3232

33-
// Should the sidebar footer be shown? You can also set this to a string
34-
// of Markdown to show in the footer. Set to `false` to disable.
35-
'footer' => true,
33+
// A string of Markdown to show in the footer. Set to `false` to disable.
34+
'footer' => '[Back to home page](../)',
3635

3736
/*
3837
|--------------------------------------------------------------------------

packages/framework/resources/views/components/docs/sidebar-brand.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<strong class="px-2">
33
@if(DocumentationPage::home())
44
<a href="{{ DocumentationPage::home() }}">
5-
{{ config('docs.sidebar.header', 'Documentation') }}
5+
{{ $sidebar->getHeader() }}
66
</a>
77
@else
8-
{{ config('docs.sidebar.header', 'Documentation') }}
8+
{{ $sidebar->getHeader() }}
99
@endif
1010
</strong>
1111
<x-hyde::navigation.theme-toggle-button class="opacity-75 hover:opacity-100"/>
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
<p>
2-
@if(is_bool(config('docs.sidebar.footer', true)))
3-
<a href="{{ Hyde::relativeLink('index.html') }}">Back to home page</a>
4-
@else
5-
{{ Hyde::markdown(config('docs.sidebar.footer')) }}
6-
@endif
7-
</p>
1+
{{ Hyde::markdown($sidebar->getFooter()) }}

packages/framework/resources/views/components/docs/sidebar-item.blade.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@php /** @var \Hyde\Framework\Features\Navigation\NavItem $item */ @endphp
21
@props(['grouped' => false])
32
<li @class(['sidebar-item -ml-4 pl-4', $grouped
43
? 'active -ml-8 pl-8 bg-black/5 dark:bg-black/10'
@@ -14,7 +13,7 @@
1413

1514
@if(config('docs.sidebar.table_of_contents.enabled', true))
1615
<span class="sr-only">Table of contents</span>
17-
{!! ($page->getTableOfContents()) !!}
16+
{!! $page->getTableOfContents() !!}
1817
@endif
1918
@else
2019
<a href="{{ $item->getDestination() }}" @class([$grouped

packages/framework/resources/views/components/docs/sidebar-items.blade.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@php /** @var \Hyde\Framework\Features\Navigation\DocumentationSidebar $sidebar */ @endphp
21
@if(! $sidebar->hasGroups())
32
<ul id="sidebar-items" role="list" class="pl-2">
43
@foreach ($sidebar->getItems() as $item)
@@ -7,16 +6,15 @@
76
</ul>
87
@else
98
<ul id="sidebar-items" role="list">
10-
@php ($collapsible = config('docs.sidebar.collapsible', true))
119
@foreach ($sidebar->getItems() as $group)
12-
<li class="sidebar-group" role="listitem" @if($collapsible) x-data="{ groupOpen: {{ $sidebar->isGroupActive($group->getIdentifier()) ? 'true' : 'false' }} }" @endif>
13-
<header @class(['sidebar-group-header p-2 px-4 -ml-2 flex justify-between items-center', 'group hover:bg-black/10' => $collapsible]) @if($collapsible) @click="groupOpen = ! groupOpen" @endif>
14-
<h4 @class(['sidebar-group-heading text-base font-semibold', 'cursor-pointer dark:group-hover:text-white' => $collapsible])>{{ $group->getLabel() }}</h4>
15-
@if($collapsible)
10+
<li class="sidebar-group" role="listitem" @if($sidebar->isCollapsible()) x-data="{ groupOpen: {{ $sidebar->isGroupActive($group->getIdentifier()) ? 'true' : 'false' }} }" @endif>
11+
<header @class(['sidebar-group-header p-2 px-4 -ml-2 flex justify-between items-center', 'group hover:bg-black/10' => $sidebar->isCollapsible()]) @if($sidebar->isCollapsible()) @click="groupOpen = ! groupOpen" @endif>
12+
<h4 @class(['sidebar-group-heading text-base font-semibold', 'cursor-pointer dark:group-hover:text-white' => $sidebar->isCollapsible()])>{{ $group->getLabel() }}</h4>
13+
@if($sidebar->isCollapsible())
1614
@include('hyde::components.docs.sidebar-group-toggle-button')
1715
@endif
1816
</header>
19-
<ul class="sidebar-group-items ml-4 px-2 mb-2" role="list" @if($collapsible) x-show="groupOpen" @endif>
17+
<ul class="sidebar-group-items ml-4 px-2 mb-2" role="list" @if($sidebar->isCollapsible()) x-show="groupOpen" @endif>
2018
@foreach ($group->getChildren() as $item)
2119
@include('hyde::components.docs.sidebar-item', ['grouped' => true])
2220
@endforeach

packages/framework/resources/views/components/docs/sidebar.blade.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
@include('hyde::components.docs.sidebar-brand')
44
</header>
55
<nav id="sidebar-navigation" class="p-2 overflow-y-auto border-y border-gray-300 dark:border-[#1b2533] h-full">
6-
@include('hyde::components.docs.sidebar-items', [
7-
'sidebar' => app('navigation.sidebar'),
8-
])
6+
@include('hyde::components.docs.sidebar-items')
97
</nav>
10-
@if(config('docs.sidebar.footer', true) !== false)
8+
@if($sidebar->hasFooter())
119
<footer id="sidebar-footer" class="h-16 p-4 w-full bottom-0 left-0 text-center leading-8">
1210
@include('hyde::components.docs.sidebar-footer-text')
1311
</footer>

packages/framework/resources/views/layouts/docs.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@include('hyde::components.skip-to-content-button')
1010
@include('hyde::components.docs.mobile-navigation')
11-
@include('hyde::components.docs.sidebar')
11+
@include('hyde::components.docs.sidebar', ['sidebar' => app('navigation.sidebar')])
1212

1313
<main id="content" class="dark:bg-gray-900 min-h-screen bg-gray-50 md:bg-white absolute top-16 md:top-0 w-screen md:left-64 md:w-[calc(100vw_-_16rem)]">
1414
@include('hyde::components.docs.documentation-article')

packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7+
use Hyde\Facades\Config;
78
use Hyde\Pages\DocumentationPage;
89
use Hyde\Support\Facades\Render;
10+
use Illuminate\Contracts\Support\Arrayable;
911
use Illuminate\Support\Str;
1012

1113
use function app;
14+
use function is_string;
1215

1316
class DocumentationSidebar extends NavigationMenu
1417
{
@@ -20,6 +23,42 @@ public static function get(): static
2023
return app('navigation.sidebar');
2124
}
2225

26+
public function __construct(Arrayable|array $items = [])
27+
{
28+
parent::__construct($items);
29+
}
30+
31+
public function getHeader(): string
32+
{
33+
return Config::get('docs.sidebar.header', 'Documentation');
34+
}
35+
36+
public function getFooter(): ?string
37+
{
38+
$option = Config::get('docs.sidebar.footer', '[Back to home page](../)');
39+
40+
if (is_string($option)) {
41+
return $option;
42+
}
43+
44+
if ($option === true) {
45+
/** @deprecated Backwards compatibility */
46+
return '[Back to home page](../)';
47+
}
48+
49+
return null;
50+
}
51+
52+
public function hasFooter(): bool
53+
{
54+
return $this->getFooter() !== null;
55+
}
56+
57+
public function isCollapsible(): bool
58+
{
59+
return Config::getBool('docs.sidebar.collapsible', true);
60+
}
61+
2362
public function hasGroups(): bool
2463
{
2564
return $this->getItems()->contains(fn (NavItem $item): bool => $item->hasChildren());

packages/framework/src/Framework/Features/Navigation/NavItem.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,17 @@ public function isCurrent(): bool
199199
/**
200200
* Add a navigation item to the children of the navigation item.
201201
*/
202-
public function addChild(NavItem $item): void
202+
public function addChild(NavItem $item): static
203203
{
204204
// Todo: Ensure that the item has a group identifier by creating it from the label if it doesn't exist?
205205
206206
$this->children[] = $item;
207+
208+
return $this;
207209
}
208210

211+
// Todo add method to add multiple children at once?
212+
209213
protected static function normalizeGroupKey(?string $group): ?string
210214
{
211215
return $group ? Str::slug($group) : null;

0 commit comments

Comments
 (0)