Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Disclosure

A button that shows and hides one panel of content, such as the opening hours or a longer explanation. A chevron and a state that a screen reader (software that reads the screen aloud) reports both show whether it is open, so colour is never the only cue.

Package
@kvirn-ui/react
Pattern
APG Disclosure
Testing
Automated. Core 0 of 2, Release 0 of 3

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#

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.

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.

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.

Last change: –

Props in this example:

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.

Closed

Props in this example:

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).

PartElement / roleARIA / stateNotes
Disclosure.Rootnone: it renders no elementnoneOwns 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-disabledClass 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 renderedhidden while closed (hidden="until-found" with hiddenUntilFound). data-open while openClass kv-disclosure-panel. No role: the panel is not a landmark, and the trigger names nothing for it
useDisclosurethe same attributes, for your own elementstriggerProps, panelProps, isOpen, isDisabled, isFocusVisible, triggerId, panelIdOptions 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-controls is the panel's id on every render (the panel is always in the DOM), aria-expanded follows the state, and the trigger is a real <button type="button">. Ids come from useId, 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. open is the state when it is given, and onOpenChange only reports. defaultOpen starts it open.
  • Disabled. disabled is the native attribute and takes the trigger out of the Tab order. With focusableWhenDisabled it is aria-disabled="true" and stays a Tab stop. Neither opens, and no handler runs.
  • Find-in-page. With hiddenUntilFound the closed panel is hidden="until-found". When the browser reveals it (a find-in-page match or a #fragment link), the hook opens the state and calls onOpenChange(true, { reason: 'find-in-page', event }). Browsers without support treat the value as hidden. A controlled consumer that ignores onOpenChange(true) keeps the panel closed: the hook puts hidden="until-found" back after the browser removed it, so the panel never shows under aria-expanded="false".
  • The state is never colour alone. The chevron flips (down to up) and aria-expanded changes. The chevron is aria-hidden: the name is the consumer's text.
  • Class, handlers and refs merge on every part. The trigger's onClick is 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-expanded carries 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 sets display: block, and content-visibility: visible when you use until-found. (2) From the same width, hide the trigger with display: none, which also removes it from the accessibility tree. The library does not support or test this.
  • Server rendering: a closed panel is hidden in the markup, and without JavaScript the trigger does nothing. Content that must be readable without JavaScript is not in a Disclosure, or is defaultOpen.
  • 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.Panel right after Disclosure.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-expanded and aria-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
KeyWhereWhat happens
Tabbefore the triggerMoves focus to the trigger. The trigger is one stop
Test: disclosure.test.tsx › Tab focuses the trigger, and is one stop
Shift+Tabon the triggerMoves focus to the previous focusable element
Test: disclosure.test.tsx › Shift+Tab leaves the trigger backwards
Enter / Spaceon the triggerOpens 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
Tabon the trigger, panel openMoves 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
Tabon the trigger, panel closedSkips the panel: its content is hidden and not focusable
Test: disclosure.test.tsx › Tab skips the content of a closed panel
Enter / Spaceon a disabled triggerNothing: 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.

PropTypeDefaultDescription
openboolean–Controlled: whether the panel is open. Pair it with onOpenChange, which only reports, so you change open yourself.
defaultOpenbooleanfalseUncontrolled: 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.
hiddenUntilFoundbooleanfalseKeeps 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.
disabledbooleanfalseBlocks opening and closing. Without focusableWhenDisabled the trigger is natively disabled and skipped by Tab.
focusableWhenDisabledbooleanfalseWith disabled, keeps the trigger in the Tab order with aria-disabled="true". Opening stays blocked.
onClickMouseEventHandler<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 attributeValuesMeaning
kv-disclosure-triggeralwaysThe part class. The default theme styles it.
kv-disclosure-iconalwaysThe 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-controlsthe panel’s idPoints at the panel, which is always rendered. Set by the Disclosure.
data-openpresent or absentThe panel is open.
data-disabledpresent or absentThe trigger is disabled.
data-focus-visiblepresent or absentThe trigger has keyboard focus (:focus-visible).
aria-disabled"true" or absentSet 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 attributeValuesMeaning
kv-disclosure-panelalwaysThe part class. The default theme styles it.
hidden"" or "until-found", or absentSet while closed. With hiddenUntilFound the value is until-found once the page has loaded.
data-openpresent or absentThe panel is open.

useDisclosure

Options

PropTypeDefaultDescription
openboolean–Controlled: whether the panel is open. Pair it with onOpenChange, which only reports, so you change open yourself.
defaultOpenbooleanfalseUncontrolled: 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.
hiddenUntilFoundbooleanfalseKeeps 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.
disabledbooleanfalseBlocks opening and closing. Without focusableWhenDisabled the trigger is natively disabled and skipped by Tab.
focusableWhenDisabledbooleanfalseWith disabled, keeps the trigger in the Tab order with aria-disabled="true". Opening stays blocked.
onClickMouseEventHandler<HTMLButtonElement>–Called after a click on the trigger, and never while disabled.

Result

PropTypeDefaultDescription
triggerPropsDisclosureTriggerPartProps–Spread on a <button>: class, id, type, aria-expanded, aria-controls and the click handler.
panelPropsDisclosurePanelPartProps–Spread on a <div>: class, id, hidden while closed and a ref. The ref must reach the element for find-in-page.
isOpenboolean–Whether the panel is open.
isDisabledboolean–Whether the trigger is disabled.
isFocusVisibleboolean–Whether the trigger has keyboard (:focus-visible) focus.
triggerIdstring–The trigger’s id.
panelIdstring–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 as hidden, 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 means useDisclosure on 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 + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassdisclosure.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassdisclosure.test.tsx (axe and role, name and state): an open disclosure: expanded, visible, data-open, no axe violations
Keyboard contract rows (Vitest browser)passn/apass6 rows, each names a test in disclosure.test.tsx
Focus order and managementpassn/apassdisclosure.test.tsx: Tab focuses the trigger, and is one stop
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

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.