Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the Accordion component with hidden='until-found' #3053

Merged
merged 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Unreleased

### New features

#### Search within accordion content on supporting browsers

We've updated the Accordion component to use the new [`hidden="until-found"` attribute value](https://developer.chrome.com/articles/hidden-until-found/).

This allows the browser's native 'find in page' functionality to search within and automatically open sections of the accordion. Currently, this functionality is only supported by recent versions of Google Chrome, Microsoft Edge and Samsung Internet.

This was added in [pull request #3053: Enhance the Accordion component with `hidden='until-found'`](https://github.com/alphagov/govuk-frontend/pull/3053).

### Fixes

- [#2998: Refactor back link and breadcrumb chevrons to use ems](https://github.com/alphagov/govuk-frontend/pull/2998)
Expand Down
17 changes: 11 additions & 6 deletions src/govuk/components/accordion/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,21 @@
padding-top: 0;
}

// Hide the body of collapsed sections
.govuk-accordion__section-content {
// Manually apply display: none to browsers that don't have a user agent
// style for it, but override it with content-visibility if the browser
// supports that instead.
.govuk-accordion__section-content[hidden] {
display: none;
@include govuk-responsive-padding(8, "bottom");
@include govuk-responsive-padding(3, "top");
@supports (content-visibility: hidden) {
content-visibility: hidden;
display: inherit;
}
}

// Show the body of expanded sections
// Hide the padding of collapsed sections
.govuk-accordion__section--expanded .govuk-accordion__section-content {
display: block;
@include govuk-responsive-padding(8, "bottom");
@include govuk-responsive-padding(3, "top");
}

.govuk-accordion__show-all {
Expand Down
20 changes: 20 additions & 0 deletions src/govuk/components/accordion/accordion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { I18n } from '../../i18n.mjs'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import '../../vendor/polyfills/Element/prototype/closest.mjs'
import '../../vendor/polyfills/String/prototype/trim.mjs'

/**
Expand Down Expand Up @@ -55,6 +56,7 @@ function Accordion ($module, config) {
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'
Expand All @@ -70,6 +72,7 @@ function Accordion ($module, config) {

this.sectionSummaryClass = 'govuk-accordion__section-summary'
this.sectionSummaryFocusClass = 'govuk-accordion__section-summary-focus'
this.sectionContentClass = 'govuk-accordion__section-content'
}

// Initialize component
Expand Down Expand Up @@ -113,6 +116,11 @@ Accordion.prototype.initControls = function () {

// Handle click events on the show/hide all button
this.$showAllButton.addEventListener('click', this.onShowOrHideAllToggle.bind(this))

// Handle 'beforematch' events, if the user agent supports them
if ('onbeforematch' in document) {
document.addEventListener('beforematch', this.onBeforeMatch.bind(this))
}
}

// Initialise section headers
Expand Down Expand Up @@ -229,6 +237,14 @@ Accordion.prototype.constructHeaderMarkup = function ($headerWrapper, index) {
$heading.appendChild($button)
}

// When a section is opened by the user agent via the 'beforematch' event
Accordion.prototype.onBeforeMatch = function (event) {
var $section = event.target.closest('.' + this.sectionClass)
if ($section) {
this.setExpanded(true, $section)
}
}

// When section toggled, set and store state
Accordion.prototype.onSectionToggle = function ($section) {
var expanded = this.isExpanded($section)
Expand Down Expand Up @@ -258,6 +274,8 @@ Accordion.prototype.setExpanded = function (expanded, $section) {
var $icon = $section.querySelector('.' + this.upChevronIconClass)
var $showHideText = $section.querySelector('.' + this.sectionShowHideTextClass)
var $button = $section.querySelector('.' + this.sectionButtonClass)
var $sectionContent = $section.querySelector('.' + this.sectionContentClass)

var newButtonText = expanded
? this.i18n.t('hideSection')
: this.i18n.t('showSection')
Expand Down Expand Up @@ -288,9 +306,11 @@ Accordion.prototype.setExpanded = function (expanded, $section) {

// Swap icon, change class
if (expanded) {
$sectionContent.removeAttribute('hidden')
$section.classList.add(this.sectionExpandedClass)
$icon.classList.remove(this.downChevronIconClass)
} else {
$sectionContent.setAttribute('hidden', 'until-found')
$section.classList.remove(this.sectionExpandedClass)
$icon.classList.add(this.downChevronIconClass)
}
Expand Down