Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/slider-audit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] Slider: after a keyboard step, `onChangeEnd` now reports the value the slider renders after your `onChange` response, as a pointer release already did, so a value you clamp or reject is no longer reported. The `onChange`, `onChangeEnd` and `disabledMessage` docs now say when each callback fires and that a disabled thumb stays focusable with or without a reason, and the Chinese docs list `width`.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

@cixzhang
32 changes: 24 additions & 8 deletions packages/core/src/Slider/Slider.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ export const docs = {
{
name: 'onChange',
type: '(value: number) => void | (value: [number, number]) => void',
description: 'Callback fired on value change during drag.',
description:
'Callback fired as the value changes: through a pointer drag and a finger’s coast, on a track press, and on each keyboard step.',
},
{
name: 'onChangeEnd',
type: '(value: number) => void | (value: [number, number]) => void',
description: 'Callback fired when drag ends.',
description:
'Callback fired once when an interaction ends: a pointer release or cancel (after a finger’s coast settles) or a completed keyboard step. It carries the value the slider renders after your `onChange` response, so a value you clamp or reject is not reported.',
},
{
name: 'min',
Expand Down Expand Up @@ -186,7 +188,7 @@ export const docs = {
name: 'disabledMessage',
type: 'string',
description:
'Explains why the slider is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the thumb focusable via aria-disabled (value changes stay blocked). Use this instead of wrapping a disabled Slider in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.',
'Explains why the slider is disabled. With isDisabled, shows a tooltip on hover and keyboard focus and describes the thumb with it. A disabled thumb stays focusable with aria-disabled either way, and value changes stay blocked. Use this instead of wrapping a disabled Slider in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.',
},
{
name: 'isOptional',
Expand Down Expand Up @@ -349,12 +351,14 @@ export const docsZh = {
{
name: 'onChange',
type: '(value: number) => void | (value: [number, number]) => void',
description: '拖拽过程中值变更时触发的回调。',
description:
'值变化时触发的回调:在指针拖拽和手指惯性滑动过程中、按下轨道时,以及每次键盘步进时。',
},
{
name: 'onChangeEnd',
type: '(value: number) => void | (value: [number, number]) => void',
description: '拖拽结束时触发的回调。',
description:
'交互结束时触发一次的回调:指针释放或取消时(手指的惯性滑动停止之后),或一次键盘步进完成时。它携带滑块在你的 `onChange` 响应之后渲染的值,因此你限制或拒绝的值不会被报告。',
},
{
name: 'min',
Expand Down Expand Up @@ -419,7 +423,7 @@ export const docsZh = {
name: 'disabledMessage',
type: 'string',
description:
'Explains why the slider is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the thumb focusable via aria-disabled (value changes stay blocked). Use this instead of wrapping a disabled Slider in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.',
'说明滑块被禁用的原因。与 isDisabled 一起使用时,在悬停和键盘聚焦时以提示显示该文本,并用它描述滑块。无论是否设置,禁用的滑块都通过 aria-disabled 保持可聚焦,值的更改仍被阻止。请使用此属性,而不要用 Tooltip 包裹禁用的 Slider:禁用的控件会吞掉外部 Tooltip 所需的悬停事件。',
},
{
name: 'isOptional',
Expand Down Expand Up @@ -454,6 +458,12 @@ export const docsZh = {
type: 'string',
description: '标签旁信息图标的提示文本。',
},
{
name: 'width',
type: 'SizeValue',
description:
'字段宽度(数字表示像素,字符串按原样使用,例如 "100%")。设置整个字段(标签、控件和状态)的宽度,使它们保持对齐。',
},
{
name: 'containerXstyle',
type: 'StyleXStyles',
Expand Down Expand Up @@ -587,8 +597,10 @@ export const docsDense = {
label: 'Label text (always rendered for a11y).',
value:
'Current value; number for single thumb, [number, number] for range.',
onChange: 'Fired on value change during drag.',
onChangeEnd: 'Fired when drag ends.',
onChange:
'Fired as the value changes: drag, finger coast, track press, each key step.',
onChangeEnd:
'Fired once when an interaction ends (release after any finger coast, or a key step), with the value rendered after your onChange.',
min: 'Minimum value.',
max: 'Maximum value.',
step: 'Step increment.',
Expand All @@ -608,5 +620,9 @@ export const docsDense = {
description: 'Description text below label.',
status: 'Status indicator ({type, message}) for validation feedback.',
labelTooltip: 'Tooltip text for info icon next to label.',
width:
'Field width (number = px, string as-is); sizes label, control and status.',
disabledMessage:
'Why it is disabled: tooltip on hover/focus, describes the thumb. A disabled thumb stays focusable either way.',
},
};
185 changes: 178 additions & 7 deletions packages/core/src/Slider/Slider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
rulesDeclaredFor,
} from '../__tests__/pressState';
import * as stylex from '@stylexjs/stylex';
import {Slider} from './Slider';
import {Slider, type SliderRangeProps, type SliderSingleProps} from './Slider';
import {FormLayout} from '../FormLayout';
import {focusOutlineStyles} from '../utils/focusOutline.stylex';
import {
Expand Down Expand Up @@ -62,6 +62,39 @@ beforeEach(() => {
});

/** A thumb's control (the track container), where the pointer handlers live. */
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
/**
* An owner that renders each value Slider reports, so a keyboard step's
* onChangeEnd sees that value rendered.
*/
function OwnedSlider(props: SliderSingleProps) {
const [value, setValue] = useState(props.value);
return (
<Slider
{...props}
value={value}
onChange={next => {
setValue(next);
props.onChange?.(next);
}}
/>
);
}

/** OwnedSlider for a range. */
function OwnedRangeSlider(props: SliderRangeProps) {
const [value, setValue] = useState(props.value);
return (
<Slider
{...props}
value={value}
onChange={next => {
setValue(next);
props.onChange?.(next);
}}
/>
);
}

function controlOf(thumb: HTMLElement): HTMLElement {
const control = thumb.closest<HTMLElement>('.astryx-slider-control');
if (control == null) {
Expand Down Expand Up @@ -611,7 +644,7 @@ describe('Slider', () => {
const handleChange = vi.fn();
const handleChangeEnd = vi.fn();
render(
<Slider
<OwnedSlider
label="Volume"
value={50}
step={5}
Expand All @@ -635,7 +668,7 @@ describe('Slider', () => {
const handleChange = vi.fn();
const handleChangeEnd = vi.fn();
render(
<Slider
<OwnedSlider
label="Opacity"
value={0.2}
min={0}
Expand All @@ -660,7 +693,7 @@ describe('Slider', () => {
const handleChange = vi.fn();
const handleChangeEnd = vi.fn();
render(
<Slider
<OwnedRangeSlider
label="Range"
value={[0.2, 0.6] as [number, number]}
min={0}
Expand All @@ -686,7 +719,7 @@ describe('Slider', () => {
const onChangeEnd = vi.fn();
// From min 1 with step 3 the grid is 1, 4, …, 100; max 101 is off it.
render(
<Slider
<OwnedSlider
label="Zoom"
min={1}
max={101}
Expand Down Expand Up @@ -747,7 +780,7 @@ describe('Slider', () => {
const user = userEvent.setup();
const handleChangeEnd = vi.fn();
render(
<Slider
<OwnedSlider
label="Volume"
value={50}
min={0}
Expand All @@ -768,7 +801,7 @@ describe('Slider', () => {
const user = userEvent.setup();
const handleChangeEnd = vi.fn();
render(
<Slider
<OwnedRangeSlider
label="Range"
value={[20, 80] as [number, number]}
min={0}
Expand Down Expand Up @@ -2388,3 +2421,141 @@ describe('Slider text value', () => {
error.mockRestore();
});
});

describe('onChangeEnd reports the value the owner renders', () => {
function ClampingSlider({
keep,
onChangeEnd,
}: {
keep: (value: number) => number;
onChangeEnd: (value: number) => void;
}) {
const [value, setValue] = useState(50);
return (
<Slider
label="Volume"
value={value}
valueDisplay="none"
onChange={next => setValue(keep(next))}
onChangeEnd={onChangeEnd}
/>
);
}

/** The control, measured as 200 px wide: value = (x - 10) / 1.8. */
function measuredTrack(thumb: HTMLElement): HTMLElement {
const track = controlOf(thumb);
track.getBoundingClientRect = () => ({
left: 0,
top: 0,
right: 200,
bottom: 20,
width: 200,
height: 20,
x: 0,
y: 0,
toJSON: () => {},
});
return track;
}

it('reports the clamped value after a keyboard step', async () => {
const user = userEvent.setup();
const onChangeEnd = vi.fn();
render(
<ClampingSlider keep={v => Math.min(v, 50)} onChangeEnd={onChangeEnd} />,
);
screen.getByRole('slider').focus();
await user.keyboard('{ArrowRight}');
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuenow', '50');
expect(onChangeEnd).toHaveBeenCalledOnce();
expect(onChangeEnd).toHaveBeenCalledWith(50);
});

it('reports the kept value when the owner rejects a keyboard step', async () => {
const user = userEvent.setup();
const onChangeEnd = vi.fn();
render(<ClampingSlider keep={() => 50} onChangeEnd={onChangeEnd} />);
screen.getByRole('slider').focus();
await user.keyboard('{End}');
expect(onChangeEnd).toHaveBeenCalledOnce();
expect(onChangeEnd).toHaveBeenCalledWith(50);
});

it('reports the clamped value once when a pointer drag is released', () => {
const onChangeEnd = vi.fn();
render(
<ClampingSlider keep={v => Math.min(v, 60)} onChangeEnd={onChangeEnd} />,
);
const thumb = screen.getByRole('slider');
const track = measuredTrack(thumb);
fireEvent.pointerDown(track, {clientX: 100, clientY: 10, pointerId: 1});
fireEvent.pointerMove(track, {clientX: 154, clientY: 10, pointerId: 1});
expect(onChangeEnd).not.toHaveBeenCalled();
fireEvent.pointerUp(track, {clientX: 154, clientY: 10, pointerId: 1});
expect(thumb).toHaveAttribute('aria-valuenow', '60');
expect(onChangeEnd).toHaveBeenCalledOnce();
expect(onChangeEnd).toHaveBeenCalledWith(60);
});

it('reports both rendered values of a range', async () => {
const user = userEvent.setup();
const onChangeEnd = vi.fn();
function ClampingRange() {
const [value, setValue] = useState<[number, number]>([20, 70]);
return (
<Slider
label="Price"
value={value}
valueDisplay="none"
onChange={([low, high]) => setValue([low, Math.min(high, 80)])}
onChangeEnd={onChangeEnd}
/>
);
}
render(<ClampingRange />);
screen.getAllByRole('slider')[1].focus();
await user.keyboard('{End}');
expect(onChangeEnd).toHaveBeenCalledOnce();
expect(onChangeEnd).toHaveBeenCalledWith([20, 80]);
});
});

describe('value tooltip', () => {
it('shows the formatted value in a tooltip on keyboard focus', async () => {
const user = userEvent.setup();
render(
<Slider
label="Volume"
value={40}
onChange={() => {}}
formatValue={v => `${v}%`}
/>,
);
await user.tab();
expect(screen.getByRole('slider')).toHaveFocus();
await waitFor(() => {
const tooltip = screen.getByRole('tooltip', {hidden: true});
expect(tooltip).toHaveAttribute('popover-open');
expect(tooltip).toHaveTextContent('40%');
});
});

it('renders no value tooltip with valueDisplay="text"', async () => {
const user = userEvent.setup();
render(
<Slider
label="Volume"
value={40}
onChange={() => {}}
formatValue={v => `${v}%`}
valueDisplay="text"
/>,
);
await user.tab();
expect(
screen.queryByRole('tooltip', {hidden: true}),
).not.toBeInTheDocument();
expect(screen.getByText('40%')).toBeInTheDocument();
});
});
Loading