The accordion component provides a set of classes for grouping content into panels that readers can expand or collapse.
Inside .accordion, use an .accordion-item for each panel. Start each item with an .accordion-item-header containing a heading with the .accordion-item-title class. The heading holds the .accordion-item-toggle button.
Place .accordion-item-content after the header. Put the main content in .accordion-item-body and add an .accordion-item-footer if you need room for extra details.
For an item that starts expanded, add .show to the item and set aria-expanded="true" on its button. For an item that starts collapsed, use .hide and aria-expanded="false". Once the accordion is initialized, readers can toggle a panel by clicking its button or pressing Enter or Space while the button has focus.
Run the accordion generator after the markup is in the DOM. The generator links each toggle to its panel and updates aria-expanded as the panel opens or closes.
import generateAccordions from "@graupl/core/accordion/generator";
generateAccordions();To let readers open or close every panel at once, put the buttons in an .accordion-control-container. Give both buttons the .accordion-control class, then add .expand-all to the button that opens the panels and .collapse-all to the one that closes them.
By default, readers can expand multiple panels and collapse all of them. To limit the accordion to one expanded panel at a time, set allowExpandMultiple to false.
Set allowCollapseAll to false to keep the last expanded panel from closing. Start with at least one panel expanded when using this setting. If you also limit the accordion to one expanded panel, start with exactly one.
Set optionalKeySupport to true so readers can use the Up and Down arrows, Home, and End to move between item toggles.
Pass these options to the generator to apply them to the accordions it initializes. To configure an individual accordion, set data-graupl-accordion-options on the .accordion element. The attribute takes JSON, such as data-graupl-accordion-options='{"optionalKeySupport":true}'.
generateAccordions({
options: {
allowExpandMultiple: false,
allowCollapseAll: false,
optionalKeySupport: true,
},
});By default, no theme variants are generated.
If you are compiling your own version of Graupl, you can set the accordion module’s $themeable variable to true to generate the .primary, .secondary, and .tertiary variants. Add the variant class to .accordion to style the whole accordion, or to an element inside it to style that part separately.
The following custom properties can be used to customize the accordion component:
| Property | Description | Default Value |
|---|---|---|
--graupl-accordion-padding-x | The horizontal padding of the accordion. | var(--graupl-spacer-0) |
--graupl-accordion-padding-y | The vertical padding of the accordion. | var(--graupl-spacer-0) |
--graupl-accordion-padding | The padding of the accordion. | var(--graupl-accordion-padding-y) var(--graupl-accordion-padding-x) |
--graupl-accordion-column-gap | The column gap of the accordion. | var(--graupl-spacer-0) |
--graupl-accordion-row-gap | The row gap of the accordion. | var(--graupl-spacer-0) |
--graupl-accordion-gap | The gap of the accordion. | var(--graupl-accordion-row-gap) var(--graupl-accordion-column-gap) |
--graupl-accordion-color | The text color of the accordion. | var(--graupl-color) |
--graupl-accordion-background | The background of the accordion. | var(--graupl-background) |
--graupl-accordion-border-color | The border color of the accordion. | var(--graupl-accordion-color) |
--graupl-accordion-item-color | The text color of the accordion item. | var(--graupl-accordion-color) |
--graupl-accordion-item-background | The background of the accordion item. | var(--graupl-accordion-background) |
--graupl-accordion-item-border-color | The border color of the accordion item. | var(--graupl-accordion-border-color) |
--graupl-accordion-item-border-top-left-radius | The border top left radius of the accordion item. | var(--graupl-border-top-left-radius) |
--graupl-accordion-item-border-top-right-radius | The border top right radius of the accordion item. | var(--graupl-border-top-right-radius) |
--graupl-accordion-item-border-bottom-right-radius | The border bottom right radius of the accordion item. | var(--graupl-border-bottom-right-radius) |
--graupl-accordion-item-border-bottom-left-radius | The border bottom left radius of the accordion item. | var(--graupl-border-bottom-left-radius) |
--graupl-accordion-item-border-radius | The border radius of the accordion item. | var(--graupl-accordion-item-border-top-left-radius) var(--graupl-accordion-item-border-top-right-radius) var(--graupl-accordion-item-border-bottom-right-radius) var(--graupl-accordion-item-border-bottom-left-radius) |
--graupl-accordion-item-border-top-width | The border top width of the accordion item. | var(--graupl-border-top-width) |
--graupl-accordion-item-border-right-width | The border right width of the accordion item. | var(--graupl-border-right-width) |
--graupl-accordion-item-border-bottom-width | The border bottom width of the accordion item. | var(--graupl-border-bottom-width) |
--graupl-accordion-item-border-left-width | The border left width of the accordion item. | var(--graupl-border-left-width) |
--graupl-accordion-item-border-width | The border width of the accordion item. | var(--graupl-accordion-item-border-top-width) var(--graupl-accordion-item-border-right-width) var(--graupl-accordion-item-border-bottom-width) var(--graupl-accordion-item-border-left-width) |
--graupl-accordion-item-border-top-style | The border top style of the accordion item. | var(--graupl-border-top-style) |
--graupl-accordion-item-border-right-style | The border right style of the accordion item. | var(--graupl-border-right-style) |
--graupl-accordion-item-border-bottom-style | The border bottom style of the accordion item. | var(--graupl-border-bottom-style) |
--graupl-accordion-item-border-left-style | The border left style of the accordion item. | var(--graupl-border-left-style) |
--graupl-accordion-item-border-style | The border style of the accordion item. | var(--graupl-accordion-item-border-top-style) var(--graupl-accordion-item-border-right-style) var(--graupl-accordion-item-border-bottom-style) var(--graupl-accordion-item-border-left-style) |
--graupl-accordion-item-header-padding-x | The horizontal padding of the item header. | var(--graupl-spacer-0) |
--graupl-accordion-item-header-padding-y | The vertical padding of the item header. | var(--graupl-spacer-0) |
--graupl-accordion-item-header-padding | The padding of the item header. | var(--graupl-accordion-item-header-padding-y) var(--graupl-accordion-item-header-padding-x) |
--graupl-accordion-item-header-margin | The margin of the item header. | calc(-1 * min(var(--graupl-accordion-item-border-top-width), var(--graupl-accordion-item-border-bottom-width), var(--graupl-accordion-item-border-left-width), var(--graupl-accordion-item-border-right-width))) |
--graupl-accordion-item-title-padding-x | The horizontal padding of the item title. | var(--graupl-spacer-0) |
--graupl-accordion-item-title-padding-y | The vertical padding of the item title. | var(--graupl-spacer-0) |
--graupl-accordion-item-title-padding | The padding of the item title. | var(--graupl-accordion-item-title-padding-y) var(--graupl-accordion-item-title-padding-x) |
--graupl-accordion-item-title-margin-x | The horizontal margin of the item title. | var(--graupl-spacer-0) |
--graupl-accordion-item-title-margin-y | The vertical margin of the item title. | var(--graupl-spacer-0) |
--graupl-accordion-item-title-margin | The margin of the item title. | var(--graupl-accordion-item-title-margin-y) var(--graupl-accordion-item-title-margin-x) |
--graupl-accordion-item-title-color | The text color of the item title. | var(--graupl-accordion-item-color) |
--graupl-accordion-item-title-background | The background of the item title. | var(--graupl-accordion-item-background) |
--graupl-accordion-item-title-border-color | The border color of the item title. | var(--graupl-accordion-item-border-color) |
--graupl-accordion-item-title-font-size | The font size of the item title. | var(--graupl-h5-font-size) |
--graupl-accordion-item-title-font-weight | The font weight of the item title. | var(--graupl-h5-font-weight) |
--graupl-accordion-item-title-font-family | The font family of the item title. | var(--graupl-h5-font-family) |
--graupl-accordion-item-title-line-height | The line height of the item title. | var(--graupl-h5-line-height) |
--graupl-accordion-item-content-color | The text color of the item panel. | var(--graupl-accordion-item-color) |
--graupl-accordion-item-content-background | The background of the item panel. | var(--graupl-accordion-item-background) |
--graupl-accordion-item-content-padding-x | The horizontal padding of the item panel. | var(--graupl-spacer-5) |
--graupl-accordion-item-content-padding-y | The vertical padding of the item panel. | var(--graupl-spacer-5) |
--graupl-accordion-item-content-padding | The padding of the item panel. | var(--graupl-accordion-item-content-padding-y) var(--graupl-accordion-item-content-padding-x) |
--graupl-accordion-item-content-column-gap | The column gap of the item panel. | var(--graupl-spacer-3) |
--graupl-accordion-item-content-row-gap | The row gap of the item panel. | var(--graupl-spacer-0) |
--graupl-accordion-item-content-gap | The gap of the item panel. | var(--graupl-accordion-item-content-row-gap) var(--graupl-accordion-item-content-column-gap) |
--graupl-accordion-item-body-padding-x | The horizontal padding of the item body. | var(--graupl-spacer-0) |
--graupl-accordion-item-body-padding-y | The vertical padding of the item body. | var(--graupl-spacer-0) |
--graupl-accordion-item-body-padding | The padding of the item body. | var(--graupl-accordion-item-body-padding-y) var(--graupl-accordion-item-body-padding-x) |
--graupl-accordion-item-footer-padding-x | The horizontal padding of the item footer. | var(--graupl-spacer-0) |
--graupl-accordion-item-footer-padding-y | The vertical padding of the item footer. | var(--graupl-spacer-0) |
--graupl-accordion-item-footer-padding | The padding of the item footer. | var(--graupl-accordion-item-footer-padding-y) var(--graupl-accordion-item-footer-padding-x) |
--graupl-accordion-item-content-transform | The transform of the item panel. | translateY(-100%) |
--graupl-accordion-item-content-open-transform | The transform of the expanded item panel. | translateY(0) |
--graupl-accordion-item-toggle-padding-x | The horizontal padding of the item toggle. | var(--graupl-button-padding-x) |
--graupl-accordion-item-toggle-padding-y | The vertical padding of the item toggle. | var(--graupl-button-padding-y) |
--graupl-accordion-item-toggle-padding | The padding of the item toggle. | var(--graupl-accordion-item-toggle-padding-y) var(--graupl-accordion-item-toggle-padding-x) |
--graupl-accordion-item-toggle-transform | The transform of the item toggle. | none |
--graupl-accordion-item-toggle-visited-transform | The transform of the visited item toggle. | var(--graupl-accordion-item-toggle-transform) |
--graupl-accordion-item-toggle-focus-transform | The transform of the focused item toggle. | var(--graupl-accordion-item-toggle-transform) |
--graupl-accordion-item-toggle-hover-transform | The transform of the hovered item toggle. | var(--graupl-accordion-item-toggle-transform) |
--graupl-accordion-item-toggle-active-transform | The transform of the active item toggle. | none |
--graupl-accordion-item-toggle-disabled-transform | The transform of the disabled item toggle. | none |
--graupl-accordion-item-toggle-pseudo-transform | The transform of the toggle indicator. | none |
--graupl-accordion-item-toggle-pseudo-open-transform | The transform of the expanded toggle indicator. | rotate(-180deg) |
--graupl-accordion-transition-duration | The transition duration of the accordion. | var(--graupl-transition-duration-slow) |
--graupl-accordion-transition-timing-function | The transition timing function of the accordion. | var(--graupl-transition-timing-function) |
--graupl-accordion-item-transition | The transition of the accordion item. | height var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) |
--graupl-accordion-item-transition-reduced-motion | The transition of the accordion item when reduced motion is enabled. | none |
--graupl-accordion-item-toggle-pseudo-transition | The transition of the toggle indicator. | transform var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) |
--graupl-accordion-item-toggle-pseudo-transition-reduced-motion | The transition of the toggle indicator when reduced motion is enabled. | none |
--graupl-accordion-item-content-transition | The transition of the item panel. | opacity var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) transform var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) |
--graupl-accordion-item-content-transition-reduced-motion | The transition of the item panel when reduced motion is enabled. | opacity var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) |
--graupl-accordion-item-toggle-transition | The transition of the item toggle. | background var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function), color var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function), transform var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) |
--graupl-accordion-item-toggle-transition-reduced-motion | The transition of the item toggle when reduced motion is enabled. | background var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function), color var(--graupl-accordion-transition-duration) var(--graupl-accordion-transition-timing-function) |
--graupl-accordion-item-toggle-color | The text color of the item toggle. | var(--graupl-accordion-item-color) |
--graupl-accordion-item-toggle-visited-color | The text color of the visited item toggle. | var(--graupl-accordion-item-toggle-color) |
--graupl-accordion-item-toggle-focus-color | The text color of the focused item toggle. | var(--graupl-accordion-item-toggle-color) |
--graupl-accordion-item-toggle-hover-color | The text color of the hovered item toggle. | var(--graupl-accordion-background) |
--graupl-accordion-item-toggle-active-color | The text color of the active item toggle. | var(--graupl-accordion-item-toggle-hover-color) |
--graupl-accordion-item-toggle-disabled-color | The text color of the disabled item toggle. | var(--graupl-theme-active--primary--200) |
--graupl-accordion-item-toggle-background | The background of the item toggle. | var(--graupl-accordion-background) |
--graupl-accordion-item-toggle-visited-background | The background of the visited item toggle. | var(--graupl-accordion-item-toggle-background) |
--graupl-accordion-item-toggle-focus-background | The background of the focused item toggle. | var(--graupl-accordion-item-toggle-background) |
--graupl-accordion-item-toggle-hover-background | The background of the hovered item toggle. | var(--graupl-accordion-color) |
--graupl-accordion-item-toggle-active-background | The background of the active item toggle. | var(--graupl-accordion-item-toggle-hover-background) |
--graupl-accordion-item-toggle-disabled-background | The background of the disabled item toggle. | var(--graupl-accordion-background) |
--graupl-accordion-item-toggle-border-color | The border color of the item toggle. | var(--graupl-accordion-item-border-color) |
--graupl-accordion-item-toggle-visited-border-color | The border color of the visited item toggle. | var(--graupl-accordion-item-toggle-border-color) |
--graupl-accordion-item-toggle-focus-border-color | The border color of the focused item toggle. | var(--graupl-accordion-item-toggle-border-color) |
--graupl-accordion-item-toggle-hover-border-color | The border color of the hovered item toggle. | var(--graupl-accordion-item-border-color) |
--graupl-accordion-item-toggle-active-border-color | The border color of the active item toggle. | var(--graupl-accordion-item-toggle-hover-border-color) |
--graupl-accordion-item-toggle-disabled-border-color | The border color of the disabled item toggle. | var(--graupl-theme-active--primary--200) |
--graupl-accordion-control-color | The text color of the bulk control. | var(--graupl-accordion-color) |
--graupl-accordion-control-visited-color | The text color of the visited bulk control. | var(--graupl-accordion-control-color) |
--graupl-accordion-control-focus-color | The text color of the focused bulk control. | var(--graupl-accordion-control-color) |
--graupl-accordion-control-hover-color | The text color of the hovered bulk control. | var(--graupl-accordion-background) |
--graupl-accordion-control-active-color | The text color of the active bulk control. | var(--graupl-accordion-control-hover-color) |
--graupl-accordion-control-disabled-color | The text color of the disabled bulk control. | var(--graupl-theme-active--primary--200) |
--graupl-accordion-control-background | The background of the bulk control. | var(--graupl-accordion-background) |
--graupl-accordion-control-visited-background | The background of the visited bulk control. | var(--graupl-accordion-control-background) |
--graupl-accordion-control-focus-background | The background of the focused bulk control. | var(--graupl-accordion-control-background) |
--graupl-accordion-control-hover-background | The background of the hovered bulk control. | var(--graupl-accordion-color) |
--graupl-accordion-control-active-background | The background of the active bulk control. | var(--graupl-accordion-control-hover-background) |
--graupl-accordion-control-disabled-background | The background of the disabled bulk control. | var(--graupl-accordion-background) |
--graupl-accordion-control-border-color | The border color of the bulk control. | var(--graupl-accordion-border-color) |
--graupl-accordion-control-visited-border-color | The border color of the visited bulk control. | var(--graupl-accordion-control-border-color) |
--graupl-accordion-control-focus-border-color | The border color of the focused bulk control. | var(--graupl-accordion-control-border-color) |
--graupl-accordion-control-hover-border-color | The border color of the hovered bulk control. | var(--graupl-accordion-border-color) |
--graupl-accordion-control-active-border-color | The border color of the active bulk control. | var(--graupl-accordion-control-hover-border-color) |
--graupl-accordion-control-disabled-border-color | The border color of the disabled bulk control. | var(--graupl-theme-active--primary--200) |
--graupl-accordion-control-container-padding-x | The horizontal padding of the bulk control container. | 0 |
--graupl-accordion-control-container-padding-y | The vertical padding of the bulk control container. | 0 |
--graupl-accordion-control-container-padding | The padding of the bulk control container. | var(--graupl-accordion-control-container-padding-y) var(--graupl-accordion-control-container-padding-x) |
--graupl-accordion-control-container-column-gap | The column gap of the bulk control container. | var(--graupl-spacer-3) |
--graupl-accordion-control-container-row-gap | The row gap of the bulk control container. | var(--graupl-spacer-3) |
--graupl-accordion-control-container-gap | The gap of the bulk control container. | var(--graupl-accordion-control-container-row-gap) var(--graupl-accordion-control-container-column-gap) |
The following Sass variables can be used to customize the generation of the accordion component:
| Variable | Description | Default Value |
|---|---|---|
$selector-base | The selector base for the component. | "." |
$modifier-selector-base | The selector base for component modifiers. | "." |
$generate-base-theme-map | Whether to include the default accordion theme mappings. | true |
$themeable | Whether to generate accordion theme variants. | false |
$accordion-selector-base | The selector base for the accordion. | "." |
$accordion-selector | The selector for the accordion. | "accordion" |
$accordion-theme-selector-base | The selector base for the accordion theme. | "." |
$accordion-theme-selector-prefix | The selector prefix for the accordion theme. | "" |
$accordion-item-selector-base | The selector base for the accordion item. | "." |
$accordion-item-selector | The selector for the accordion item. | "accordion-item" |
$accordion-item-header-selector-base | The selector base for the accordion item header. | "." |
$accordion-item-header-selector | The selector for the accordion item header. | "accordion-item-header" |
$accordion-item-title-selector-base | The selector base for the accordion item title. | "." |
$accordion-item-title-selector | The selector for the accordion item title. | "accordion-item-title" |
$accordion-item-toggle-selector-base | The selector base for the accordion item toggle. | "." |
$accordion-item-toggle-selector | The selector for the accordion item toggle. | "accordion-item-toggle" |
$accordion-item-content-selector-base | The selector base for the accordion item content. | "." |
$accordion-item-content-selector | The selector for the accordion item content. | "accordion-item-content" |
$accordion-item-body-selector-base | The selector base for the accordion item body. | "." |
$accordion-item-body-selector | The selector for the accordion item body. | "accordion-item-body" |
$accordion-item-footer-selector-base | The selector base for the accordion item footer. | "." |
$accordion-item-footer-selector | The selector for the accordion item footer. | "accordion-item-footer" |
$accordion-item-open-selector-base | The selector base for the expanded item. | "." |
$accordion-item-open-selector | The selector for the expanded item. | "show" |
$accordion-item-close-selector-base | The selector base for the collapsed item. | "." |
$accordion-item-close-selector | The selector for the collapsed item. | "hide" |
$accordion-item-transition-selector-base | The selector base for the transitioning item. | "." |
$accordion-item-transition-selector | The selector for the transitioning item. | "transitioning" |
$accordion-item-toggle-pseudo-selector | The pseudo-element selector for the toggle indicator. | "after" |
$accordion-control-selector-base | The selector base for the accordion control. | "." |
$accordion-control-selector | The selector for the accordion control. | "accordion-control" |
$accordion-control-container-selector-base | The selector base for the accordion control container. | "." |
$accordion-control-container-selector | The selector for the accordion control container. | "accordion-control-container" |
$accordion-item-toggle-initial-transform | The initial transform of the accordion item toggle. | none |
$accordion-item-toggle-final-transform | The transform of the item toggle while pressed. | none |
$accordion-item-toggle-disabled-transform | The disabled transform of the accordion item toggle. | none |
$accordion-item-toggle-pseudo-initial-transform | The initial transform of the toggle indicator. | none |
$accordion-item-toggle-pseudo-content | The content of the toggle indicator. | "▼" |
$accordion-item-content-initial-transform | The transform of the panel when collapsed. | translateY(-100%) |
$accordion-item-content-final-transform | The transform of the panel when expanded. | translateY(0) |
$accordion-theme-mappings | A map of properties and color shades used to generate all accordion variants. | map.merge($-accordion-theme-mappings, $accordion-theme-mappings) |
$accordion-theme-map | A map of all properties, colors, and color shades used to generate all accordion variants. | map.deep-merge($-accordion-theme-map, $accordion-theme-map) |