Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/toggle-button-group-press-tracking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feat] Under the touch presentation, ToggleButtonGroup tracks a finger's or a pen's press across its toggles, like a menu. Press a toggle and slide across the others: the highlight follows, and only the toggle you lift on changes. Lifting outside the group changes nothing. A mouse press is unchanged: it acts as a native toggle's, on the toggle it began on. With a finger, a toggle the group tracks keeps its 44 px target, now drawn by the group. A toggle without a value keeps its own press, and the keyboard is unchanged. Before, each toggle tracked only its own press, and sliding off it changed nothing. An enabled ToggleButtonGroup root is position: relative under a coarse pointer, so absolutely positioned children anchor to the group. With a fine primary pointer, the group is unchanged.

@vjeux
60 changes: 30 additions & 30 deletions docs/specs/components/Button/Button.spec.md

Large diffs are not rendered by default.

74 changes: 52 additions & 22 deletions docs/specs/components/ToggleButton/ToggleButtonGroup.spec.md

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions packages/core/src/Button/Button.hitArea.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/**
* @file Button.hitArea.test.tsx
* @input Button, ButtonGroup, and the shared hit area (mocked)
* @output Unit coverage that a standalone Button takes the shared touch band,
* on its content wrapper, which leaves a caller's ::after alone, and that a
* grouped, disabled or busy-only Button takes none
* @output Unit coverage that a standalone Button takes the shared touch band
* on its content wrapper, which leaves a caller's ::after alone. A grouped
* Button takes none: a ButtonGroup's, or one whose tracking group draws its
* band. A disabled or busy-only Button takes none either.
* @position Button's FR42 touch-target evidence beside Button.test.tsx
*/

Expand All @@ -16,6 +17,7 @@ import type * as HitAreaModule from '../utils/hitArea';
import type {HitInsets} from '../utils/hitArea';
import {touchHitAreaStyles} from '../internal/touchHitArea.stylex';
import {ButtonGroup} from '../ButtonGroup/ButtonGroup';
import {PressGroupBandContext} from '../hooks/pressGroupContext';
import {Button} from './Button';

// On a coarse pointer, the shared hit area measures neighbours in a real
Expand Down Expand Up @@ -130,4 +132,15 @@ describe('Button touch target (AST-094 FR42)', () => {
expect(isWatched(cut)).toBe(false);
expect(bandElementOf(cut)).toBeUndefined();
});

it('leaves a button to a tracking group that draws its band: it draws none of its own', () => {
render(
<PressGroupBandContext value>
<Button label="Bold" />
</PressGroupBandContext>,
);
const bold = screen.getByRole('button', {name: 'Bold'});
expect(isWatched(bold)).toBe(false);
expect(bandElementOf(bold)).toBeUndefined();
});
});
8 changes: 5 additions & 3 deletions packages/core/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {EDGE_COMP_ATTR} from '../Layout/edgeCompensation.stylex';
import {useSize} from '../SizeContext/SizeContext';
import {useButtonGroup} from '../ButtonGroup/ButtonGroupContext';
import {
PressGroupBandContext,
PressGroupContext,
pressGroupItemProps,
} from '../hooks/pressGroupContext';
Expand Down Expand Up @@ -755,9 +756,10 @@ export function Button({
const isBusyOnlyDisabled =
!isTrueDisabled && isLoadingState && !isInterruptible;
const buttonDisabled = isTrueDisabled || isBusyOnlyDisabled;
// The group draws one band for all its members.
// A disabled or busy-only button takes no tap.
const hasHitArea = buttonGroup == null && !buttonDisabled;
// The group draws one band for all its members: a ButtonGroup, or a
// tracking group that says so. A disabled or busy-only button takes no tap.
const groupDrawsBand = use(PressGroupBandContext);
const hasHitArea = buttonGroup == null && !groupDrawsBand && !buttonDisabled;

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 (non-blocking): ToggleButton sets PressGroupBandContext for every valued toggle in a tracking group, but a toggle with aria-haspopup (menu, listbox or true) is left untracked above (MENU_POPUPS), and the group's hit layer grows only tracked toggles. Such a toggle then draws no band of its own and gets none from the group, while on main it has its own (spec:AST-094 FR42). No caller does this today. Skipping the band only for a tracked button keeps it:

const hasHitArea =
  buttonGroup == null && !(groupDrawsBand && isGroupTracked) && !buttonDisabled;

const hitAreaRef = useTouchHitArea(hasHitArea, use(ButtonHitAreaContext));
// isIconOnly prop is the source of truth for icon-only rendering.
// When false (default), label is always rendered as visible text.
Expand Down
90 changes: 56 additions & 34 deletions packages/core/src/ToggleButton/ToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
* - /packages/cli/assets/templates/blocks/components/ToggleButton/ (showcase blocks)
*/

import React, {useOptimistic, type ReactNode} from 'react';
import React, {use, useOptimistic, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {colorVars, fontWeightVars} from '../theme/tokens.stylex';

import {Button, type ButtonSize} from '../Button';
import {
PressGroupBandContext,
PressGroupContext,
} from '../hooks/pressGroupContext';
import {GROUP_PRESS_POINTERS} from '../hooks/useGroupPressTracking';
import type {Elevation} from '../utils/types';
import {useToggleButtonGroup} from './ToggleButtonGroup';
import type {BaseProps} from '../BaseProps';
Expand Down Expand Up @@ -342,40 +347,57 @@ export function ToggleButton({
</span>
) : undefined;

// In a group, the group tracks only a toggle with a value. A toggle without
// one keeps its own press. Outside a group, the toggle follows its parent.
const parentPointers = use(PressGroupContext);
const groupPointers =
group == null
? parentPointers
: group.tracksPress === true && value != null
? GROUP_PRESS_POINTERS
: null;

// The group's hit layer is the touch band of each toggle it tracks.
const isInGroupBand = group != null && groupPointers != null;

return (
<Button
ref={ref}
label={label}
variant="ghost"
size={size}
elevation={elevation}
isDisabled={isDisabled}
isLoading={isLoading}
isInterruptible
isIconOnly={isIconOnly}
icon={resolvedIcon}
tooltip={tooltip}
{...mergeProps(
themeProps('toggle-button', {
isPressed: isPressed ? 'true' : 'false',
elevation,
}),
className,
)}
xstyle={[isPressed ? pressedStyles.background : undefined, xstyle]}
style={style}
onClick={handleClick}
clickAction={clickAction}
{...props}
// The pressed state this component owns wins over a caller's
// same-named attributes (public-component-api INV5).
aria-pressed={isPressed}
{...themeDataAttributes({
isPressed: isPressed ? 'true' : 'false',
elevation,
})}>
{labelContent}
</Button>
<PressGroupContext value={groupPointers}>
<PressGroupBandContext value={isInGroupBand}>
<Button
ref={ref}
label={label}
variant="ghost"
size={size}
elevation={elevation}
isDisabled={isDisabled}
isLoading={isLoading}
isInterruptible
isIconOnly={isIconOnly}
icon={resolvedIcon}
tooltip={tooltip}
{...mergeProps(
themeProps('toggle-button', {
isPressed: isPressed ? 'true' : 'false',
elevation,
}),
className,
)}
xstyle={[isPressed ? pressedStyles.background : undefined, xstyle]}
style={style}
onClick={handleClick}
clickAction={clickAction}
{...props}
// The pressed state this component owns wins over a caller's
// same-named attributes (public-component-api INV5).
aria-pressed={isPressed}
{...themeDataAttributes({
isPressed: isPressed ? 'true' : 'false',
elevation,
})}>
{labelContent}
</Button>
</PressGroupBandContext>
</PressGroupContext>
);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/ToggleButton/ToggleButtonGroup.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const docs = {
'togglegroup',
'buttontogglegroup',
],
description: 'Groups toggle buttons for exclusive (single) or multi-select behavior. Uses discriminated union on type for type-safe value/onChange.',
description: 'Groups toggle buttons for exclusive (single) or multi-select behavior. Uses discriminated union on type for type-safe value/onChange. Under the touch presentation a press tracks like a menu: press a toggle and slide across the others, and the highlight follows. Only the toggle you lift on toggles, and lifting outside the group toggles nothing. With a finger, each toggle\'s target grows toward 44 px, up to half the gap to a neighbour, without the toggles growing.',
props: [
{
name: 'children',
Expand Down Expand Up @@ -85,7 +85,7 @@ export const docs = {
export const docsZh = {
name: 'ToggleButtonGroup',
displayName: 'Toggle Button Group',
description: '将切换按钮分组,支持单选或多选行为。通过 type 判别联合类型实现类型安全。',
description: '将切换按钮分组,支持单选或多选行为。通过 type 判别联合类型实现类型安全。在触摸呈现下(主要指针为粗指针时),按下时分组像菜单一样跟踪按压:按下一个切换按钮并滑过其他按钮,高亮随之移动。只有松开时所在的按钮会切换,在分组外松开则不切换任何按钮。使用手指时,每个按钮的触控目标向 44 px 扩展,最多到与相邻按钮间距的一半,按钮本身不变大。',
propDescriptions: {
children: 'ToggleButton 子元素;每个成员都需要 value。分组控制按下状态,并忽略成员的 isPressed、onPressedChange 和 pressedChangeAction。激活成员只调用分组的 onChange,不启动成员的 Action 过渡。',
label: '分组的无障碍标签 (aria-label)。',
Expand All @@ -103,7 +103,7 @@ export const docsZh = {
export const docsDense = {
name: 'ToggleButtonGroup',
displayName: 'Toggle Button Group',
description: 'groups toggle btns for exclusive/multi-select; discriminated union on type',
description: 'groups toggle btns for exclusive/multi-select; discriminated union on type; on touch a press slides across toggles like a menu and only the one lifted on toggles; a mouse press stays native; finger targets grow toward 44 px, up to half the gap to a neighbour',
propDescriptions: {
children: 'ToggleButton children; each member needs a value. Group owns pressed state; member isPressed/onPressedChange/pressedChangeAction are ignored. Selection uses group onChange, with no member Action transition.',
label: 'a11y label (aria-label)',
Expand Down
47 changes: 42 additions & 5 deletions packages/core/src/ToggleButton/ToggleButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

/**
* @file ToggleButtonGroup.tsx
* @input Layer-scoped React context, ToggleButton children
* @input Layer-scoped React context, ToggleButton children, and the shared
* group press tracking (hooks/useGroupPressTracking)
* @output Exports ToggleButtonGroup component and types
* @position Groups toggle buttons for single or multi-select behavior
*
Expand All @@ -18,12 +19,24 @@
* - /packages/cli/assets/templates/blocks/components/ToggleButton/ (showcase blocks)
*/

import {useCallback, use, useMemo, type ReactNode} from 'react';
import {
useCallback,
use,
useMemo,
useRef,
type ReactNode,
type Ref,
} from 'react';
import {createLayerScopedContext} from '../Layer/layerScopedContext';
import * as stylex from '@stylexjs/stylex';
import {spacingVars} from '../theme/tokens.stylex';
import type {ButtonSize} from '../Button';
import type {BaseProps} from '../BaseProps';
import {PressGroupContext} from '../hooks/pressGroupContext';
import {useTouchPresentation} from '../utils/touchPresentation';
import {useGroupPressTracking} from '../hooks/useGroupPressTracking';
import {useMergedRefs} from '../hooks/useMergedRefs';
import {touchHitAreaStyles} from '../internal/touchHitArea.stylex';
import {mergeProps} from '../utils';
import type {StyleXStyles} from '@stylexjs/stylex';
import {themeProps} from '../utils/themeProps';
Expand All @@ -41,6 +54,8 @@ interface ToggleButtonGroupContextValue {
size?: ButtonSize;
/** Group disabled state. */
isDisabled?: boolean;
/** The group tracks a press across its toggles that have a value. */
tracksPress?: boolean;
}

const ToggleButtonGroupContext =
Expand Down Expand Up @@ -73,6 +88,11 @@ const styles = stylex.create({
flexDirection: 'column',
alignItems: 'stretch',
},
// While the group tracks presses, its hit layer is the root's `::before`,
// below its toggles, so the root is the layer's containing block.
tracking: {
position: 'relative',
},
});

// =============================================================================
Expand Down Expand Up @@ -237,29 +257,46 @@ export function ToggleButtonGroup(props: ToggleButtonGroupProps): ReactNode {
[isMultiple, props.value, props.onChange],
);

// Only under the touch presentation does a press slide across the toggles.
// A desktop press stays the native toggle's.
const {isTouch} = useTouchPresentation();
const tracksPress = !isDisabled && isTouch;
const contextValue = useMemo(
() => ({selectedValues, toggle, size, isDisabled}),
[selectedValues, toggle, size, isDisabled],
() => ({selectedValues, toggle, size, isDisabled, tracksPress}),
[selectedValues, toggle, size, isDisabled, tracksPress],
);
const groupRef = useRef<HTMLDivElement>(null);
const {hitAreaRef} = useGroupPressTracking(groupRef, tracksPress);
// A caller's `ref` reaches the root, like the other host props. The
// group's own refs join it.
const callerRef = (
props as ToggleButtonGroupProps & {ref?: Ref<HTMLDivElement>}
).ref;
const mergedRef = useMergedRefs(callerRef, groupRef, hitAreaRef);

return (
<ToggleButtonGroupContext value={contextValue}>
<div
{...hostProps}
ref={mergedRef}
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
role="group"
aria-label={label}
data-testid={testId}
{...mergeProps(
themeProps('toggle-button-group'),
stylex.props(
styles.group,
tracksPress && styles.tracking,
tracksPress && touchHitAreaStyles.layerBelowChildren,
orientation === 'vertical' && styles.vertical,
xstyle,
),
className,
style,
)}>
{children}
{/* Only a ToggleButton with a value joins the tracking (ToggleButton
reads the group). Any other child keeps its own press. */}
<PressGroupContext value={null}>{children}</PressGroupContext>
</div>
</ToggleButtonGroupContext>
);
Expand Down
Loading
Loading