Responsive Variant Styles Based on Props – Feasibility with stylex.create? #1084
JuanFuentes20
started this conversation in
General
Replies: 1 comment
-
This pattern is explicitly documented here: https://stylexjs.com/docs/learn/recipes/variants/ Specifically, you can define various styles as you described: const componentStyles = stylex.create({
warning: { ... },
success: { ... },
info: { ... }
}); And then, your component can accept one of the keys as a prop: type Props = {variant: keyof typeof componentStyles};
function Component({variant}: Props) {
return <div {...stylex.props(componentStyles[variant])}> ... </div>;
} That said, this pattern is explicitly forbidden: You should instead pre-define combinations for various screen-sizes rather than configuring different screen sizes manually. This decision was made specifically to improve the reliability of styles and ensure you don't accidentally mix nonsense combinations for mobile vs desktop styles. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi StyleX team,
Thank you for your excellent work on StyleX.
I’m currently exploring a feature pattern and would like to know if StyleX currently supports (or plans to support) this use case:
Desired Feature
We’d like to create components that accept a variant prop with either:
The corresponding styles are defined using stylex.create like so:
Goal
We want the component to apply different variant styles responsively, based on the values provided in the variant object.
Attempted Solution
We attempted to use a dynamic function inside stylex.create like this:
However, we ran into the limitation that spreading (or referencing styles dynamically from the StyleX object) is not allowed within stylex.create, due to the static analysis and compile-time requirements.
Workaround Tried
We tried manually assigning each style key like this:
This worked to an extent but:
My Question
Is there any current or planned capability in StyleX to support this pattern of responsive variant styling, where variant values (keys of a style map) can be passed responsively and resolved to full style objects per breakpoint?
This pattern would be very powerful for building highly reusable, theme-aware, and responsive components.
Thanks so much in advance for your time
Beta Was this translation helpful? Give feedback.
All reactions