-
Notifications
You must be signed in to change notification settings - Fork 236
chore: add storybook-screen-reader plugin #5911
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
Draft
TarunAdobe
wants to merge
5
commits into
main
Choose a base branch
from
ttomar/screen-reader-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,399
−5
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77d5610
chore: add storybook-screen-reader plugin
TarunAdobe 736dcbb
chore: update storybook-screen-reader plugin
TarunAdobe d9f1fdf
feat: add local storybook screen reader addon
TarunAdobe 01edbc7
feat: let storybook build the addon
TarunAdobe 9c470bc
chore: move screen reader plugin to second-gen
TarunAdobe 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
15 changes: 15 additions & 0 deletions
15
2nd-gen/packages/swc/.storybook/addons/screen-reader-addon/manager.js
This file contains hidden or 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,15 @@ | ||
| /** | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| // Storybook manager addon entry point | ||
| // Imports TypeScript source directly - Storybook's esbuild compiles on the fly | ||
| import './src/register.tsx'; |
44 changes: 44 additions & 0 deletions
44
2nd-gen/packages/swc/.storybook/addons/screen-reader-addon/package.json
This file contains hidden or 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,44 @@ | ||
| { | ||
| "author": "Adobe", | ||
| "bugs": { | ||
| "url": "https://github.com/adobe/spectrum-web-components/issues" | ||
| }, | ||
| "dependencies": { | ||
| "aria-query": "^5.3.0", | ||
| "dom-accessibility-api": "^0.7.0", | ||
| "query-selector-shadow-dom": "^1.0.0" | ||
| }, | ||
| "description": "A Screen Reader Storybook addon for accessibility testing", | ||
| "homepage": "https://opensource.adobe.com/spectrum-web-components/", | ||
| "keywords": [ | ||
| "storybook", | ||
| "storybook-addons", | ||
| "addons", | ||
| "accessibility", | ||
| "a11y", | ||
| "screen-reader", | ||
| "wcag" | ||
| ], | ||
| "license": "Apache-2.0", | ||
| "name": "@spectrum-web-components/screen-reader-addon", | ||
| "peerDependencies": { | ||
| "@lit/react": ">=1.0.0", | ||
| "@spectrum-web-components/field-label": ">=1.0.0", | ||
| "@spectrum-web-components/help-text": ">=1.0.0", | ||
| "@spectrum-web-components/switch": ">=1.0.0", | ||
| "@spectrum-web-components/textfield": ">=1.0.0", | ||
| "@spectrum-web-components/theme": ">=1.0.0", | ||
| "@storybook/components": ">=8.0.0", | ||
| "@storybook/core-events": ">=8.0.0", | ||
| "@storybook/manager-api": ">=8.0.0", | ||
| "lit": "^2.5.0 || ^3.1.3", | ||
| "react": ">=18.0.0" | ||
| }, | ||
| "private": true, | ||
| "repository": { | ||
| "directory": "2nd-gen/packages/swc/.storybook/addons/screen-reader-addon", | ||
| "type": "git", | ||
| "url": "https://github.com/adobe/spectrum-web-components.git" | ||
| }, | ||
| "version": "1.0.0" | ||
| } |
303 changes: 303 additions & 0 deletions
303
.../packages/swc/.storybook/addons/screen-reader-addon/src/components/screen-reader-panel.ts
This file contains hidden or 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,303 @@ | ||
| /** | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { css, html, LitElement } from 'lit'; | ||
| import { addons } from '@storybook/manager-api'; | ||
| import { STORY_CHANGED } from '@storybook/core-events'; | ||
|
|
||
| // Import Spectrum Web Components | ||
| import '@spectrum-web-components/switch/sp-switch.js'; | ||
| import '@spectrum-web-components/theme/sp-theme.js'; | ||
| import '@spectrum-web-components/theme/src/spectrum-two/themes-core-tokens.js'; | ||
| import '@spectrum-web-components/textfield/sp-textfield.js'; | ||
| import '@spectrum-web-components/help-text/sp-help-text.js'; | ||
| import '@spectrum-web-components/field-label/sp-field-label.js'; | ||
|
|
||
| import ScreenReader from '../screen-reader/screenReader.js'; | ||
|
|
||
| interface ScreenReaderTextEvent extends CustomEvent { | ||
| detail: { | ||
| text: string; | ||
| }; | ||
| } | ||
|
|
||
| export class ScreenReaderPanel extends LitElement { | ||
| // Using static properties instead of decorators for compatibility | ||
| // with Storybook's internal esbuild (no decorator compilation needed) | ||
| static override properties = { | ||
| voice: { type: Boolean }, | ||
| text: { type: Boolean }, | ||
| isActive: { type: Boolean }, | ||
| screenReaderText: { type: String }, | ||
| themeColor: { type: String }, | ||
| }; | ||
|
|
||
| // Use 'declare' to avoid class field definition overriding Lit's reactive properties | ||
| declare voice: boolean; | ||
| declare text: boolean; | ||
| declare isActive: boolean; | ||
| declare screenReaderText: string; | ||
| declare themeColor: 'light' | 'dark'; | ||
|
|
||
| private screenReader: ScreenReader | null = null; | ||
| private channel: ReturnType<typeof addons.getChannel> | null = null; | ||
| private themeMediaQuery: MediaQueryList | null = null; | ||
|
|
||
| static override styles = css` | ||
| :host { | ||
| display: block; | ||
| padding: 16px; | ||
| } | ||
|
|
||
| .toggle-row { | ||
| display: flex; | ||
| align-items: center; | ||
| margin-bottom: 12px; | ||
| } | ||
|
|
||
| .output-section { | ||
| margin-top: 16px; | ||
| } | ||
|
|
||
| sp-textfield { | ||
| width: 100%; | ||
| } | ||
|
|
||
| sp-help-text { | ||
| margin-top: 12px; | ||
| } | ||
| `; | ||
|
|
||
| constructor() { | ||
| super(); | ||
| // Initialize reactive properties | ||
| this.voice = false; | ||
| this.text = false; | ||
| this.isActive = false; | ||
| this.screenReaderText = ''; | ||
| this.themeColor = this.detectTheme(); | ||
| // Bind event handlers | ||
| this.handleTextChange = this.handleTextChange.bind(this); | ||
| this.handleStoryChange = this.handleStoryChange.bind(this); | ||
| this.handleThemeChange = this.handleThemeChange.bind(this); | ||
| } | ||
|
|
||
| private detectTheme(): 'light' | 'dark' { | ||
| // Detect theme by checking Storybook's actual background color | ||
| // This works for both explicit themes (1st-gen) and auto themes (2nd-gen) | ||
| const body = document.body; | ||
| const computedStyle = getComputedStyle(body); | ||
| const bgColor = computedStyle.backgroundColor; | ||
|
|
||
| // Parse RGB values | ||
| const rgbMatch = bgColor.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/); | ||
| if (rgbMatch) { | ||
| const [, r, g, b] = rgbMatch.map(Number); | ||
| // Calculate relative luminance | ||
| const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255; | ||
|
|
||
| // If background is dark (luminance < 0.5), use dark theme | ||
| return luminance < 0.5 ? 'dark' : 'light'; | ||
| } | ||
|
|
||
| // Fallback to system preference | ||
| if (window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
| return 'dark'; | ||
| } | ||
|
|
||
| return 'light'; | ||
| } | ||
|
|
||
| private handleThemeChange(): void { | ||
| this.themeColor = this.detectTheme(); | ||
| } | ||
|
|
||
| override connectedCallback(): void { | ||
| super.connectedCallback(); | ||
|
|
||
| // Listen for text changes from the screen reader | ||
| window.addEventListener( | ||
| 'screen-reader-text-changed', | ||
| this.handleTextChange as EventListener | ||
| ); | ||
|
|
||
| // Listen for story changes via Storybook API | ||
| this.channel = addons.getChannel(); | ||
| this.channel.on(STORY_CHANGED, this.handleStoryChange); | ||
|
|
||
| // Listen for system theme changes (for auto-theme Storybooks like 2nd-gen) | ||
| this.themeMediaQuery = window.matchMedia( | ||
| '(prefers-color-scheme: dark)' | ||
| ); | ||
| this.themeMediaQuery.addEventListener('change', this.handleThemeChange); | ||
| } | ||
|
|
||
| override disconnectedCallback(): void { | ||
| super.disconnectedCallback(); | ||
|
|
||
| window.removeEventListener( | ||
| 'screen-reader-text-changed', | ||
| this.handleTextChange as EventListener | ||
| ); | ||
|
|
||
| if (this.channel) { | ||
| this.channel.off(STORY_CHANGED, this.handleStoryChange); | ||
| } | ||
|
|
||
| if (this.themeMediaQuery) { | ||
| this.themeMediaQuery.removeEventListener( | ||
| 'change', | ||
| this.handleThemeChange | ||
| ); | ||
| } | ||
|
|
||
| this.stopScreenReader(); | ||
| } | ||
|
|
||
| private handleTextChange(event: ScreenReaderTextEvent): void { | ||
| this.screenReaderText = event.detail.text; | ||
| } | ||
|
|
||
| private handleStoryChange(): void { | ||
| if (this.isActive && this.screenReader) { | ||
| this.screenReader.stop(); | ||
| this.screenReader = null; | ||
|
|
||
| // Wait for new story to load, then restart | ||
| setTimeout(() => { | ||
| if (this.voice || this.text) { | ||
| this.startScreenReader(); | ||
| } | ||
| }, 500); | ||
| } | ||
| } | ||
|
|
||
| private handleVoiceToggle(event: Event): void { | ||
| this.voice = (event.target as HTMLInputElement).checked; | ||
| this.updateScreenReader(); | ||
| } | ||
|
|
||
| private handleTextToggle(event: Event): void { | ||
| this.text = (event.target as HTMLInputElement).checked; | ||
| this.updateScreenReader(); | ||
| } | ||
|
|
||
| private findStorybookIframe(): HTMLIFrameElement | null { | ||
| return ( | ||
| (document.getElementById( | ||
| 'storybook-preview-iframe' | ||
| ) as HTMLIFrameElement) || | ||
| (document.querySelector( | ||
| 'iframe[data-is-storybook="true"]' | ||
| ) as HTMLIFrameElement) || | ||
| (document.querySelector( | ||
| 'iframe[title*="storybook"]' | ||
| ) as HTMLIFrameElement) || | ||
| (document.querySelector('iframe') as HTMLIFrameElement) | ||
| ); | ||
| } | ||
|
|
||
| private startScreenReader(): void { | ||
| const iframe = this.findStorybookIframe(); | ||
|
|
||
| if (!iframe) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('[Screen Reader Addon] Cannot find preview iframe'); | ||
| return; | ||
| } | ||
|
|
||
| this.screenReader = new ScreenReader(); | ||
| this.screenReader.voiceEnabled = this.voice; | ||
| this.screenReader.textEnabled = this.text; | ||
| this.screenReader.start(iframe); | ||
|
|
||
| this.isActive = true; | ||
| } | ||
|
|
||
| private stopScreenReader(): void { | ||
| if (this.screenReader) { | ||
| this.screenReader.stop(); | ||
| this.screenReader = null; | ||
| } | ||
| this.isActive = false; | ||
| this.screenReaderText = ''; | ||
| } | ||
|
|
||
| private updateScreenReader(): void { | ||
| const shouldBeActive = this.voice || this.text; | ||
|
|
||
| if (shouldBeActive && !this.isActive) { | ||
| this.startScreenReader(); | ||
| } else if (!shouldBeActive && this.isActive) { | ||
| this.stopScreenReader(); | ||
| } else if (shouldBeActive && this.screenReader) { | ||
| this.screenReader.voiceEnabled = this.voice; | ||
| this.screenReader.textEnabled = this.text; | ||
| } | ||
| } | ||
|
|
||
| override render() { | ||
| return html` | ||
| <sp-theme | ||
| scale="medium" | ||
| color=${this.themeColor} | ||
| system="spectrum-two" | ||
| > | ||
| <div class="toggle-row"> | ||
| <sp-switch | ||
| ?checked=${this.voice} | ||
| @change=${this.handleVoiceToggle} | ||
| > | ||
| Voice Reader | ||
| </sp-switch> | ||
| </div> | ||
|
|
||
| <div class="toggle-row"> | ||
| <sp-switch | ||
| ?checked=${this.text} | ||
| @change=${this.handleTextToggle} | ||
| > | ||
| Text Reader | ||
| </sp-switch> | ||
| </div> | ||
|
|
||
| ${this.text | ||
| ? html` | ||
| <div class="output-section"> | ||
| <sp-field-label for="screen-reader-output"> | ||
| Screen reader output | ||
| </sp-field-label> | ||
| <sp-textfield | ||
| id="screen-reader-output" | ||
| multiline | ||
| readonly | ||
| rows="1" | ||
| placeholder="Navigate to hear announcements..." | ||
| .value=${this.screenReaderText} | ||
| ></sp-textfield> | ||
| </div> | ||
| ` | ||
| : ''} | ||
| ${this.isActive | ||
| ? html` | ||
| <sp-help-text> | ||
| Use Tab or arrow keys to navigate. Focus changes | ||
| will be announced. | ||
| </sp-help-text> | ||
| ` | ||
| : ''} | ||
| </sp-theme> | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| customElements.define('screen-reader-panel', ScreenReaderPanel); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.