Compose compound components and slots
Compose parts around one state owner
A compound component is a family of cooperating components, such as a disclosure root, its trigger and its panel. A disclosure reveals or hides content. The root owns the relationship between the parts, and the caller chooses their arrangement. A slot is a named place for caller-supplied content; it can be an ordinary prop containing a React node. This chapter uses an original community workshop capacity board. React 18+ syntax is the proposed baseline. The examples require an approved React DOM project; none is a standalone browser script. Prefer plain props for small components; context is useful when several parts need shared information through intermediate wrappers.
import React, {createContext, useContext, useId, useState} from 'react';import type {ReactNode} from 'react';type DisclosureState = {open: boolean; toggle: () => void; panelId: string};const DisclosureContext = createContext<DisclosureState | null>(null);function useDisclosureContext() { const value = useContext(DisclosureContext); if (!value) throw new Error('Disclosure parts require a root'); return value;}export function Disclosure({children}: {children: ReactNode}) { const [open, setOpen] = useState(false); const panelId = useId(); return <DisclosureContext.Provider value={{open, panelId, toggle: () => setOpen(v => !v)}}> {children}</DisclosureContext.Provider>;}export function Trigger({children}: {children: ReactNode}) { const {open, toggle, panelId} = useDisclosureContext(); return <button type="button" aria-expanded={open} aria-controls={panelId} onClick={toggle}>{children}</button>;}export function Panel({children}: {children: ReactNode}) { const {open, panelId} = useDisclosureContext(); return <div id={panelId} hidden={!open}>{children}</div>;}The root provides an open flag, toggle function and generated panel ID. The native button supports ordinary keyboard activation and retains focus when toggled. The panel remains mounted but hidden while closed, so its local React state is not reset solely by closing it. The contract is one trigger and one panel per root; do not insert multiple panels with the same generated ID. For multiple independent disclosures create multiple roots. Missing roots produce a clear render error; an enclosing error boundary can recover. Context does not cross independent React roots.
Use named slots when structure is fixed
import React from 'react';import type {ReactNode} from 'react';import {Disclosure, Trigger, Panel} from './Disclosure';function Card({heading, actions, children}: {heading: string; actions: ReactNode; children: ReactNode}) { return <article><h2>{heading}</h2><div>{children}</div><footer>{actions}</footer></article>;}export default function Workshop() { return <Card heading="Woodworking" actions={<a href="#schedule">View schedule</a>}> <Disclosure><Trigger>Capacity details</Trigger><Panel>Four places available.</Panel></Disclosure> <p id="schedule">Next session: Monday.</p> </Card>;}The expected initial view is a heading, a collapsed details control, a schedule and a link. Activating Capacity details reveals Four places available. The card fixes heading, body and footer placement but does not inspect or rewrite the caller’s elements. A slot may be absent if the API allows it. A portal can relocate DOM output while preserving React context; it does not automatically implement focus management. Use a portal for an overlay only when layout requires it, not to move ordinary card contents.
Maintain legacy child transformation without depending on it
import React, {Children, cloneElement, isValidElement} from 'react';import type {ReactElement, ReactNode} from 'react';type ItemProps = {highlighted?: boolean; children?: ReactNode};export function Item({highlighted, children}: ItemProps) { return <li className={highlighted ? 'highlighted' : undefined}>{children}</li>;}export function LegacyRow({children}: {children: ReactNode}) { return <ul>{Children.map(children, child => { if (isValidElement<ItemProps>(child) && child.type === Item) { return cloneElement(child, {highlighted: true}); } return child; })}</ul>;}export function Demo() { const item: ReactElement<ItemProps> = <Item>Sandpaper</Item>; return <LegacyRow>{item}</LegacyRow>;}This maintenance-only example accepts direct Item elements and highlights them. Children traversal sees the supplied element structure, not the DOM or the render results of custom components. It does not recursively traverse element children or fragments. cloneElement creates a new element with shallowly merged props; overriding key or ref changes those fields. This can hide where a prop originated. If a caller wraps Item inside another component, the direct type check no longer matches. Do not treat arbitrary strings or other elements as valid list items merely because the function accepts ReactNode. For new code prefer explicit slots or a render function that receives the relevant data. Practice: compose two independent disclosures and predict their state when only one trigger is activated.