Skip to content

Commit 993473c

Browse files
committed
Allow auto-expand of accordions based on URL
1 parent 5f00d79 commit 993473c

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

_layouts/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ <h1 class="usa-display">{{ page.title }}</h1>
153153
<script src="{{ site.baseurl }}/assets/js/toggle_view.js"></script>
154154
<script src="{{ site.baseurl }}/assets/js/main.js"></script>
155155
<script src="{{ site.baseurl }}/assets/js/anchor.js"></script>
156+
<script src="{{ site.baseurl }}/assets/js/anchor_accordion.js"></script>
156157

157158
<script>
158159
anchors.add([

assets/js/anchor_accordion.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(() => {
2+
const onChange = (_event) => {
3+
const urlHash = new URL(document.URL).hash;
4+
const idFromHash = urlHash.match(/^(#[A-Za-z0-9_-]+)$/);
5+
if (idFromHash) {
6+
const accordionHeading = document.querySelector(
7+
`${idFromHash[1]}.usa-accordion__heading > button`,
8+
);
9+
if (accordionHeading) {
10+
accordionHeading.click();
11+
}
12+
}
13+
};
14+
document.addEventListener('DOMContentLoaded', onChange);
15+
window.addEventListener('hashchange', onChange);
16+
})();

spec/integration/accordion_js_spec.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@
22
require 'pry-byebug'
33

44
RSpec.describe 'accordions on /oidc/authorization/', :js, type: :feature do
5-
it 'loads the page' do
6-
visit '/oidc/authorization/'
7-
first_summary = find_all('summary').first
8-
expect(first_summary.text).to eq 'Type of Service Level'
5+
it 'is not expanded by default' do
6+
visit '/oidc/authorization'
7+
expect(page).to have_element('dt')
8+
expect(page).to have_no_element('dd')
9+
accordion_button = find('#service_level button')
10+
expect(accordion_button.native.attribute('aria-expanded')).to eq('false')
911
end
10-
end
12+
13+
it 'is expanded with an anchor in the URL' do
14+
visit '/oidc/authorization#service_level'
15+
expect(page).to have_element('dt')
16+
expect(page).to have_element('dd')
17+
definition = find('#service_level ~ dd').text
18+
expect(definition).to include('acr.login.gov:verified')
19+
accordion_button = find('#service_level > button')
20+
expect(accordion_button.native.attribute('aria-expanded')).to eq('true')
21+
22+
visit '/oidc/authorization#aal_values'
23+
next_definition = find('#aal_values ~ dd').text
24+
expect(next_definition).to include('http://idmanagement.gov/ns/assurance/aal/2')
25+
end
26+
end

0 commit comments

Comments
 (0)