# Feed

> A list of articles that grows as the user scrolls, like a timeline. Page Down and Page Up move between articles, and a screenreader hears where each one is.

`<slab-feed>` · Data · built on `[role=feed] + <article>s`

## Install

```sh
pnpm add slab-elements lit
```

## Usage

### React

`MyFeed.jsx`. React 19 or later, which sets props on custom elements as properties.

```jsx
import 'slab-elements/feed.js';

export function MyFeed() {
  return (
    <slab-feed label="Updates" total={-1}>
      <article>
        <h2>Forms set</h2>
        <p>…</p>
      </article>
      <article>
        <h2>Pour finished</h2>
        <p>…</p>
      </article>
    </slab-feed>
  );
}
```

### Lit

`my-feed.ts`. Slab is built on Lit, so its elements are at home in Lit's templates.

```ts
import 'slab-elements/feed.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

@customElement('my-feed')
export class MyFeed extends LitElement {
  render() {
    return html`
      <slab-feed label="Updates" total="-1">
        <article>
          <h2>Forms set</h2>
          <p>…</p>
        </article>
        <article>
          <h2>Pour finished</h2>
          <p>…</p>
        </article>
      </slab-feed>
    `;
  }
}
```

### Vue

`MyFeed.vue`. Let Vue's compiler know slab- tags are custom elements with isCustomElement.

```vue
<script setup>
import 'slab-elements/feed.js';
</script>

<template>
  <slab-feed label="Updates" :total="-1">
    <article>
      <h2>Forms set</h2>
      <p>…</p>
    </article>
    <article>
      <h2>Pour finished</h2>
      <p>…</p>
    </article>
  </slab-feed>
</template>
```

### Svelte

`MyFeed.svelte`. Svelte binds properties and events to custom elements like any other element.

```svelte
<script>
  import 'slab-elements/feed.js';
</script>

<slab-feed label="Updates" total={-1}>
  <article>
    <h2>Forms set</h2>
    <p>…</p>
  </article>
  <article>
    <h2>Pour finished</h2>
    <p>…</p>
  </article>
</slab-feed>
```

### Ember

`MyFeed.gjs`. Ember sets attributes, and a dynamic value like {{this.max}} sets the property when the element has one. {{on}} listens for any event by its name.

```gjs
import 'slab-elements/feed.js';

export const MyFeed = <template>
  <slab-feed label="Updates" total="-1">
    <article>
      <h2>Forms set</h2>
      <p>…</p>
    </article>
    <article>
      <h2>Pour finished</h2>
      <p>…</p>
    </article>
  </slab-feed>
</template>;
```

### HTML

`index.html`. Any bundler (or an import map) takes care of the imports.

```html
<script type="module">
  import 'slab-elements/feed.js';
</script>

<slab-feed label="Updates" total="-1">
  <article>
    <h2>Forms set</h2>
    <p>…</p>
  </article>
  <article>
    <h2>Pour finished</h2>
    <p>…</p>
  </article>
</slab-feed>
```

## Example

Here's a styled version, `<slab-styled-feed>`. It's not in the package — it's an example of what you can build with the headless element. Copy it and make it yours at the bottom of this page.

```html
<slab-styled-feed label="Job log" total="-1">
  <article>
    <h3>Forms set</h3>
    <p>Braced by 6 a.m. on the north wall. <a href="#forms">See the photos</a></p>
  </article>
  <article>
    <h3>Pour finished</h3>
    <p>38 cubic yards of 4,000 psi, placed from the northeast corner. <a href="#ticket">See the ticket</a></p>
  </article>
  <article>
    <h3>Curing</h3>
    <p>Kept wet for seven days before it takes a load. <a href="#schedule">See the schedule</a></p>
  </article>
</slab-styled-feed>
```

Swap `slab-styled-` for `slab-` in that markup and you've got the same example as it ships: the headless elements with no CSS at all. That's your starting point.

Each child is an article. Feed puts it in the tab sequence, gives it `aria-posinset` and `aria-setsize`, and names it by its first heading, so a screenreader says "Pour finished, 2 of 3" as focus lands on it. An article that isn't an `<article>` becomes a `[role=article]`, and one with a name or a role of its own keeps it.

Loading is yours. Append articles as the user nears the end, such as when an `IntersectionObserver` sees the last one, and set `busy` while you do, so a screenreader waits until they're in rather than reading half a page. Set `total` to how many articles there are in all, or -1 when you don't know, as in an endless timeline. Unset, it's the number of articles there are now.

## Keyboard

| Key                       | What it does                                                                                     |
| ------------------------- | ------------------------------------------------------------------------------------------------ |
| Tab                       | Moves to each article, then through the links and buttons in it.                                 |
| Page Down, Page Up        | From anywhere in an article, moves to the next or previous article. Past either end, it scrolls. |
| Control+End, Control+Home | Moves focus out of the feed, to the first thing after it or the last thing before it.            |

## Behavior

A feed with no visual styles, following the APG feed pattern: a list of articles that grows as the user scrolls, such as a timeline or a page of search results that loads more at the end.

Each slotted element is an article. It's put in the tab sequence and given `aria-posinset` and `aria-setsize`, so a screenreader says where it is in the feed. It's a `[role=article]` unless it's an `<article>` or has a role of its own, and it's named by its first heading unless it has a name of its own.

Page Down and Page Up move focus to the next and previous article, from anywhere in one, so a keyboard user can skip through the feed without tabbing through each article's links. Control+End and Control+Home move focus out of the feed, to what's after and before it.

Loading is yours. Add articles to the end, or the start, and set `busy` while you do, so a screenreader waits until they're in. `total` is how many articles there are in all, when you know, or -1 when you don't, and the number of articles there are now when it's unset.

## API

### `<slab-feed>`

#### Attributes

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `busy` | `boolean` | `false` | Articles are being added or removed. A screenreader waits until it's unset. |
| `label` (required) | `string` |  | Names the feed. |
| `total` | `number` |  | How many articles there are in all, including those not added yet, or -1 when that's unknown. The number added when unset. |

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| `items` (read-only) | `Element[]` | Its Feed Items, in order. |

#### Slots

| Name | Type | Description |
| --- | --- | --- |
| `(default)` | `Element` | The articles |

#### CSS parts

| Name | Description |
| --- | --- |
| `feed` | The articles |

## Copy and own it

`<slab-styled-feed>` isn't in the package. It's an example — copy it into your project and change whatever you want. Keyboard, focus, ARIA, forms, and events all come from the headless element, and the styles only reach in through `::part()` and `:state()`, same as yours would.

### Lit

Under the hood, it's just the headless element, subclassed, with `styles` set. That's it. To use it, copy these files, swap the tag in `@customElement()` for one of your own, and import it. `tokens.styles.ts` holds the colors and fonts every styled example shares, so you only need it once.

#### TypeScript

These files are TypeScript, and `@customElement()` is one of Lit's decorators (TypeScript's experimental flavor). You'll need two options in your tsconfig, per [Lit's docs](https://lit.dev/docs/components/decorators/#decorators-typescript): `experimentalDecorators` to compile them, and `useDefineForClassFields` turned off so class fields don't stomp on the accessors Lit sets up.

```json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "useDefineForClassFields": false
  }
}
```

Using plain JavaScript? Drop the types and register the class with `customElements.define()` instead of the decorator.

#### feed.ts

```ts
import { customElement } from 'lit/decorators.js';
import Feed from 'slab-elements/feed.js';
import styles from './feed.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-feed': StyledFeed;
  }
}

/**
 * Feed, styled: a column of raised articles with slab-serif headings, the
 * focused one outlined in ember, and the column dimmed while it's busy.
 */
@customElement('slab-styled-feed')
export default class StyledFeed extends Feed {
  static override styles = [tokens, styles];
}
```

#### feed.styles.ts

```ts
import { css } from 'lit';

/*
  The articles are slotted in, so they're styled through "::slotted()". Their
  headings and text are the consumer's own and inherit the article's type.
*/
export default css`
  :host {
    color: var(--ink);
    font-family: var(--font-body);
  }

  [part~='feed'] {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    transition: opacity 150ms;
  }

  :host([busy]) [part~='feed'] {
    opacity: 0.6;
  }

  ::slotted(*) {
    background-color: var(--paper);
    border: var(--border);
    box-shadow: var(--shadow);
    padding: 1rem 1.25rem;
  }

  ::slotted(:focus-visible) {
    outline: 3px solid var(--ember);
    outline-offset: 3px;
  }
`;
```

#### tokens.styles.ts

```ts
import { css } from 'lit';

/*
  The styled components' palette and type, taken from the logo: ink for the
  courses, ember for the base, paper behind them.

  Each is a short alias for a "--slab-styled-*" property, with the default as
  its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
  which every shadow root inherits. Were the defaults declared under those
  names here instead, each host's own declaration would shadow the page's.

  Each default is "light-dark()", so the components follow the page's
  "color-scheme": "dark" for dark, "light" for light, and "light dark" to
  follow the operating system. Ink and paper trade places in the dark, so
  ink is always the text and paper always what's behind it. Contrast is given
  as light, then dark.
*/
export default css`
  :host {
    /* Secondary text. 6.8:1 and 6.9:1 on paper. */
    --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
    --border: 2px solid var(--ink);

    /* Behind disabled and read-only controls, and hovered rows. */
    --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

    /*
      The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
      enough for borders, fills and large text, but not always for small text,
      which takes "--ember-deep".
    */
    --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

    /*
      Orange for small text on paper, and for paper text on it. 5.7:1 and
      7.4:1.
    */
    --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
    --font-body: var(
      --slab-styled-font-body,
      'IBM Plex Sans',
      system-ui,
      sans-serif
    );
    --font-display: var(
      --slab-styled-font-display,
      'Zilla Slab',
      rockwell,
      'Roboto Slab',
      serif
    );
    --font-mono: var(
      --slab-styled-font-mono,
      'IBM Plex Mono',
      ui-monospace,
      monospace
    );
    --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
    --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

    /* The hard, offset shadow a raised surface casts. */
    --shadow: 4px 4px 0 var(--ink);
  }

  /*
    Forced colors, such as Windows' contrast themes, replace every color that
    isn't a system color, so ember fills and paper marks all become the same
    background, and a checked Checkbox looks like an unchecked one. The palette
    becomes the user's system colors instead, which are kept. Ember is the
    highlight, the color of what's selected, and ember-deep, being for text and
    for paper text on it, is the text color.
  */
  @media (forced-colors: active) {
    :host {
      --ash: GrayText;
      --chalk: Canvas;
      --ember: Highlight;
      --ember-deep: CanvasText;
      --ink: CanvasText;
      --paper: Canvas;
    }
  }
`;
```

### React, Vue, Svelte and Ember

The same styles, written from outside the element, so there's nothing to subclass. The CSS styles every `<slab-feed>` on the page. Import `tokens.css`, the colors and fonts every styled example shares, once, wherever your global styles go.

In React and Vue, each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.

In Svelte, import `feed.js` once, such as in your root layout, and use `<slab-feed>` as is. A Svelte component around it would take `slot="…"` content as its own slot and drop it.

In Ember, each component registers its headless element and imports its CSS. Use it like the element: its attributes and its content go to the element.

#### Feed.jsx

```jsx
import 'slab-elements/feed.js';
import './feed.css';

export function Feed(props) {
  return <slab-feed {...props} />;
}
```

#### Feed.vue

```vue
<script setup>
import 'slab-elements/feed.js';
import './feed.css';
</script>

<template>
  <slab-feed><slot /></slab-feed>
</template>
```

#### feed.js

```js
import 'slab-elements/feed.js';
import './feed.css';
```

#### Feed.gjs

```gjs
import 'slab-elements/feed.js';
import './feed.css';

export const Feed = <template>
  <slab-feed ...attributes>{{yield}}</slab-feed>
</template>;
```

#### feed.css

```css
slab-feed {
  color: var(--ink);
  font-family: var(--font-body);
}

slab-feed::part(feed) {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  transition: opacity 150ms;
}

slab-feed[busy]::part(feed) {
  opacity: 0.6;
}

slab-feed > * {
  background-color: var(--paper);
  border: var(--border);
  box-shadow: var(--shadow);
  padding: 1rem 1.25rem;
}

slab-feed > :focus-visible {
  outline: 3px solid var(--ember);
  outline-offset: 3px;
}
```

#### tokens.css

```css
:root {
  /* Secondary text. 6.8:1 and 6.9:1 on paper. */
  --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
  --border: 2px solid var(--ink);

  /* Behind disabled and read-only controls, and hovered rows. */
  --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

  /*
    The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
    enough for borders, fills and large text, but not always for small text,
    which takes "--ember-deep".
  */
  --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

  /*
    Orange for small text on paper, and for paper text on it. 5.7:1 and
    7.4:1.
  */
  --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
  --font-body: var(
    --slab-styled-font-body,
    'IBM Plex Sans',
    system-ui,
    sans-serif
  );
  --font-display: var(
    --slab-styled-font-display,
    'Zilla Slab',
    rockwell,
    'Roboto Slab',
    serif
  );
  --font-mono: var(
    --slab-styled-font-mono,
    'IBM Plex Mono',
    ui-monospace,
    monospace
  );
  --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
  --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

  /* The hard, offset shadow a raised surface casts. */
  --shadow: 4px 4px 0 var(--ink);
}

/*
  Forced colors, such as Windows' contrast themes, replace every color that
  isn't a system color, so ember fills and paper marks all become the same
  background, and a checked Checkbox looks like an unchecked one. The palette
  becomes the user's system colors instead, which are kept. Ember is the
  highlight, the color of what's selected, and ember-deep, being for text and
  for paper text on it, is the text color.
*/
@media (forced-colors: active) {
  :host {
    --ash: GrayText;
    --chalk: Canvas;
    --ember: Highlight;
    --ember-deep: CanvasText;
    --ink: CanvasText;
    --paper: Canvas;
  }
}
```
