-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/govuk-frontend/src/govuk/components/details/details.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { ElementError } from '../../errors/index.mjs' | ||
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs' | ||
|
||
/** | ||
* Details component | ||
* | ||
* @preserve | ||
*/ | ||
export class Details extends GOVUKFrontendComponent { | ||
/** @private */ | ||
$module | ||
|
||
/** | ||
* @param {Element | null} $module - HTML element to use for skip link | ||
* @throws {ElementError} when $module is not set or the wrong type | ||
* @throws {ElementError} when no summary element is present | ||
*/ | ||
constructor($module) { | ||
super() | ||
|
||
if (!($module instanceof HTMLDetailsElement)) { | ||
throw new ElementError({ | ||
componentName: 'Details', | ||
element: $module, | ||
expectedType: 'HTMLAnchorElement', | ||
identifier: 'Root element (`$module`)' | ||
}) | ||
} | ||
|
||
this.$module = $module | ||
this.$summary = this.$module.querySelector('.govuk-details__summary') | ||
|
||
if (!this.$summary) { | ||
throw new ElementError({ | ||
componentName: 'Details', | ||
identifier: | ||
'Summary element (`<summary class="govuk-details__summary">`)' | ||
}) | ||
} | ||
|
||
// Give the summary a button role so that dragon recognises it. | ||
// This is a known issue with dragon and the details element and ideally would | ||
// be fixed at the vendor level. | ||
this.$summary.setAttribute('role', 'button') | ||
this.setSummaryExpandedState() | ||
|
||
this.$module.addEventListener('click', () => this.setSummaryExpandedState()) | ||
} | ||
|
||
/** | ||
* Set `aria-expanded` state of summary element | ||
* | ||
* @private | ||
*/ | ||
setSummaryExpandedState() { | ||
this.$summary.setAttribute( | ||
'aria-expanded', | ||
this.$module.open ? 'true' : 'false' | ||
) | ||
} | ||
|
||
/** | ||
* Name for the component used when initialising using data-module attributes. | ||
*/ | ||
static moduleName = 'govuk-details' | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/govuk-frontend/src/govuk/components/details/template.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters