npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solid-aria/breadcrumbs

v0.1.4

Published

Primitives for building accessible breadcrumbs component.

Downloads

4,462

Readme

@solid-aria/breadcrumbs

pnpm turborepo size version stage

Breadcrumbs display a heirarchy of links to the current page or resource in an application.

Installation

npm install @solid-aria/breadcrumbs
# or
yarn add @solid-aria/breadcrumbs
# or
pnpm add @solid-aria/breadcrumbs

createBreadcrumbs and createBreadcrumbItem

Provides the behavior and accessibility implementation for a breadcrumbs component.

Features

Breadcrumbs provide a list of links to parent pages of the current page in hierarchical order. createBreadcrumbs and createBreadcrumbItem help implement these in an accessible way.

  • Support for mouse, touch, and keyboard interactions on breadcrumbs
  • Support for navigation links via <a> elements or custom element types via ARIA
  • Support for disabled breadcrumbs
  • Support for breadcrumbs as a heading

How to use it

This example displays a basic list of breadcrumbs using an HTML <nav> element, and a <ol> for the list of links. Each link is a span because we are handling the interactions locally via onPress. createBreadcrumbItem automatically handles exposing these spans as links to assistive technology.

The chevrons between each link are rendered using a span with aria-hidden="true" so that screen readers do not pick them up. You could also render them similarly using SVG icons, CSS :after, or other techniques.

The last link is non-interactive since it represents the current page. This is done by adding the isCurrent prop.

import {
  AriaBreadcrumbItemProps,
  AriaBreadcrumbsProps,
  createBreadcrumbItem,
  createBreadcrumbs
} from "@solid-aria/breadcrumbs";
import { mergeProps, Show } from "solid-js";

function Breadcrumbs(props: AriaBreadcrumbsProps) {
  const { navProps } = createBreadcrumbs(props);

  return (
    <nav {...navProps}>
      <ol style={{ display: "flex", "list-style": "none", margin: 0, padding: 0 }}>
        {props.children}
      </ol>
    </nav>
  );
}

function BreadcrumbItem(props: AriaBreadcrumbItemProps) {
  let ref: HTMLSpanElement | undefined;

  props = mergeProps({ elementType: "span" }, props);

  const { itemProps } = createBreadcrumbItem<HTMLSpanElement>(props, () => ref);

  return (
    <li>
      <span
        {...itemProps}
        ref={ref}
        style={{
          color: "blue",
          "text-decoration": props.isCurrent ? undefined : "underline",
          "font-weight": props.isCurrent ? "bold" : undefined,
          cursor: props.isCurrent ? "default" : "pointer"
        }}
      >
        {props.children}
      </span>
      <Show when={!props.isCurrent}>
        <span aria-hidden="true" style={{ padding: "0 5px" }}>
          {"›"}
        </span>
      </Show>
    </li>
  );
}

function App() {
  return (
    <Breadcrumbs>
      <BreadcrumbItem onPress={() => alert("Pressed Folder 1")}>Folder 1</BreadcrumbItem>
      <BreadcrumbItem onPress={() => alert("Pressed Folder 2")}>Folder 2</BreadcrumbItem>
      <BreadcrumbItem isCurrent>Folder 3</BreadcrumbItem>
    </Breadcrumbs>
  );
}

Navigation links

This example is similar to the previous one, except the individual breadcrumbs are native navigation links to other pages rather than handled by JavaScript.

import {
  AriaBreadcrumbItemProps,
  AriaBreadcrumbsProps,
  createBreadcrumbItem,
  createBreadcrumbs
} from "@solid-aria/breadcrumbs";
import { JSX, Show } from "solid-js";

function Breadcrumbs(props: AriaBreadcrumbsProps) {
  const { navProps } = createBreadcrumbs(props);

  return (
    <nav {...navProps}>
      <ol style={{ display: "flex", "list-style": "none", margin: 0, padding: 0 }}>
        {props.children}
      </ol>
    </nav>
  );
}

type BreadcrumbItemProps = AriaBreadcrumbItemProps & JSX.AnchorHTMLAttributes<HTMLAnchorElement>;

function BreadcrumbItem(props: BreadcrumbItemProps) {
  let ref: HTMLAnchorElement | undefined;

  const { itemProps } = createBreadcrumbItem(props, () => ref);

  return (
    <li>
      <a
        {...itemProps}
        ref={ref}
        href={props.href}
        style={{
          color: "blue",
          "font-weight": props.isCurrent ? "bold" : undefined,
          cursor: props.isCurrent ? "default" : "pointer"
        }}
      >
        {props.children}
      </a>
      <Show when={!props.isCurrent}>
        <span aria-hidden="true" style={{ padding: "0 5px" }}>
          {"›"}
        </span>
      </Show>
    </li>
  );
}

function App() {
  return (
    <Breadcrumbs>
      <BreadcrumbItem href="/">Home</BreadcrumbItem>
      <BreadcrumbItem href="/solid-aria">Solid Aria</BreadcrumbItem>
      <BreadcrumbItem isCurrent>createBreadcrumbs</BreadcrumbItem>
    </Breadcrumbs>
  );
}

Breadcrumbs as a heading

If you use breadcrumbs in the context where a heading would normally be used, you should make sure that the proper elementType for each breadcrumb is communicated to createBreadcrumbItem so that you receive the correct itemProps to spread on your breadcrumb. This can be seen in the example below where we specify that the last breadcrumb has an <h3> and all other breadcrumbs are of type <a>.

import {
  AriaBreadcrumbItemProps,
  AriaBreadcrumbsProps,
  createBreadcrumbItem,
  createBreadcrumbs
} from "@solid-aria/breadcrumbs";
import { JSX, Show } from "solid-js";

function Breadcrumbs(props: AriaBreadcrumbsProps) {
  const { navProps } = createBreadcrumbs(props);

  return (
    <nav {...navProps}>
      <ol style={{ display: "flex", "list-style": "none", margin: 0, padding: 0 }}>
        {props.children}
      </ol>
    </nav>
  );
}

type BreadcrumbItemProps = AriaBreadcrumbItemProps & JSX.AnchorHTMLAttributes<HTMLAnchorElement>;

function BreadcrumbItem(props: BreadcrumbItemProps) {
  let ref: any;

  const { itemProps } = createBreadcrumbItem<any, any>(
    {
      ...props,
      get elementType() {
        return props.isCurrent ? "h3" : "a";
      }
    },
    () => ref
  );

  return (
    <li>
      <Show
        when={props.isCurrent}
        fallback={
          <>
            <a
              {...itemProps}
              ref={ref}
              href={props.href}
              style={{
                color: props.isDisabled ? "gray" : "blue",
                "font-weight": props.isCurrent ? "bold" : undefined,
                cursor: props.isCurrent ? "default" : "pointer"
              }}
            >
              {props.children}
            </a>
            <span aria-hidden="true" style={{ padding: "0 5px" }}>
              {"›"}
            </span>
          </>
        }
      >
        <h3
          {...itemProps}
          ref={ref}
          style={{
            margin: 0,
            "font-size": "1em",
            color: "blue"
          }}
        >
          {props.children}
        </h3>
      </Show>
    </li>
  );
}

function App() {
  return (
    <Breadcrumbs>
      <BreadcrumbItem href="/">Home</BreadcrumbItem>
      <BreadcrumbItem href="/solid-aria">Solid Aria</BreadcrumbItem>
      <BreadcrumbItem isCurrent>createBreadcrumbs</BreadcrumbItem>
    </Breadcrumbs>
  );
}

Changelog

All notable changes are described in the CHANGELOG.md file.