Closed alex-vladut closed 3 years ago
What happens if the path specified to the token in the object doesn't exist?
The property passed to then CSS will be undefined
, I think this is what the data providers return when the key doesn't exist. In that case I think it shouldn't throw an error, but most likely the property will be ignored. Would you think we should rather throw an error in that case or maybe at least show a warning for the developers to fix it?
Amazing work @alex-vladut!! π π π !!!
Regarding the DT files, what about putting them into their own Views/DesignTokens.js
file so we keep them separate from the data implementation?
On its internals, I have two suggestions, let me know what you think about them:
E.g.:
import { DataProvider, useDataValue } from 'Views/Data.js'
import get from 'lodash/get'
import tokens from 'DesignSystem/tokens.json'
import React from 'react'
let defaultTheme = Object.keys(tokens)[0]
export function DesignTokens({ theme = defaultTheme, children, viewPath }) {
return (
<DataProvider
context="design_tokens_theme"
value={theme}
viewPath={viewPath}
>
{children}
</DataProvider>
)
}
export function useDesignTokenValue({ path, viewPath }) {
let theme = useDataValue({
context: 'design_tokens_theme',
viewPath,
})
return get(tokens, `${theme}.${path}`)
}
Your suggestion makes sense to me, I will give it a try and implement it like this π
With these change support was added for design tokens. In order the make use of the design tokens an application should be wrapped by the
ThemeDesignTokensProvider
like this:Let me know if you think this approach is not right or there is a better way and we can discuss.
Here are a couple of cases supported with this approach:
Simple reference to a design token:
morphed as:
Reference to a design token and allow to override through component props:
morphed as:
With
when
conditions:will be morphed to:
With
when
conditions and override through component props:will be morphed to:
In this case it will take into account first the prop and, if not specified, will default to the design token value.