An open source design system that's fully customizable and agent ready
TypeScript
feat(core): let CheckboxInput and Switch name their own label theme target (#5183)
freddymeta75b5af2947ee
diff --git a/.changeset/checkbox-switch-label-theme-target.md b/.changeset/checkbox-switch-label-theme-target.mdnew file mode 100644index 0000000000..b8c663be1c--- /dev/null+++ b/.changeset/checkbox-switch-label-theme-target.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[feat] CheckboxInput and Switch now name their own label as a theme target: `astryx-checkbox-label` and `astryx-switch-label`, alongside the `astryx-field-label` every label already carries. A theme could previously only reach one `astryx-field-label` target, so styling a checkbox's label — which shares a row with its control — meant styling every form field's label above its input too. The control passes the target down, so the name says what the thing is rather than encoding how it is arranged, and nothing can set it untruthfully.++@freddymetadiff --git a/packages/core/src/CheckboxInput/CheckboxInput.doc.mjs b/packages/core/src/CheckboxInput/CheckboxInput.doc.mjsindex d7ae6c3fcc..1e5fa22786 100644--- a/packages/core/src/CheckboxInput/CheckboxInput.doc.mjs+++ b/packages/core/src/CheckboxInput/CheckboxInput.doc.mjs@@ -133,6 +133,7 @@ export const docs = {{className: 'astryx-checkbox-input', visualProps: ['size']},{className: 'astryx-checkbox-indicator', visualProps: ['size'], states: ['checked', 'disabled']},{className: 'astryx-checkbox', visualProps: ['size'], states: ['checked', 'disabled'], deprecatedFor: 'checkbox-indicator'},+ {className: 'astryx-checkbox-label'},],},usage: {@@ -209,6 +210,7 @@ export const docsZh = {},{className: 'astryx-checkbox-indicator', visualProps: ['size'], states: ['checked', 'disabled']},{className: 'astryx-checkbox', visualProps: ['size'], states: ['checked', 'disabled'], deprecatedFor: 'checkbox-indicator'},+ {className: 'astryx-checkbox-label'},],},};diff --git a/packages/core/src/CheckboxInput/CheckboxInput.test.tsx b/packages/core/src/CheckboxInput/CheckboxInput.test.tsxindex 77969bbd94..f2ac19f4e0 100644--- a/packages/core/src/CheckboxInput/CheckboxInput.test.tsx+++ b/packages/core/src/CheckboxInput/CheckboxInput.test.tsx@@ -719,3 +719,18 @@ describe('focus ring ownership (WCAG 2.4.7)', () => {expect(indicatorOf(container).style.outlineStyle).toBe('');});});++describe('label theme target', () => {+ it('names its own label so a theme can style it apart from a field label', () => {+ // The control knows this label shares a row with it; the label does not.+ // Both classes land on the one element, so a theme reaches every label+ // through `astryx-field-label` and only this kind through+ // `astryx-checkbox-label`.+ render(+ <CheckboxInput label="Notify me" value={false} onChange={() => {}} />,+ );+ const label = screen.getByText('Notify me').closest('label');+ expect(label).toHaveClass('astryx-field-label');+ expect(label).toHaveClass('astryx-checkbox-label');+ });+});diff --git a/packages/core/src/CheckboxInput/CheckboxInput.tsx b/packages/core/src/CheckboxInput/CheckboxInput.tsxindex 4ea12e4f46..73abae279a 100644--- a/packages/core/src/CheckboxInput/CheckboxInput.tsx+++ b/packages/core/src/CheckboxInput/CheckboxInput.tsx@@ -478,6 +478,11 @@ export function CheckboxInput({</div><div {...stylex.props(styles.labelWrapper)}><FieldLabel+ // A checkbox's label shares a row with its control, unlike a form+ // field's label above its input. Naming the label rather than the+ // arrangement means a theme asks for the thing it wants, and the+ // component that actually knows what this is says so.+ {...themeProps('checkbox-label')}label={label}inputID={id}isLabelHidden={isLabelHidden}diff --git a/packages/core/src/Field/FieldLabel.test.tsx b/packages/core/src/Field/FieldLabel.test.tsxindex c99f339bcd..d74a067fc9 100644--- a/packages/core/src/Field/FieldLabel.test.tsx+++ b/packages/core/src/Field/FieldLabel.test.tsx@@ -15,6 +15,7 @@ import userEvent from '@testing-library/user-event';import {TestIcon} from '../__tests__/TestIcon';import {InternationalizationProvider} from '../i18n';import {FieldLabel} from './FieldLabel';+import {themeProps} from '../utils/themeProps';describe('FieldLabel', () => {it('renders label text', () => {@@ -197,4 +198,32 @@ describe('FieldLabel', () => {expect(description.closest('label')).toBeNull();});});++ describe('a control can name its own label target', () => {+ it('composes a passed target onto the field-label one', () => {+ // How CheckboxInput and Switch reach their label: the control spreads+ // its own themeProps in, and both classes end up on the same element,+ // so a theme can style every label or just this kind.+ render(+ <FieldLabel+ {...themeProps('checkbox-label')}+ label="Notify me"+ inputID="notify-input"+ />,+ );+ const label = screen.getByText('Notify me').closest('label');+ expect(label).toHaveClass('astryx-field-label');+ expect(label).toHaveClass('astryx-checkbox-label');+ });++ it('describes no placement of its own', () => {+ // The label cannot know how its caller arranged it, so it says nothing:+ // a `data-layout` here would be a claim that is wrong for at least one+ // caller (Field's horizontal-labels puts a label beside its control),+ // and a consumer could set it untruthfully.+ render(<FieldLabel label="Email" inputID="email-input" />);+ const label = screen.getByText('Email').closest('label');+ expect(label).not.toHaveAttribute('data-layout');+ });+ });});diff --git a/packages/core/src/Field/FieldLabel.tsx b/packages/core/src/Field/FieldLabel.tsxindex d30c3abc90..0ea8543ff9 100644--- a/packages/core/src/Field/FieldLabel.tsx+++ b/packages/core/src/Field/FieldLabel.tsx@@ -285,6 +285,13 @@ export function FieldLabel({htmlFor={isGroupLabel ? undefined : inputID}{...rest}{...mergeProps(+ // A control that knows what kind of label this is passes its own+ // target down (CheckboxInput's `checkbox-label`, Switch's+ // `switch-label`); it arrives as `className` and composes onto this+ // one. The label itself does not describe its own placement — it+ // cannot know it, and any encoding it guessed would be wrong for a+ // caller that arranges labels differently (Field's+ // `horizontal-labels`).themeProps('field-label'),stylex.props(styles.label,diff --git a/packages/core/src/Switch/Switch.doc.mjs b/packages/core/src/Switch/Switch.doc.mjsindex 17107b1805..ad380bddc1 100644--- a/packages/core/src/Switch/Switch.doc.mjs+++ b/packages/core/src/Switch/Switch.doc.mjs@@ -167,6 +167,7 @@ export const docs = {className: 'astryx-switch-field',visualProps: ['labelPosition', 'labelSpacing'],},+ {className: 'astryx-switch-label'},],},usage: {@@ -338,6 +339,7 @@ export const docsZh = {className: 'astryx-switch-field',visualProps: ['labelPosition', 'labelSpacing'],},+ {className: 'astryx-switch-label'},],},usage: {diff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsxindex d6330bf54b..2571402e30 100644--- a/packages/core/src/Switch/Switch.test.tsx+++ b/packages/core/src/Switch/Switch.test.tsx@@ -823,3 +823,13 @@ describe('forced colors (WCAG 1.4.11)', () => {expect(getForcedColorsRules()).not.toContain('color-mix');});});++describe('label theme target', () => {+ it('names its own label so a theme can style it apart from a field label', () => {+ // See CheckboxInput: the control names the label it owns.+ render(<Switch label="Wi-Fi" value={false} onChange={() => {}} />);+ const label = screen.getByText('Wi-Fi').closest('label');+ expect(label).toHaveClass('astryx-field-label');+ expect(label).toHaveClass('astryx-switch-label');+ });+});diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsxindex c9b7f803e9..91c8b54ccd 100644--- a/packages/core/src/Switch/Switch.tsx+++ b/packages/core/src/Switch/Switch.tsx@@ -633,6 +633,9 @@ export function Switch({const labelElement = (<div {...stylex.props(styles.labelWrapper, labelWrapperSizeStyles[size])}><FieldLabel+ // See CheckboxInput: the control names its own label target rather+ // than the label guessing at its placement.+ {...themeProps('switch-label')}label={label}inputID={id}isLabelHidden={isLabelHidden}