Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Tokenizer): place the caret after the last token (#7922)

Kenton Quatmanee74c1822577
diff --git a/.changeset/tokenizer-caret-after-last-token.md b/.changeset/tokenizer-caret-after-last-token.md
new file mode 100644
index 0000000000..a86461c99f
--- /dev/null
+++ b/.changeset/tokenizer-caret-after-last-token.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Tokenizer: once a field has tokens, its text cursor sits right after the last token, on the last row, instead of at the end of the first row. PowerSearch gets the same fix. When the last row is too full for the cursor, focusing starts a new row for it; when it fits the cursor but not the typing area, the first keystroke moves to a new row. At rest the field keeps its height.
+@kentonquatman
diff --git a/.github/scripts/story-play-guard.js b/.github/scripts/story-play-guard.js
index 15312b2fae..95564ecf6a 100644
--- a/.github/scripts/story-play-guard.js
+++ b/.github/scripts/story-play-guard.js
@@ -152,6 +152,19 @@ const TARGETS = [
guards:
'on a coarse pointer, the compact combobox and full Clear all hit area remain separate in RTL as well',
},
+ {
+ component: 'Tokenizer',
+ story: 'core-tokenizer--caret-placement',
+ guards:
+ 'a focused empty combobox puts its caret right after the last token on the last row without moving tokens, and a row too full for the caret keeps its resting height and starts a new row only on focus, never for a read-only field',
+ },
+ {
+ component: 'Tokenizer (RTL)',
+ story: 'core-tokenizer--caret-placement',
+ direction: 'rtl',
+ guards:
+ 'the caret follows the last token at its inline end, and a too-full row stays one row at rest and while read-only, under RTL as well',
+ },
// spec:AST-059 — the layer runtime's viewport inset. Each story is one
// claim of the record; the guard is its rendered evidence, at a desktop
// viewport and at a phone's, because a cap that holds only where the
diff --git a/apps/storybook/stories/Tokenizer.stories.tsx b/apps/storybook/stories/Tokenizer.stories.tsx
index 355c0cb98e..d85226ab45 100644
--- a/apps/storybook/stories/Tokenizer.stories.tsx
+++ b/apps/storybook/stories/Tokenizer.stories.tsx
@@ -589,3 +589,212 @@ export const LongTokenEndLane: Story = {
},
name: 'Long token beside the end lane',
};
+
+/**
+ * Where the caret goes once the field has tokens. Focused with an empty query,
+ * it sits right after the last token, on the last row, where typed text will
+ * start (top). A row with no room left for it (bottom, narrowed by the play
+ * function) keeps its height at rest; focusing moves the caret to the start of
+ * a new row, as wrapped text would, unless the field is read-only.
+ */
+export const CaretPlacement: Story = {
+ render: args => {
+ const [wrapped, setWrapped] = useState(users.slice(0, 5));
+ const [oneRow, setOneRow] = useState(users.slice(0, 2));
+ return (
+ <div style={{display: 'grid', gap: 16}}>
+ <Tokenizer
+ {...args}
+ searchSource={userSource}
+ value={wrapped}
+ onChange={items => setWrapped(items)}
+ />
+ <div data-full-row-host="">
+ <Tokenizer
+ {...args}
+ label="Reviewers"
+ searchSource={userSource}
+ value={oneRow}
+ onChange={items => setOneRow(items)}
+ />
+ </div>
+ </div>
+ );
+ },
+ args: {
+ label: 'Team Members',
+ placeholder: 'Add more...',
+ hasClear: true,
+ },
+ name: 'Caret placement',
+ play: async ({canvasElement}) => {
+ const [wrappedField, fullRowField] =
+ canvasElement.querySelectorAll<HTMLElement>('.astryx-tokenizer');
+ const host = canvasElement.querySelector<HTMLElement>(
+ '[data-full-row-host]',
+ );
+ if (!wrappedField || !fullRowField || !host) {
+ throw new Error('Caret fixtures did not render as expected');
+ }
+ const settle = () =>
+ new Promise<void>(resolve =>
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve())),
+ );
+ const parts = (field: HTMLElement) => {
+ const input = field.querySelector<HTMLInputElement>('[role="combobox"]');
+ const tokens = [...field.querySelectorAll<HTMLElement>('.astryx-token')];
+ const lastToken = tokens.at(-1);
+ if (!input || !lastToken) {
+ throw new Error('A caret fixture is missing its input or tokens');
+ }
+ return {input, firstToken: tokens[0], lastToken, tokens};
+ };
+ // Room left between the last token and the field's inline-end content
+ // edge, which already stops short of the end lane.
+ const roomAfter = (field: HTMLElement, token: HTMLElement) => {
+ const style = getComputedStyle(field);
+ const fieldRect = field.getBoundingClientRect();
+ const tokenRect = token.getBoundingClientRect();
+ return style.direction === 'rtl'
+ ? tokenRect.left -
+ (fieldRect.left +
+ Number.parseFloat(style.borderLeftWidth) +
+ Number.parseFloat(style.paddingLeft))
+ : fieldRect.right -
+ Number.parseFloat(style.borderRightWidth) -
+ Number.parseFloat(style.paddingRight) -
+ tokenRect.right;
+ };
+ // An empty input paints its caret at the edge of its content box that its
+ // text alignment starts from.
+ const caretOf = (input: HTMLInputElement) => {
+ const style = getComputedStyle(input);
+ const isRtl = style.direction === 'rtl';
+ const rect = input.getBoundingClientRect();
+ const {textAlign} = style;
+ const alignsRight =
+ textAlign === 'right' ||
+ (textAlign === 'start' && isRtl) ||
+ (textAlign === 'end' && !isRtl);
+ return {
+ isRtl,
+ x: alignsRight
+ ? rect.right - Number.parseFloat(style.paddingRight)
+ : rect.left + Number.parseFloat(style.paddingLeft),
+ y: rect.top + rect.height / 2,
+ };
+ };
+ const heightOf = (field: HTMLElement) =>
+ field.getBoundingClientRect().height;
+
+ await document.fonts.ready;
+ const reserveDeadline = performance.now() + 2000;
+ while (
+ [wrappedField, fullRowField].some(
+ field =>
+ field.style.getPropertyValue('--_tokenizer-end-lane-reserve') === '',
+ )
+ ) {
+ if (performance.now() >= reserveDeadline) {
+ throw new Error('Clear-all lane reserve was not measured');
+ }
+ await new Promise<void>(resolve =>
+ requestAnimationFrame(() => resolve()),
+ );
+ }
+ await settle();
+
+ // Wrapped tokens: the caret follows the last token, on its row.
+ {
+ const {input, lastToken, tokens} = parts(wrappedField);
+ const rows = new Set(
+ tokens.map(token => Math.round(token.getBoundingClientRect().top)),
+ );
+ if (rows.size < 2 || roomAfter(wrappedField, lastToken) < 20) {
+ throw new Error(
+ 'Fixture no longer wraps with room after the last token',
+ );
+ }
+ const restingHeight = heightOf(wrappedField);
+ const restingToken = lastToken.getBoundingClientRect();
+ input.focus();
+ await settle();
+ if (document.activeElement !== input) {
+ throw new Error('The combobox did not take focus');
+ }
+ if (Math.abs(heightOf(wrappedField) - restingHeight) > 0.5) {
+ throw new Error(
+ `Focusing changed the field height from ${restingHeight.toFixed(2)} to ${heightOf(wrappedField).toFixed(2)}`,
+ );
+ }
+ const token = lastToken.getBoundingClientRect();
+ if (
+ Math.abs(token.left - restingToken.left) > 0.5 ||
+ Math.abs(token.top - restingToken.top) > 0.5
+ ) {
+ throw new Error('Focusing moved the last token');
+ }
+ const caret = caretOf(input);
+ if (caret.y < token.top || caret.y > token.bottom) {
+ throw new Error(
+ `The caret is not on the last token's row: caret y ${caret.y.toFixed(2)}, last token ${token.top.toFixed(2)}–${token.bottom.toFixed(2)}`,
+ );
+ }
+ const offset = caret.isRtl ? token.left - caret.x : caret.x - token.right;
+ if (offset < 0 || offset > 12) {
+ throw new Error(
+ `The caret is ${offset.toFixed(2)}px from the last token's inline end; expected it right after the token`,
+ );
+ }
+ input.blur();
+ }
+
+ // A row with less room than the caret's text inset: no blank row at rest,
+ // and focusing starts a new row.
+ {
+ const {input, firstToken, lastToken} = parts(fullRowField);
+ const restingHeight = heightOf(fullRowField);
+ const room = roomAfter(fullRowField, lastToken);
+ host.style.width = `${host.getBoundingClientRect().width - (room - 3)}px`;
+ await settle();
+ if (Math.abs(roomAfter(fullRowField, lastToken) - 3) > 0.5) {
+ throw new Error('Could not leave 3px after the last token');
+ }
+ if (Math.abs(heightOf(fullRowField) - restingHeight) > 0.5) {
+ throw new Error(
+ `With 3px left on the row, the resting field grew from ${restingHeight.toFixed(2)} to ${heightOf(fullRowField).toFixed(2)}`,
+ );
+ }
+ input.focus();
+ await settle();
+ const caret = caretOf(input);
+ const token = lastToken.getBoundingClientRect();
+ const rowStart = firstToken.getBoundingClientRect();
+ const startOffset = caret.isRtl
+ ? rowStart.right - caret.x
+ : caret.x - rowStart.left;
+ if (caret.y <= token.bottom || startOffset < 0 || startOffset > 8) {
+ throw new Error(
+ 'With no room after the last token, the focused caret did not start a new row',
+ );
+ }
+ input.blur();
+ await settle();
+
+ // A disabled field with a disabledMessage stays focusable, but read-only.
+ // Nothing can be typed there, so focusing it must not start a new row.
+ input.readOnly = true;
+ input.focus();
+ await settle();
+ const readOnlyHeight = heightOf(fullRowField);
+ input.blur();
+ input.readOnly = false;
+ host.style.width = '';
+ if (Math.abs(readOnlyHeight - restingHeight) > 0.5) {
+ throw new Error(
+ `With 3px left on the row, focusing a read-only field grew it from ${restingHeight.toFixed(2)} to ${readOnlyHeight.toFixed(2)}`,
+ );
+ }
+ }
+ },
+};
diff --git a/packages/core/src/Tokenizer/Tokenizer.tsx b/packages/core/src/Tokenizer/Tokenizer.tsx
index 16627c2a24..10988427ce 100644
--- a/packages/core/src/Tokenizer/Tokenizer.tsx
+++ b/packages/core/src/Tokenizer/Tokenizer.tsx
@@ -272,12 +272,8 @@ export interface TokenizerProps<T extends SearchableItem> extends Omit<
// because the input's reserve is derived from the same value.
const END_LANE_INSET = spacingVars['--spacing-2'];
const TOKEN_ROW_INSET = `calc(${spacingVars['--spacing-1']} - 1px)`;
-// InputClearButton grows 2px past its visible box under `(pointer: coarse)`.
-// Keep this maximum reserve in sync with Field/InputClearButton.tsx.
-const INPUT_CLEAR_COARSE_POINTER_OUTSET = '2px';
-// Kept in sync with useEndLaneReserve: wrapperWithTokens and inputCompact read
-// the custom property directly, to keep token rows and the empty-query hit
-// surface short of the lane.
+// Kept in sync with useEndLaneReserve: wrapperWithTokens reads the custom
+// property directly, to keep token rows and the input short of the lane.
const END_LANE_RESERVE_VAR = '--_tokenizer-end-lane-reserve';
const styles = stylex.create({
@@ -350,47 +346,36 @@ const styles = stylex.create({
position: 'absolute',
},
inputCompact: {
- position: {
- default: 'relative',
- ':placeholder-shown': 'absolute',
- },
+ position: 'relative',
boxSizing: 'border-box',
- insetInlineStart: {
- default: 'auto',
- ':placeholder-shown': TOKEN_ROW_INSET,
- },
zIndex: 0,
+ // The input follows the last token in flow, so a focused empty input's
+ // caret sits where typed text will start. Empty, it asks only for that
+ // inset, and only while focused and editable: blurred or read-only (a
+ // disabled field with a disabledMessage stays focusable), it claims no
+ // room at all, not even the column gap, so it never grows the field a
+ // blank row. Typing restores the full editing width, which may wrap.
minWidth: {
default: '40px',
- ':placeholder-shown': '1px',
+ ':placeholder-shown': 0,
},
flex: {
default: '1 1 40px',
- ':placeholder-shown': '0 0 auto',
+ ':placeholder-shown': '1 1 0px',
},
- width: {
+ width: 0,
+ marginInlineStart: {
default: 0,
- ':placeholder-shown': `calc(100% - ${TOKEN_ROW_INSET} - var(${END_LANE_RESERVE_VAR}, ${TOKEN_ROW_INSET}) - ${INPUT_CLEAR_COARSE_POINTER_OUTSET} - 1px)`,
+ ':placeholder-shown:is(:not(:focus), :read-only)': `calc(-1 * ${spacingVars['--spacing-1']})`,
},
- // The empty input is a first-row background hit surface. Taking it out of
- // flex layout means it can neither become a blank final row nor compete
- // with the pills for width. Logical insets stop it before the measured end
- // lane plus InputClearButton's maximum invisible pointer outset; tokens and
- // controls sit above it, leaving only genuine whitespace clickable as
- // search. Typing restores the ordinary in-flow input.
+ // Restore normal text inset when input follows tokens, since the
+ // wrapper padding is reduced for border concentricity.
paddingInlineStart: {
default: `calc(${spacingVars['--spacing-2']} - ${spacingVars['--spacing-1']} + 1px)`,
- ':placeholder-shown': 0,
- },
- paddingInlineEnd: {
- // wrapperWithTokens already keeps the in-flow input short of the lane.
- default: 0,
- ':placeholder-shown': spacingVars['--spacing-1'],
- },
- textAlign: {
- default: 'start',
- ':placeholder-shown': 'end',
+ ':placeholder-shown:is(:not(:focus), :read-only)': 0,
},
+ // wrapperWithTokens already keeps the in-flow input short of the lane.
+ paddingInlineEnd: 0,
},
truncatedWrapper: {
flexWrap: 'nowrap',
@@ -432,39 +417,6 @@ const endSectionSizeStyles = stylex.create({
},
});
-const compactInputSizeStyles = stylex.create({
- sm: {
- insetBlockStart: {
- default: 'auto',
- ':placeholder-shown': `calc(${sizeVars['--size-element-sm']} / 2 - 1px)`,
- },
- transform: {
- default: 'none',
- ':placeholder-shown': 'translateY(-50%)',
- },
- },
- md: {
- insetBlockStart: {
- default: 'auto',
- ':placeholder-shown': `calc(${sizeVars['--size-element-md']} / 2 - 1px)`,
- },
- transform: {
- default: 'none',
- ':placeholder-shown': 'translateY(-50%)',
- },
- },
- lg: {
- insetBlockStart: {
- default: 'auto',
- ':placeholder-shown': `calc(${sizeVars['--size-element-lg']} / 2 - 1px)`,
- },
- transform: {
- default: 'none',
- ':placeholder-shown': 'translateY(-50%)',
- },
- },
-});
-
const truncatedSizeStyles = stylex.create({
sm: {height: sizeVars['--size-element-sm']},
md: {height: sizeVars['--size-element-md']},
@@ -1102,11 +1054,11 @@ export function Tokenizer<T extends SearchableItem>({
isAtMax || isTruncated
? styles.inputAtMax
: value.length > 0
- ? [styles.inputCompact, compactInputSizeStyles[size]]
+ ? styles.inputCompact
: undefined,
- // The compact-token style owns the same reserve with an empty-query
- // exception. This standalone rule is only for an empty Tokenizer;
- // it still resolves to zero until the lane publishes a width.
+ // With tokens, wrapperWithTokens reserves the lane for every row.
+ // This rule is only for an empty Tokenizer; it still resolves to
+ // zero until the lane publishes a width.
!(isAtMax || isTruncated || value.length > 0) && laneReserve,
]}
/>
diff --git a/packages/core/src/Tokenizer/useEndLaneReserve.ts b/packages/core/src/Tokenizer/useEndLaneReserve.ts
index f61fea9282..3db3df912b 100644
--- a/packages/core/src/Tokenizer/useEndLaneReserve.ts
+++ b/packages/core/src/Tokenizer/useEndLaneReserve.ts
@@ -81,8 +81,9 @@ const reserveStyles = stylex.create({
*
* Takes the lane's inset from the field's inline-end border, as the CSS
* expression that positions it. Returns a ref callback for the lane, and the
- * ordinary input reserve style. Tokenizer's compact input reads the same custom
- * property directly so its empty-query hit surface can stop before the lane.
+ * ordinary input reserve style. With tokens, Tokenizer's field wrapper reads the
+ * same custom property directly so every token row, and the input after the
+ * last token, stops before the lane.
* With no lane mounted the variable is absent and either rule resolves to its
* no-lane fallback, so neither path needs to know whether a search is running.
*/