Skip to content
Open
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/reorder-internal-shared-parts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[chore] Internal: a new internal entry, `@astryxdesign/core/internal/reorder-mode`, reserved for Astryx's own packages, gives a collection that runs its own reorder mode the parts a List's uses: its mode hint, along either axis, its strings, its pointer input with a finger's hold, its edge autoscroll, scroll watch and bounds, the preview copy, and the text it names items by. The internal reorder entry gains a way for the object's `start(key)` to reach such a collection. The catalog gains Move left and Move right for the hint's inline moves. The entry is not part of the public API. Apps see no change.

@humbertovirtudes
24 changes: 24 additions & 0 deletions docs/specs/components/List/List.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ verified_by:
packages/core/src/List/listLayoutUpdates.test.tsx,
packages/core/src/List/__tests__/ListUpdates.interaction.browser.spec.ts,
packages/core/src/List/__tests__/ListReorder.test.tsx,
packages/core/src/internal/reorderShared.test.tsx,
packages/core/src/List/__tests__/ListReorderLayout.test.tsx,
packages/core/src/List/__tests__/ListReorderTouch.interaction.browser.spec.ts,
packages/core/src/List/__tests__/ListReorderPointer.interaction.browser.spec.ts,
Expand Down Expand Up @@ -116,6 +117,29 @@ Consumer migration instructions belong in consumer docs and release notes.
through `@astryxdesign/core/internal/reorder`. It tells whether an element
is a reorder handle, so a Lab collection that puts the handle beside its
own controls tells them apart: ListInput's field focus skips the handle.
- The parts of List's reorder that a Lab collection running its own reorder
mode shares, through the internal entry
`@astryxdesign/core/internal/reorder-mode` (`internal/reorder-mode.ts`):
- the mode's hint, `ReorderModeHint` (`List/ReorderModeHint.tsx`), with
Move up and Move down on the block axis or Move left and Move right on
the inline axis;
- its strings, `reorderMessagesFrom` (`List/reorderMessages.ts`);
- its pointer input, `attachReorderMouse` and `attachReorderHold`
(`List/reorderPointer.ts`): a mouse, or a pen where the primary pointer
is fine, drags at once; a finger, or a pen where it is coarse, after the
hold;
- its edge autoscroll, `reorderAutoscroll` (`List/reorderAutoscroll.ts`)
with `autoscrollStep`, and its watch of the scroller,
`watchReorderScroll` (`List/reorderScrollWatch.ts`), which folds a
scroll the drag did not cause into the drag in a later frame;
- its drop bounds, `movableRange`, and its preview copy, `previewCopyOf`
with the bare box `bareCopyOf` it falls back to, and
`paintedBackground`;
- the text it names an item's handle by, `getNodeText`.

`mountReorderStarter`, in `@astryxdesign/core/internal/reorder`, lets the
object's `start(key)` reach that collection (`spec:AST-103` FR4a).

- `@astryxdesign/core/internal/reorderHandle.stylex` holds the reorder
handle's grip and reach sizes (`grip`, `targetReach`, `touchReach`), which
List, ListInput and TransferList share. It is internal, like the other
Expand Down
8 changes: 8 additions & 0 deletions packages/core/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@
"defaultMessage": "Move down",
"description": "Accessible label of the down-arrow button in the hint a List row's reorder mode shows: it moves the row one place down, as the Down arrow key does."
},
"@astryx.list.reorderMoveLeft": {
"defaultMessage": "Move left",
"description": "Accessible label of the left-arrow button in the hint that reorder mode shows for items in a row, such as a Table's columns: it moves the item one place to the left, as the Left arrow key does."
},
"@astryx.list.reorderMoveRight": {
"defaultMessage": "Move right",
"description": "Accessible label of the right-arrow button in the hint that reorder mode shows for items in a row, such as a Table's columns: it moves the item one place to the right, as the Right arrow key does."
},
"@astryx.list.reorderDone": {
"defaultMessage": "Done",
"description": "Button in the hint a List row's reorder mode shows: it ends the mode and keeps the moves, as Escape does."
Expand Down
5 changes: 5 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,11 @@
"types": "./dist/internal/reorder.d.ts",
"default": "./dist/internal/reorder.js"
},
"./internal/reorder-mode": {
"source": "./src/internal/reorder-mode.ts",
"types": "./dist/internal/reorder-mode.d.ts",
"default": "./dist/internal/reorder-mode.js"
},
"./internal/reorderHandle.stylex": {
"source": "./src/internal/reorderHandle.stylex.ts",
"types": "./dist/internal/reorderHandle.stylex.d.ts",
Expand Down
200 changes: 200 additions & 0 deletions packages/core/src/List/ReorderModeHint.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

/**
* @file ReorderModeHint.tsx
* @input The handle of the item in reorder mode, its collection's axis, and
* Core's keyboard hint layer (hooks/anchoredHint.tsx)
* @output `ReorderModeHint`: reorder mode's hint, anchored below the start of
* the handle. Its glyphs and Done are buttons that do what their keys do
* @position Rendered by List's reorder session (List/listReorderEngine.tsx),
* and by Lab collections through `@astryxdesign/core/internal/reorder-mode`
*/

import {useRef, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {anchoredHintStyles, renderAnchoredHint} from '../hooks/anchoredHint';
import {useCommittedRef} from '../hooks/useCommittedRef';
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
import english from '../i18n/generated-english/List/ReorderModeHint.generated';
import {
registerEnglishFallback,
useComponentTranslator,
} from '../i18n/useComponentTranslator';
import {touchHitAreaStyles} from '../internal/touchHitArea.stylex';
import {Kbd} from '../Kbd/Kbd';
import {useLayer} from '../Layer/useLayer';
import {useLayerDismissal} from '../Layer/useLayerDismissal';
import {spacingVars} from '../theme/tokens.stylex';
import {focusOutlineProps} from '../utils/focusOutline.stylex';
import {useTouchHitArea} from '../utils/touchHitArea';

registerEnglishFallback(english);

/** A move reorder mode's hint offers: one place in a direction on screen. */
export type ReorderHintMove = 'up' | 'down' | 'left' | 'right';

/** What `ReorderModeHint` takes. */
export interface ReorderModeHintProps {
/** The handle of the item in reorder mode, or null outside the mode. */
readonly target: HTMLElement | null;
/**
* The axis the collection orders along: `block` offers Up and Down,
* `inline` offers Left and Right.
*/
readonly axis: 'block' | 'inline';
/** Does what the move's arrow key does. */
readonly onMove: (move: ReorderHintMove) => void;
/** Ends reorder mode, as Escape does. */
readonly onDone: () => void;
/** Receives the hint's element, which holds its buttons. */
readonly onHintElement?: (element: HTMLElement | null) => void;
}

const styles = stylex.create({
// A glyph or Done: the hint's text, with nothing of a button's own, the
// same size under every pointer. A finger's 44 px target is the shared
// touch hit layer, which stops short of a neighbouring target.
button: {
position: 'relative',
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
margin: 0,
padding: 0,
borderWidth: 0,
borderRadius: 4,
backgroundColor: 'transparent',
color: 'inherit',
fontFamily: 'inherit',
fontSize: 'inherit',
lineHeight: 'inherit',
cursor: {
default: 'pointer',
':is(:disabled,[aria-disabled="true"])': 'default',
},
},
done: {
paddingInline: spacingVars['--spacing-1'],
},
});

/**
* One of the hint's buttons: it never takes the focus from the handle (a
* press does not move it), and does what its key does.
*/
function HintButton({
label,
onPress,
isText = false,
children,
}: {
label?: string;
onPress: () => void;
isText?: boolean;
children: ReactNode;
}) {
// A finger's target: the shared touch hit area grows the button's hit
// layer where the primary pointer is coarse.
const hitAreaRef = useTouchHitArea(true);
return (
<button
ref={hitAreaRef}
type="button"
tabIndex={-1}
aria-label={label}
onPointerDown={event => event.preventDefault()}
onMouseDown={event => event.preventDefault()}
onClick={onPress}
{...focusOutlineProps.focusVisible([
styles.button,
isText && styles.done,
touchHitAreaStyles.layer,
])}>
{children}
</button>
);
}

/**
* Reorder mode's hint. It is anchored below the start of the handle whose
* item is in the mode, in Core's keyboard hint's layer and theme roles. It
* shows the moves for the collection's axis and Done until the mode ends.
* Each move is a button that does what its key does, and the focus stays on
* the handle. Done ends the mode as Escape does. So a pointer or a finger
* can reorder by tapping the handle, the moves, then Done.
*/
export function ReorderModeHint({
target,
axis,
onMove,
onDone,
onHintElement,
}: ReorderModeHintProps): ReactNode {
const t = useComponentTranslator();
const layer = useLayer({mode: 'context'});
// The latest layer handle, for the effect below, which re-runs only when
// the mode's item changes.
const layerRef = useCommittedRef(layer);
const hintRef = useRef<HTMLElement | null>(null);
// External system: the hint's popover in the top layer (useLayer),
// anchored to the handle of the item in reorder mode. Shown while that
// item is in the mode; hidden and unanchored when the mode ends or moves on.
useIsomorphicLayoutEffect(() => {
if (target == null) {
return undefined;
}
const shown = layerRef.current;
shown.ref(target);
shown.show();
return () => {
shown.hide();
shown.ref(null);
};
}, [target]);
// The shared dismissal stack. While the hint is up, an Escape that reaches
// the stack, with the focus in the hint, ends reorder mode. A layer below
// it, such as a Dialog, stays open. The handle claims its own Escape first.
useLayerDismissal({
isActive: target != null,
onDismiss: onDone,
getContainer: () => hintRef.current,
});
const [back, forward]: ReadonlyArray<ReorderHintMove> =
axis === 'block' ? ['up', 'down'] : ['left', 'right'];
return renderAnchoredHint(
layer,
target == null ? null : (
<span
ref={element => {
hintRef.current = element;
onHintElement?.(element);
}}
{...stylex.props(anchoredHintStyles.keys)}>
<HintButton
label={
axis === 'block'
? t('@astryx.list.reorderMoveUp')
: t('@astryx.list.reorderMoveLeft')
}
onPress={() => onMove(back)}>
<Kbd keys={back} aria-hidden />
</HintButton>
<HintButton
label={
axis === 'block'
? t('@astryx.list.reorderMoveDown')
: t('@astryx.list.reorderMoveRight')
}
onPress={() => onMove(forward)}>
<Kbd keys={forward} aria-hidden />
</HintButton>
<HintButton isText onPress={onDone}>
{t('@astryx.list.reorderDone')}
</HintButton>
</span>
),
{isInteractive: true},
);
}
14 changes: 14 additions & 0 deletions packages/core/src/List/listReorderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ export interface ReorderRows {
holds(element: Element, container: HTMLElement): boolean;
}

/** A collection that starts reorder mode for one of its items, by key. */
export interface ReorderStarter {
/** Starts reorder mode for the item with `key`; returns whether it did. */
startByKey(key: Key): boolean;
}

/** The machinery a reorderable object carries. */
export interface ListReorderEngine {
/**
Expand All @@ -230,6 +236,14 @@ export interface ListReorderEngine {
* one mounted last if several.
*/
start(options: ListReorderOptions, key: Key): void;
/**
* Lets `start` reach a collection that runs its own reorder mode, mounted
* with the object with `options`. Returns the removal.
*/
mountStarter(
options: ListReorderOptions,
starter: ReorderStarter,
): () => void;
}

/** What a reorderable object holds (reorderable/createReorderable.ts). */
Expand Down
Loading
Loading