Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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, including keyboard steps and track presses; 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
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, on a track press, and on each keyboard step.',
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
},
{
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.',
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
},
{
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, finger coast, track press, key step.',
onChangeEnd:
'Fired once when an interaction ends: release (after any finger coast) or a key step.',
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.',
},
};
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 => setValue(Math.min(next, 60))}
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
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();
});
});
26 changes: 19 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,30 @@ 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, on a track press, 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
* cancel (after a finger's coast settles) or a completed keyboard step.
*/
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, on a track press, 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
* cancel (after a finger's coast settles) or a completed keyboard step.
*/
onChangeEnd?: (value: [number, number]) => void;
/** Minimum number of steps between thumbs. */
minStepsBetweenThumbs?: number;
Expand Down
Loading