Skip to content
Merged
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/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, each toggle's target grows toward 44 px, up to half the gap to a neighbour, without the toggles growing. 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.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

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

Large diffs are not rendered by default.

78 changes: 46 additions & 32 deletions packages/core/src/ToggleButton/ToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
* - /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 {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 +344,52 @@ export function ToggleButton({
</span>
) : undefined;

// In a group, only a toggle with a value is tracked; a toggle without one
// keeps its own press. Outside a group, the toggle follows its parent.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
const parentPointers = use(PressGroupContext);
const groupPointers =
group == null
? parentPointers
: group.tracksPress === true && value != null
? GROUP_PRESS_POINTERS
: 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', {
<PressGroupContext value={groupPointers}>
<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,
}),
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>
})}>
{labelContent}
</Button>
</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 as createContext} 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 '../utils/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 @@ -69,6 +84,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 @@ -233,29 +253,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