Add aria attributes to selected menu items

This commit is contained in:
Timothy Warren 2020-05-18 12:53:00 -04:00
parent 493f849aeb
commit ebe8626ba7
3 changed files with 43 additions and 8 deletions

View File

@ -14,7 +14,8 @@ $hasManga = stripos($_SERVER['REQUEST_URI'], 'manga') !== FALSE;
<?php if(strpos($route_path, 'collection') === FALSE): ?> <?php if(strpos($route_path, 'collection') === FALSE): ?>
<?= $helper->a( <?= $helper->a(
$urlGenerator->defaultUrl($url_type), $urlGenerator->defaultUrl($url_type),
$whose . ucfirst($url_type) . ' List' $whose . ucfirst($url_type) . ' List',
['aria-current'=> 'page']
) ?> ) ?>
<?php if($config->get("show_{$url_type}_collection")): ?> <?php if($config->get("show_{$url_type}_collection")): ?>
[<?= $helper->a( [<?= $helper->a(
@ -35,7 +36,8 @@ $hasManga = stripos($_SERVER['REQUEST_URI'], 'manga') !== FALSE;
<?php else: ?> <?php else: ?>
<?= $helper->a( <?= $helper->a(
$url->generate("{$url_type}.collection.view") . $extraSegment, $url->generate("{$url_type}.collection.view") . $extraSegment,
$whose . ucfirst($url_type) . ' Collection' $whose . ucfirst($url_type) . ' Collection',
['aria-current'=> 'page']
) ?> ) ?>
<?php if($config->get("show_{$other_type}_collection")): ?> <?php if($config->get("show_{$other_type}_collection")): ?>
[<?= $helper->a( [<?= $helper->a(
@ -79,15 +81,22 @@ $hasManga = stripos($_SERVER['REQUEST_URI'], 'manga') !== FALSE;
</span> </span>
<?php endif ?> <?php endif ?>
</div> </div>
<?php if ($container->get('util')->isViewPage() && ($hasAnime || $hasManga)): ?>
<nav> <nav>
<?php if ($container->get('util')->isViewPage() && ($hasAnime || $hasManga)): ?>
<?= $helper->menu($menu_name) ?> <?= $helper->menu($menu_name) ?>
<?php if (stripos($_SERVER['REQUEST_URI'], 'history') === FALSE): ?> <?php if (stripos($_SERVER['REQUEST_URI'], 'history') === FALSE): ?>
<br /> <br />
<ul> <ul>
<li class="<?= Util::isNotSelected('list', $lastSegment) ?>"><a href="<?= $urlGenerator->url($route_path) ?>">Cover View</a></li> <?php $currentView = Util::eq('list', $lastSegment) ? 'list' : 'cover' ?>
<li class="<?= Util::isSelected('list', $lastSegment) ?>"><a href="<?= $urlGenerator->url("{$route_path}/list") ?>">List View</a></li> <li class="<?= Util::isNotSelected('list', $lastSegment) ?>">
<a aria-current="<?= Util::ariaCurrent($currentView === 'cover') ?>"
href="<?= $urlGenerator->url($route_path) ?>">Cover View</a>
</li>
<li class="<?= Util::isSelected('list', $lastSegment) ?>">
<a aria-current="<?= Util::ariaCurrent($currentView === 'list') ?>"
href="<?= $urlGenerator->url("{$route_path}/list") ?>">List View</a>
</li>
</ul> </ul>
<?php endif ?> <?php endif ?>
<?php endif ?>
</nav> </nav>
<?php endif ?>

View File

@ -105,7 +105,10 @@ final class MenuGenerator extends UrlGenerator {
$has = StringType::from($this->path())->contains($path); $has = StringType::from($this->path())->contains($path);
$selected = ($has && mb_strlen($this->path()) >= mb_strlen($path)); $selected = ($has && mb_strlen($this->path()) >= mb_strlen($path));
$link = $this->helper->a($this->url($path), $title); $linkAttrs = ($selected)
? ['aria-current' => 'location']
: [];
$link = $this->helper->a($this->url($path), $title, $linkAttrs);
$attrs = $selected $attrs = $selected
? ['class' => 'selected'] ? ['class' => 'selected']

View File

@ -54,6 +54,29 @@ class Util {
$this->setContainer($container); $this->setContainer($container);
} }
/**
* Absolutely equal?
*
* @param $left
* @param $right
* @return bool
*/
public static function eq($left, $right): bool
{
return $left === $right;
}
/**
* Set aria-current attribute based on a condition check
*
* @param bool $condition
* @return string
*/
public static function ariaCurrent(bool $condition): string
{
return $condition ? 'true' : 'false';
}
/** /**
* HTML selection helper function * HTML selection helper function
* *
@ -63,7 +86,7 @@ class Util {
*/ */
public static function isSelected(string $left, string $right): string public static function isSelected(string $left, string $right): string
{ {
return ($left === $right) ? 'selected' : ''; return static::eq($left, $right) ? 'selected' : '';
} }
/** /**