An open source design system that's fully customizable and agent ready
TypeScript
XDSCheckboxInput props (#58)
Andrey Goder044cc8cb5798
diff --git a/apps/storybook/stories/CheckboxInput.stories.tsx b/apps/storybook/stories/CheckboxInput.stories.tsxindex 8f74ce87ba..63f44ff836 100644--- a/apps/storybook/stories/CheckboxInput.stories.tsx+++ b/apps/storybook/stories/CheckboxInput.stories.tsx@@ -1,6 +1,6 @@-import { useState } from 'react';-import type { Meta, StoryObj } from '@storybook/react';-import { XDSCheckboxInput } from '@xds/core/CheckboxInput';+import {useState} from 'react';+import type {Meta, StoryObj} from '@storybook/react';+import {XDSCheckboxInput} from '@xds/core/CheckboxInput';const meta: Meta<typeof XDSCheckboxInput> = {title: 'Core/XDSCheckboxInput',@@ -13,7 +13,8 @@ const meta: Meta<typeof XDSCheckboxInput> = {},isLabelHidden: {control: 'boolean',- description: 'Visually hide the label (still accessible to screen readers)',+ description:+ 'Visually hide the label (still accessible to screen readers)',},description: {control: 'text',@@ -22,12 +23,22 @@ const meta: Meta<typeof XDSCheckboxInput> = {value: {control: 'select',options: [true, false, 'indeterminate'],- description: 'Whether the checkbox is checked, unchecked, or indeterminate',+ description:+ 'Whether the checkbox is checked, unchecked, or indeterminate',},isDisabled: {control: 'boolean',description: 'Whether the checkbox is disabled',},+ isRequired: {+ control: 'boolean',+ description: 'Whether the checkbox is required',+ },+ size: {+ control: 'select',+ options: ['sm', 'md'],+ description: 'Size of the checkbox',+ },},};@@ -35,8 +46,10 @@ export default meta;type Story = StoryObj<typeof XDSCheckboxInput>;export const Default: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? false);+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? false+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -45,8 +58,10 @@ export const Default: Story = {};export const Checked: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? true);+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? true+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -56,8 +71,10 @@ export const Checked: Story = {};export const WithDescription: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? false);+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? false+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -67,8 +84,10 @@ export const WithDescription: Story = {};export const WithHiddenLabel: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? false);+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? false+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -78,8 +97,10 @@ export const WithHiddenLabel: Story = {};export const Indeterminate: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? 'indeterminate');+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? 'indeterminate'+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -90,8 +111,10 @@ export const Indeterminate: Story = {};export const Disabled: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? false);+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? false+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -102,8 +125,10 @@ export const Disabled: Story = {};export const DisabledChecked: Story = {- render: (args) => {- const [value, setValue] = useState<boolean | 'indeterminate'>(args.value ?? true);+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? true+ );return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;},args: {@@ -117,21 +142,25 @@ export const AllVariations: Story = {render: () => {const [value1, setValue1] = useState<boolean | 'indeterminate'>(false);const [value2, setValue2] = useState<boolean | 'indeterminate'>(true);- const [value3, setValue3] = useState<boolean | 'indeterminate'>('indeterminate');+ const [value3, setValue3] = useState<boolean | 'indeterminate'>(+ 'indeterminate'+ );const [value4, setValue4] = useState<boolean | 'indeterminate'>(false);const [value5, setValue5] = useState<boolean | 'indeterminate'>(true);return (- <div style={{ display: 'flex', flexDirection: 'column', gap: '16px', maxWidth: '400px' }}>+ <div+ style={{+ display: 'flex',+ flexDirection: 'column',+ gap: '16px',+ maxWidth: '400px',+ }}><XDSCheckboxInputlabel="Unchecked"value={value1}onChange={setValue1}/>- <XDSCheckboxInput- label="Checked"- value={value2}- onChange={setValue2}- />+ <XDSCheckboxInput label="Checked" value={value2} onChange={setValue2} /><XDSCheckboxInputlabel="Indeterminate"description="Some items are selected"@@ -154,3 +183,59 @@ export const AllVariations: Story = {);},};++export const SmallSize: Story = {+ render: args => {+ const [value, setValue] = useState<boolean | 'indeterminate'>(+ args.value ?? false+ );+ return <XDSCheckboxInput {...args} value={value} onChange={setValue} />;+ },+ args: {+ label: 'Compact checkbox',+ size: 'sm',+ },+};++export const SizeComparison: Story = {+ render: () => {+ const [value1, setValue1] = useState<boolean | 'indeterminate'>(false);+ const [value2, setValue2] = useState<boolean | 'indeterminate'>(false);+ const [value3, setValue3] = useState<boolean | 'indeterminate'>(true);+ const [value4, setValue4] = useState<boolean | 'indeterminate'>(true);+ return (+ <div+ style={{+ display: 'flex',+ flexDirection: 'column',+ gap: '16px',+ maxWidth: '400px',+ }}>+ <XDSCheckboxInput+ label="Medium size (default)"+ value={value1}+ onChange={setValue1}+ size="md"+ />+ <XDSCheckboxInput+ label="Small size"+ value={value2}+ onChange={setValue2}+ size="sm"+ />+ <XDSCheckboxInput+ label="Medium size checked"+ value={value3}+ onChange={setValue3}+ size="md"+ />+ <XDSCheckboxInput+ label="Small size checked"+ value={value4}+ onChange={setValue4}+ size="sm"+ />+ </div>+ );+ },+};diff --git a/packages/core/package.json b/packages/core/package.jsonindex 5fa7251907..5ce4c639f8 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -63,6 +63,11 @@"types": "./dist/TextInput/index.d.ts","import": "./dist/TextInput/index.mjs","require": "./dist/TextInput/index.js"+ },+ "./hooks": {+ "types": "./dist/hooks/index.d.ts",+ "import": "./dist/hooks/index.mjs",+ "require": "./dist/hooks/index.js"}},"scripts": {diff --git a/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx b/packages/core/src/CheckboxInput/XDSCheckboxInput.tsxindex 6df99ba4b1..f25287779a 100644--- a/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx+++ b/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx@@ -1,6 +1,6 @@/*** @file XDSCheckboxInput.tsx- * @input Uses React forwardRef, useId, ChangeEvent, XDSFieldLabel+ * @input Uses React forwardRef, useId, ChangeEvent, XDSFieldLabel, useHoverState* @output Exports XDSCheckboxInput component, XDSCheckboxInputProps* @position Core implementation; consumed by index.ts, tested by XDSCheckboxInput.test.tsx*@@ -11,7 +11,7 @@* - /apps/storybook/stories/CheckboxInput.stories.tsx (storybook stories)*/-import {forwardRef, useId, type ChangeEvent} from 'react';+import {forwardRef, useId, type ChangeEvent, type FocusEvent} from 'react';import * as stylex from '@stylexjs/stylex';import {colorVars,@@ -21,6 +21,7 @@ import {typographyVars,} from '../theme/tokens.stylex';import {XDSFieldLabel} from '../Field/XDSFieldLabel';+import {useHoverState} from '../hooks/useHoverState';const styles = stylex.create({container: {@@ -34,13 +35,9 @@ const styles = stylex.create({alignItems: 'center',justifyContent: 'center',flexShrink: 0,- width: 24,- height: 24,},input: {position: 'absolute',- width: 24,- height: 24,margin: 0,padding: 0,opacity: 0,@@ -53,8 +50,6 @@ const styles = stylex.create({display: 'flex',alignItems: 'center',justifyContent: 'center',- width: 22,- height: 22,borderWidth: 1,borderStyle: 'solid',borderColor: colorVars['--color-divider-emphasized'],@@ -64,8 +59,13 @@ const styles = stylex.create({transitionDuration: transitionVars['--transition-fast'],},checkboxHover: {+ backgroundColor: colorVars['--color-deemphasized'],borderColor: colorVars['--color-divider-high-contrast'],},+ checkboxCheckedHover: {+ backgroundColor: '#0050B8',+ borderColor: '#0050B8',+ },checkboxChecked: {backgroundColor: colorVars['--color-accent'],borderColor: colorVars['--color-accent'],@@ -83,8 +83,6 @@ const styles = stylex.create({},checkmark: {display: 'none',- width: 14,- height: 14,fill: 'white',},checkmarkVisible: {@@ -92,8 +90,6 @@ const styles = stylex.create({},indeterminateMark: {display: 'none',- width: 12,- height: 2,backgroundColor: 'white',borderRadius: 1,},@@ -104,7 +100,6 @@ const styles = stylex.create({display: 'flex',flexDirection: 'column',gap: spacingVars['--spacing-0-5'],- marginTop: 3,},description: {fontFamily: typographyVars['--font-body'],@@ -113,6 +108,61 @@ const styles = stylex.create({},});+const wrapperSizeStyles = stylex.create({+ sm: {+ width: 20,+ height: 20,+ },+ md: {+ width: 24,+ height: 24,+ },+});++const checkboxSizeStyles = stylex.create({+ sm: {+ width: 18,+ height: 18,+ },+ md: {+ width: 22,+ height: 22,+ },+});++const checkmarkSizeStyles = stylex.create({+ sm: {+ width: 12,+ height: 12,+ },+ md: {+ width: 14,+ height: 14,+ },+});++const indeterminateSizeStyles = stylex.create({+ sm: {+ width: 10,+ height: 2,+ },+ md: {+ width: 12,+ height: 2,+ },+});++const labelWrapperSizeStyles = stylex.create({+ sm: {+ marginTop: 1,+ },+ md: {+ marginTop: 3,+ },+});++export type XDSCheckboxInputSize = keyof typeof wrapperSizeStyles;+export interface XDSCheckboxInputProps {/*** Label text for the checkbox (always rendered for accessibility).@@ -140,6 +190,26 @@ export interface XDSCheckboxInputProps {* @default false*/isDisabled?: boolean;+ /**+ * Whether the checkbox is required.+ * @default false+ */+ isRequired?: boolean;+ /**+ * The size of the checkbox.+ * - 'sm': Compact size (28px row height)+ * - 'md': Default size (36px row height)+ * @default 'md'+ */+ size?: XDSCheckboxInputSize;+ /**+ * Callback fired when the checkbox receives focus.+ */+ onFocus?: (e: FocusEvent<HTMLInputElement>) => void;+ /**+ * Callback fired when the checkbox loses focus.+ */+ onBlur?: (e: FocusEvent<HTMLInputElement>) => void;}/**@@ -172,44 +242,69 @@ export const XDSCheckboxInput = forwardRef<onChange,value,isDisabled = false,+ isRequired = false,+ size = 'md',+ onFocus,+ onBlur,},- ref,+ ref) => {const id = useId();const descriptionID = useId();+ const {isHovered, onMouseEnter, onMouseLeave} = useHoverState();const isIndeterminate = value === 'indeterminate';const isChecked = value === true;+ const showHover = isHovered && !isDisabled;return (- <div {...stylex.props(styles.container)}>- <div {...stylex.props(styles.checkboxWrapper)}>+ <div+ {...stylex.props(styles.container)}+ onMouseEnter={onMouseEnter}+ onMouseLeave={onMouseLeave}>+ <div {...stylex.props(styles.checkboxWrapper, wrapperSizeStyles[size])}><inputref={ref}id={id}type="checkbox"checked={isChecked}disabled={isDisabled}+ required={isRequired}onChange={e => onChange(e.target.checked, e)}+ onFocus={onFocus}+ onBlur={onBlur}aria-describedby={description ? descriptionID : undefined}- {...stylex.props(styles.input, isDisabled && styles.inputDisabled)}+ {...stylex.props(+ styles.input,+ wrapperSizeStyles[size],+ isDisabled && styles.inputDisabled+ )}/><divaria-hidden="true"{...stylex.props(styles.checkbox,+ checkboxSizeStyles[size],(isChecked || isIndeterminate) && styles.checkboxChecked,+ showHover &&+ !isChecked &&+ !isIndeterminate &&+ styles.checkboxHover,+ showHover &&+ (isChecked || isIndeterminate) &&+ styles.checkboxCheckedHover,isDisabled && styles.checkboxDisabled,isDisabled &&!isChecked &&!isIndeterminate &&- styles.checkboxDisabledUnchecked,+ styles.checkboxDisabledUnchecked)}><svgviewBox="0 0 10 10"{...stylex.props(styles.checkmark,- isChecked && styles.checkmarkVisible,+ checkmarkSizeStyles[size],+ isChecked && styles.checkmarkVisible)}><pathd="M8.5 2.5L4 7.5L1.5 5"@@ -223,12 +318,14 @@ export const XDSCheckboxInput = forwardRef<<div{...stylex.props(styles.indeterminateMark,- isIndeterminate && styles.indeterminateMarkVisible,+ indeterminateSizeStyles[size],+ isIndeterminate && styles.indeterminateMarkVisible)}/></div></div>- <div {...stylex.props(styles.labelWrapper)}>+ <div+ {...stylex.props(styles.labelWrapper, labelWrapperSizeStyles[size])}><XDSFieldLabellabel={label}inputID={id}@@ -243,7 +340,7 @@ export const XDSCheckboxInput = forwardRef<</div></div>);- },+ });XDSCheckboxInput.displayName = 'XDSCheckboxInput';diff --git a/packages/core/src/CheckboxInput/index.ts b/packages/core/src/CheckboxInput/index.tsindex 42022edebb..e38b33de48 100644--- a/packages/core/src/CheckboxInput/index.ts+++ b/packages/core/src/CheckboxInput/index.ts@@ -1,11 +1,14 @@/*** @file index.ts* @input Imports XDSCheckboxInput component and types from XDSCheckboxInput.tsx- * @output Exports XDSCheckboxInput, XDSCheckboxInputProps+ * @output Exports XDSCheckboxInput, XDSCheckboxInputProps, XDSCheckboxInputSize* @position Component entry point; re-exported by /packages/core/src/index.ts** SYNC: When modified, update this header and /packages/core/src/CheckboxInput/README.md*/-export { XDSCheckboxInput } from './XDSCheckboxInput';-export type { XDSCheckboxInputProps } from './XDSCheckboxInput';+export {XDSCheckboxInput} from './XDSCheckboxInput';+export type {+ XDSCheckboxInputProps,+ XDSCheckboxInputSize,+} from './XDSCheckboxInput';diff --git a/packages/core/src/hooks/index.ts b/packages/core/src/hooks/index.tsnew file mode 100644index 0000000000..5e186c4bc7--- /dev/null+++ b/packages/core/src/hooks/index.ts@@ -0,0 +1,11 @@+/**+ * @file index.ts+ * @input Imports hooks from individual files+ * @output Exports all hooks+ * @position Hook entry point; re-exported by /packages/core/src/index.ts+ *+ * SYNC: When modified, update this header+ */++export {useHoverState} from './useHoverState';+export type {UseHoverStateResult} from './useHoverState';diff --git a/packages/core/src/hooks/useHoverState.ts b/packages/core/src/hooks/useHoverState.tsnew file mode 100644index 0000000000..7d793c8516--- /dev/null+++ b/packages/core/src/hooks/useHoverState.ts@@ -0,0 +1,51 @@+/**+ * @file useHoverState.ts+ * @input Uses React useState, useCallback+ * @output Exports useHoverState hook+ * @position Utility hook for tracking hover state on elements+ *+ * SYNC: When modified, update this header+ */++import {useState, useCallback} from 'react';++export interface UseHoverStateResult {+ isHovered: boolean;+ onMouseEnter: () => void;+ onMouseLeave: () => void;+}++/**+ * A hook for tracking hover state on an element.+ *+ * @example+ * ```tsx+ * const {isHovered, onMouseEnter, onMouseLeave} = useHoverState();+ * return (+ * <div+ * onMouseEnter={onMouseEnter}+ * onMouseLeave={onMouseLeave}+ * style={{backgroundColor: isHovered ? 'blue' : 'transparent'}}+ * >+ * Hover me+ * </div>+ * );+ * ```+ */+export function useHoverState(): UseHoverStateResult {+ const [isHovered, setIsHovered] = useState(false);++ const onMouseEnter = useCallback(() => {+ setIsHovered(true);+ }, []);++ const onMouseLeave = useCallback(() => {+ setIsHovered(false);+ }, []);++ return {+ isHovered,+ onMouseEnter,+ onMouseLeave,+ };+}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex fd61e597c2..cae46e559b 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -26,5 +26,8 @@ export * from './Layer';// Skeleton loading placeholderexport * from './Skeleton';+// Hooks+export * from './hooks';+// Themeexport * from './theme';