import { mergeConfigs } from '../../common/index.mjs'; import { normaliseDataset } from '../../common/normalise-dataset.mjs'; import { ElementError } from '../../errors/index.mjs'; import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'; import { I18n } from '../../i18n.mjs'; /** * Accordion component * * This allows a collection of sections to be collapsed by default, showing only * their headers. Sections can be expanded or collapsed individually by clicking * their headers. A "Show all sections" button is also added to the top of the * accordion, which switches to "Hide all sections" when all the sections are * expanded. * * The state of each section is saved to the DOM via the `aria-expanded` * attribute, which also provides accessibility. * * @preserve */ class Accordion extends GOVUKFrontendComponent { /** * @param {Element | null} $root - HTML element to use for accordion * @param {AccordionConfig} [config] - Accordion config */ constructor($root, config = {}) { super($root); this.config = void 0; this.i18n = void 0; this.controlsClass = 'govuk-accordion__controls'; this.showAllClass = 'govuk-accordion__show-all'; this.showAllTextClass = 'govuk-accordion__show-all-text'; this.sectionClass = 'govuk-accordion__section'; this.sectionExpandedClass = 'govuk-accordion__section--expanded'; this.sectionButtonClass = 'govuk-accordion__section-button'; this.sectionHeaderClass = 'govuk-accordion__section-header'; this.sectionHeadingClass = 'govuk-accordion__section-heading'; this.sectionHeadingDividerClass = 'govuk-accordion__section-heading-divider'; this.sectionHeadingTextClass = 'govuk-accordion__section-heading-text'; this.sectionHeadingTextFocusClass = 'govuk-accordion__section-heading-text-focus'; this.sectionShowHideToggleClass = 'govuk-accordion__section-toggle'; this.sectionShowHideToggleFocusClass = 'govuk-accordion__section-toggle-focus'; this.sectionShowHideTextClass = 'govuk-accordion__section-toggle-text'; this.upChevronIconClass = 'govuk-accordion-nav__chevron'; this.downChevronIconClass = 'govuk-accordion-nav__chevron--down'; this.sectionSummaryClass = 'govuk-accordion__section-summary'; this.sectionSummaryFocusClass = 'govuk-accordion__section-summary-focus'; this.sectionContentClass = 'govuk-accordion__section-content'; this.$sections = void 0; this.$showAllButton = null; this.$showAllIcon = null; this.$showAllText = null; this.config = mergeConfigs(Accordion.defaults, config, normaliseDataset(Accordion, this.$root.dataset)); this.i18n = new I18n(this.config.i18n); const $sections = this.$root.querySelectorAll(`.${this.sectionClass}`); if (!$sections.length) { throw new ElementError({ component: Accordion, identifier: `Sections (\`