-
Notifications
You must be signed in to change notification settings - Fork 97
feat: Improve keyboard & screen-reader accessibility #286
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: master
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 | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,7 @@ import type { FC } from 'react'; | |||||||||||||||||||||||||||||||||||||||
| import React, { useRef } from 'react'; | ||||||||||||||||||||||||||||||||||||||||
| import useColorDrag from '../hooks/useColorDrag'; | ||||||||||||||||||||||||||||||||||||||||
| import type { BaseColorPickerProps, TransformOffset } from '../interface'; | ||||||||||||||||||||||||||||||||||||||||
| import { calcOffset, calculateColor } from '../util'; | ||||||||||||||||||||||||||||||||||||||||
| import { calcOffset, calculateColor, generateColor } from '../util'; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| import { useEvent } from '@rc-component/util'; | ||||||||||||||||||||||||||||||||||||||||
| import Handler from './Handler'; | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -17,6 +17,7 @@ const Picker: FC<PickerProps> = ({ | |||||||||||||||||||||||||||||||||||||||
| prefixCls, | ||||||||||||||||||||||||||||||||||||||||
| onChangeComplete, | ||||||||||||||||||||||||||||||||||||||||
| disabled, | ||||||||||||||||||||||||||||||||||||||||
| locale, | ||||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||||
| const pickerRef = useRef(); | ||||||||||||||||||||||||||||||||||||||||
| const transformRef = useRef(); | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,6 +44,16 @@ const Picker: FC<PickerProps> = ({ | |||||||||||||||||||||||||||||||||||||||
| disabledDrag: disabled, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // ===================== Keyboard (2-D handler) ===================== | ||||||||||||||||||||||||||||||||||||||||
| const hsb = color.toHsb(); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Build a new color from the current one with a single HSB channel changed. | ||||||||||||||||||||||||||||||||||||||||
| const changeColor = (channel: 's' | 'b', percent: number) => { | ||||||||||||||||||||||||||||||||||||||||
| const next = generateColor({ ...hsb, [channel]: percent / 100 }); | ||||||||||||||||||||||||||||||||||||||||
| colorRef.current = next; | ||||||||||||||||||||||||||||||||||||||||
| onChange(next); | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+55
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. In a controlled component, rapid keyboard interactions (like pressing or holding arrow keys) can trigger multiple state updates before the parent component re-renders and passes down the updated To prevent this race condition, we should keep
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||
| <div | ||||||||||||||||||||||||||||||||||||||||
| ref={pickerRef} | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -52,7 +63,30 @@ const Picker: FC<PickerProps> = ({ | |||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||
| <Palette prefixCls={prefixCls}> | ||||||||||||||||||||||||||||||||||||||||
| <Transform x={offset.x} y={offset.y} ref={transformRef}> | ||||||||||||||||||||||||||||||||||||||||
| <Handler color={color.toRgbString()} prefixCls={prefixCls} /> | ||||||||||||||||||||||||||||||||||||||||
| <Handler | ||||||||||||||||||||||||||||||||||||||||
| color={color.toRgbString()} | ||||||||||||||||||||||||||||||||||||||||
| prefixCls={prefixCls} | ||||||||||||||||||||||||||||||||||||||||
| disabled={disabled} | ||||||||||||||||||||||||||||||||||||||||
| x={{ | ||||||||||||||||||||||||||||||||||||||||
| 'aria-label': locale.picker, | ||||||||||||||||||||||||||||||||||||||||
| 'aria-roledescription': locale.pickerDescription, | ||||||||||||||||||||||||||||||||||||||||
| 'aria-valuetext': `${locale.saturation} ${Math.round( | ||||||||||||||||||||||||||||||||||||||||
| hsb.s * 100, | ||||||||||||||||||||||||||||||||||||||||
| )}%, ${locale.brightness} ${Math.round(hsb.b * 100)}%`, | ||||||||||||||||||||||||||||||||||||||||
| min: 0, | ||||||||||||||||||||||||||||||||||||||||
| max: 100, | ||||||||||||||||||||||||||||||||||||||||
| value: hsb.s * 100, | ||||||||||||||||||||||||||||||||||||||||
| onChange: percent => changeColor('s', percent), | ||||||||||||||||||||||||||||||||||||||||
| onChangeComplete: () => onChangeComplete?.(colorRef.current), | ||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||
| y={{ | ||||||||||||||||||||||||||||||||||||||||
| min: 0, | ||||||||||||||||||||||||||||||||||||||||
| max: 100, | ||||||||||||||||||||||||||||||||||||||||
| value: hsb.b * 100, | ||||||||||||||||||||||||||||||||||||||||
| onChange: percent => changeColor('b', percent), | ||||||||||||||||||||||||||||||||||||||||
| onChangeComplete: () => onChangeComplete?.(colorRef.current), | ||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||
| </Transform> | ||||||||||||||||||||||||||||||||||||||||
| <div | ||||||||||||||||||||||||||||||||||||||||
| className={`${prefixCls}-saturation`} | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
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.
Since the vertical axis (
y) is not a native range input, its value changes are handled entirely in JavaScript viaonKeyDown. In controlled mode, rapid key presses or holding down the arrow keys can causehandleKeyDownandonKeyUpto read staley.valueprops before the parent component has committed the state update and re-rendered.To ensure smooth and correct keyboard navigation for the vertical axis, we should track the latest value in a mutable ref (
lastValueRef) and update it synchronously on each key down/up event.