Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
Address additional ToggleSwitch a11y feedback#3902
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0c22731
Address additional ToggleSwitch a11y feedback
camertron 2ffa8cb
Add test ensuring clicking the status label cannot toggle a disabled …
camertron 0d50aa4
Add changeset
camertron c157d7b
Update src/ToggleSwitch/ToggleSwitch.tsx
camertron af748d4
Update src/ToggleSwitch/ToggleSwitch.tsx
camertron 9b72456
Move aria-describedby
camertron 209c921
Update snapshot
camertron de0c7c5
Merge branch 'main' into toggle_switch_a11y_take4
camertron 1d02071
Add loadingLabelDelay to prop docs; make a few changes suggested in r…
camertron 51dea9e
Merge branch 'main' into toggle_switch_a11y_take4
camertron f6e6745
Merge branch 'toggle_switch_a11y_take4' of github.com:primer/react in…
camertron 6cdc7b5
Merge branch 'main' into toggle_switch_a11y_take4
camertron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': minor | ||
| --- | ||
| Address additional ToggleSwitch a11y feedback |
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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| #!/bin/bash | ||
| args="$@" | ||
| set -x | ||
| docker run --rm \ | ||
| --network host \ | ||
| -v $(pwd):/workspace \ | ||
| -w /workspace \ | ||
| -it mcr.microsoft.com/playwright:v1.37.0-jammy \ | ||
| /bin/bash -c "npm install && STORYBOOK_URL=http://host.docker.internal:6006 npx playwright test $@" | ||
| /bin/bash -c "npm install && STORYBOOK_URL=http://host.docker.internal:6006 npx playwright test $args" |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import React, {MouseEventHandler, useCallback, useEffect} from 'react' | ||
| import {useId} from '../hooks/useId' | ||
| import styled, {css} from 'styled-components' | ||
| import {variant} from 'styled-system' | ||
| import Box from '../Box' | ||
| @@ -8,11 +9,14 @@ import {get} from '../constants' | ||
| import {useProvidedStateOrCreate} from '../hooks' | ||
| import sx, {BetterSystemStyleObject, SxProp} from '../sx' | ||
| import {CellAlignment} from '../DataTable/column' | ||
| import VisuallyHidden from '../_VisuallyHidden' | ||
| const TRANSITION_DURATION = '80ms' | ||
| const EASE_OUT_QUAD_CURVE = 'cubic-bezier(0.5, 1, 0.89, 1)' | ||
| export interface ToggleSwitchProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>, SxProp { | ||
| /** The id of the DOM node that labels the switch */ | ||
| ['aria-labelledby']: string | ||
| /** Uncontrolled - whether the switch is turned on */ | ||
| defaultChecked?: boolean | ||
| /** Whether the switch is ready for user input */ | ||
| @@ -31,6 +35,9 @@ export interface ToggleSwitchProps extends Omit<React.HTMLAttributes<HTMLDivElem | ||
| * **This should only be changed when the switch's alignment needs to be adjusted.** For example: It needs to be left-aligned because the label appears above it and the caption appears below it. | ||
| */ | ||
| statusLabelPosition?: CellAlignment | ||
| /** If the switch is in the loading state, this value controls the amount of delay in milliseconds before | ||
| * the word "Loading" is announced to screen readers. Default: 2000. */ | ||
| loadingLabelDelay?: number | ||
| } | ||
| const sizeVariants = variant({ | ||
| @@ -127,8 +134,12 @@ const SwitchButton = styled.button<SwitchButtonProps>` | ||
| } | ||
| ${props => { | ||
| if (props.disabled) { | ||
| if (props['aria-disabled']) { | ||
camertron marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return css` | ||
| @media (forced-colors: active) { | ||
| border-color: GrayText; | ||
| } | ||
| background-color: ${get('colors.switchTrack.disabledBg')}; | ||
| border-color: transparent; | ||
| cursor: not-allowed; | ||
| @@ -166,11 +177,12 @@ const SwitchButton = styled.button<SwitchButtonProps>` | ||
| ${sx} | ||
| ${sizeVariants} | ||
| ` | ||
| const ToggleKnob = styled.div<{checked?: boolean; disabled?: boolean}>` | ||
| const ToggleKnob = styled.div<{checked?: boolean; 'aria-disabled': React.AriaAttributes['aria-disabled']}>` | ||
| background-color: ${get('colors.switchKnob.bg')}; | ||
| border-width: 1px; | ||
| border-style: solid; | ||
| border-color: ${props => (props.disabled ? get('colors.switchTrack.disabledBg') : get('colors.switchKnob.border'))}; | ||
| border-color: ${props => | ||
| props['aria-disabled'] ? get('colors.switchTrack.disabledBg') : get('colors.switchKnob.border')}; | ||
| border-radius: calc(${get('radii.2')} - 1px); /* -1px to account for 1px border around the control */ | ||
| width: 50%; | ||
| position: absolute; | ||
| @@ -187,8 +199,12 @@ const ToggleKnob = styled.div<{checked?: boolean; disabled?: boolean}>` | ||
| } | ||
| ${props => { | ||
| if (props.disabled) { | ||
| if (props['aria-disabled']) { | ||
| return css` | ||
| @media (forced-colors: active) { | ||
| color: GrayText; | ||
| } | ||
| border-color: ${get('colors.switchTrack.disabledBg')}; | ||
| ` | ||
| } | ||
| @@ -219,27 +235,63 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren | ||
| onClick, | ||
| size = 'medium', | ||
| statusLabelPosition = 'start', | ||
| loadingLabelDelay = 2000, | ||
| sx: sxProp, | ||
| ...rest | ||
| } = props | ||
| const isControlled = typeof checked !== 'undefined' | ||
| const [isOn, setIsOn] = useProvidedStateOrCreate<boolean>(checked, onChange, Boolean(defaultChecked)) | ||
| const acceptsInteraction = !disabled && !loading | ||
| const [loadingLabelTimeoutId, setLoadingLabelTimeoutId] = React.useState<number | null>(null) | ||
| const [isLoadingLabelVisible, setIsLoadingLabelVisible] = React.useState(false) | ||
| const loadingLabelId = useId('loadingLabel') | ||
| const resetLoadingLabel = useCallback(() => { | ||
| if (loadingLabelTimeoutId) { | ||
| clearTimeout(loadingLabelTimeoutId) | ||
| setLoadingLabelTimeoutId(null) | ||
| } | ||
| if (isLoadingLabelVisible) { | ||
| setIsLoadingLabelVisible(false) | ||
| } | ||
| }, [loadingLabelTimeoutId, isLoadingLabelVisible]) | ||
| const handleToggleClick: MouseEventHandler = useCallback( | ||
| e => { | ||
| if (disabled || loading) return | ||
| if (!isControlled) { | ||
| setIsOn(!isOn) | ||
| resetLoadingLabel() | ||
| } | ||
| onClick && onClick(e) | ||
| }, | ||
| [onClick, isControlled, isOn, setIsOn], | ||
| [disabled, isControlled, loading, onClick, setIsOn, isOn, resetLoadingLabel], | ||
| ) | ||
| useEffect(() => { | ||
| if (onChange && isControlled) { | ||
| if (onChange && isControlled && !disabled) { | ||
| onChange(Boolean(checked)) | ||
| } | ||
| }, [onChange, checked, isControlled]) | ||
| }, [onChange, checked, isControlled, disabled]) | ||
| if (loading) { | ||
| if (!loadingLabelTimeoutId) { | ||
| setLoadingLabelTimeoutId( | ||
| setTimeout(() => { | ||
| setLoadingLabelTimeoutId(null) | ||
| setIsLoadingLabelVisible(true) | ||
| }, loadingLabelDelay) as unknown as number, | ||
| ) | ||
| } | ||
| } else { | ||
| resetLoadingLabel() | ||
| } | ||
| let switchButtonDescribedBy = loadingLabelId | ||
| if (ariaDescribedby) switchButtonDescribedBy = `${switchButtonDescribedBy} ${ariaDescribedby}` | ||
| return ( | ||
| <Box | ||
| @@ -249,13 +301,20 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren | ||
| sx={sxProp} | ||
| {...rest} | ||
| > | ||
| {isLoadingLabelVisible ? ( | ||
| <VisuallyHidden> | ||
| <span aria-live="polite" id={loadingLabelId}> | ||
| Loading | ||
| </span> | ||
| </VisuallyHidden> | ||
| ) : null} | ||
| {loading ? <Spinner size="small" /> : null} | ||
| <Text | ||
| color={acceptsInteraction ? 'fg.default' : 'fg.muted'} | ||
| fontSize={size === 'small' ? 0 : 1} | ||
| mx={2} | ||
| aria-hidden="true" | ||
| sx={{position: 'relative', cursor: 'pointer'}} | ||
| sx={{position: 'relative', cursor: acceptsInteraction ? 'pointer' : 'not-allowed'}} | ||
| onClick={handleToggleClick} | ||
| > | ||
| <Box textAlign="right" sx={isOn ? null : hiddenTextStyles}> | ||
| @@ -269,11 +328,11 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren | ||
| ref={ref} | ||
| onClick={handleToggleClick} | ||
| aria-labelledby={ariaLabelledby} | ||
| aria-describedby={ariaDescribedby} | ||
| aria-describedby={switchButtonDescribedBy} | ||
| aria-pressed={isOn} | ||
| checked={isOn} | ||
| size={size} | ||
| disabled={!acceptsInteraction} | ||
| aria-disabled={!acceptsInteraction} | ||
| > | ||
| <Box aria-hidden="true" display="flex" alignItems="center" width="100%" height="100%" overflow="hidden"> | ||
| <Box | ||
| @@ -305,7 +364,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren | ||
| <CircleIcon size={size} /> | ||
| </Box> | ||
| </Box> | ||
| <ToggleKnob aria-hidden="true" disabled={!acceptsInteraction} checked={isOn} /> | ||
| <ToggleKnob aria-hidden="true" aria-disabled={!acceptsInteraction} checked={isOn} /> | ||
| </SwitchButton> | ||
| </Box> | ||
| ) | ||
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
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.