Skip to content

Conversation

@vincentriemer
Copy link
Contributor

"Styling-agnostic" react component systems, such as base-ui, expose state based styling hooks through the use of dom element attributes. Actually listening to these in StyleX, while possible through usage of the :is() psedudoselector, is overly verbose and uncessary. This diff addresses this by adding support for attribute selectors in stylex.when calls and conditional styles.

Motivation

  • Allow stylex.when and conditional style objects to accept attribute selectors (including value-matching forms) rather than only attribute-existence markers.
  • Treat attribute selectors like pseudo-classes in the preprocessing and validation pipeline so downstream CSS generation remains correct.
  • Surface attribute-selector usage in types and docs so consumers get proper editor/compile-time guidance.

Description

  • Accept keys starting with [ in validation and preprocessing by updating basic-validation.js, flatten-raw-style-obj.js, and PreRule.pseudos to treat attribute selectors like pseudos.
  • Extend when helpers to accept attribute selectors by adding a WhenSelector union, updating validatePseudoSelector to allow [...], and applying the selector when building :where / :has clauses in when.js.
  • Update Flow/TypeScript definitions in StyleXTypes.js and StyleXTypes.d.ts to permit attribute selectors for stylex.when.* signatures.
  • Add/update tests and documentation to use value-based attribute selectors (tests and snapshots in transform-stylex-when-test.js and flatten-raw-style-obj-test.js, and docs in when.mdx).

Testing

  • Ran yarn workspace @stylexjs/babel-plugin test -- transform-stylex-when-test --updateSnapshot, where the test suite passed and snapshots were updated but the overall command returned non-zero due to global coverage thresholds being unmet.
  • The changed test file transform-stylex-when-test.js passed (all tests green) and snapshots were updated successfully.
  • Ran npm run lint, which completed successfully.
  • Pre-commit formatting/lint hooks ran as part of the commit and completed successfully.

@vercel
Copy link

vercel bot commented Jan 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
stylex Ready Ready Preview, Comment Jan 14, 2026 6:06pm

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 13, 2026
@github-actions
Copy link

github-actions bot commented Jan 13, 2026

workflow: benchmarks/size

Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.
yarn workspace v1.22.22
yarn run v1.22.22
$ node ./compare.js /tmp/tmp.jMNS0SzsOA /tmp/tmp.tuVzmyL6c5

Results Base Patch Ratio
@stylexjs/stylex/lib/cjs/stylex.js
· compressed 1,311 1,311 1.00
· minified 4,150 4,150 1.00
@stylexjs/stylex/lib/cjs/inject.js
· compressed 1,793 1,793 1.00
· minified 4,915 4,915 1.00
benchmarks/size/.build/bundle.js
· compressed 496,650 496,650 1.00
· minified 4,847,840 4,847,840 1.00
benchmarks/size/.build/stylex.css
· compressed 99,853 99,853 1.00
· minified 747,541 747,541 1.00
Done in 0.09s.
Done in 0.32s.

@github-actions
Copy link

github-actions bot commented Jan 13, 2026

workflow: benchmarks/perf

Comparison of performance test results, measured in operations per second. Larger is better.
yarn workspace v1.22.22
yarn run v1.22.22
$ node ./compare.js /tmp/tmp.gFy47FPc3o /tmp/tmp.ovxnuAaV8F

Results Base Patch Ratio
babel-plugin: stylex.create
· basic create 519 534 1.03 +
· complex create 63 64 1.02 +
babel-plugin: stylex.createTheme
· basic themes 432 425 0.98 -
· complex themes 34 32 0.94 !!
Done in 0.09s.
Done in 0.36s.

"Styling-agnostic" react component systems, such as base-ui, expose state based styling hooks through the use of dom element attributes. Actually listening to these in StyleX, while possible through usage of the `:is()` psedudoselector, is overly verbose and uncessary. This diff addresses this by adding support for attribute selectors in stylex.when calls and conditional styles.

### Motivation

- Allow `stylex.when` and conditional style objects to accept attribute selectors (including value-matching forms) rather than only attribute-existence markers.
- Treat attribute selectors like pseudo-classes in the preprocessing and validation pipeline so downstream CSS generation remains correct.
- Surface attribute-selector usage in types and docs so consumers get proper editor/compile-time guidance.

### Description

- Accept keys starting with `[` in validation and preprocessing by updating `basic-validation.js`, `flatten-raw-style-obj.js`, and `PreRule.pseudos` to treat attribute selectors like pseudos.
- Extend `when` helpers to accept attribute selectors by adding a `WhenSelector` union, updating `validatePseudoSelector` to allow `[`...`]`, and applying the selector when building `:where` / `:has` clauses in `when.js`.
- Update Flow/TypeScript definitions in `StyleXTypes.js` and `StyleXTypes.d.ts` to permit attribute selectors for `stylex.when.*` signatures.
- Add/update tests and documentation to use value-based attribute selectors (tests and snapshots in `transform-stylex-when-test.js` and `flatten-raw-style-obj-test.js`, and docs in `when.mdx`).

### Testing

- Ran `yarn workspace @stylexjs/babel-plugin test -- transform-stylex-when-test --updateSnapshot`, where the test suite passed and snapshots were updated but the overall command returned non-zero due to global coverage thresholds being unmet.
- The changed test file `transform-stylex-when-test.js` passed (all tests green) and snapshots were updated successfully.
- Ran `npm run lint`, which completed successfully.
- Pre-commit formatting/lint hooks ran as part of the commit and completed successfully.
@vincentriemer vincentriemer merged commit be1b393 into main Jan 14, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants