what is the difference between 4 options of applying styles #2284
-
Based on my understanding, there are 4 ways to use theme ui Option 1 - using theme.jsI can create a variant style in theme.js section: {
backgroundColor: `black`,
mt: [10],
} and then use it like this <Box as="section" variant="section"></Box> Option 2 - applying styles directlyI could do <Box as="section" backgroundColor="black" mt="10"></Box> Option 3 - using sxI could use sx directly <Box as="section" sx={{backgroundColor: 'black' , mt: [10]}}></Box> Option 4 using constantconst sectionStyles = {
backgroundColor: `black`,
mt: [10],
}
<Box as="section" sx={{ ...sectionStyles }} ></Box> what is the difference between all these cases and is there a recommended method? Edit: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As often with software development, it depends™️ Option 1: I use this when other users are consuming a site I built with Theme UI and they should be able to easily style the UI without going into the React component itself. This is even more important if the UI is shipped as a npm package and they can't actually change the React components. Option 2 & Option 3: With the given example I'd think this is just preference for syntax. I often create my own Box component that only accepts certain props and then only the ones from the theme, then I prefer the syntax from Option 2 as it looks cleaner to me. Option 3: Important to note: You can apply this to any element, e.g. Option 4: Is the same as Option 3 to me |
Beta Was this translation helpful? Give feedback.
As often with software development, it depends™️
Option 1:
I use this when other users are consuming a site I built with Theme UI and they should be able to easily style the UI without going into the React component itself. This is even more important if the UI is shipped as a npm package and they can't actually change the React components.
Option 2 & Option 3:
With the given example I'd think this is just preference for syntax. I often create my own Box component that only accepts certain props and then only the ones from the theme, then I prefer the syntax from Option 2 as it looks cleaner to me.
Option 3:
Important to note: You can apply this to any elem…