Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): a disabled Slider no longer runs a caller's handlers (#9729)

Parinith41cce0b0a2c4
diff --git a/.changeset/slider-disabled-caller-handlers.md b/.changeset/slider-disabled-caller-handlers.md
new file mode 100644
index 0000000000..28cff68ce8
--- /dev/null
+++ b/.changeset/slider-disabled-caller-handlers.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A disabled `Slider` no longer runs a caller's `onPointerDown`, `onPointerMove`, `onPointerUp`, `onPointerCancel`, `onKeyDown`, `onClick` or any other `on*` prop. Before, a press, drag or key on a disabled slider still reached them, for both the single and the range form. Now a disabled `Slider` runs no caller handler other than `onFocus` and `onBlur`, as a disabled `TextInput` and `TextArea` do (`spec:AST-085` FR2). An enabled `Slider` is unchanged. No API changes.
+
+@parinith-web
diff --git a/packages/core/src/Chat/ChatComposerInput.tsx b/packages/core/src/Chat/ChatComposerInput.tsx
index 603c53011a..384f7ce17a 100644
--- a/packages/core/src/Chat/ChatComposerInput.tsx
+++ b/packages/core/src/Chat/ChatComposerInput.tsx
@@ -68,6 +68,7 @@ import {
import {Badge, type BadgeProps} from '../Badge';
import {useChatComposerContext} from './ChatContext';
import {themeProps} from '../utils/themeProps';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import type {EnterKeyHint} from '../Field/textEntryTraits';
import {
registerEnglishFallback,
@@ -390,13 +391,7 @@ export function ChatComposerInput(props: ChatComposerInputProps) {
} = props;
// Keep focus/blur observation available, but do not forward activation or
// editing callbacks from the disabled root (including capture handlers).
- const rootProps = isDisabled
- ? Object.fromEntries(
- Object.entries(rest).filter(
- ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
- ),
- )
- : rest;
+ const rootProps = withholdCallerHandlers(rest, isDisabled);
const label = labelFromProps ?? t('@astryx.chat.composerInput.label');
const placeholder =
placeholderFromProps ??
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index f08252e060..90bb2bc035 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -76,6 +76,7 @@ import {
import {useMultiCombobox} from './hooks';
import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';
import {joinAriaIDs} from '../utils/inputAria';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import {devWarn, warnOnce} from '../utils/devWarning';
import {useInteractionModalityTracking} from '../utils/interactionModality';
import {useAnnounce} from '../hooks/useAnnounce';
@@ -2468,13 +2469,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
// A disabled trigger stays focusable but, like a natively disabled button,
// runs no caller event handler other than focus and blur, and a press on it
// reaches no handler at all (`spec:AST-085` FR2).
- const callerProps = isDisabled
- ? Object.fromEntries(
- Object.entries(rest).filter(
- ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
- ),
- )
- : rest;
+ const callerProps = withholdCallerHandlers(rest, isDisabled);
const triggerSharedProps: React.HTMLAttributes<HTMLElement> = {
...callerProps,
id: triggerId,
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index 9c66b224ba..e2c8e8233d 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -53,6 +53,7 @@ import {VisuallyHidden} from '../VisuallyHidden';
import {useTooltip} from '../Tooltip';
import {getInputARIA} from '../utils';
import {joinAriaIDs} from '../utils/inputAria';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import {interactionOverlayStyles} from '../utils/interactionOverlay.stylex';
import {usePressFeedback} from '../hooks/usePressFeedback';
import {inputDisabledFocusStyles} from '../Field/inputDisabledFocus';
@@ -633,13 +634,7 @@ export function NumberInput({
const showsDisabledMessage = isDisabled && !!disabledMessage;
// A disabled field keeps its tab stop but, like a natively disabled one,
// runs no caller event handler other than focus and blur (`spec:AST-085` FR2).
- const fieldProps = isDisabled
- ? Object.fromEntries(
- Object.entries(rest).filter(
- ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
- ),
- )
- : rest;
+ const fieldProps = withholdCallerHandlers(rest, isDisabled);
const disabledMessageTooltip = useTooltip({
placement: 'above',
// The container div is not naturally focusable; focusin bubbles up from
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index 8990645f9a..ee50746dad 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -79,6 +79,7 @@ import {SelectorOption} from './SelectorOption';
import {SelectorRowLayoutContext} from './SelectorRowLayoutContext';
import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';
import {joinAriaIDs} from '../utils/inputAria';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import {warnOnce} from '../utils/devWarning';
import {useSize} from '../SizeContext/SizeContext';
import {
@@ -1988,13 +1989,7 @@ export function Selector<T extends SelectorOptionType>(
// A disabled trigger stays focusable but, like a natively disabled button,
// runs no caller event handler other than focus and blur, and a press on it
// reaches no handler at all (`spec:AST-085` FR2).
- const callerProps = isDisabled
- ? Object.fromEntries(
- Object.entries(rest).filter(
- ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
- ),
- )
- : rest;
+ const callerProps = withholdCallerHandlers(rest, isDisabled);
const triggerSharedProps: React.HTMLAttributes<HTMLElement> = {
...callerProps,
onClick: isDisabled
diff --git a/packages/core/src/Slider/Slider.test.tsx b/packages/core/src/Slider/Slider.test.tsx
index c8bf725868..a0126f304c 100644
--- a/packages/core/src/Slider/Slider.test.tsx
+++ b/packages/core/src/Slider/Slider.test.tsx
@@ -496,6 +496,99 @@ describe('Slider', () => {
expect(handleChange).not.toHaveBeenCalled();
});
+ describe('caller handlers while disabled (spec:AST-085 FR2)', () => {
+ // A disabled slider stays focusable (aria-disabled, not native disabled),
+ // so the browser no longer blocks its events: the component withholds
+ // every caller handler except focus and blur, as a disabled text field does.
+ const makeHandlers = () => ({
+ onPointerDown: vi.fn(),
+ onPointerMove: vi.fn(),
+ onPointerUp: vi.fn(),
+ onPointerCancel: vi.fn(),
+ onKeyDown: vi.fn(),
+ onClick: vi.fn(),
+ onDoubleClick: vi.fn(),
+ });
+
+ const exercise = async (
+ user: ReturnType<typeof userEvent.setup>,
+ thumb: HTMLElement,
+ ) => {
+ const control = thumb.parentElement!;
+ fireEvent.pointerDown(control, {clientX: 10, clientY: 10, pointerId: 1});
+ fireEvent.pointerMove(control, {clientX: 20, clientY: 10, pointerId: 1});
+ fireEvent.pointerUp(control, {clientX: 20, clientY: 10, pointerId: 1});
+ fireEvent.pointerCancel(control, {pointerId: 1});
+ await user.dblClick(thumb);
+ act(() => thumb.focus());
+ await user.keyboard('{ArrowRight}');
+ };
+
+ it('runs no caller handler from a disabled single slider, but still reports focus and blur', async () => {
+ const user = userEvent.setup();
+ const handlers = makeHandlers();
+ const onFocus = vi.fn();
+ const onBlur = vi.fn();
+ render(
+ <Slider
+ label="Volume"
+ value={50}
+ isDisabled
+ onFocus={onFocus}
+ onBlur={onBlur}
+ {...handlers}
+ />,
+ );
+ const thumb = screen.getByRole('slider');
+ await exercise(user, thumb);
+ for (const [name, handler] of Object.entries(handlers)) {
+ expect(handler, name).not.toHaveBeenCalled();
+ }
+ expect(onFocus).toHaveBeenCalled();
+ act(() => thumb.blur());
+ expect(onBlur).toHaveBeenCalled();
+ });
+
+ it('runs no caller handler from a disabled range slider', async () => {
+ const user = userEvent.setup();
+ const handlers = makeHandlers();
+ render(
+ <Slider label="Price" value={[20, 80]} isDisabled {...handlers} />,
+ );
+ const [thumb] = screen.getAllByRole('slider');
+ await exercise(user, thumb);
+ // The base props of a range slider land on the role="group" control.
+ await user.click(screen.getByRole('group', {name: 'Price'}));
+ for (const [name, handler] of Object.entries(handlers)) {
+ expect(handler, name).not.toHaveBeenCalled();
+ }
+ });
+
+ it('still runs every caller handler when the slider is enabled', async () => {
+ const user = userEvent.setup();
+ const handlers = makeHandlers();
+ render(<Slider label="Volume" value={50} {...handlers} />);
+ const thumb = screen.getByRole('slider');
+ await exercise(user, thumb);
+ for (const [name, handler] of Object.entries(handlers)) {
+ expect(handler, name).toHaveBeenCalled();
+ }
+ });
+
+ it('resumes caller handlers once re-enabled', async () => {
+ const user = userEvent.setup();
+ const onClick = vi.fn();
+ const {rerender} = render(
+ <Slider label="Volume" value={50} isDisabled onClick={onClick} />,
+ );
+ await user.click(screen.getByRole('slider'));
+ expect(onClick).not.toHaveBeenCalled();
+ rerender(<Slider label="Volume" value={50} onClick={onClick} />);
+ await user.click(screen.getByRole('slider'));
+ expect(onClick).toHaveBeenCalledOnce();
+ });
+ });
+
// --- onChangeEnd on keyboard ---
it('fires onChangeEnd on keyboard ArrowRight', async () => {
diff --git a/packages/core/src/Slider/Slider.tsx b/packages/core/src/Slider/Slider.tsx
index 4b84739552..a145b05474 100644
--- a/packages/core/src/Slider/Slider.tsx
+++ b/packages/core/src/Slider/Slider.tsx
@@ -48,6 +48,7 @@ import {VisuallyHidden} from '../VisuallyHidden';
import type {InputStatus} from '../Field/types';
import {mergeProps, rtlStyles} from '../utils';
import {focusOutlineStyles} from '../utils/focusOutline.stylex';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import {joinAriaIDs} from '../utils/inputAria';
import {useInteractionModalityTracking} from '../utils/interactionModality';
import {isRtlElement} from '../hooks/isRtlElement';
@@ -742,15 +743,20 @@ export function Slider({ref, ...props}: SliderProps) {
const isHorizontal = orientation === 'horizontal';
- // Consumer handlers run after the component's own and always run: the
- // component's handlers call preventDefault() for their own reasons, which
- // must not silence a consumer.
+ // Consumer handlers run after the component's own, so the component's
+ // preventDefault() calls never silence an enabled slider's consumer.
const after =
<E,>(own: (event: E) => void, consumer?: (event: E) => void) =>
(event: E) => {
own(event);
consumer?.(event);
};
+
+ // A disabled slider keeps its tab stop, so the browser still sends its events.
+ // Drop the caller's handlers, but keep focus and blur.
+ const callerProps = withholdCallerHandlers(rest, isDisabled);
+ const callerHandler = <E,>(handler?: (event: E) => void) =>
+ isDisabled ? undefined : handler;
const generatedId = useId();
const id = idProp ?? generatedId;
const labelID = useId();
@@ -1475,7 +1481,7 @@ export function Slider({ref, ...props}: SliderProps) {
) => (
<div
key={thumbIndex}
- {...(!isRange ? rest : undefined)}
+ {...(!isRange ? callerProps : undefined)}
ref={trigger != null ? trigger.ref : !isRange ? ref : undefined}
id={!isRange ? id : undefined}
role="slider"
@@ -1504,7 +1510,7 @@ export function Slider({ref, ...props}: SliderProps) {
}
onKeyDown={after(
e => handleKeyDown(thumbIndex, e),
- !isRange ? onKeyDown : undefined,
+ !isRange ? callerHandler(onKeyDown) : undefined,
)}
onFocus={after(
e => handleThumbFocus(thumbIndex, e),
@@ -1634,10 +1640,10 @@ export function Slider({ref, ...props}: SliderProps) {
<div
// In range mode the role="group" control takes the base props; a
// single slider gives them to its role="slider" thumb.
- {...(isRange ? rest : undefined)}
+ {...(isRange ? callerProps : undefined)}
{...(isRange
? {
- onKeyDown,
+ onKeyDown: callerHandler(onKeyDown),
onFocus,
onBlur,
'aria-describedby': ariaDescribedByProp,
@@ -1655,10 +1661,13 @@ export function Slider({ref, ...props}: SliderProps) {
'aria-labelledby': joinAriaIDs(labelID, ariaLabelledByProp),
}
: undefined)}
- onPointerDown={after(handlePointerDown, onPointerDown)}
- onPointerMove={after(handlePointerMove, onPointerMove)}
- onPointerUp={after(handlePointerUp, onPointerUp)}
- onPointerCancel={after(handlePointerUp, onPointerCancel)}
+ onPointerDown={after(handlePointerDown, callerHandler(onPointerDown))}
+ onPointerMove={after(handlePointerMove, callerHandler(onPointerMove))}
+ onPointerUp={after(handlePointerUp, callerHandler(onPointerUp))}
+ onPointerCancel={after(
+ handlePointerUp,
+ callerHandler(onPointerCancel),
+ )}
{...mergeProps(
themeProps('slider-control', {
orientation,
diff --git a/packages/core/src/TextArea/TextArea.tsx b/packages/core/src/TextArea/TextArea.tsx
index 9776d72b5d..31a02db4eb 100644
--- a/packages/core/src/TextArea/TextArea.tsx
+++ b/packages/core/src/TextArea/TextArea.tsx
@@ -45,6 +45,7 @@ import {
} from '../Field';
import {Icon, renderIconSlot, type IconType} from '../Icon';
import {isRenderable} from '../utils/isRenderable';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import {Spinner} from '../Spinner';
import {useTooltip} from '../Tooltip';
import {mergeProps} from '../utils';
@@ -476,13 +477,7 @@ export function TextArea({
const showsDisabledMessage = isDisabled && !!disabledMessage;
// A disabled field keeps its tab stop but, like a natively disabled one,
// runs no caller event handler other than focus and blur (`spec:AST-085` FR2).
- const fieldProps = isDisabled
- ? Object.fromEntries(
- Object.entries(rest).filter(
- ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
- ),
- )
- : rest;
+ const fieldProps = withholdCallerHandlers(rest, isDisabled);
const disabledMessageTooltip = useTooltip({
placement: 'above',
// The container div is not naturally focusable; focusin bubbles up from
diff --git a/packages/core/src/TextInput/TextInput.tsx b/packages/core/src/TextInput/TextInput.tsx
index 41d6df3258..3cfa41aab4 100644
--- a/packages/core/src/TextInput/TextInput.tsx
+++ b/packages/core/src/TextInput/TextInput.tsx
@@ -46,6 +46,7 @@ import {
import type {InputStatus} from '../Field/types';
import {renderIconSlot, type IconType} from '../Icon/Icon';
import {isRenderable} from '../utils/isRenderable';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
import {Spinner} from '../Spinner/Spinner';
import {useTooltip} from '../Tooltip/useTooltip';
import {VisuallyHidden} from '../VisuallyHidden/VisuallyHidden';
@@ -374,13 +375,7 @@ export function TextInput({
const isEffectivelyRequired = useResolvedRequired({isRequired, isOptional});
// A disabled field keeps its tab stop but, like a natively disabled one,
// runs no caller event handler other than focus and blur (`spec:AST-085` FR2).
- const fieldProps = isDisabled
- ? Object.fromEntries(
- Object.entries(rest).filter(
- ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
- ),
- )
- : rest;
+ const fieldProps = withholdCallerHandlers(rest, isDisabled);
const size = useSize(sizeProp, 'md');
const generatedID = useId();
diff --git a/packages/core/src/utils/disabledInteraction.ts b/packages/core/src/utils/disabledInteraction.ts
index 6de990ce04..ea154f57a7 100644
--- a/packages/core/src/utils/disabledInteraction.ts
+++ b/packages/core/src/utils/disabledInteraction.ts
@@ -2,8 +2,9 @@
/**
* @file disabledInteraction.ts
- * @input An optional caller keydown handler
- * @output Event props that keep a focusable, aria-disabled control inert
+ * @input An optional caller keydown handler, or a control's caller props
+ * @output Event props that keep a focusable, aria-disabled control inert, and
+ * the caller props such a control hands its element
* @position Shared by controls that replace native `disabled` with
* `aria-disabled` under spec:AST-085 (FR1, FR2)
*/
@@ -38,3 +39,23 @@ export function disabledInteractionProps<T extends HTMLElement>(
},
};
}
+
+/**
+ * The caller props a disabled, focusable control hands its element: every
+ * `on*` handler is dropped except focus and blur, so the control runs no
+ * caller callback, as a natively disabled one runs none. Enabled, `props` pass
+ * through unchanged.
+ */
+export function withholdCallerHandlers<P extends object>(
+ props: P,
+ isDisabled: boolean,
+): P {
+ if (!isDisabled) {
+ return props;
+ }
+ return Object.fromEntries(
+ Object.entries(props).filter(
+ ([key]) => !/^on[A-Z]/.test(key) || /^on(Focus|Blur)/.test(key),
+ ),
+ ) as P;
+}