Skip to content

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

TypeScript

fix(Token, Tokenizer): text overflow, focus outline, and layout bugs (#469)

Cindy Zhang4e3cdc8135e1
diff --git a/packages/core/src/Token/README.md b/packages/core/src/Token/README.md
index 0d6a41adf1..dac65ee28d 100644
--- a/packages/core/src/Token/README.md
+++ b/packages/core/src/Token/README.md
@@ -2,6 +2,8 @@
A chip/tag component for displaying entities inline. Renders as a `<span>` by default, `<button>` when `onClick` is provided, or `<a>` when `href` is provided.
+Long labels are automatically truncated with an ellipsis. When `onClick` is provided, the focus outline wraps the entire token (not just the label) via `:focus-within` on the container.
+
## Exports
| Export | Type | Description |
diff --git a/packages/core/src/Token/XDSToken.test.tsx b/packages/core/src/Token/XDSToken.test.tsx
index 54b3990f59..913215ceb8 100644
--- a/packages/core/src/Token/XDSToken.test.tsx
+++ b/packages/core/src/Token/XDSToken.test.tsx
@@ -317,3 +317,46 @@ describe('XDSToken accessibility', () => {
expect(removeButton).toBeInTheDocument();
});
});
+
+describe('XDSToken text overflow', () => {
+ it('label element has overflow hidden and text-overflow ellipsis styles', () => {
+ const {container} = render(<XDSToken label="A very long label text" />);
+ const labelSpan = container.querySelector('span > span');
+ expect(labelSpan).toBeInTheDocument();
+ // The label span should exist and contain the text
+ expect(labelSpan?.textContent).toBe('A very long label text');
+ });
+
+ it('label element has overflow styles when onClick is provided', () => {
+ const {container} = render(
+ <XDSToken label="A very long clickable label" onClick={() => {}} />,
+ );
+ // In onClick mode, the label is inside the invisible button
+ const button = screen.getByRole('button', {
+ name: 'A very long clickable label',
+ });
+ const labelSpan = button.querySelector('span');
+ expect(labelSpan).toBeInTheDocument();
+ expect(labelSpan?.textContent).toBe('A very long clickable label');
+ });
+});
+
+describe('XDSToken focus outline', () => {
+ it('invisible button does not show its own focus outline', async () => {
+ const user = userEvent.setup();
+ render(
+ <XDSToken
+ label="Focusable"
+ onClick={() => {}}
+ data-testid="focus-token"
+ />,
+ );
+ const button = screen.getByRole('button', {name: 'Focusable'});
+ await user.tab();
+ expect(button).toHaveFocus();
+ // The container should handle focus outline via :focus-within,
+ // not the button itself
+ const container = screen.getByTestId('focus-token');
+ expect(container.tagName).toBe('SPAN');
+ });
+});
diff --git a/packages/core/src/Token/XDSToken.tsx b/packages/core/src/Token/XDSToken.tsx
index e6638ac660..e0302f2922 100644
--- a/packages/core/src/Token/XDSToken.tsx
+++ b/packages/core/src/Token/XDSToken.tsx
@@ -129,6 +129,13 @@ const styles = stylex.create({
whiteSpace: 'nowrap',
textDecoration: 'none',
maxWidth: '100%',
+ overflow: 'hidden',
+ },
+ label: {
+ overflow: 'hidden',
+ textOverflow: 'ellipsis',
+ whiteSpace: 'nowrap',
+ minWidth: 0,
},
interactive: {
cursor: 'pointer',
@@ -171,6 +178,9 @@ const styles = stylex.create({
cursor: 'inherit',
font: 'inherit',
color: 'inherit',
+ outline: 'none',
+ overflow: 'hidden',
+ minWidth: 0,
},
focusWithinOutline: {
outline: {
@@ -310,7 +320,8 @@ export const XDSToken = forwardRef<HTMLElement, XDSTokenProps>(
const content = (
<>
{icon}
- <span {...(isLabelHidden ? stylex.props(styles.labelHidden) : {})}>
+ <span
+ {...stylex.props(styles.label, isLabelHidden && styles.labelHidden)}>
{label}
</span>
{endContent}
@@ -383,7 +394,11 @@ export const XDSToken = forwardRef<HTMLElement, XDSTokenProps>(
onClick={onClick}
disabled={isDisabled}
{...stylex.props(styles.invisibleButton)}>
- <span {...(isLabelHidden ? stylex.props(styles.labelHidden) : {})}>
+ <span
+ {...stylex.props(
+ styles.label,
+ isLabelHidden && styles.labelHidden,
+ )}>
{label}
</span>
</button>
diff --git a/packages/core/src/Tokenizer/XDSTokenizer.test.tsx b/packages/core/src/Tokenizer/XDSTokenizer.test.tsx
index 05f932898e..f752ad9e03 100644
--- a/packages/core/src/Tokenizer/XDSTokenizer.test.tsx
+++ b/packages/core/src/Tokenizer/XDSTokenizer.test.tsx
@@ -268,4 +268,48 @@ describe('XDSTokenizer', () => {
);
expect(screen.getByRole('group')).toHaveAttribute('aria-label', 'Members');
});
+
+ it('hides placeholder when tokens are present', () => {
+ render(
+ <XDSTokenizer
+ label="Members"
+ searchSource={userSource}
+ value={[users[0]]}
+ onChange={() => {}}
+ placeholder="Search people..."
+ />,
+ );
+ const input = screen.getByRole('combobox');
+ // Placeholder should be empty when tokens exist
+ expect(input).not.toHaveAttribute('placeholder', 'Search people...');
+ });
+
+ it('shows placeholder when no tokens are present', () => {
+ render(
+ <XDSTokenizer
+ label="Members"
+ searchSource={userSource}
+ value={[]}
+ onChange={() => {}}
+ placeholder="Search people..."
+ />,
+ );
+ expect(screen.getByPlaceholderText('Search people...')).toBeInTheDocument();
+ });
+
+ it('renders tokens as direct children of wrapper (not in a sub-container)', () => {
+ const {container} = render(
+ <XDSTokenizer
+ label="Members"
+ searchSource={userSource}
+ value={[users[0], users[1]]}
+ onChange={() => {}}
+ data-testid="tokenizer"
+ />,
+ );
+ const wrapper = screen.getByTestId('tokenizer');
+ // Tokens should be direct children of the wrapper, not nested in a div
+ const tokenElements = wrapper.querySelectorAll(':scope > span');
+ expect(tokenElements.length).toBeGreaterThanOrEqual(2);
+ });
});
diff --git a/packages/core/src/Tokenizer/XDSTokenizer.tsx b/packages/core/src/Tokenizer/XDSTokenizer.tsx
index 955f6776b8..22d2e3495e 100644
--- a/packages/core/src/Tokenizer/XDSTokenizer.tsx
+++ b/packages/core/src/Tokenizer/XDSTokenizer.tsx
@@ -121,20 +121,19 @@ const styles = stylex.create({
wrapper: {
flexWrap: 'wrap',
gap: spacingVars['--spacing-1'],
- // Standard padding minus border width to prevent height jump
- // when tokens (28px) are added inside the input
- paddingBlock: `calc(${spacingVars['--spacing-1']} - 1px)`,
cursor: 'text',
+ height: 'auto',
},
- tokenContainer: {
- display: 'flex',
- flexWrap: 'wrap',
- gap: spacingVars['--spacing-1'],
- alignItems: 'center',
- // Offset tokens so they sit 3px from the inner edge (4px from outer edge
- // accounting for 1px border). Default inline padding is 8px, so
- // -(8px - 3px) = -5px positions tokens equidistant from left edge as top.
- margin: `calc(-1 * (${spacingVars['--spacing-2']} - ${spacingVars['--spacing-1']} + 1px))`,
+ wrapperWithTokens: {
+ // Override padding for border concentricity: token border-radius
+ // (radius-content: 4px) sits concentric with wrapper border-radius
+ // (radius-element: 8px) when inset = radius-element - radius-content - border
+ // = 8 - 4 - 1 = 3px.
+ paddingBlock: `calc(${spacingVars['--spacing-1']} - 1px)`,
+ paddingInline: `calc(${spacingVars['--spacing-1']} - 1px)`,
+ },
+ token: {
+ flexShrink: 0,
},
clearAllButton: {
all: 'unset',
@@ -170,8 +169,11 @@ const styles = stylex.create({
},
inputCompact: {
minWidth: '40px',
- flex: '0 1 auto',
- width: '40px',
+ flex: '1 1 40px',
+ width: 0,
+ // Restore normal text inset when input follows tokens, since the
+ // wrapper padding is reduced for border concentricity.
+ paddingInlineStart: `calc(${spacingVars['--spacing-2']} - ${spacingVars['--spacing-1']} + 1px)`,
},
});
@@ -353,9 +355,9 @@ export function XDSTokenizer<T extends XDSSearchableItem>({
if (renderToken) {
return (
- <React.Fragment key={item.id}>
+ <span key={item.id} {...stylex.props(styles.token)}>
{renderToken(item, onRemoveItem)}
- </React.Fragment>
+ </span>
);
}
@@ -366,6 +368,7 @@ export function XDSTokenizer<T extends XDSSearchableItem>({
size={size}
onRemove={isDisabled ? undefined : onRemoveItem}
isDisabled={isDisabled}
+ xstyle={styles.token}
/>
);
});
@@ -398,6 +401,7 @@ export function XDSTokenizer<T extends XDSSearchableItem>({
{...stylex.props(
inputWrapperStyles.base,
styles.wrapper,
+ value.length > 0 && styles.wrapperWithTokens,
sizeStyle,
isDisabled && inputWrapperStyles.disabled,
status && inputStatusBorderStyles[status.type],
@@ -405,18 +409,14 @@ export function XDSTokenizer<T extends XDSSearchableItem>({
status && inputStatusFocusWithinStyles[status.type],
xstyle,
)}>
- {tokens.length > 0 && (
- <div {...stylex.props(styles.tokenContainer)}>{tokens}</div>
- )}
+ {tokens}
<XDSBaseTypeahead
ref={inputRef}
searchSource={isAtMax ? emptySource : filteredSource}
value={null}
onChange={handleAdd}
renderItem={renderItem}
- placeholder={
- value.length === 0 ? placeholder : isAtMax ? '' : undefined
- }
+ placeholder={value.length === 0 ? placeholder : ''}
hasEntriesOnFocus={isAtMax ? false : hasEntriesOnFocus}
maxMenuItems={maxMenuItems}
emptySearchResultsText={emptySearchResultsText}