| 1 |
<?php |
| 2 |
|
| 3 |
namespace SimpleAnalytics\UI; |
| 4 |
|
| 5 |
use SimpleAnalytics\Settings\Tab; |
| 6 |
|
| 7 |
class TabListComponent |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @readonly |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
private $pageSlug; |
| 14 |
/** |
| 15 |
* @readonly |
| 16 |
* @var \SimpleAnalytics\Settings\Tab |
| 17 |
*/ |
| 18 |
private $currentTab; |
| 19 |
/** |
| 20 |
* @readonly |
| 21 |
* @var mixed[] |
| 22 |
*/ |
| 23 |
private $tabs = []; |
| 24 |
public function __construct(string $pageSlug, Tab $currentTab, array $tabs = []) |
| 25 |
{ |
| 26 |
$this->pageSlug = $pageSlug; |
| 27 |
$this->currentTab = $currentTab; |
| 28 |
/** @var Tab[] */ |
| 29 |
$this->tabs = $tabs; |
| 30 |
} |
| 31 |
public function __invoke(): void |
| 32 |
{ |
| 33 |
?> |
| 34 |
<nav class="-mb-px flex gap-5"> |
| 35 |
<?php foreach ($this->tabs as $tab): ?> |
| 36 |
<a |
| 37 |
href="<?php echo $this->tabUrl($tab); ?>" |
| 38 |
class="<?php echo $this->tabClass($tab); ?>" |
| 39 |
> |
| 40 |
<?php echo $this->tabIcon($tab); ?> |
| 41 |
<?php echo $tab->getName(); ?> |
| 42 |
</a> |
| 43 |
<?php endforeach; ?> |
| 44 |
</nav> |
| 45 |
<?php |
| 46 |
} |
| 47 |
|
| 48 |
protected function tabUrl(Tab $tab): string |
| 49 |
{ |
| 50 |
$url = admin_url('options-general.php'); |
| 51 |
|
| 52 |
return add_query_arg(['page' => $this->pageSlug, 'tab' => $tab->getSlug()], $url); |
| 53 |
} |
| 54 |
|
| 55 |
protected function tabClass(Tab $tab): string |
| 56 |
{ |
| 57 |
return implode(' ', [ |
| 58 |
'pb-2 border-b-3 px-3.5', |
| 59 |
'whitespace-nowrap text-sm font-medium', |
| 60 |
$this->currentTab->getSlug() === $tab->getSlug() |
| 61 |
? 'border-primary text-primary' |
| 62 |
: 'text-littleMuted border-transparent hover:border-gray-300 hover:text-gray-700' |
| 63 |
]); |
| 64 |
} |
| 65 |
|
| 66 |
protected function tabIcon(Tab $tab): ?string |
| 67 |
{ |
| 68 |
if ($icon = $tab->getIcon()) { |
| 69 |
return $icon->class('mr-1 inline-block h-4 w-4'); |
| 70 |
} |
| 71 |
|
| 72 |
return null; |
| 73 |
} |
| 74 |
} |
| 75 |
|