-
Notifications
You must be signed in to change notification settings - Fork 7
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
Update on this page and disclosure components to use SVG icons #3261
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1731add
Refactor disclosure component to use SVG icons
davidrapson c8d4554
Refactor on this page to use SVG icons
davidrapson 56a00ab
Remove shared toggle code
davidrapson 33e18b7
Reduce specificity of disclosure styles
davidrapson f2ff212
Update references
davidrapson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
describe('On this page', () => { | ||
it('should allow toggling nested links', () => { | ||
cy.visitComponentUrl('on_this_page/with_nested_links'); | ||
|
||
assertClosed(); | ||
|
||
cy.findByRole('button', { name: /Show sub-headings for Link 2/i }) | ||
.click() | ||
.should('have.attr', 'aria-expanded', 'true'); | ||
|
||
assertOpen(); | ||
|
||
cy.findByRole('button', { name: /Hide sub-headings for Link 2/i }) | ||
.click() | ||
.should('have.attr', 'aria-expanded', 'false'); | ||
|
||
assertClosed(); | ||
}); | ||
|
||
function assertOpen() { | ||
cy.findByText('Link 2.1').should('be.visible'); | ||
} | ||
|
||
function assertClosed() { | ||
cy.findByText('Link 2.1').should('not.be.visible'); | ||
} | ||
}); |
Binary file modified
BIN
-98 Bytes
(97%)
...bitmaps_reference/backstop_default_Components_Disclosure_0_document_0_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-53 Bytes
(99%)
...itmaps_reference/backstop_default_Components_Disclosure_0_document_1_medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-61 Bytes
(99%)
...bitmaps_reference/backstop_default_Components_Disclosure_0_document_2_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+16 Bytes
(100%)
...ference/backstop_default_Components_Disclosure_fallback_0_document_1_medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+16 Bytes
(100%)
...eference/backstop_default_Components_Disclosure_fallback_0_document_2_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+6 Bytes
(100%)
...ps_reference/backstop_default_Components_Disclosure_open_0_document_0_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10 Bytes
(100%)
...s_reference/backstop_default_Components_Disclosure_open_0_document_1_medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+11 Bytes
(100%)
...efault_Components_Disclosure_without_open_summary_-_open_0_document_0_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-19 Bytes
(100%)
...fault_Components_Disclosure_without_open_summary_-_open_0_document_1_medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-106 Bytes
(96%)
...kstop_default_Components_Disclosure_without_open_summary_0_document_0_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-52 Bytes
(99%)
...stop_default_Components_Disclosure_without_open_summary_0_document_1_medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-49 Bytes
(99%)
...kstop_default_Components_Disclosure_without_open_summary_0_document_2_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+16 Bytes
(100%)
...ce/backstop_default_Components_On_this_page_with_columns_0_document_2_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+48 Bytes
(100%)
...p_default_Components_On_this_page_with_nested_links_open_0_document_0_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+16 Bytes
(100%)
...backstop_default_Components_On_this_page_with_no_columns_0_document_2_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions
7
engine/app/components/citizens_advice_components/disclosure.html.erb
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
<div class="cads-disclosure js-disclosure"> | ||
<div class="cads-disclosure js-cads-disclosure"> | ||
<%= tag.button(**button_attrs) do %> | ||
<span class="cads-disclosure__summary js-disclosure-summary"> | ||
<%= render CitizensAdviceComponents::Icons::PlusMinus.new %> | ||
<span class="cads-disclosure__summary js-cads-disclosure-summary"> | ||
<%= closed_summary %> | ||
</span> | ||
<% end %> | ||
<div class="cads-disclosure__details js-disclosure-details" id="<%= id %>"> | ||
<div class="cads-disclosure__details js-cads-disclosure-details" id="<%= id %>"> | ||
<%= content %> | ||
</div> | ||
</div> |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,70 @@ | ||
import initShowHideWithToggle from '../utils/show-hide'; | ||
|
||
const selectors = { | ||
component: '.js-disclosure', | ||
toggle: '.js-disclosure-toggle', | ||
}; | ||
|
||
const classes = { | ||
toggleWhenHidden: 'cads-icon_plus', | ||
toggleWhenShowing: 'cads-icon_minus', | ||
elementIsOpen: 'cads-disclosure__details--open', | ||
}; | ||
|
||
const attributes = { | ||
labelWhenHidden: 'data-label-when-hiding', | ||
labelWhenShowing: 'data-label-when-showing', | ||
target: 'data-toggle-target-id', | ||
}; | ||
|
||
const disclosureSelectors = { | ||
summary: '.js-disclosure-summary', | ||
details: '.js-disclosure-details', | ||
}; | ||
|
||
const disclosureAttributes = { | ||
closedSummary: 'data-closed-summary', | ||
openSummary: 'data-open-summary', | ||
}; | ||
function initDisclosureToggle(disclosure: Element) { | ||
const toggle = disclosure.querySelector(selectors.toggle); | ||
const summary = disclosure.querySelector(disclosureSelectors.summary); | ||
const details = disclosure.querySelector(disclosureSelectors.details); | ||
|
||
toggle?.addEventListener('click', () => { | ||
if (toggle && summary && details) { | ||
if (details.classList.contains(classes.elementIsOpen)) { | ||
summary.textContent = toggle.getAttribute( | ||
disclosureAttributes.openSummary, | ||
); | ||
} else { | ||
summary.textContent = toggle.getAttribute( | ||
disclosureAttributes.closedSummary, | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
const initDisclosure = (): void => { | ||
if (selectors.component) { | ||
initShowHideWithToggle(selectors, classes, attributes); | ||
function isExpanded(toggle: Element) { | ||
const ariaExpanded = toggle.getAttribute('aria-expanded'); | ||
return ariaExpanded === 'true'; | ||
} | ||
|
||
function setAriaExpanded(toggle: Element, expanded: boolean) { | ||
toggle.setAttribute('aria-expanded', expanded.toString()); | ||
} | ||
|
||
function setAriaDescription(toggle: Element) { | ||
const ariaDescription = isExpanded(toggle) | ||
? toggle.getAttribute('data-label-when-showing') | ||
: toggle.getAttribute('data-label-when-hiding'); | ||
|
||
if (ariaDescription) { | ||
toggle.setAttribute('aria-label', ariaDescription); | ||
} | ||
} | ||
|
||
function setTargetState(toggle: Element) { | ||
const parentEl = toggle.parentElement; | ||
const state = isExpanded(toggle) ? 'open' : 'closed'; | ||
parentEl?.setAttribute('data-disclosure-state', state); | ||
} | ||
|
||
function setIconClass(toggle: Element) { | ||
const icon = toggle.querySelector('.cads-icon--plus-minus'); | ||
icon?.classList.toggle('show-minus'); | ||
} | ||
|
||
function setSummaryText(toggle: Element) { | ||
const summary = toggle.querySelector('.js-cads-disclosure-summary'); | ||
|
||
const disclosures = document.querySelectorAll(selectors.component); | ||
for (let i = 0; i < disclosures.length; i++) { | ||
const disclosure = disclosures[i]; | ||
initDisclosureToggle(disclosure); | ||
const newTextContent = isExpanded(toggle) | ||
? toggle.getAttribute('data-open-summary') | ||
: toggle.getAttribute('data-closed-summary'); | ||
|
||
if (summary && newTextContent) { | ||
summary.textContent = newTextContent; | ||
} | ||
} | ||
|
||
function initDisclosureFor(component: Element) { | ||
const toggles = component.querySelectorAll('.js-cads-disclosure-toggle'); | ||
|
||
for (let i = 0; i < toggles.length; i++) { | ||
const toggle = toggles[i]; | ||
|
||
// Set status to false on load (in case not already set) | ||
// Makes calculating the other states easier as we can rely on the default | ||
setAriaExpanded(toggle, false); | ||
setAriaDescription(toggle); | ||
setTargetState(toggle); | ||
|
||
toggle.addEventListener('click', () => { | ||
setAriaExpanded(toggle, !isExpanded(toggle)); | ||
setAriaDescription(toggle); | ||
setIconClass(toggle); | ||
setSummaryText(toggle); | ||
setTargetState(toggle); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
export default initDisclosure; | ||
export default function initDisclosure() { | ||
const components = document.querySelectorAll('.js-cads-disclosure'); | ||
for (let i = 0; i < components.length; i++) { | ||
initDisclosureFor(components[i]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been switched out as the component no longer needs
data-toggle-target-id
to work. Testing against aria attributes is preferable too.