How to creating a <Box /> component. #813
-
Describe the feature requestI would love if there was some documentation on how to create a component that are often at the root of design systems if it is possible. We want 0 runtime css with a props driven component api like this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
@taylorpreston I actually went through a thought evolution like this, but where I ended up is not as generalizable as you're imagining. Instead, if I find myself repeating the same properties over and over again, I can make it a component that accepts the style arguments as component arguments like My overall realization is that a Furthermore, the I don't have enough time to explain how it works right now, but I'll paste some relevant code. If this sparks any interest, just let me know. https://gist.github.com/zaydek/9dd8746abd26dce5d0044fb329f7e079 |
Beta Was this translation helpful? Give feedback.
-
@taylorpreston A generic This often becomes an anti-pattern when trying to implement responsive layouts. Your design system should include a set of "values". Those values should change based on the viewport breakpoints internally. You should not be mixing and matching arbitrary values for different viewport breakpoints when using the So, I would recommend something like the following instead: <Box p="sm_fixed">Hello World</Box>
<Box p="sm"_scaling>Hello World</Box> function Box({p, ...props}) {
return <div
{...stylex.props(styles.base, paddingStyles[p])}
{...props}
/>;
}
const styles = styles.create({
base: {boxSizing: 'border-box'},
});
const TABLET = '@media (min-width: 768px) and (max-width: 1279.9px)';
const DESKTOP = '@media (min-width: 1280px)';
const paddingStyles = stylex.create({
sm_fixed: {padding: 4},
sm_scaling: {
padding: {
default: 4,
[TABLET]: 8,
[DESKTOP]: 16,
},
},
md_fixed: {padding: 8},
...
}); Basically, you should have an opinion about which values belong together ahead of time in your design system. For example, I think a more locked down design system with named values such as const TABLET = '@media (min-width: 768px) and (max-width: 1279.9px)';
const DESKTOP = '@media (min-width: 1280px)';
const paddingStyles = stylex.create({
'2': {padding: 2},
'2-4-8': {
padding: {
default: 2,
[TABLET]: 4,
[DESKTOP]: 8,
},
},
'4': {padding: 4},
'4-8-16': {
padding: {
default: 4,
[TABLET]: 8,
[DESKTOP]: 16,
},
},
...
}); This approach is less prescriptive but it still gives you a consolidated place to see all the way the values may be used together and let's the Combining the values under various breakpoints into a single atomic value can feel restrictive at first. Specially when coming from other styling solutions. But, over time it enforces much better design consistency, encourages good practices for responsive design and makes style composition more predictable and simpler to deal with. (You don't have to manually unset styles for every break point. Setting Additionally, this also makes the styling more consistent and reliable across classNames, inline styles and React Native. |
Beta Was this translation helpful? Give feedback.
@taylorpreston A generic
Box
component can be created but what is not supported is the configuration on a per-breakpoint basis.This often becomes an anti-pattern when trying to implement responsive layouts. Your design system should include a set of "values". Those values should change based on the viewport breakpoints internally. You should not be mixing and matching arbitrary values for different viewport breakpoints when using the
Box
component.So, I would recommend something like the following instead: