Expandable
An expandable, sometimes called an accordion, is an element that can be hidden / revealed with a sliding transition.
From a purely visual standpoint, all you need is a container of class s-expandable
and a child of class s-expandable--content
. Toggle the state class is-expanded
on the container to collapse / expand the content. Care must be taken to keep the collapsing functionality accessible.
Accusata reprimique interesset in sea, est graece dictas scriptorem ea. Consul facete mediocritatem quo et. Idque hendrerit theophrastus in vim, eum possit feugiat fastidii id. Idque invenire at quo, accusata salutandi sadipscing et qui. Eos doming adipiscing ea, eu pri debitis indoctum dissentiet. At libris labores adolescens eos, ex nec nobis aeterno.
In the usual case in which you have a button or other clickable element that toggles the expanded state, you can use the built-in s-expandable-control
functionality. See the JavaScript introduction for general information on JS in Stacks.
- Give the
s-expandable
anid
attribute. - Give your button an
aria-controls
attribute whose value is the ID from 1. - Give your button an
aria-expanded
attribute whose value istrue
orfalse
depending on the initial state of your expandable. - Give your button a
data-controller="s-expandable-control"
attribute. - If your button is an element that is not focusable by default, be sure to make it keyboard-reachable with
tabindex="0"
- Give your button is an
aria-label
attribute that makes it clear that this is a expanding/collapsing control, unless this is already clear from its text content
A common need is for one or more CSS classes to be applied to the expandable control whenever the state is toggled, e.g. to indicate a “selected” state. To achieve this, you can add the data-s-expandable-control-toggle-class
attribute. The value should be a space-separated list of CSS classes. These classes will be added to the control whenever it expands the expandable, and removed whenever it collapses it.
See the previous section for an example of all of this.
If you are writing custom CSS to indicate the selected state of the expandable control, consider targetting [aria-controls="true"]
instead of creating a new class.
If your expandable control is a checkbox or radio button, then the visibility of the expandable will be synchronized with the radio / checkbox state:
When an s-expandable-control
collapses or expands an element, it will fire an event of type s-expandable-control:hide
or s-expandable-control:show
, respectively. This event is fired synchronously. The event’s target element is the s-expandable-control
element, not the expandable itself.