|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +'use client'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @file ReorderModeHint.tsx |
| 7 | + * @input The handle of the item in reorder mode, its collection's axis, and |
| 8 | + * Core's keyboard hint layer (hooks/anchoredHint.tsx) |
| 9 | + * @output `ReorderModeHint`: reorder mode's hint, anchored below the start of |
| 10 | + * the handle. Its glyphs and Done are buttons that do what their keys do |
| 11 | + * @position Rendered by List's reorder session (List/listReorderEngine.tsx), |
| 12 | + * and by Lab collections through `@astryxdesign/core/internal/reorder-mode` |
| 13 | + */ |
| 14 | + |
| 15 | +import {useRef, type ReactNode} from 'react'; |
| 16 | +import * as stylex from '@stylexjs/stylex'; |
| 17 | +import {anchoredHintStyles, renderAnchoredHint} from '../hooks/anchoredHint'; |
| 18 | +import {useCommittedRef} from '../hooks/useCommittedRef'; |
| 19 | +import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect'; |
| 20 | +import english from '../i18n/generated-english/List/ReorderModeHint.generated'; |
| 21 | +import { |
| 22 | + registerEnglishFallback, |
| 23 | + useComponentTranslator, |
| 24 | +} from '../i18n/useComponentTranslator'; |
| 25 | +import {touchHitAreaStyles} from '../internal/touchHitArea.stylex'; |
| 26 | +import {Kbd} from '../Kbd/Kbd'; |
| 27 | +import {useLayer} from '../Layer/useLayer'; |
| 28 | +import {useLayerDismissal} from '../Layer/useLayerDismissal'; |
| 29 | +import {spacingVars} from '../theme/tokens.stylex'; |
| 30 | +import {focusOutlineProps} from '../utils/focusOutline.stylex'; |
| 31 | +import {useTouchHitArea} from '../utils/touchHitArea'; |
| 32 | + |
| 33 | +registerEnglishFallback(english); |
| 34 | + |
| 35 | +/** A move reorder mode's hint offers: one place in a direction on screen. */ |
| 36 | +export type ReorderHintMove = 'up' | 'down' | 'left' | 'right'; |
| 37 | + |
| 38 | +/** What `ReorderModeHint` takes. */ |
| 39 | +export interface ReorderModeHintProps { |
| 40 | + /** The handle of the item in reorder mode, or null outside the mode. */ |
| 41 | + readonly target: HTMLElement | null; |
| 42 | + /** |
| 43 | + * The axis the collection orders along: `block` offers Up and Down, |
| 44 | + * `inline` offers Left and Right. |
| 45 | + */ |
| 46 | + readonly axis: 'block' | 'inline'; |
| 47 | + /** Does what the move's arrow key does. */ |
| 48 | + readonly onMove: (move: ReorderHintMove) => void; |
| 49 | + /** Ends reorder mode, as Escape does. */ |
| 50 | + readonly onDone: () => void; |
| 51 | + /** Receives the hint's element, which holds its buttons. */ |
| 52 | + readonly onHintElement?: (element: HTMLElement | null) => void; |
| 53 | +} |
| 54 | + |
| 55 | +const styles = stylex.create({ |
| 56 | + // A glyph or Done: the hint's text, with nothing of a button's own, the |
| 57 | + // same size under every pointer. A finger's 44 px target is the shared |
| 58 | + // touch hit layer, which stops short of a neighbouring target. |
| 59 | + button: { |
| 60 | + position: 'relative', |
| 61 | + display: 'inline-flex', |
| 62 | + alignItems: 'center', |
| 63 | + justifyContent: 'center', |
| 64 | + margin: 0, |
| 65 | + padding: 0, |
| 66 | + borderWidth: 0, |
| 67 | + borderRadius: 4, |
| 68 | + backgroundColor: 'transparent', |
| 69 | + color: 'inherit', |
| 70 | + fontFamily: 'inherit', |
| 71 | + fontSize: 'inherit', |
| 72 | + lineHeight: 'inherit', |
| 73 | + cursor: { |
| 74 | + default: 'pointer', |
| 75 | + ':is(:disabled,[aria-disabled="true"])': 'default', |
| 76 | + }, |
| 77 | + }, |
| 78 | + done: { |
| 79 | + paddingInline: spacingVars['--spacing-1'], |
| 80 | + }, |
| 81 | +}); |
| 82 | + |
| 83 | +/** |
| 84 | + * One of the hint's buttons: it never takes the focus from the handle (a |
| 85 | + * press does not move it), and does what its key does. |
| 86 | + */ |
| 87 | +function HintButton({ |
| 88 | + label, |
| 89 | + onPress, |
| 90 | + isText = false, |
| 91 | + children, |
| 92 | +}: { |
| 93 | + label?: string; |
| 94 | + onPress: () => void; |
| 95 | + isText?: boolean; |
| 96 | + children: ReactNode; |
| 97 | +}) { |
| 98 | + // A finger's target: the shared touch hit area grows the button's hit |
| 99 | + // layer where the primary pointer is coarse. |
| 100 | + const hitAreaRef = useTouchHitArea(true); |
| 101 | + return ( |
| 102 | + <button |
| 103 | + ref={hitAreaRef} |
| 104 | + type="button" |
| 105 | + tabIndex={-1} |
| 106 | + aria-label={label} |
| 107 | + onPointerDown={event => event.preventDefault()} |
| 108 | + onMouseDown={event => event.preventDefault()} |
| 109 | + onClick={onPress} |
| 110 | + {...focusOutlineProps.focusVisible([ |
| 111 | + styles.button, |
| 112 | + isText && styles.done, |
| 113 | + touchHitAreaStyles.layer, |
| 114 | + ])}> |
| 115 | + {children} |
| 116 | + </button> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Reorder mode's hint. It is anchored below the start of the handle whose |
| 122 | + * item is in the mode, in Core's keyboard hint's layer and theme roles. It |
| 123 | + * shows the moves for the collection's axis and Done until the mode ends. |
| 124 | + * Each move is a button that does what its key does, and the focus stays on |
| 125 | + * the handle. Done ends the mode as Escape does. So a pointer or a finger |
| 126 | + * can reorder by tapping the handle, the moves, then Done. |
| 127 | + */ |
| 128 | +export function ReorderModeHint({ |
| 129 | + target, |
| 130 | + axis, |
| 131 | + onMove, |
| 132 | + onDone, |
| 133 | + onHintElement, |
| 134 | +}: ReorderModeHintProps): ReactNode { |
| 135 | + const t = useComponentTranslator(); |
| 136 | + const layer = useLayer({mode: 'context'}); |
| 137 | + // The latest layer handle, for the effect below, which re-runs only when |
| 138 | + // the mode's item changes. |
| 139 | + const layerRef = useCommittedRef(layer); |
| 140 | + const hintRef = useRef<HTMLElement | null>(null); |
| 141 | + // External system: the hint's popover in the top layer (useLayer), |
| 142 | + // anchored to the handle of the item in reorder mode. Shown while that |
| 143 | + // item is in the mode; hidden and unanchored when the mode ends or moves on. |
| 144 | + useIsomorphicLayoutEffect(() => { |
| 145 | + if (target == null) { |
| 146 | + return undefined; |
| 147 | + } |
| 148 | + const shown = layerRef.current; |
| 149 | + shown.ref(target); |
| 150 | + shown.show(); |
| 151 | + return () => { |
| 152 | + shown.hide(); |
| 153 | + shown.ref(null); |
| 154 | + }; |
| 155 | + }, [target]); |
| 156 | + // The shared dismissal stack. While the hint is up, an Escape that reaches |
| 157 | + // the stack, with the focus in the hint, ends reorder mode. A layer below |
| 158 | + // it, such as a Dialog, stays open. The handle claims its own Escape first. |
| 159 | + useLayerDismissal({ |
| 160 | + isActive: target != null, |
| 161 | + onDismiss: onDone, |
| 162 | + getContainer: () => hintRef.current, |
| 163 | + }); |
| 164 | + const [back, forward]: ReadonlyArray<ReorderHintMove> = |
| 165 | + axis === 'block' ? ['up', 'down'] : ['left', 'right']; |
| 166 | + return renderAnchoredHint( |
| 167 | + layer, |
| 168 | + target == null ? null : ( |
| 169 | + <span |
| 170 | + ref={element => { |
| 171 | + hintRef.current = element; |
| 172 | + onHintElement?.(element); |
| 173 | + }} |
| 174 | + {...stylex.props(anchoredHintStyles.keys)}> |
| 175 | + <HintButton |
| 176 | + label={ |
| 177 | + axis === 'block' |
| 178 | + ? t('@astryx.list.reorderMoveUp') |
| 179 | + : t('@astryx.list.reorderMoveLeft') |
| 180 | + } |
| 181 | + onPress={() => onMove(back)}> |
| 182 | + <Kbd keys={back} aria-hidden /> |
| 183 | + </HintButton> |
| 184 | + <HintButton |
| 185 | + label={ |
| 186 | + axis === 'block' |
| 187 | + ? t('@astryx.list.reorderMoveDown') |
| 188 | + : t('@astryx.list.reorderMoveRight') |
| 189 | + } |
| 190 | + onPress={() => onMove(forward)}> |
| 191 | + <Kbd keys={forward} aria-hidden /> |
| 192 | + </HintButton> |
| 193 | + <HintButton isText onPress={onDone}> |
| 194 | + {t('@astryx.list.reorderDone')} |
| 195 | + </HintButton> |
| 196 | + </span> |
| 197 | + ), |
| 198 | + {isInteractive: true}, |
| 199 | + ); |
| 200 | +} |
0 commit comments