Compose accessible reusable components
Make field ownership and error relationships explicit
A reusable field should connect visible label, control and supporting text while letting its parent own the value. Do not copy props into state just to synchronize them. In the example the caller supplies the input text and decides when to validate it. A field error is not merely a red border: it needs an explanation associated with the control. Use distinct generated IDs for repeated instances; do not generate list keys with useId.
import React, {useId} from 'react';export type TextFieldProps = { label: string; name: string; value: string; onChange: (value: string) => void; hint?: string; error?: string; required?: boolean;};export function TextField({label, name, value, onChange, hint, error, required}: TextFieldProps) { const id = useId(); const description = [hint && `${id}-hint`, error && `${id}-error`].filter(Boolean).join(' '); return <div className="ds-field"> <label htmlFor={id}>{label}</label> <input id={id} name={name} value={value} required={required} aria-invalid={error ? true : undefined} aria-describedby={description || undefined} onChange={event => onChange(event.target.value)} /> {hint && <small id={`${id}-hint`}>{hint}</small>} {error && <p id={`${id}-error`} className="ds-error">{error}</p>} </div>;}When error is absent the error node and its ID reference are absent. When both messages exist the input references both. Error announcements during dynamic updates are not guaranteed by aria-describedby alone; use a suitable status or error summary and focus strategy after failed submission. The value contract is always a string, so the component never intentionally switches between controlled and uncontrolled modes. Native required validation and server-side validation are separate from the displayed error. Test repeated instances, long labels and empty error values.
Use composition for content slots
import React, {useId, type ReactNode} from 'react';export type CardProps = {title: string; children: ReactNode; footer?: ReactNode};export function Card({title, children, footer}: CardProps) { const heading = useId(); return <article className="ds-card" aria-labelledby={heading}> <h2 id={heading}>{title}</h2> <div>{children}</div> {footer != null && <footer>{footer}</footer>} </article>;}A footer is a named composition slot: the caller passes content rather than asking Card to know every possible action. The component supplies an article label through its heading. This bounded implementation assumes the caller needs an article and an h2; a more general library might offer a carefully documented heading-level contract or a neutral surface primitive. Avoid giving every decorative container a landmark role. The footer has no click handler and does not make the entire card an interactive control.
Delegate modal behavior to the native dialog
import React, {useId, useRef, type ReactNode} from 'react';export type DialogProps = {title: string; triggerLabel: string; children: ReactNode};export function Dialog({title, triggerLabel, children}: DialogProps) { const id = useId(); const dialog = useRef<HTMLDialogElement>(null); const trigger = useRef<HTMLButtonElement>(null); return <> <button ref={trigger} type="button" className="ds-button" data-variant="secondary" onClick={() => {if (dialog.current && !dialog.current.open) dialog.current.showModal();}}>{triggerLabel}</button> <dialog ref={dialog} className="ds-dialog" aria-labelledby={id} onClose={() => trigger.current?.focus()}> <h2 id={id}>{title}</h2> <div>{children}</div> <button type="button" className="ds-button" data-variant="secondary" onClick={() => dialog.current?.close()}>Close</button> </dialog> </>;}Opening invokes showModal(), which uses the browser modal top layer and makes the rest of the same document inert. Escape normally requests closure; a close event restores focus to the still-mounted trigger in this fixture. Do not replace showModal with the open attribute, which alone is not modal. This internal-trigger component does not offer a controlled open prop or an async confirmation contract; if those are needed, design an explicit state and dismissal policy, including when a trigger disappears. Use simple content and avoid nested dialogs here. Test focus entry, Escape, Tab, close button and restoration in the actual browser and assistive technologies; native semantics are not proof that every composition is accessible. Practice: combine a Card, controlled TextField and Dialog with a heading and test two separate instances.