-
Notifications
You must be signed in to change notification settings - Fork 52
Accessibility fixes for copy button #313
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
base: release/2.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,8 @@ export function sleep(ms) { | |
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| export function copyToClipboard(copyData, eventTarget) { | ||
| export async function copyToClipboard(copyData, eventTarget) { | ||
| const btnEl = eventTarget?.target; | ||
| // In lots of places we have more than a couple of spaces for <pre> display purposes, we remove those extra spaces here. | ||
| let data = copyData?.trim().replace(/\s{8}/g, ' '); | ||
| try { | ||
|
|
@@ -32,26 +33,20 @@ export function copyToClipboard(copyData, eventTarget) { | |
| } catch (error) { | ||
| // Ignore non JSON text; | ||
| } | ||
|
|
||
| const textArea = document.createElement('textarea'); | ||
| textArea.value = data; | ||
| textArea.style.position = 'fixed'; // avoid scrolling to bottom | ||
| document.body.appendChild(textArea); | ||
| textArea.focus(); | ||
| textArea.select(); | ||
| try { | ||
| document.execCommand('copy'); | ||
| const btnEl = eventTarget?.target; | ||
| await window.navigator.clipboard.writeText(data); | ||
| if (btnEl) { | ||
| const label = btnEl.getAttribute('aria-label'); | ||
| btnEl.innerText = getI18nText('operations.copied'); | ||
| btnEl.setAttribute('aria-label', getI18nText('operations.copied')); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really the correct thing to do for a screen reader? Setting the aria-label and changing it? That feels wrong to me, but I don't really understand how aria-labels work. What's your thinking here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit complicated because the button itself contains the status update text for the success ('copied'). We want to let users know the 'copied' success status, so there's an aria-live region on the button. But screen readers will never read the actual content of the button if aria-label is also set, they will always read that instead. So we have to update the aria-label as well for the status to be announced.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refuse to believe this is right way to solve this problem for screen readers. Why would a screen reader announce the button label change? Multiple llms suggested to me to have a separate hidden div with the text that can be targeted for update instead of this here. Because actually I worry that since we are updating the button text twice which means that it could actually display both the "copied" message and then again the "label" message which not correct. Is that accurate, IDK, but that feels better than this implementation. |
||
| setTimeout(() => { | ||
| btnEl.innerText = getI18nText('operations.copy'); | ||
| btnEl.setAttribute('aria-label', label); | ||
| }, 5000); | ||
| } | ||
| } catch (err) { | ||
| console.error('Unable to copy', err); // eslint-disable-line no-console | ||
| } | ||
| document.body.removeChild(textArea); | ||
| } | ||
|
|
||
| export function getBaseUrlFromUrl(url) { | ||
|
|
||
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.
Almost forgot, all of these labels need to be i18n keys instead of hard coded text because we support multiple languages.