Replies: 2 comments 4 replies
-
A bit of an update. After lots of different discussions with different people, it seems that it all comes down to:
I think the way the library currently is, people can choose their conventions. This is really good. This means that people can tick the boxes they need. For example: const { styled } = createStyled({
// know if they're using an invalid token. optional, defaults to `false`
strict: true,
tokens: {
colors: {
// easy and convenient DX
// differentiate a token from a value
$black: '#111',
$red: 'tomato'
}
}
})
const Component = styled('div', {
// easy and convenient DX
// differentiate a token from a value
backgroundColor: '$black',
// not a token
color: 'red',
// know if they're using an invalid token
borderColor: 'primary' // TS error, this is not a token
// know if they're using an invalid token
borderColor: 'primary' as any // make explicit that you don't want to use a token
}) ☝️ This ticks most of our boxes:
The only thing missing is the ability to receive the For that, I propose: const Component = styled('div', t =>({
// set padding top based on the sizes scale
transform: translateY(t.sizes[4])
}))
|
Beta Was this translation helpful? Give feedback.
-
@peduarte Enjoyed reading this write up. Very thorough walk through of the problems. Excited for strict mode to land. +1 for the function that receives theme object to access any value from the theme directly when needed. Overall I'm excited about the direction and perspective that Stitches is taking. |
Beta Was this translation helpful? Give feedback.
-
A discussion regarding the API of token-aware values emerged from a call and was then mentioned here #78
Im moving that to its own issue, so we can make a call on how to move forward and no pollute the negative value issue, although they'll be somewhat impacted by each other.
Option 1 - current
So, currently, token-aware values are completely seamless:
The main benefit of this is that its seamless, and it feels great to use! Styled System introduced this and overall it had a good reaction. Although I believe that devs that haven't fully experienced this generally seem to have a negative reaction to it.
On the downside, if it's mistyped, it's not possible to know - because all values also allow
string
s. So,backgroundColor: "prmay"
won't throw any errors, but you won't get what you expect, sinceprmay
is not a token nor a valid css colour value.It's also harder to refactor, so if you rename
primary
to something else, you need to manually refactor the code.Then, there's the issue with using negative values. With TS we cant dynamically add keys to an object, more info #78 (comment)
Finally, the mapping of CSS Properties to Theme Scales is automatic, and this could be a seen as a good or a bad thing. Its fair to assume you may nearly always want values from a specific scale, but, I don't think its safe to say that it's always the case. So that means, escape hatch will need to be introduced.
Option 2
In this comment, @hadihallak suggests prefixing the tokens, so something like:
I like this because its a bit more explicit that its not a raw CSS value, but still easy to type. No need to interpolate, or import anything else.
On the other hand, however, I think it would have the same issues with TS and negative values. But please correct me if Im wrong here @hadihallak. The refactoring issue mentioned above exists here too.
As with the example above, it also relies on the automatic mapping of CSS Properties to Theme Scales.
Note: This is actually already possible, but users need to do it themselves.
Option 3
Another option is a function:
I like this because its explicit what is happening, it gives the users the ability to pick the scale, and its type safe.
I think it would still have a refactoring issue mentioned above.
There's the extra chore of having to import the
token
function all the time, personally, I really dislike this, and think it has a big weight on the DX.Option 4
Finally, we could receive the theme object
I like this because its a pattern people are used to. Typing wise seems pretty straight forward as it would be inferred from th object keys. Its easy to refactor. Its quite explicit, so no confusion between tokens and raw CSS values.
The only downside I can think of is that you need to wrap the object in a function, but that's not a big deal I think?
This approach could also work with Option 3, instead of receiving the object, it receives a
token
functionFinally, it's worth mentioning that currently the library is trying to be the least opinionated as possible. So if users wish to prefix their tokens, they can already do that!
So I think we should consider the friction the library introduces too. Perhaps being too opinionated isn't a good idea
Beta Was this translation helpful? Give feedback.
All reactions