docs: switch to aria-current in active links page (#2019)

This commit is contained in:
Reed von Redwitz
2023-11-28 17:32:48 +01:00
committed by GitHub
parent 325850719a
commit 6a90fdbf1b
3 changed files with 21 additions and 17 deletions
+18 -14
View File
@@ -3,39 +3,43 @@ description: |
Style active links with ease in Fresh Style active links with ease in Fresh
--- ---
When Fresh renders an `<a>`-element, it will automatically add a Fresh automatically enhances the accessibility of <a> elements by adding the
[data attribute](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) aria-current attribute when rendering links that match the current URL. This
to the link which can be used for styling purposes. Fresh will add one of the attribute is recognized by assistive technologies and clearly indicates the
two following attributes: current page within a set of pages.
- `data-current` - Added to links with an exact path match - aria-current="page" - Added to links with an exact path match, enhancing
- `data-ancestor` - Added to links which partially match the current URL accessibility by indicating the current page to assistive technologies.
As we aim to improve accessibility, we encourage the use of aria-current for
styling current links where applicable.
## Styling with CSS ## Styling with CSS
`data-*` attributes can be natively styled in CSS via attribute selectors. The aria-current attribute is easily styled with CSS using attribute selectors,
providing a native way to visually differentiate the active link.
```css ```css
/* Give links pointing to the current page a green color */ /* Give links pointing to the current page a green color */
a[data-current] { a[aria-current='page'] {
color: green; color: green;
} }
/* Color all ancestor links of the current page */ /* Color all ancestor links of the current page */
a[data-ancestor] { a[aria-current='true'] {
color: peachpuff; color: peachpuff;
} }
``` ```
## Tailwind / Twind ## Tailwind / Twind
In tailwind or tailwind-like styling solutions you can style `data-*`-attributes In Tailwind or similar CSS frameworks, you can apply styles to elements with the
by prepending the classes with `[data-current]:` aria-current attribute by using bracket notation in your class definitions.
```tsx ```tsx
function Menu() { function Menu() {
return ( return (
<a href="/foo" class="[data-current]:text-green-600"> <a href="/foo" class="[aria-current]:text-green-600">
link to some page link to some page
</a> </a>
); );
@@ -48,7 +52,7 @@ The original twind plugin (`import twindPlugin from "$fresh/plugins/twind.ts";`)
supports the above style: supports the above style:
```tsx ```tsx
class="[data-current]:text-green-600" class="[aria-current='page']:text-green-600"
``` ```
### TwindV1 Plugin ### TwindV1 Plugin
@@ -57,5 +61,5 @@ The new twind plugin (`import twindPlugin from "$fresh/plugins/twindv1.ts";`)
requires a slightly different syntax (note the position of the left bracket): requires a slightly different syntax (note the position of the left bracket):
```tsx ```tsx
class="data-[current]:text-green-600" class="aria-[current='page']:text-green-600"
``` ```
+2 -2
View File
@@ -54,7 +54,7 @@ export function SidebarCategory(props: {
<li class="my-2 block"> <li class="my-2 block">
<a <a
href={href} href={href}
class="text-gray-900 hover:text-gray-600 data-[current]:text-green-700 data-[current]:hover:underline font-bold" class="text-gray-900 hover:text-gray-600 aria-[current]:text-green-700 aria-[current]:hover:underline font-bold"
> >
{title} {title}
</a> </a>
@@ -78,7 +78,7 @@ export function SidebarEntry(props: {
<li class="py-[1px]"> <li class="py-[1px]">
<a <a
href={href} href={href}
class="data-[current]:text-green-700 data-[current]:border-green-600 data-[current]:bg-green-50 border-l-4 border-transparent px-4 py-0.5 transition-colors hover:text-green-500 font-normal block" class="aria-[current]:text-green-700 aria-[current]:border-green-600 aria-[current]:bg-green-50 border-l-4 border-transparent px-4 py-0.5 transition-colors hover:text-green-500 font-normal block"
> >
{title} {title}
</a> </a>
+1 -1
View File
@@ -31,7 +31,7 @@ export default function NavigationBar(
href={item.href} href={item.href}
class={`p-1 sm:p-2 ${ class={`p-1 sm:p-2 ${
isHome ? "text-green-900" : "text-gray-600" isHome ? "text-green-900" : "text-gray-600"
} hover:underline data-[current]:font-bold data-[ancestor]:font-bold`} } hover:underline aria-[current]:font-bold`}
> >
{item.name} {item.name}
</a> </a>