Product
Trail shoes, size 9, $84.
What the design kit does as a visitor uses a page: a refused form, a busy button, the arrow keys through a grid, focus moved after a change and a part of the page that loads, each working.
Nothing here acts until you press or type. Each example speaks on a status line of its own, the line out of sight that a screen reader reads without moving focus, and what it said last is shown under it. Under that is its code. To see the other theme live, choose Light or Dark next to Theme, at the top of the page.
Press Save rule with the fields empty, or with an address that is not one. The kit stops the submit, draws what is wrong in each field and moves focus to the first field in error. It says that field’s name and error on the status line, and how many fields need attention where more are wrong. With both fields right, the page saves and says so.
The status line said nothing yet.
<form id="rule-form">
<div class="dk-field">
<label class="dk-field-label" for="rule-name" id="rule-name-label">Rule name</label>
<input class="dk-input" required="" id="rule-name" name="name" />
</div>
<div class="dk-field">
<label class="dk-field-label" for="rule-email" id="rule-email-label">
Email for alerts
</label>
<p class="dk-field-hint" id="rule-email-hint">Where the alert is sent</p>
<input
class="dk-input"
type="email"
required=""
id="rule-email"
aria-describedby="rule-email-hint"
name="email"
/>
</div>
<div class="dk-actions">
<button id="rule-save" type="submit" class="dk-button dk-button-accent">Save rule</button>
</div>
</form>
<p class="dk-visually-hidden screen-status" role="status"></p>import { type FormEvent, useState } from 'react';
import {
Button,
InputField,
StatusProvider,
useFormRefusal,
useStatus,
} from '@jasonsdevspace/design-kit/react';
const Rule = () => {
const say = useStatus();
const [errors, setErrors] = useState<Record<string, string>>({});
const [form] = useFormRefusal<HTMLFormElement>({
invalid: (failures) => {
setErrors(Object.fromEntries(failures.map(({ field, words }) => [field.id, words])));
},
});
// Only a submit whose fields pass the browser's constraints reaches the page.
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
setErrors({});
say('The rule was saved.');
};
return (
<form ref={form} onSubmit={submit}>
<InputField
id="rule-name"
name="name"
label="Rule name"
required
error={errors['rule-name']}
/>
<InputField
id="rule-email"
name="email"
type="email"
label="Email for alerts"
hint="Where the alert is sent"
required
error={errors['rule-email']}
/>
<div className="dk-actions">
<Button type="submit" accent id="rule-save">
Save rule
</Button>
</div>
</form>
);
};
export const RuleScreen = () => (
<StatusProvider>
<Rule />
</StatusProvider>
);
Press Save the round. The button is off at once, so a second press does nothing, and keeps its look for --dk-wait, 400 milliseconds, so a fast save never flashes it gray. Once that wait has passed it is drawn off and its words are said on the status line. The save here takes three seconds, then says it is done.
The status line said nothing yet.
<p>
<button id="save-round" type="button" class="dk-button dk-button-accent">Save the round</button>
</p>
<p class="dk-visually-hidden screen-status" role="status"></p>import { useState } from 'react';
import { Button, StatusProvider, useBusy, useStatus } from '@jasonsdevspace/design-kit/react';
/** The save, which takes as long as it takes. */
const saveRound = () => new Promise((done) => setTimeout(done, 3000));
const SaveRound = () => {
const say = useStatus();
const busy = useBusy<HTMLButtonElement>();
const [saving, setSaving] = useState(false);
const save = () => {
setSaving(true);
void saveRound().then(() => {
setSaving(false);
say('The round was saved.');
});
};
return (
<Button ref={busy} accent busy={saving ? 'Saving the round.' : false} onClick={save}>
Save the round
</Button>
);
};
export const RoundScreen = () => (
<StatusProvider>
<SaveRound />
</StatusProvider>
);
Tab to the cards, then move among them with the arrow keys, Home and End, since the grid is one stop for the Tab key. Press a card to choose it or put it back. The 10 is off, so it keeps its place and a press does nothing. The count of cards chosen waits for the presses to stop before it is said, so a quick run of presses is heard once.
The status line said nothing yet.
<div class="dk-button-grid site-hand" role="group" aria-label="Cards in hand" id="hand">
<button type="button" class="dk-button" aria-pressed="false">3</button>
<button type="button" class="dk-button" aria-pressed="false">4</button>
<button type="button" class="dk-button" aria-pressed="false">5</button>
<button type="button" class="dk-button" aria-pressed="false">6</button>
<button type="button" class="dk-button" aria-pressed="true">7</button>
<button type="button" class="dk-button" aria-pressed="false">8</button>
<button type="button" class="dk-button" aria-pressed="true">9</button>
<button type="button" class="dk-button" aria-pressed="false" aria-disabled="true">10</button>
</div>
<p class="dk-visually-hidden screen-status" role="status"></p>import { useState } from 'react';
import { Button, StatusProvider, useRoving, useStatus } from '@jasonsdevspace/design-kit/react';
const CARDS = [3, 4, 5, 6, 7, 8, 9, 10];
/** How many cards are chosen, in words. */
const chosenWords = (count: number) =>
count === 1 ? '1 card chosen.' : `${String(count)} cards chosen.`;
const Hand = () => {
const say = useStatus();
const grid = useRoving<HTMLDivElement>();
const [chosen, setChosen] = useState<readonly number[]>([7, 9]);
const flip = (card: number) => {
const next = chosen.includes(card)
? chosen.filter((each) => each !== card)
: [...chosen, card];
setChosen(next);
// The count changes with each press, so it waits for the presses to stop.
say(chosenWords(next.length), { settle: 700 });
};
return (
<div ref={grid} className="dk-button-grid site-hand" role="group" aria-label="Cards in hand">
{CARDS.map((card) => (
<Button
key={card}
pressed={chosen.includes(card)}
off={card === 10}
onClick={() => {
flip(card);
}}
>
{String(card)}
</Button>
))}
</div>
);
};
export const HandScreen = () => (
<StatusProvider>
<Hand />
</StatusProvider>
);
Press Remove beside a player. The row goes, and the button that had focus with it, so focus moves to the next player’s Remove, or to the one before when the last row goes, and to the list’s heading once no player is left. A press of Put the players back brings the list back as it was.
No players. Put the players back brings them back.
The status line said nothing yet.
<div class="dk-stack dk-stack-heading">
<h3 id="players-heading" tabindex="-1">Players</h3>
<ul class="dk-rows" role="list" id="players">
<li>
<div class="dk-row">
<span class="dk-row-text"><span class="dk-row-title">Ann</span></span>
<button type="button" class="dk-button dk-button-text" id="remove-ann">
Remove
<span class="dk-visually-hidden"> Ann</span>
</button>
</div>
</li>
<li>
<div class="dk-row">
<span class="dk-row-text"><span class="dk-row-title">Ben</span></span>
<button type="button" class="dk-button dk-button-text" id="remove-ben">
Remove
<span class="dk-visually-hidden"> Ben</span>
</button>
</div>
</li>
<li>
<div class="dk-row">
<span class="dk-row-text"><span class="dk-row-title">Cy</span></span>
<button type="button" class="dk-button dk-button-text" id="remove-cy">
Remove
<span class="dk-visually-hidden"> Cy</span>
</button>
</div>
</li>
<li>
<div class="dk-row">
<span class="dk-row-text"><span class="dk-row-title">Dee</span></span>
<button type="button" class="dk-button dk-button-text" id="remove-dee">
Remove
<span class="dk-visually-hidden"> Dee</span>
</button>
</div>
</li>
</ul>
<p><button id="players-back" type="button" class="dk-button">Put the players back</button></p>
</div>
<p class="dk-visually-hidden screen-status" role="status"></p>import { useState } from 'react';
import {
Button,
StatusProvider,
useFocusAfterDraw,
useStatus,
} from '@jasonsdevspace/design-kit/react';
const PLAYERS = ['Ann', 'Ben', 'Cy', 'Dee'];
const Players = () => {
const say = useStatus();
const focusNext = useFocusAfterDraw();
const [players, setPlayers] = useState(PLAYERS);
const remove = (index: number) => {
const next = players[index + 1] ?? players[index - 1];
setPlayers(players.filter((_, at) => at !== index));
// The button that had focus goes with its row, so focus goes to the next row's button, or
// the list's heading once no row is left.
focusNext(next === undefined ? 'players-heading' : `remove-${next.toLowerCase()}`, {
fallbacks: ['players-heading'],
});
say(`${players[index] ?? ''} was removed.`);
};
return (
<div className="dk-stack dk-stack-heading">
<h3 id="players-heading" tabIndex={-1}>
Players
</h3>
{players.length === 0 ? (
<p>No players. Put the players back brings them back.</p>
) : (
<ul className="dk-rows" role="list">
{players.map((player, index) => (
<li key={player}>
<div className="dk-row">
<span className="dk-row-text">
<span className="dk-row-title">{player}</span>
</span>
<button
type="button"
className="dk-button dk-button-text"
id={`remove-${player.toLowerCase()}`}
onClick={() => {
remove(index);
}}
>
Remove
<span className="dk-visually-hidden"> {player}</span>
</button>
</div>
</li>
))}
</ul>
)}
<p>
<Button
onClick={() => {
setPlayers(PLAYERS);
say('The players are back.');
}}
>
Put the players back
</Button>
</p>
</div>
);
};
export const PlayersScreen = () => (
<StatusProvider>
<Players />
</StatusProvider>
);
Press Load again. The product is marked busy while it waits, so a screen reader holds off reading it half drawn, and its loading line shows once the wait outlasts --dk-wait. Then the product comes back and the status line says so.
Press Show the failure, and the same load fails. The alert reads what happened, and focus stays where it was. Try again goes with the failure, so focus moves to the product’s heading as it loads again.
Trail shoes, size 9, $84.
The product could not be loaded. Check your connection, then choose Try again.
The status line said nothing yet.
<article class="dk-stack dk-stack-heading" aria-labelledby="product-heading">
<h3 id="product-heading" tabindex="-1">Product</h3>
<div data-empty="" id="product-failure" class="dk-callout dk-callout-danger">
<div class="dk-callout-body" role="alert"></div>
</div>
<div id="product"><p>Trail shoes, size 9, $84.</p></div>
<div class="dk-actions">
<button id="product-load" type="button" class="dk-button">Load again</button>
</div>
</article>
<p class="dk-visually-hidden screen-status" role="status"></p>import { useRef, useState } from 'react';
import {
Button,
Callout,
StatusProvider,
useFocusAfterDraw,
useStatus,
} from '@jasonsdevspace/design-kit/react';
/**
* How long a failure waits after the callout empties, so a failure that comes back at once is
* still a change a screen reader hears.
*/
const SETTLE = 100;
type Loading = 'loading' | 'failed' | 'loaded';
const Product = ({ load }: { readonly load: () => Promise<string> }) => {
const say = useStatus();
const focusNext = useFocusAfterDraw();
const [state, setState] = useState<Loading>('loaded');
const [product, setProduct] = useState('Trail shoes, size 9, $84.');
// Each load counts, so a load overtaken by another draws and says nothing.
const loads = useRef(0);
const loading = useRef(false);
const start = () => {
loads.current += 1;
const mine = loads.current;
const newest = () => mine === loads.current;
loading.current = true;
setState('loading');
// Said once the wait outlasts --dk-wait, 400ms, and dropped where the load ends first.
say(() => (loading.current && newest() ? 'Loading the product.' : null), { settle: 400 });
load().then(
(loaded) => {
if (!newest()) return;
loading.current = false;
setProduct(loaded);
setState('loaded');
say('Product loaded.');
},
() => {
if (!newest()) return;
loading.current = false;
window.setTimeout(() => {
if (newest()) setState('failed');
}, SETTLE);
}
);
};
const tryAgain = () => {
start();
// Try again goes with the failure, so focus goes to what is loading again.
focusNext('product-heading');
};
const failed = state === 'failed';
return (
<article className="dk-stack dk-stack-heading" aria-labelledby="product-heading">
<h3 id="product-heading" tabIndex={-1}>
Product
</h3>
<Callout
tone="danger"
live="alert"
actions={failed ? <Button onClick={tryAgain}>Try again</Button> : undefined}
>
{failed ? (
<p>
The product could not be loaded. Check your connection, then choose Try
again.
</p>
) : null}
</Callout>
<div aria-busy={state === 'loading'}>
{state === 'loading' ? <p className="dk-loading">Loading the product.</p> : null}
{state === 'loaded' ? <p>{product}</p> : null}
</div>
<div className="dk-actions">
<Button onClick={start}>Load again</Button>
</div>
</article>
);
};
export const ProductScreen = ({ load }: { readonly load: () => Promise<string> }) => (
<StatusProvider>
<Product load={load} />
</StatusProvider>
);