Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
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
---

[docs] Slider: the `onChange` and `onChangeEnd` docs say when each callback fires, for a drag, a mouse press or a finger's tap on the track, a cancelled press and a keyboard step; the `disabledMessage` docs say a disabled thumb stays focusable with or without a reason; the Chinese docs list `width` and translate `disabledMessage`, and the dense docs describe both.

@cixzhang
2 changes: 1 addition & 1 deletion apps/storybook/stories/Slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const meta: Meta<typeof Slider> = {
disabledMessage: {
control: 'text',
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.',
'Explains why the slider is disabled. With isDisabled, shows a tooltip on hover and keyboard focus and describes the thumb. 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.',
},
min: {
control: 'number',
Expand Down
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 on each value step: through a pointer drag and a finger’s coast, at once on a mouse press on the track, from the lift of a finger’s or a pen’s tap on the track, 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 a cancelled drag (after a finger’s coast or a track tap’s spring settles, or at a press that stops it), or a completed keyboard step. A finger’s or a pen’s touch on the track that sets no value (a tap on the thumb’s own value or during a coast, or a swipe) reports nothing, and so do a finger’s or a pen’s press that the browser cancels before it moves and a drag that disabling the slider cancels before the value moves.',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miss: this bound is by pointer type, but the case is the thumb. A mouse press on a thumb that a window blur or a pointercancel ends before the value moves reports nothing either, while the same press released normally reports once (run against this head: no call with the cancel, onChangeEnd(50) without it, both under a fine and a coarse primary pointer; after the value has moved the cancel reports onChangeEnd(60)). A thumb press belongs to the touch controller for every pointer (Slider.tsx:1138-1148), and a cancel there ends it with no report; a mouse press on the track keeps reporting, since it sets the value at once. For example:

'Callback fired once when an interaction ends: a pointer release or a cancelled drag (after a finger’s coast or a track tap’s spring settles, or at a press that stops it), or a completed keyboard step. A finger’s or a pen’s touch on the track that sets no value (a tap on the thumb’s own value or during a coast, or a swipe) reports nothing, and so do a press on a thumb that the browser cancels before the value moves, a finger’s or a pen’s press on the track that it cancels, and a drag that disabling the slider cancels before the value moves.'

The same bound is in the Chinese entry (line 361), the dense line (line 603) and both onChangeEnd JSDoc blocks in Slider.tsx.

},
{
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:
'交互结束时触发一次的回调:指针释放或拖拽被取消时(在手指的惯性滑动或轻点轨道后的弹簧动画停止之后,或在一次按压使其停止时),或一次键盘步进完成时。手指或触控笔在轨道上未设置值的触摸(轻点滑块当前值的位置、在惯性滑动期间轻点,或滑扫)不会触发它;手指或触控笔的按压若在移动之前被浏览器取消,或拖拽在值改变之前因禁用滑块而被取消,也不会触发它。',
},
{
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 一起使用时,在悬停和键盘聚焦时以提示显示该文本,并用它描述滑块。无论是否设置 disabledMessage,禁用的滑块都通过 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 on each value step: drag and finger coast, mouse track press at once, finger or pen track tap on lift, key step.',
onChangeEnd:
'Fired once when an interaction ends: release or cancelled drag (after a coast or tap spring settles, or a press stops it), or a key step. A touch that sets no value, a finger or pen press cancelled before moving, or a drag cancelled by disabling reports nothing.',
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
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:
'A disabled thumb stays focusable with or without this message; the message shows on hover and keyboard focus and describes the thumb.',
},
};
84 changes: 84 additions & 0 deletions packages/core/src/Slider/Slider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2388,3 +2388,87 @@ describe('Slider text value', () => {
error.mockRestore();
});
});

describe('onChangeEnd after a pointer release', () => {
/** 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 value the owner renders when it clamps the drag', () => {
const onChangeEnd = vi.fn();
function ClampingSlider() {
const [value, setValue] = useState(50);
return (
<Slider
label="Volume"
value={value}
valueDisplay="none"
onChange={(next: number) => setValue(Math.min(next, 60))}
onChangeEnd={onChangeEnd}
/>
);
}
render(<ClampingSlider />);
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);
});
});

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();
});
});
38 changes: 31 additions & 7 deletions packages/core/src/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export interface SliderBaseProps extends Omit<
/**
* Explains why the slider is disabled. When set together with `isDisabled`,
* the slider shows a tooltip with this text on hover and keyboard focus, and
* the thumb stays focusable (via `aria-disabled`) so the reason is
* discoverable by keyboard and assistive technology. Value changes stay
* blocked.
* the thumb is described by it, so the reason is discoverable by keyboard
* and assistive technology. A disabled thumb stays focusable with
* `aria-disabled` either way. Value changes stay blocked.
*
* Use this instead of wrapping a disabled slider in `Tooltip` — disabled
* controls don't emit the pointer events an external tooltip needs.
Expand Down Expand Up @@ -158,18 +158,42 @@ export interface SliderBaseProps extends Omit<
export interface SliderSingleProps extends SliderBaseProps {
/** Current value (single thumb mode). */
value: number;
/** Callback fired on value change during drag. */
/**
* Callback fired on each value step: through a pointer drag and a finger's
* coast, at once on a mouse press on the track, from the lift of a finger's
* or a pen's tap on the track, and on each keyboard step.
*/
onChange?: (value: number) => void;
/** Callback fired when drag ends (on pointer up or keyboard). */
/**
* Callback fired once when an interaction ends: a pointer release or a
* cancelled drag (after a finger's coast or a track tap's spring settles,
* or at a press that stops it), or a completed keyboard step. A finger's or
* a pen's touch on the track that sets no value (a tap on the thumb's own
* value or during a coast, or a swipe) reports nothing, and so do a
* finger's or a pen's press that the browser cancels before it moves and a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miss: same bound as the docs entry — a mouse press on a thumb that the browser cancels before the value moves reports nothing too, so the pointer type is not what separates the cases. Please bound it by the thumb here and at line 194:

   * value or during a coast, or a swipe) reports nothing, and so do a press
   * on a thumb that the browser cancels before the value moves, a finger's
   * or a pen's press on the track that it cancels, and a drag that disabling
   * the slider cancels before the value moves.

* drag that disabling the slider cancels before the value moves.
*/
onChangeEnd?: (value: number) => void;
}

export interface SliderRangeProps extends SliderBaseProps {
/** Current value (range mode: [min, max]). */
value: [number, number];
/** Callback fired on value change during drag. */
/**
* Callback fired on each value step: through a pointer drag and a finger's
* coast, at once on a mouse press on the track, from the lift of a finger's
* or a pen's tap on the track, and on each keyboard step.
*/
onChange?: (value: [number, number]) => void;
/** Callback fired when drag ends (on pointer up or keyboard). */
/**
* Callback fired once when an interaction ends: a pointer release or a
* cancelled drag (after a finger's coast or a track tap's spring settles,
* or at a press that stops it), or a completed keyboard step. A finger's or
* a pen's touch on the track that sets no value (a tap on the thumb's own
* value or during a coast, or a swipe) reports nothing, and so do a
* finger's or a pen's press that the browser cancels before it moves and a
* drag that disabling the slider cancels before the value moves.
*/
onChangeEnd?: (value: [number, number]) => void;
/** Minimum number of steps between thumbs. */
minStepsBetweenThumbs?: number;
Expand Down
Loading