Repository navigation
fix(react-ui): disabled fields use the disabled-field tokens, like the CMS #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roger-datocms
wants to merge
2
commits into
2.x
Choose a base branch
from
fix/react-ui-disabled-field-tokens
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "datocms-react-ui": patch | ||
| --- | ||
|
|
||
| Disabled `TextInput`, `TextareaInput` and `SelectInput` fields now match native DatoCMS fields. They use the new `--color--disabled-field--*` tokens, which fixes the low-contrast value text in dark mode. An empty disabled field shows a dimmed placeholder, and a disabled text field no longer reacts to hover. `SelectInput` also maps react-select's palette slot for slot like the CMS does, so menus no longer fall back to light-only greys in the slots the CMS maps. On older DatoCMS versions that don't send the new tokens, fields fall back to the previous `--color--disabled--*` colors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // The repo's @types/node predates the `node:` specifiers | ||
| // biome-ignore lint/style/useNodejsImportProtocol: see above | ||
| import { readFileSync } from 'fs'; | ||
| // biome-ignore lint/style/useNodejsImportProtocol: see above | ||
| import { fileURLToPath } from 'url'; | ||
| import postcss, { type Rule } from 'postcss'; | ||
| import postcssNested from 'postcss-nested'; | ||
| import type { CSSObjectWithLabel, Theme } from 'react-select'; | ||
| import { buildStyles, themeConfig } from '../src/SelectInput/theme'; | ||
|
|
||
| const disabledFieldSurface = | ||
| 'var(--color--disabled-field--surface, var(--color--disabled--surface))'; | ||
| const disabledFieldInk = | ||
| 'var(--color--disabled-field--ink, var(--color--disabled--ink))'; | ||
| const disabledFieldInkPlaceholder = | ||
| 'var(--color--disabled-field--ink-placeholder, var(--color--ink-placeholder))'; | ||
|
|
||
| /** react-select's own default theme, as passed to a `ThemeConfig` function. */ | ||
| const reactSelectDefaultTheme: Theme = { | ||
| borderRadius: 4, | ||
| colors: { | ||
| primary: '#2684FF', | ||
| primary75: '#4C9AFF', | ||
| primary50: '#B2D4FF', | ||
| primary25: '#DEEBFF', | ||
| danger: '#DE350B', | ||
| dangerLight: '#FFBDAD', | ||
| neutral0: 'hsl(0, 0%, 100%)', | ||
| neutral5: 'hsl(0, 0%, 95%)', | ||
| neutral10: 'hsl(0, 0%, 90%)', | ||
| neutral20: 'hsl(0, 0%, 80%)', | ||
| neutral30: 'hsl(0, 0%, 70%)', | ||
| neutral40: 'hsl(0, 0%, 60%)', | ||
| neutral50: 'hsl(0, 0%, 50%)', | ||
| neutral60: 'hsl(0, 0%, 40%)', | ||
| neutral70: 'hsl(0, 0%, 30%)', | ||
| neutral80: 'hsl(0, 0%, 20%)', | ||
| neutral90: 'hsl(0, 0%, 10%)', | ||
| }, | ||
| spacing: { baseUnit: 4, controlHeight: 38, menuGutter: 8 }, | ||
| }; | ||
|
|
||
| // The style functions only read the state flags below; the rest of | ||
| // react-select's state object is irrelevant to the colors under test. | ||
| const callStyle = (key: string, state: Record<string, unknown>, styles: any) => | ||
| styles[key]({} as CSSObjectWithLabel, state) as Record<string, unknown>; | ||
|
|
||
| describe('SelectInput themeConfig', () => { | ||
| const { colors, borderRadius } = themeConfig(reactSelectDefaultTheme); | ||
|
|
||
| it('maps the palette slots the CMS maps, to the same tokens', () => { | ||
| expect(borderRadius).toBe(0); | ||
| expect(colors).toMatchObject({ | ||
| neutral0: 'var(--color--surface-raised)', | ||
| neutral10: disabledFieldSurface, | ||
| neutral20: 'var(--color--border)', | ||
| neutral30: 'var(--color--border-hover)', | ||
| neutral40: 'var(--color--ink-disabled)', | ||
| neutral80: 'var(--color--ink)', | ||
| primary: 'var(--color--selected--surface)', | ||
| primary25: 'var(--color--surface-raised-hover)', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SelectInput styles', () => { | ||
| const permutations = [false, true].flatMap((isDisabled) => | ||
| [false, true].flatMap((error) => | ||
| [false, true].map((isFocused) => ({ isDisabled, error, isFocused })), | ||
| ), | ||
| ); | ||
|
|
||
| it.each(permutations)( | ||
| 'control: disabled=$isDisabled error=$error focused=$isFocused', | ||
| ({ isDisabled, error, isFocused }) => { | ||
| const control = callStyle( | ||
| 'control', | ||
| { isFocused, isDisabled }, | ||
| buildStyles(isDisabled, error), | ||
| ); | ||
|
|
||
| expect(control.backgroundColor).toBe( | ||
| isDisabled ? disabledFieldSurface : 'var(--color--surface-raised)', | ||
| ); | ||
| expect(control.borderColor).toBe( | ||
| error | ||
| ? 'var(--color--danger-soft--border)' | ||
| : isFocused | ||
| ? 'var(--color--focus--border)' | ||
| : 'var(--color--border)', | ||
| ); | ||
| expect(control.boxShadow).toBe( | ||
| isFocused | ||
| ? `0 0 0 4px var(--color--${ | ||
| error ? 'danger-soft' : 'focus' | ||
| }--outline)` | ||
| : undefined, | ||
| ); | ||
| expect(control.minHeight).toBe(40); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([false, true])('placeholder: disabled=%s', (isDisabled) => { | ||
| const placeholder = callStyle('placeholder', { isDisabled }, buildStyles()); | ||
|
|
||
| expect(placeholder.color).toBe( | ||
| isDisabled | ||
| ? disabledFieldInkPlaceholder | ||
| : 'var(--color--ink-placeholder)', | ||
| ); | ||
| }); | ||
|
|
||
| it.each([false, true])('singleValue: disabled=%s', (isDisabled) => { | ||
| const singleValue = callStyle('singleValue', { isDisabled }, buildStyles()); | ||
|
|
||
| expect(singleValue.color).toBe( | ||
| isDisabled ? disabledFieldInk : 'var(--color--ink)', | ||
| ); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { isSelected: false, isFocused: false, expected: undefined }, | ||
| { | ||
| isSelected: false, | ||
| isFocused: true, | ||
| expected: 'var(--color--surface-raised-hover)', | ||
| }, | ||
| { | ||
| isSelected: true, | ||
| isFocused: false, | ||
| expected: 'var(--color--selected--surface)', | ||
| }, | ||
| { | ||
| isSelected: true, | ||
| isFocused: true, | ||
| expected: 'var(--color--selected--surface)', | ||
| }, | ||
| ])( | ||
| 'option: selected=$isSelected focused=$isFocused', | ||
| ({ isSelected, isFocused, expected }) => { | ||
| const option = callStyle( | ||
| 'option', | ||
| { isSelected, isFocused }, | ||
| buildStyles(), | ||
| ); | ||
|
|
||
| expect(option.backgroundColor).toBe(expected); | ||
| expect(option.color).toBe('var(--color--ink)'); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| /** Flattens a CSS module's nesting and returns its declarations by selector. */ | ||
| const declarationsBySelector = (component: string) => { | ||
| const source = readFileSync( | ||
| fileURLToPath( | ||
| new URL(`../src/${component}/styles.module.css`, import.meta.url).href, | ||
| ), | ||
| 'utf8', | ||
| ); | ||
| const root = postcss([postcssNested]).process(source, { | ||
| from: undefined, | ||
| }).root; | ||
| const result: Record<string, Record<string, string>> = {}; | ||
|
|
||
| root.walkRules((rule: Rule) => { | ||
| result[rule.selector] ??= {}; | ||
| rule.walkDecls((decl) => { | ||
| result[rule.selector][decl.prop] = decl.value.replace(/\s+/g, ' '); | ||
| }); | ||
| }); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| /** Collapses `var( a, b )` whitespace the formatter adds across lines. */ | ||
| const normalize = (value: string) => | ||
| value.replace(/\(\s+/g, '(').replace(/\s+\)/g, ')'); | ||
|
|
||
| describe.each(['TextInput', 'TextareaInput'])('%s disabled colors', (name) => { | ||
| const rules = declarationsBySelector(name); | ||
| const disabled = rules[`.${name}--disabled`]; | ||
|
|
||
| it('uses the disabled-field surface and ink, with fallbacks', () => { | ||
| expect(normalize(disabled.background)).toBe(disabledFieldSurface); | ||
| expect(normalize(disabled.color)).toBe(disabledFieldInk); | ||
| expect(disabled['border-color']).toBe('var(--color--border)'); | ||
| }); | ||
|
|
||
| it('dims the placeholder of an empty disabled field', () => { | ||
| expect(normalize(rules[`.${name}--disabled::placeholder`].color)).toBe( | ||
| disabledFieldInkPlaceholder, | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps the border static on hover, like the CMS', () => { | ||
| expect(rules[`.${name}--disabled:hover`]['border-color']).toBe( | ||
| 'var(--color--border)', | ||
| ); | ||
| }); | ||
|
|
||
| it('declares disabled overrides after the base rules, so they win the tie', () => { | ||
| const selectors = Object.keys(rules); | ||
|
|
||
| for (const pseudo of ['::placeholder', ':hover']) { | ||
| expect(selectors.indexOf(`.${name}--disabled${pseudo}`)).toBeGreaterThan( | ||
| selectors.indexOf(`.${name}${pseudo}`), | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Claude here) This test guards the cascade from the comment above. If someone moves the disabled block above the base
:hoveror::placeholderrules, the token tests still pass but the styling breaks. This order check catches that.