When to use it#
- Use it for content that some readers want and most don’t: opening hours, help with a question, a longer explanation after a short one.
- Name the button after what the panel holds (“Opening hours”) and keep the name the same open and closed. The chevron and a screen reader show whether it is open.
- Keep what the page needs out of it: an error message, or the only way to finish a task.
- For a list of questions and answers, use an Accordion, which puts a heading around each button.
- Not for a menu that shows in full on a wide screen and sits behind a button on a narrow one. The button would say “closed” while the menu is on screen, and the library has no component for that. For a floating panel over the page, use a Popover.
Example#
Monday to Friday 10 to 19. Saturday 10 to 15. Closed on Sunday.
import { Disclosure } from '@kvirn-ui/react'
import { useDisclosureTexts } from './texts.ts'
export function DefaultDisclosure() {
const { texts } = useDisclosureTexts()
return (
<Disclosure.Root>
<Disclosure.Trigger>{texts.openingHours}</Disclosure.Trigger>
<Disclosure.Panel>
<p>{texts.openingHoursText}</p>
</Disclosure.Panel>
</Disclosure.Root>
)
}Use cases#
Show more under a short text
A news item or a page gives its main point first, and the details sit behind one button for the readers who want them. The panel follows the button, so the next Tab goes into the details.
The library has moved. From 1 March the main library is in the old town hall on Torget.
The children’s library and the reading room move at the same time. The loan desk is open during the move, and you can return books in the book drop outside.
import { Disclosure } from '@kvirn-ui/react'
import { useDisclosureTexts } from './texts.ts'
export function ShowMore() {
const { texts } = useDisclosureTexts()
return (
<div>
<p>
<strong>{texts.news}.</strong> {texts.newsIntro}
</p>
<Disclosure.Root>
<Disclosure.Trigger>{texts.showMore}</Disclosure.Trigger>
<Disclosure.Panel>
<p>{texts.newsMore}</p>
</Disclosure.Panel>
</Disclosure.Root>
</div>
)
}Information: Did you know?
Don’t swap “Show more” for “Show less” when it opens. Name the content (“More about the move”) and leave the text alone. A screen reader already says whether the button is expanded, so a changing name says it twice (see What you need to do).
Help with a form question
A form asks for something people may wonder about. The explanation sits behind a button right after the field, so those who need it can read it and the form stays short.
We call you only if something is missing from your application.
import { Disclosure, Field, Stack, TextInput } from '@kvirn-ui/react'
import { useDisclosureTexts } from './texts.ts'
export function HelpWithAQuestion() {
const { texts } = useDisclosureTexts()
return (
<Stack className="kv-stack--gap-8">
<Field.Root>
<Field.Label>{texts.phone}</Field.Label>
<TextInput name="phone" type="tel" autoComplete="tel" />
</Field.Root>
<Disclosure.Root>
<Disclosure.Trigger>{texts.whyPhone}</Disclosure.Trigger>
<Disclosure.Panel>
<p>{texts.whyPhoneText}</p>
</Disclosure.Panel>
</Disclosure.Root>
</Stack>
)
}Findable with the browser’s search
A resident presses Ctrl+F for a word that is inside a closed panel. With hiddenUntilFound the browser finds the word and opens the panel. onOpenChange reports it with the reason find-in-page, so your own state stays in step.
Use the book drop outside the entrance, or hand the book in at the loan desk. Your loan ends when we scan it.
Last change: –
import { Disclosure } from '@kvirn-ui/react'
import { useState } from 'react'
import { useDisclosureTexts } from './texts.ts'
export function FindInPage() {
const { texts } = useDisclosureTexts()
const [reason, setReason] = useState('–')
return (
<div>
<Disclosure.Root
hiddenUntilFound
onOpenChange={(_open, details) => setReason(details.reason)}
>
<Disclosure.Trigger>{texts.help}</Disclosure.Trigger>
<Disclosure.Panel>
<p>{texts.helpText}</p>
</Disclosure.Panel>
</Disclosure.Root>
<p>
{texts.lastReason} <code>{reason}</code>
</p>
</div>
)
}Props in this example:
hiddenUntilFoundon Disclosure.RootonOpenChangeon Disclosure.Root
Information: Did you know?
Only some browsers can reveal a hidden panel. In the others the panel stays hidden, so nothing breaks, but a match inside it isn’t found.
Opened from somewhere else
Another button or a saved choice opens the panel. Keep the state yourself with open and onOpenChange. onOpenChange only reports what the reader did, so you change open yourself.
Monday to Friday 10 to 19. Saturday 10 to 15. Closed on Sunday.
Closed
import { Button, Disclosure } from '@kvirn-ui/react'
import { useState } from 'react'
import { useDisclosureTexts } from './texts.ts'
export function ControlledDisclosure() {
const { texts } = useDisclosureTexts()
const [open, setOpen] = useState(false)
return (
<div>
<Disclosure.Root open={open} onOpenChange={setOpen}>
<Disclosure.Trigger>{texts.openingHours}</Disclosure.Trigger>
<Disclosure.Panel>
<p>{texts.openingHoursText}</p>
</Disclosure.Panel>
</Disclosure.Root>
<p>{open ? texts.stateOpen : texts.stateClosed}</p>
<Button aria-expanded={open} onClick={() => setOpen(!open)}>
{texts.toggleIt}
</Button>
</div>
)
}Props in this example:
openon Disclosure.RootonOpenChangeon Disclosure.Root
Accessibility#
What it does for you#
- APG pattern: Disclosure: a button that shows and hides one panel of content.
- Deviations: none from the APG keyboard practice. Escape is not handled: APG does not require it for a disclosure, and a panel is not a layer. Two decisions: find-in-page is opt-in (
hiddenUntilFound), and a panel that is shown from a breakpoint without JavaScript is not supported (see Consumer responsibilities). - Native elements used:
<button type="button">(Trigger),<div>(Panel). Nothing for Root: it renders no element.
A Disclosure is a button that shows and hides a panel of content. It holds the open state, wires the two together, and never moves focus. Accordion is a list of disclosures with headings (accordion.a11y.md).
| Part | Element / role | ARIA / state | Notes |
|---|---|---|---|
| Disclosure.Root | none: it renders no element | none | Owns the open state: open / defaultOpen / onOpenChange, disabled, focusableWhenDisabled, hiddenUntilFound |
| Disclosure.Trigger | <button type="button"> | aria-expanded="true" | "false", aria-controls (the panel's id). data-open while open, data-disabled | Class kv-disclosure-trigger. A decorative Icon follows the content: chevron-down closed, chevron-up open, class kv-disclosure-icon, aria-hidden |
| Disclosure.Panel | <div>, always rendered | hidden while closed (hidden="until-found" with hiddenUntilFound). data-open while open | Class kv-disclosure-panel. No role: the panel is not a landmark, and the trigger names nothing for it |
useDisclosure | the same attributes, for your own elements | triggerProps, panelProps, isOpen, isDisabled, isFocusVisible, triggerId, panelId | Options as the Root. panelProps.ref must reach the panel for find-in-page |
What it does, each rule proved by an automated test:
- Wiring.
aria-controlsis the panel's id on every render (the panel is always in the DOM),aria-expandedfollows the state, and the trigger is a real<button type="button">. Ids come fromuseId, so the server and the client agree. - Closed means hidden. A closed panel is
hidden, so its content is out of the Tab order and the accessibility tree. An open panel's content follows the trigger in the Tab order. - Controlled and uncontrolled.
openis the state when it is given, andonOpenChangeonly reports.defaultOpenstarts it open. - Disabled.
disabledis the native attribute and takes the trigger out of the Tab order. WithfocusableWhenDisabledit isaria-disabled="true"and stays a Tab stop. Neither opens, and no handler runs. - Find-in-page. With
hiddenUntilFoundthe closed panel ishidden="until-found". When the browser reveals it (a find-in-page match or a#fragmentlink), the hook opens the state and callsonOpenChange(true, { reason: 'find-in-page', event }). Browsers without support treat the value ashidden. A controlled consumer that ignoresonOpenChange(true)keeps the panel closed: the hook putshidden="until-found"back after the browser removed it, so the panel never shows underaria-expanded="false". - The state is never colour alone. The chevron flips (down to up) and
aria-expandedchanges. The chevron isaria-hidden: the name is the consumer's text. - Class, handlers and refs merge on every part. The trigger's
onClickis gated: it isn't called while disabled. - Dev warnings: a Trigger or Panel outside a Root.
It announces nothing.
What you need to do#
- Name the trigger with visible text that says what the panel holds ("Opening hours"), the same open and closed (4.1.2 Name, Role, Value; 2.5.3 Label in Name). Never "Show more" for one state and "Show less" for the other:
aria-expandedcarries the state. - Render the Panel right after the Trigger, in the same parent, so the Tab order matches the visual order (2.4.3 Focus Order).
- Keep what the page needs to work out of a closed panel, and do not hide an error message or the only way to complete a task in one (3.3.1 Error Identification).
- A panel shown from a breakpoint, expanded without JavaScript, is not a Disclosure feature. The trigger's
aria-expanded="false"would lie while the panel is visible. A navigation menu that is always shown on a wide screen and a button on a narrow one is not provided: no menu component does it (the Navigation component covers nested lists and a horizontal bar). Use your own CSS: (1) from that width, show the panel with a rule on.kv-disclosure-panel[hidden]that setsdisplay: block, andcontent-visibility: visiblewhen you useuntil-found. (2) From the same width, hide the trigger withdisplay: none, which also removes it from the accessibility tree. The library does not support or test this. - Server rendering: a closed panel is
hiddenin the markup, and without JavaScript the trigger does nothing. Content that must be readable without JavaScript is not in a Disclosure, or isdefaultOpen. - Everything the Button page asks of you applies to the trigger too.
Warning: Don't forget
Put the panel right after its button, in the same parent. If it sits elsewhere, Tab goes to the panel in a different order from what people see (2.4.3 Focus Order).
Visual and modes#
- Focus indicator: the trigger shows a focus ring with at least 3:1 contrast against its background (2.4.7 Focus Visible, AA; 2.4.13 Focus Appearance, AAA).
- Target size: the trigger is larger than the 24 by 24 pixel minimum in both densities (2.5.8 Target Size (Minimum), AA).
- Colour: the trigger's text has at least 4.5:1 contrast on the page. The open state is shown by the chevron and
aria-expanded, never by colour (1.4.1 Use of Color, A). It adds no new colour pair. - Forced colours: the chevron uses the text colour, so it follows the system's text colour. The state is the shape of the chevron.
- Reduced motion: nothing animates. The panel appears and disappears at once.
- Reflow: at 320 pixels wide the trigger's text wraps, even inside a long word, and the chevron stays at the end of the line (1.4.10 Reflow, AA).
- RTL: the chevron is at the left, the text starts at the right. A chevron pointing up or down needs no mirroring.
Focus management#
- Initial focus: not moved. Opening or closing never moves focus: a keyboard user stays on the trigger, and Tab goes into the panel, which is next in the DOM (render
Disclosure.Panelright afterDisclosure.Trigger). - Trap: no.
- Restore to: not needed. Focus is on the trigger when it closes. If the consumer closes the panel from inside it (a "Close" button in the content), they return focus to the trigger themselves.
- Never obscured by: the panel is in flow, so a sticky header is the consumer's
scroll-padding(2.4.11 Focus Not Obscured (Minimum)).
WCAG success criteria#
- 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): a native
<button>,aria-expandedandaria-controls. - 1.4.1 Use of Color (A), 1.4.11 Non-text Contrast (AA): the chevron changes shape, and it uses the text colour.
- 1.4.10 Reflow (AA), 1.4.12 Text Spacing (AA): wrapping text, no fixed heights.
- 2.1.1 Keyboard (A), 2.1.2 No Keyboard Trap (A), 2.1.4 Character Key Shortcuts (A): the native button keys, Tab leaves, no shortcuts.
- 2.4.3 Focus Order (A), 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA), 2.5.8 Target Size (Minimum) (AA).
- 3.2.1 On Focus (A), 3.2.2 On Input (A): opening needs a press, and nothing moves focus.
- 2.5.3 Label in Name (A): the trigger's name is its visible text, the same open and closed (
disclosure.test.tsx › the trigger keeps its name open and closed). Choosing words that match is yours.
Keyboard#
- Focus
- native
- Selection follows focus
- n/a
- Arrows wrap
- n/a
- Shortcuts
- none
| Key | Where | What happens |
|---|---|---|
| Tab | before the trigger | Moves focus to the trigger. The trigger is one stop Test: disclosure.test.tsx › Tab focuses the trigger, and is one stop |
| Shift+Tab | on the trigger | Moves focus to the previous focusable element Test: disclosure.test.tsx › Shift+Tab leaves the trigger backwards |
| Enter / Space | on the trigger | Opens the panel, or closes it when it is open. Focus stays on the trigger Test: disclosure.test.tsx › Enter and Space on the trigger toggle the panel |
| Tab | on the trigger, panel open | Moves focus into the panel, to its first focusable element, which follows the trigger Test: disclosure.test.tsx › Tab goes from the trigger into an open panel |
| Tab | on the trigger, panel closed | Skips the panel: its content is hidden and not focusable Test: disclosure.test.tsx › Tab skips the content of a closed panel |
| Enter / Space | on a disabled trigger | Nothing: the panel does not change. A natively disabled trigger is not a Tab stop Test: disclosure.test.tsx › a disabled trigger does not open, and leaves the Tab order |
The disclosure handles no other key. Escape is the page's own (APG does not require it), and the arrow keys, Home and End are the content's. The component prevents none of them.
API reference#
import { Disclosure, useDisclosure } from '@kvirn-ui/react'Disclosure.Root
Renders no element. It owns the open state and passes it to the trigger and the panel inside it. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled: whether the panel is open. Pair it with onOpenChange, which only reports, so you change open yourself. |
defaultOpen | boolean | false | Uncontrolled: whether the panel starts open. |
onOpenChange | (open: boolean, details: DisclosureChangeDetails) => void | – | Called when the user opens or closes the panel, with the new state and { reason, event }. The reason is "trigger-press" or "find-in-page". Never called for a press while disabled. |
boolean | false | Keeps the closed panel’s text findable with the browser’s find-in-page and #fragment links (hidden="until-found"). The browser then opens it and onOpenChange gets the reason "find-in-page". A browser without support treats it as hidden. | |
disabled | boolean | false | Blocks opening and closing. Without focusableWhenDisabled the trigger is natively disabled and skipped by Tab. |
focusableWhenDisabled | boolean | false | With disabled, keeps the trigger in the Tab order with aria-disabled="true". Opening stays blocked. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called after a click on the trigger, and never while disabled. |
Disclosure.Trigger
Renders <button type="button"> with aria-expanded and aria-controls, and a chevron after its content. It takes every attribute of a <button> except id, type, aria-expanded, aria-controls, aria-disabled and disabled, and passes ref to it. Set disabled on Disclosure.Root.
| Class or attribute | Values | Meaning |
|---|---|---|
kv-disclosure-trigger | always | The part class. The default theme styles it. |
kv-disclosure-icon | always | The chevron after the text: down while closed, up while open. It is aria-hidden. |
aria-expanded | "true" or "false" | Whether the panel is open. Set by the Disclosure: you can’t pass it. |
aria-controls | the panel’s id | Points at the panel, which is always rendered. Set by the Disclosure. |
data-open | present or absent | The panel is open. |
data-disabled | present or absent | The trigger is disabled. |
data-focus-visible | present or absent | The trigger has keyboard focus (:focus-visible). |
aria-disabled | "true" or absent | Set when disabled and focusableWhenDisabled are both on. |
Disclosure.Panel
Renders <div> with no role, always rendered and hidden while closed. It takes every attribute of a <div> except id and hidden, and passes ref to it. Render it right after the trigger.
| Class or attribute | Values | Meaning |
|---|---|---|
kv-disclosure-panel | always | The part class. The default theme styles it. |
hidden | "" or "until-found", or absent | Set while closed. With hiddenUntilFound the value is until-found once the page has loaded. |
data-open | present or absent | The panel is open. |
useDisclosure
Options
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled: whether the panel is open. Pair it with onOpenChange, which only reports, so you change open yourself. |
defaultOpen | boolean | false | Uncontrolled: whether the panel starts open. |
onOpenChange | (open: boolean, details: DisclosureChangeDetails) => void | – | Called when the user opens or closes the panel, with the new state and { reason, event }. The reason is "trigger-press" or "find-in-page". Never called for a press while disabled. |
boolean | false | Keeps the closed panel’s text findable with the browser’s find-in-page and #fragment links (hidden="until-found"). The browser then opens it and onOpenChange gets the reason "find-in-page". A browser without support treats it as hidden. | |
disabled | boolean | false | Blocks opening and closing. Without focusableWhenDisabled the trigger is natively disabled and skipped by Tab. |
focusableWhenDisabled | boolean | false | With disabled, keeps the trigger in the Tab order with aria-disabled="true". Opening stays blocked. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called after a click on the trigger, and never while disabled. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
triggerProps | DisclosureTriggerPartProps | – | Spread on a <button>: class, id, type, aria-expanded, aria-controls and the click handler. |
panelProps | DisclosurePanelPartProps | – | Spread on a <div>: class, id, hidden while closed and a ref. The ref must reach the element for find-in-page. |
isOpen | boolean | – | Whether the panel is open. |
isDisabled | boolean | – | Whether the trigger is disabled. |
isFocusVisible | boolean | – | Whether the trigger has keyboard (:focus-visible) focus. |
triggerId | string | – | The trigger’s id. |
panelId | string | – | The panel’s id, which aria-controls uses. |
Known issues#
hidden="until-found"support differs. Chromium reveals the panel on a match. A browser without support treats the panel ashidden, so find-in-page misses it, and nothing else breaks. It is opt-in for that reason.- No Escape. APG does not require it for a disclosure. Closing from inside a long panel is the trigger or the consumer's own button.
- The chevron is part of
Disclosure.Trigger. A different icon or no icon meansuseDisclosureon your own button.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | disclosure.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | disclosure.test.tsx (axe and role, name and state): an open disclosure: expanded, visible, data-open, no axe violations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 6 rows, each names a test in disclosure.test.tsx |
| Focus order and management | pass | n/a | pass | disclosure.test.tsx: Tab focuses the trigger, and is one stop |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
With a screen reader and the keyboard, try:
- Tab to the button: its name and "collapsed" are read.
- Press Enter, then Space: "expanded" is read, and focus stays on the button.
- Open, Tab goes into the panel's first control. Closed, Tab skips the panel.
- The name is the same open and closed.
- With
hiddenUntilFound, find-in-page on a word in the closed panel opens it. - At 320px the text wraps and the chevron stays visible.