Sources

Slant / People & chat

The citations behind an answer, folded under a count.

Demo

Assistant

Usage

@tuttn/slant
import { Sources, Text } from '@tuttn/slant'

const sources = [
  { href: 'https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/', title: 'Disclosure pattern',
    description: 'How a button shows and hides a section.' },
  // A relative link has no host to show, so name its site.
  { href: '/lab/slant/docs/disclosure', title: 'Disclosure', site: 'Slant docs' },
]

// Folded under "2 sources". Open, it is a numbered list in the order you
// pass, so "[2]" in the answer is the second.
<Sources items={sources} />

// Open each link in a new tab; screen readers hear "(opens in a new tab)".
<Sources items={sources} external />

// Your own title, open to start, and more after the list.
<Sources items={sources} title="Read more" defaultOpen>
  <Text variant="hint">Searched 14 pages.</Text>
</Sources>

Props

PropTypeDefaultDescription
itemsreadonly SourceItem[]—The sources, in the order the answer cites them: `{ href, title, site?, description? }`. The list is numbered, so a citation "[2]" matches the second.
titleReactNodemessages.sourcesCount(items.length) ("3 sources")Replaces the folded title.
externalboolean—Opens each link in a new tab (`target="_blank"`, `rel="noreferrer"`).
openboolean—Whether the panel is open. Pass it to control the disclosure; leave it out and it keeps its own state.
defaultOpenboolean—Whether the panel starts open when uncontrolled.
onOpenChange(open: boolean) => void—Called with the requested state when the toggle is pressed.
heading'h2' | 'h3' | 'h4' | 'div'—The element wrapping the toggle button, so it sits in the page's outline.
disabledboolean—Disables the toggle button: the panel stays as it is, open or closed, and the toggle leaves the tab order.
togglePropsOmit<ButtonHTMLAttributes<HTMLButtonElement>, 'type' | 'disabled' | 'aria-expanded' | 'aria-controls' | 'children'>—Extra attributes for the toggle button, such as `id`, `aria-describedby` or `aria-disabled`. With `aria-disabled` the toggle stays focusable but a press does nothing. Its `onClick` runs first and can call `preventDefault()` to keep the panel as it is.
childrenReactNode—The panel's content. The panel sets a muted body text style, so pass plain text or `Text`.
item.hrefstring—Where the source lives: the link’s href.
item.titleReactNode—The link’s text.
item.sitestringthe host of hrefShown muted beside the link. A relative or malformed href has no host, so nothing shows unless you name the site.
item.descriptionReactNode—A line quoted or summarised from the source, under the link.
…restHTMLAttributes<HTMLDivElement>—The ref, className, style and other attributes go on the root div. `open`, `defaultOpen`, `onOpenChange`, `heading`, `disabled` and `toggleProps` work as on Disclosure.

Keyboard

Tabfocuses the toggle, then each link in turn while open
Enter / Spaceopens or folds the list
Enterfollows the focused link

Accessibility

  • The toggle is a button inside a heading (heading, an h3 by default), with aria-expanded and aria-controls on the panel, as Disclosure’s. It is named by the title alone: the site initials beside it are hidden from screen readers.
  • The list is an <ol>, so screen readers hear how many sources there are and the number of each, which is the number a citation such as “[2]” refers to.
  • Each link is named by its title. With external, it also says messages.sourcesNewTab (“(opens in a new tab)”) in hidden text, and the arrow icon after it is hidden.

Sites

Each link shows its site, or else the host of its href (developer.mozilla.org). The host is read without a base URL, so the server and the browser render the same thing: a relative link, a malformed one or a mailto: link shows no site unless you name one. The folded title shows the first three distinct sites as initials.

Direction

The numbers sit at the inline start, the site follows the link, and the new-tab arrow turns to point the way the text runs, so under dir="rtl" the whole list mirrors.

Full source