-
const styles = stylex.create(
padding: (size: "0.5rem"|"1rem") => {
padding: size
}
) This should be replaced with static classes at compile-time, as the type is constrained to enumeration of constants. Not sure if already possible. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I've considered this before and even investigated what it would take to implement something like this. I came to the conclusion that this was not viable, as there are a lot of edge-cases. The biggest being the inherent lack of Safety in typescript. ( For the reason, there is an explicit warning against over-using dynamic styles. It's not something you should be using for "convenience". They should only be used when the style value is truly dynamic. Instead, your should be doing something like this: const padding = stylex.create({
"0.5rem": { padding: "0.5rem" },
"1rem": { padding: "1rem" },
});
// Usage
<div {...stylex.props(padding["0.5rem"])} />
<div {...stylex.props(padding["1rem"])} /> There is a little bit of repetition, but, eventually, this is good for readability. We also like the clear rule that functions always result in inline styles, but objects never do. This makes behaviour predictable. |
Beta Was this translation helpful? Give feedback.
-
and what about stuff like this? const styles = stylex.create({
padding: (px) => {
padding: `${px}px`
}
}) |
Beta Was this translation helpful? Give feedback.
I've considered this before and even investigated what it would take to implement something like this. I came to the conclusion that this was not viable, as there are a lot of edge-cases. The biggest being the inherent lack of Safety in typescript. (
any
allows you to bypass any type).For the reason, there is an explicit warning against over-using dynamic styles. It's not something you should be using for "convenience". They should only be used when the style value is truly dynamic.
Instead, your should be doing something like this: