Skip to content
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

feat: dynamic styles set inherits to false #794

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

Samantha-Zhan
Copy link
Contributor

What changed / motivation ?

Addresses task #734.

Generate @property declarations for all variables that are created for dynamic styles and declare them as not inheriting. Increase stylex variable compiler efficiency by disabling all dynamic styles from cascading down from the node defining the variable.

Linked PR/Issues

Fixes #734

Additional Context

npx jest stylex-transform-create-test

Test file stylex-transform-create-test.js should have @property injection added for all stylex dynamic styles.

Pre-flight checklist

@Samantha-Zhan Samantha-Zhan requested a review from nmn December 3, 2024 23:18
@facebook-github-bot facebook-github-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 Dec 3, 2024
Copy link

github-actions bot commented Dec 3, 2024

workflow: benchmarks/size

Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.

@stylexjs/[email protected] size:compare
./size-compare.js /tmp/tmp.BcB1G81fFf /tmp/tmp.2zUBwnUong

Results Base Patch Ratio
stylex/lib/stylex.js
· compressed 729 729 1.00
· minified 2,541 2,541 1.00
stylex/lib/StyleXSheet.js
· compressed 1,266 1,266 1.00
· minified 3,776 3,776 1.00
rollup-example/.build/bundle.js
· compressed 562,983 562,983 1.00
· minified 10,185,368 10,185,368 1.00
rollup-example/.build/stylex.css
· compressed 99,229 99,229 1.00
· minified 745,845 745,845 1.00

Comment on lines 116 to 133
// add injection that mark variables used for dynamic styles as `inherits: false`
const injectedInheritStyles: { [string]: InjectableStyle } = {};
if (fns != null) {
const dynamicFnsNames = Object.values(fns)
?.map((entry) => Object.keys(entry[1]))
.flat();
dynamicFnsNames.forEach((fnsName) => {
injectedInheritStyles[fnsName] = {
priority: 0,
ltr: `@property ${fnsName} { inherits: false }`,
rtl: null,
};
});
}

if (!confident) {
throw path.buildCodeFrameError(messages.NON_STATIC_VALUE, SyntaxError);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you add this code after the if condition that checks confident. Otherwise you'll end up throwing errors because fns will be null or undefined.

Suggested change
// add injection that mark variables used for dynamic styles as `inherits: false`
const injectedInheritStyles: { [string]: InjectableStyle } = {};
if (fns != null) {
const dynamicFnsNames = Object.values(fns)
?.map((entry) => Object.keys(entry[1]))
.flat();
dynamicFnsNames.forEach((fnsName) => {
injectedInheritStyles[fnsName] = {
priority: 0,
ltr: `@property ${fnsName} { inherits: false }`,
rtl: null,
};
});
}
if (!confident) {
throw path.buildCodeFrameError(messages.NON_STATIC_VALUE, SyntaxError);
}
if (!confident) {
throw path.buildCodeFrameError(messages.NON_STATIC_VALUE, SyntaxError);
}
// add injection that mark variables used for dynamic styles as `inherits: false`
const injectedInheritStyles: { [string]: InjectableStyle } = {};
if (fns != null) {
const dynamicFnsNames = Object.values(fns)
?.map((entry) => Object.keys(entry[1]))
.flat();
dynamicFnsNames.forEach((fnsName) => {
injectedInheritStyles[fnsName] = {
priority: 0,
ltr: `@property ${fnsName} { inherits: false }`,
rtl: null,
};
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call, thanks for fixing it!

@nmn nmn merged commit 30b5300 into main Dec 4, 2024
7 of 8 checks passed
@nmn nmn deleted the feat/dynamic-styles-inherits-false branch December 4, 2024 23:31
@necolas
Copy link
Contributor

necolas commented Dec 5, 2024

I noticed that this change didn't show up in the CSS bundle size benchmark. We don't have any dynamic styles syntax used in the fixture data. It would be good to capture the impact on bundle size by updating the benchmark's fixture data to include a significant number of dynamic styles. (The original fixture was scraped from a snapshot of Comet that pre-dates support for dynamic styles.)

When using inline styles directly, you don't incur the runtime cost of using custom properties that necessitates this patch. For most dynamic use cases, we can simply render inline styles without using custom properties or adding generated CSS. There are runtime and bundle size wins to be had there. With improved fixture data we should be able to get a sense of how to quantify the impact of future optimizations to the implementation.

@necolas
Copy link
Contributor

necolas commented Dec 5, 2024

Created new tasks: #799, #800

aminaopio pushed a commit to aminaopio/stylex that referenced this pull request Dec 22, 2024
* feat: dynamic styles set inherits to false
* update tests
aminaopio pushed a commit to aminaopio/stylex that referenced this pull request Dec 22, 2024
* feat: dynamic styles set inherits to false
* update tests
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.

Mark variables used for dynamic styles as inherits: false
4 participants