Files
sweet/docs/latest/examples/active-links.md
T

1.4 KiB

description
description
Style active links with ease in Fresh

When Fresh renders an <a>-element, it will automatically add a data attribute to the link which can be used for styling purposes. Fresh will add one of the two following attributes:

  • data-current - Added to links with an exact path match
  • data-ancestor - Added to links which partially match the current URL

Styling with CSS

data-* attributes can be natively styled in CSS via attribute selectors.

/* Give links pointing to the current page a green color */
a[data-current] {
  color: green;
}

/* Color all ancestor links of the current page */
a[data-ancestor] {
  color: peachpuff;
}

Tailwind / Twind

In tailwind or tailwind-like styling solutions you can style data-*-attributes by prepending the classes with [data-current]:

function Menu() {
  return (
    <a href="/foo" class="[data-current]:text-green-600">
      link to some page
    </a>
  );
}

Twind Plugin

The original twind plugin (import twindPlugin from "$fresh/plugins/twind.ts";) supports the above style:

class="[data-current]:text-green-600"

TwindV1 Plugin

The new twind plugin (import twindPlugin from "$fresh/plugins/twindv1.ts";) requires a slightly different syntax (note the position of the left bracket):

class="data-[current]:text-green-600"