Repository navigation
Expand file tree
/
Copy pathTabs.php
More file actions
91 lines (77 loc) · 2.96 KB
/
Copy pathTabs.php
File metadata and controls
91 lines (77 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Helper;
use InvalidArgumentException;
use PHPForge\Debug\Exception\Message;
use PHPForge\Debug\Theme\Css;
use UIAwesome\Html\Flow\Div;
use UIAwesome\Html\List\{Li, Ul};
use UIAwesome\Html\Palpable\A;
use function array_key_exists;
/**
* Renders the shared accessible tab pattern used by debug panels.
*/
final class Tabs
{
/**
* Renders the tab list and its panels as one markup fragment.
*
* @param non-empty-string $id Base ID every tab and panel ID derives from.
* @param non-empty-string $ariaLabel Accessible name announced for the tab list.
* @param list<array{label: string, content: string}> $tabs Tabs in display order.
* @param int $activeIndex Zero-based index of the tab selected on initial render.
*
* @return string Rendered tab list followed by its panels.
*/
public static function render(string $id, string $ariaLabel, array $tabs, int $activeIndex = 0): string
{
if ($tabs !== [] && !array_key_exists($activeIndex, $tabs)) {
throw new InvalidArgumentException(
Message::ACTIVE_TAB_INDEX_INVALID->getMessage(),
);
}
$items = [];
$panels = [];
foreach ($tabs as $index => $tab) {
$active = $index === $activeIndex;
$tabId = "{$id}-tab-{$index}";
$panelId = "{$id}-panel-{$index}";
$items[] = Li::tag()
->class(Css::TAB)
->addAttribute('role', 'presentation')
->html(
A::tag()
->id($tabId)
->addAriaAttribute('controls', $panelId)
->addAriaAttribute('selected', $active ? 'true' : 'false')
->addAttribute('data-yii-debug-toggle', 'tab')
->addAttribute('role', 'tab')
->addAttribute('tabindex', $active ? '0' : '-1')
->class($active ? Css::TAB_LINK . ' is-active' : Css::TAB_LINK)
->content($tab['label'])
->href("#{$panelId}"),
);
$panel = Div::tag()
->id($panelId)
->addAriaAttribute('labelledby', $tabId)
->addAttribute('role', 'tabpanel')
->class($active ? Css::TAB_PANEL . ' is-active' : Css::TAB_PANEL)
->html($tab['content']);
if ($active === false) {
$panel = $panel->addAttribute('hidden', true);
}
$panels[] = $panel;
}
$tabList = Ul::tag()
->class(Css::TABS)
->addAriaAttribute('label', $ariaLabel)
->addAttribute('role', 'tablist')
->html(...$items)
->render();
$content = Div::tag()
->class(Css::TAB_CONTENT)
->html(...$panels)
->render();
return "{$tabList}{$content}";
}
}