Skip to content

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

TypeScript

feat(field): add width prop to field-based components (#2755) (#2908)

Cindy Zhang4d1f289c5b3f
diff --git a/.changeset/field-width-prop.md b/.changeset/field-width-prop.md
new file mode 100644
index 0000000000..efde9231ac
--- /dev/null
+++ b/.changeset/field-width-prop.md
@@ -0,0 +1,11 @@
+---
+'@xds/core': minor
+---
+
+Add a `width` prop to field-based components so they can size predictably. `width` (a `SizeValue` — numbers are pixels, strings like `'100%'` are used as-is) is applied to the outer `XDSField` container, so the whole field — label, control, and status — sizes together and stays aligned.
+
+Previously the only way to size these components was `xstyle`/`className`/`style`, which land on the inner control box: setting `width: '100%'` there stretched the bordered input but left the label and status at their natural width (issue #2755). `width` targets the right element and avoids that mismatch.
+
+This is additive and backward compatible — `xstyle`/`className`/`style` continue to target the inner control box exactly as before.
+
+Affected: `XDSTextInput`, `XDSTextArea`, `XDSNumberInput`, `XDSDateInput`, `XDSDateRangeInput`, `XDSDateTimeInput`, `XDSTimeInput`, `XDSFileInput`, `XDSSelector`, `XDSMultiSelector`, `XDSTypeahead`, `XDSTokenizer`, `XDSSlider`, `XDSCheckboxInput`, `XDSCheckboxList`, `XDSRadioList`, `XDSSwitch`, and `XDSField` itself.
diff --git a/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx b/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx
index df83739794..f10da51e5d 100644
--- a/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx
+++ b/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx
@@ -38,6 +38,7 @@ import {
borderVars,
} from '../theme/tokens.stylex';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {XDSFieldLabel} from '../Field/XDSFieldLabel';
import {XDSFieldStatus} from '../FieldStatus/XDSFieldStatus';
import type {XDSIconType} from '../Icon';
@@ -285,6 +286,12 @@ export interface XDSCheckboxInputProps extends Omit<XDSBaseProps, 'onChange'> {
* @default false
*/
isRequired?: boolean;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* The size of the checkbox.
* - 'sm': Compact size (28px row height)
@@ -311,6 +318,11 @@ export interface XDSCheckboxInputProps extends Omit<XDSBaseProps, 'onChange'> {
status?: XDSInputStatus;
}
+// Dynamic field width (number -> px, string used as-is).
+const dynamicWidthStyles = stylex.create({
+ width: (width: SizeValue | null) => ({width}),
+});
+
/**
* A checkbox input component for toggling boolean values.
*
@@ -346,6 +358,7 @@ export function XDSCheckboxInput({
onBlur,
labelIcon,
status,
+ width,
xstyle,
className,
style,
@@ -389,7 +402,7 @@ export function XDSCheckboxInput({
<div
{...mergeProps(
xdsThemeProps('checkbox-input', {size}),
- stylex.props(xstyle),
+ stylex.props(width != null && dynamicWidthStyles.width(width), xstyle),
className,
style,
)}>
diff --git a/packages/core/src/CheckboxList/XDSCheckboxList.tsx b/packages/core/src/CheckboxList/XDSCheckboxList.tsx
index 1315217c8d..067be58887 100644
--- a/packages/core/src/CheckboxList/XDSCheckboxList.tsx
+++ b/packages/core/src/CheckboxList/XDSCheckboxList.tsx
@@ -25,6 +25,7 @@ import {
type ReactNode,
} from 'react';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {XDSField} from '../Field/XDSField';
import type {XDSInputStatus} from '../Field/types';
import {XDSList} from '../List/XDSList';
@@ -99,6 +100,12 @@ export interface XDSCheckboxListProps extends Omit<
* @default false
*/
isReadOnly?: boolean;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Checkbox list items to render.
*/
@@ -137,6 +144,7 @@ export function XDSCheckboxList({
isReadOnly = false,
children,
ref,
+ width,
xstyle,
className,
style,
@@ -214,6 +222,7 @@ export function XDSCheckboxList({
: undefined
}
statusVariant="detached"
+ width={width}
xstyle={xstyle}
{...mergeProps(xdsThemeProps('checkbox-list'), {className, style})}>
<XDSCheckboxListContext value={contextValue}>
diff --git a/packages/core/src/DateInput/XDSDateInput.tsx b/packages/core/src/DateInput/XDSDateInput.tsx
index 05b99ec106..f90d938270 100644
--- a/packages/core/src/DateInput/XDSDateInput.tsx
+++ b/packages/core/src/DateInput/XDSDateInput.tsx
@@ -134,6 +134,7 @@ export type {
} from '../Field';
import {mergeProps, mergeRefs} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
export interface XDSDateInputProps extends Omit<
@@ -234,6 +235,12 @@ export interface XDSDateInputProps extends Omit<
*/
status?: XDSInputStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -285,6 +292,7 @@ export function XDSDateInput({
labelTooltip,
hasClear = false,
numberOfMonths = 1,
+ width,
xstyle,
className,
style,
@@ -502,7 +510,8 @@ export function XDSDateInput({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
ref={popover.triggerRef}
{...rest}
diff --git a/packages/core/src/DateRangeInput/XDSDateRangeInput.tsx b/packages/core/src/DateRangeInput/XDSDateRangeInput.tsx
index e11b7d6b1d..f788ee97d8 100644
--- a/packages/core/src/DateRangeInput/XDSDateRangeInput.tsx
+++ b/packages/core/src/DateRangeInput/XDSDateRangeInput.tsx
@@ -50,6 +50,7 @@ import {XDSCalendar, type ISODateString, type DateRange} from '../Calendar';
import {useXDSPopover} from '../Popover';
import {mergeProps} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {xdsThemeProps} from '../utils/xdsThemeProps';
@@ -305,6 +306,12 @@ export interface XDSDateRangeInputProps extends Omit<
*/
status?: XDSInputStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -354,6 +361,7 @@ export function XDSDateRangeInput({
status,
labelTooltip,
numberOfMonths = 2,
+ width,
xstyle,
className,
style,
@@ -476,7 +484,8 @@ export function XDSDateRangeInput({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
ref={popover.triggerRef}
{...rest}
diff --git a/packages/core/src/DateTimeInput/XDSDateTimeInput.tsx b/packages/core/src/DateTimeInput/XDSDateTimeInput.tsx
index e3f30fe37f..ae0096fdb5 100644
--- a/packages/core/src/DateTimeInput/XDSDateTimeInput.tsx
+++ b/packages/core/src/DateTimeInput/XDSDateTimeInput.tsx
@@ -76,6 +76,7 @@ import {
} from '../utils/plainDate';
import type {StyleXStyles} from '@stylexjs/stylex';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {xdsThemeProps} from '../utils/xdsThemeProps';
@@ -293,6 +294,12 @@ export interface XDSDateTimeInputProps extends Omit<
*/
status?: XDSInputStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -382,6 +389,7 @@ export function XDSDateTimeInput({
status,
labelTooltip,
numberOfMonths = 1,
+ width,
xstyle,
className,
style,
@@ -777,7 +785,8 @@ export function XDSDateTimeInput({
: undefined
}
labelTooltip={labelTooltip}
- statusVariant="detached">
+ statusVariant="detached"
+ width={width}>
<div
{...rest}
{...mergeProps(
diff --git a/packages/core/src/Field/Field.doc.mjs b/packages/core/src/Field/Field.doc.mjs
index e5da4d0ac0..844717edd2 100644
--- a/packages/core/src/Field/Field.doc.mjs
+++ b/packages/core/src/Field/Field.doc.mjs
@@ -102,6 +102,11 @@ export const docs = {
description: 'How the status message renders relative to the input. Attached overlaps the input border; detached floats below.',
default: "'attached'",
},
+ {
+ name: 'width',
+ type: 'SizeValue',
+ description: 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field — label, control, and status — so they stay aligned. Prefer this over setting width via xstyle/className/style, which only size the inner control box.',
+ },
{
name: 'ref',
type: 'React.Ref<HTMLDivElement>',
diff --git a/packages/core/src/Field/XDSField.test.tsx b/packages/core/src/Field/XDSField.test.tsx
index d7554f9abb..9faaf2205b 100644
--- a/packages/core/src/Field/XDSField.test.tsx
+++ b/packages/core/src/Field/XDSField.test.tsx
@@ -264,6 +264,65 @@ describe('XDSField', () => {
warnSpy.mockRestore();
});
+ // ─── width prop (#2755) ───────────────────────────────────────────────
+
+ describe('width prop', () => {
+ it('applies a string width to the outer field root', () => {
+ render(
+ <XDSField
+ label="Name"
+ inputID="name-input"
+ width="100%"
+ data-testid="field">
+ <input id="name-input" data-testid="control" />
+ </XDSField>,
+ );
+ const field = screen.getByTestId('field');
+ // StyleX compiles the dynamic width to an inline CSS custom property.
+ expect(field.getAttribute('style')).toContain('100%');
+ expect(field.className).toContain('dynamicStyles.width');
+ });
+
+ it('applies a numeric width (treated as pixels)', () => {
+ render(
+ <XDSField
+ label="Name"
+ inputID="name-input"
+ width={240}
+ data-testid="field">
+ <input id="name-input" />
+ </XDSField>,
+ );
+ const field = screen.getByTestId('field');
+ expect(field.getAttribute('style')).toContain('240');
+ });
+
+ it('does not size the inner control element', () => {
+ render(
+ <XDSField
+ label="Name"
+ inputID="name-input"
+ width="100%"
+ data-testid="field">
+ <input id="name-input" data-testid="control" />
+ </XDSField>,
+ );
+ const control = screen.getByTestId('control');
+ // The width var lives on the field root, not the control itself.
+ expect(control.getAttribute('style') ?? '').not.toContain('100%');
+ });
+
+ it('omits width styling when the prop is not provided', () => {
+ render(
+ <XDSField label="Name" inputID="name-input" data-testid="field">
+ <input id="name-input" />
+ </XDSField>,
+ );
+ const field = screen.getByTestId('field');
+ expect(field.className).not.toContain('dynamicStyles.width');
+ });
+ });
+
// ─── Horizontal-labels context ────────────────────────────────────────
describe('horizontal-labels layout', () => {
diff --git a/packages/core/src/Field/XDSField.tsx b/packages/core/src/Field/XDSField.tsx
index ca94d7eecc..fb165f0d7a 100644
--- a/packages/core/src/Field/XDSField.tsx
+++ b/packages/core/src/Field/XDSField.tsx
@@ -19,6 +19,7 @@
import {type ReactNode, use} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {XDSFieldLabel} from './XDSFieldLabel';
import {XDSFieldStatus} from '../FieldStatus/XDSFieldStatus';
import {spacingVars, borderVars} from '../theme/tokens.stylex';
@@ -52,6 +53,14 @@ const styles = stylex.create({
},
});
+// Dynamic style for the consumer-controlled field width. Numbers are treated
+// as pixels by StyleX; strings (e.g. '100%') are used as-is.
+const dynamicStyles = stylex.create({
+ width: (width: SizeValue | null) => ({width}),
+});
+
+export type {SizeValue} from '../utils/types';
+
export type XDSFieldStatusType = 'warning' | 'error' | 'success';
export interface XDSFieldStatusInput {
@@ -132,6 +141,14 @@ export interface XDSFieldProps extends Omit<
* @default 'attached'
*/
statusVariant?: 'attached' | 'detached';
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field — label, control, and status — so
+ * the control and its surrounding chrome stay aligned. Prefer this over
+ * setting `width` via `xstyle`/`className`/`style`, which only size the inner
+ * control box and leave the label and status at their natural width.
+ */
+ width?: SizeValue;
/**
* The input or control to render inside the field.
*/
@@ -163,6 +180,7 @@ export function XDSField({
status,
labelTooltip,
statusVariant = 'attached',
+ width,
xstyle,
children,
className,
@@ -250,6 +268,7 @@ export function XDSField({
stylex.props(
styles.container,
!isLabelHidden && styles.containerGap,
+ width != null && dynamicStyles.width(width),
xstyle,
),
className,
diff --git a/packages/core/src/FileInput/XDSFileInput.tsx b/packages/core/src/FileInput/XDSFileInput.tsx
index f26ffdcebf..d4858e9f10 100644
--- a/packages/core/src/FileInput/XDSFileInput.tsx
+++ b/packages/core/src/FileInput/XDSFileInput.tsx
@@ -51,6 +51,7 @@ export type {
} from '../Field';
import {mergeProps, mergeRefs} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
function formatFileSize(bytes: number): string {
@@ -354,6 +355,12 @@ export interface XDSFileInputProps extends Omit<
* @default false
*/
isOptional?: boolean;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -387,6 +394,7 @@ export function XDSFileInput({
mode = 'input',
isOptional = false,
labelTooltip,
+ width,
xstyle,
className,
style,
@@ -655,7 +663,8 @@ export function XDSFileInput({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
role="button"
tabIndex={isDisabled ? -1 : 0}
diff --git a/packages/core/src/MultiSelector/XDSMultiSelector.tsx b/packages/core/src/MultiSelector/XDSMultiSelector.tsx
index c86e24bff2..dbac26a203 100644
--- a/packages/core/src/MultiSelector/XDSMultiSelector.tsx
+++ b/packages/core/src/MultiSelector/XDSMultiSelector.tsx
@@ -66,6 +66,7 @@ import {
import {useMultiCombobox} from './hooks';
import {mergeProps} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {xdsThemeProps} from '../utils/xdsThemeProps';
@@ -440,6 +441,12 @@ export interface XDSMultiSelectorProps<
*/
status?: XDSMultiSelectorStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -558,6 +565,7 @@ export function XDSMultiSelector<T extends XDSMultiSelectorOptionType>({
renderOption,
isDefaultOpen = false,
'data-testid': testId,
+ width,
xstyle,
className,
style,
@@ -1107,7 +1115,8 @@ export function XDSMultiSelector<T extends XDSMultiSelectorOptionType>({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
ref={el => {
popover.triggerRef(el);
diff --git a/packages/core/src/NumberInput/XDSNumberInput.tsx b/packages/core/src/NumberInput/XDSNumberInput.tsx
index fd5bcf804f..1792eb667e 100644
--- a/packages/core/src/NumberInput/XDSNumberInput.tsx
+++ b/packages/core/src/NumberInput/XDSNumberInput.tsx
@@ -130,6 +130,7 @@ export type {
} from '../Field';
import {mergeProps, mergeRefs} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
interface XDSNumberInputPropsBase extends Omit<
@@ -199,6 +200,12 @@ interface XDSNumberInputPropsBase extends Omit<
* Placeholder text shown when the input is empty.
*/
placeholder?: string;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -360,6 +367,7 @@ export function XDSNumberInput({
hasClear,
onEnter,
onKeyDown,
+ width,
xstyle,
className,
style,
@@ -607,7 +615,8 @@ export function XDSNumberInput({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
{inputWrapper}
</XDSField>
);
diff --git a/packages/core/src/RadioList/XDSRadioList.tsx b/packages/core/src/RadioList/XDSRadioList.tsx
index 0f7bc80735..6759b564bb 100644
--- a/packages/core/src/RadioList/XDSRadioList.tsx
+++ b/packages/core/src/RadioList/XDSRadioList.tsx
@@ -23,6 +23,7 @@ import {XDSField} from '../Field/XDSField';
import type {XDSInputStatus} from '../Field/types';
import {mergeProps} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
/**
@@ -116,6 +117,12 @@ export interface XDSRadioListProps extends Omit<
* @default 'md'
*/
size?: XDSRadioListSize;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -159,6 +166,7 @@ export function XDSRadioList({
size = 'md',
status,
labelTooltip,
+ width,
xstyle,
className,
style,
@@ -198,6 +206,7 @@ export function XDSRadioList({
}
labelTooltip={labelTooltip}
statusVariant="detached"
+ width={width}
xstyle={xstyle}
className={className}
style={style}>
diff --git a/packages/core/src/Selector/XDSSelector.tsx b/packages/core/src/Selector/XDSSelector.tsx
index cfa26956a3..b0f9524c9b 100644
--- a/packages/core/src/Selector/XDSSelector.tsx
+++ b/packages/core/src/Selector/XDSSelector.tsx
@@ -64,6 +64,7 @@ import {XDSSelectorOption} from './XDSSelectorOption';
import {mergeProps} from '../utils';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
const styles = stylex.create({
@@ -404,6 +405,12 @@ interface XDSSelectorPropsBase<
*/
status?: XDSSelectorStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -539,6 +546,7 @@ export function XDSSelector<T extends XDSSelectorOptionType>(
placement,
isDefaultOpen = false,
'data-testid': testId,
+ width,
xstyle,
className,
style,
@@ -856,7 +864,8 @@ export function XDSSelector<T extends XDSSelectorOptionType>(
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
ref={el => {
popover.triggerRef(el);
diff --git a/packages/core/src/Slider/XDSSlider.tsx b/packages/core/src/Slider/XDSSlider.tsx
index 64b7157a0c..3805d9b494 100644
--- a/packages/core/src/Slider/XDSSlider.tsx
+++ b/packages/core/src/Slider/XDSSlider.tsx
@@ -40,6 +40,7 @@ import {XDSTooltip} from '../Tooltip/XDSTooltip';
import type {XDSInputStatus} from '../Field/types';
import {mergeProps, mergeRefs} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
// =============================================================================
@@ -66,6 +67,12 @@ export interface XDSSliderBaseProps extends Omit<
isRequired?: boolean;
/** Status indicator for the slider. */
status?: XDSInputStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/** Tooltip text to display in an info icon at the end of the label. */
labelTooltip?: string;
/** Minimum value. @default 0 */
@@ -335,6 +342,7 @@ export function XDSSlider({ref, ...props}: XDSSliderProps) {
formatValue,
valueDisplay = 'tooltip',
marks,
+ width,
xstyle,
className,
style,
@@ -737,6 +745,7 @@ export function XDSSlider({ref, ...props}: XDSSliderProps) {
}
labelTooltip={labelTooltip}
statusVariant="detached"
+ width={width}
xstyle={xstyle}
className={className}
style={style}>
diff --git a/packages/core/src/Switch/XDSSwitch.tsx b/packages/core/src/Switch/XDSSwitch.tsx
index 19242dbaad..7016906066 100644
--- a/packages/core/src/Switch/XDSSwitch.tsx
+++ b/packages/core/src/Switch/XDSSwitch.tsx
@@ -42,6 +42,7 @@ import {XDSSpinner} from '../Spinner';
import {mergeProps} from '../utils';
import {switchScope} from './switch.markers.stylex';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
// Fixed dimensions: 40px width, 24px height, 16px thumb (off), 20px thumb (on)
@@ -258,6 +259,12 @@ export interface XDSSwitchProps extends Omit<XDSBaseProps, 'onChange'> {
* Icon to display before the label text.
*/
labelIcon?: ReactNode | XDSIconType;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -283,6 +290,11 @@ export interface XDSSwitchProps extends Omit<XDSBaseProps, 'onChange'> {
status?: XDSInputStatus;
}
+// Dynamic field width (number -> px, string used as-is).
+const dynamicWidthStyles = stylex.create({
+ width: (width: SizeValue | null) => ({width}),
+});
+
/**
* A toggle switch component for boolean values.
*
@@ -319,6 +331,7 @@ export function XDSSwitch({
labelPosition = 'end',
labelSpacing = 'default',
status,
+ width,
xstyle,
className,
style,
@@ -435,7 +448,7 @@ export function XDSSwitch({
labelPosition: labelPosition !== 'end' ? labelPosition : undefined,
labelSpacing: labelSpacing !== 'default' ? labelSpacing : undefined,
}),
- stylex.props(xstyle),
+ stylex.props(width != null && dynamicWidthStyles.width(width), xstyle),
className,
style,
)}>
diff --git a/packages/core/src/TextArea/XDSTextArea.tsx b/packages/core/src/TextArea/XDSTextArea.tsx
index a74d2a7f19..6cef9694ab 100644
--- a/packages/core/src/TextArea/XDSTextArea.tsx
+++ b/packages/core/src/TextArea/XDSTextArea.tsx
@@ -45,6 +45,7 @@ import {XDSIcon, renderIconSlot, type XDSIconType} from '../Icon';
import {XDSSpinner} from '../Spinner';
import {mergeProps, mergeRefs} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {useInputContainer} from '../hooks/useInputContainer';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {xdsThemeProps} from '../utils/xdsThemeProps';
@@ -204,6 +205,12 @@ export interface XDSTextAreaProps extends Omit<
* If message is provided, displays a floating message box below the textarea.
*/
status?: XDSTextAreaStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -288,6 +295,7 @@ export function XDSTextArea({
htmlName,
onFocus,
onBlur,
+ width,
xstyle,
className,
style,
@@ -370,7 +378,8 @@ export function XDSTextArea({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
ref={containerRef}
onClick={handleWrapperClick}
diff --git a/packages/core/src/TextInput/XDSTextInput.test.tsx b/packages/core/src/TextInput/XDSTextInput.test.tsx
index 41eac19a98..53f51a2ea4 100644
--- a/packages/core/src/TextInput/XDSTextInput.test.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.test.tsx
@@ -499,4 +499,29 @@ describe('XDSTextInput', () => {
expect(input).toHaveFocus();
});
});
+
+ describe('width prop (#2755)', () => {
+ it('sizes the outer field, not just the input wrapper', () => {
+ render(
+ <XDSTextInput label="Name" value="" onChange={() => {}} width="100%" />,
+ );
+ const input = screen.getByRole('textbox');
+ const inputWrapper = input.parentElement!;
+ // Field root is the labelled ancestor that owns the width.
+ const fieldRoot = input.closest('.xds-field') as HTMLElement;
+ expect(fieldRoot).toBeTruthy();
+ expect(fieldRoot.getAttribute('style')).toContain('100%');
+ // The inner control wrapper is not the element carrying the width.
+ expect(fieldRoot).not.toBe(inputWrapper);
+ expect(inputWrapper.getAttribute('style') ?? '').not.toContain('100%');
+ });
+
+ it('does not set width when the prop is omitted', () => {
+ render(<XDSTextInput label="Name" value="" onChange={() => {}} />);
+ const fieldRoot = screen
+ .getByRole('textbox')
+ .closest('.xds-field') as HTMLElement;
+ expect(fieldRoot.getAttribute('style') ?? '').not.toContain('100%');
+ });
+ });
});
diff --git a/packages/core/src/TextInput/XDSTextInput.tsx b/packages/core/src/TextInput/XDSTextInput.tsx
index 70e2582d28..ad624f7126 100644
--- a/packages/core/src/TextInput/XDSTextInput.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.tsx
@@ -118,6 +118,7 @@ import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {useInputContainer} from '../hooks/useInputContainer';
import {useXDSInputGroup} from '../InputGroup/XDSInputGroupContext';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
export type XDSTextInputType = 'text' | 'password' | 'email';
@@ -198,6 +199,12 @@ export interface XDSTextInputProps extends Omit<
* Placeholder text shown when the input is empty.
*/
placeholder?: string;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -259,6 +266,7 @@ export function XDSTextInput({
htmlName,
onEnter,
onKeyDown,
+ width,
xstyle,
className,
style,
@@ -419,7 +427,8 @@ export function XDSTextInput({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
{inputWrapper}
</XDSField>
);
diff --git a/packages/core/src/TimeInput/XDSTimeInput.tsx b/packages/core/src/TimeInput/XDSTimeInput.tsx
index b5ce1b5703..e3747242e8 100644
--- a/packages/core/src/TimeInput/XDSTimeInput.tsx
+++ b/packages/core/src/TimeInput/XDSTimeInput.tsx
@@ -60,6 +60,7 @@ import {
mergeRefs,
} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {useInputContainer} from '../hooks/useInputContainer';
import {xdsThemeProps} from '../utils/xdsThemeProps';
@@ -267,6 +268,12 @@ export interface XDSTimeInputProps extends Omit<
*/
status?: XDSInputStatus;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/**
* Tooltip text to display in an info icon at the end of the label.
*/
@@ -309,6 +316,7 @@ export function XDSTimeInput({
size: sizeProp,
status,
labelTooltip,
+ width,
xstyle,
className,
style,
@@ -520,7 +528,8 @@ export function XDSTimeInput({
}
: undefined
}
- labelTooltip={labelTooltip}>
+ labelTooltip={labelTooltip}
+ width={width}>
<div
ref={containerRef}
onClick={handleWrapperClick}
diff --git a/packages/core/src/Tokenizer/XDSTokenizer.tsx b/packages/core/src/Tokenizer/XDSTokenizer.tsx
index 5602345242..c181dc5cc0 100644
--- a/packages/core/src/Tokenizer/XDSTokenizer.tsx
+++ b/packages/core/src/Tokenizer/XDSTokenizer.tsx
@@ -26,6 +26,7 @@ import React, {
} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import {XDSBaseTypeahead} from '../Typeahead/XDSBaseTypeahead';
import {useXDSSize} from '../SizeContext/XDSSizeContext';
import {
@@ -114,6 +115,12 @@ export interface XDSTokenizerProps<T extends XDSSearchableItem> extends Omit<
* Accepts a ReactNode (e.g. `<XDSIcon icon={SearchIcon} />`) or an SVG icon component directly.
*/
startIcon?: ReactNode | XDSIconType;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/** Label tooltip. */
labelTooltip?: string;
/** Search source providing items. */
@@ -365,6 +372,7 @@ export function XDSTokenizer<T extends XDSSearchableItem>({
onChangeQuery,
onFocus,
onBlur,
+ width,
xstyle,
className,
style,
@@ -815,6 +823,7 @@ export function XDSTokenizer<T extends XDSSearchableItem>({
: undefined
}
labelTooltip={labelTooltip}
+ width={width}
xstyle={xstyle}
className={className}
style={style}>
diff --git a/packages/core/src/Typeahead/XDSTypeahead.tsx b/packages/core/src/Typeahead/XDSTypeahead.tsx
index 87a5ebc658..c5f562867a 100644
--- a/packages/core/src/Typeahead/XDSTypeahead.tsx
+++ b/packages/core/src/Typeahead/XDSTypeahead.tsx
@@ -42,6 +42,7 @@ import {renderIconSlot, type XDSIconType} from '../Icon';
import {spacingVars, sizeVars} from '../theme/tokens.stylex';
import {mergeProps} from '../utils';
import type {XDSBaseProps} from '../XDSBaseProps';
+import type {SizeValue} from '../utils/types';
import type {XDSSearchableItem, XDSSearchSource} from './types';
import {xdsThemeProps} from '../utils/xdsThemeProps';
@@ -74,6 +75,12 @@ export interface XDSTypeaheadProps<T extends XDSSearchableItem> extends Omit<
* Accepts a ReactNode (e.g. `<XDSIcon icon={SearchIcon} />`) or an SVG icon component directly.
*/
startIcon?: ReactNode | XDSIconType;
+ /**
+ * Width of the field. Numbers are treated as pixels, strings are used as-is
+ * (e.g. `'100%'`). Sizes the whole field (label, control, and status) so they
+ * stay aligned, unlike setting width via `xstyle`/`className`/`style`.
+ */
+ width?: SizeValue;
/** Label tooltip. */
labelTooltip?: string;
/** Search source providing items. */
@@ -208,6 +215,7 @@ export function XDSTypeahead<T extends XDSSearchableItem>({
debounceMs,
onChangeQuery,
onOpenChange,
+ width,
xstyle,
className,
style,
@@ -358,6 +366,7 @@ export function XDSTypeahead<T extends XDSSearchableItem>({
: undefined
}
labelTooltip={labelTooltip}
+ width={width}
xstyle={xstyle}
className={className}
style={style}>