-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documenta presets e adiciona helper para transformar presets para web #21
Conversation
token-presets/index.ts
Outdated
export interface Preset { | ||
fontFamily: string; | ||
fontSize: number | string; | ||
fontWeight: string; | ||
letterSpacing: number | string; | ||
lineHeight: number | string; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa tipagem aqui não está amarrada com a definição dos presets, então ela não tem uma garantia forte de que vai de fato corresponder ao formato dos tokens. Isso poderia ser resolvido tipando os presets. (E aí teria que mudar pelo menos a tipagem do fontWeight
para poder ter apenas os valores permitidos)
Mas vc precisou criar esse tipo por causa da função toWebPreset
, certo? Uma outra alternativa seria ela não precisar saber o formato do preset todo, mas apenas exigir que receba um objeto que tenha o campo lineHeight
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eu tinha criado, pra além dessa razão, para garantir o retorno da função toWebPreset
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pensando só na necessidade dessa função, talvez a tipagem dela poderia ser algo como
export const toWebPreset = <Preset extends { lineHeight: number }>(preset: Preset): Preset & { lineHeight: string } => {
return { ...preset, lineHeight: toPx(preset.lineHeight) };
};
se não quiser criar o tipo do Preset
.
helpers/webOnlyHelpers.ts
Outdated
(Object.keys(preset) as Array<keyof Preset>).map((style) => { | ||
if (style === 'lineHeight') { | ||
return (webPreset[style] = toPx(preset[style])); | ||
} else { | ||
return (webPreset[style] = preset[style] as string); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Não dá pra fazer só webPreset.lineHeight = toPx(preset.lineHeight)
em vez do for? Ou até condensar o código todo da função num return, do tipo
return { ...preset, lineHeight: toPx(preset.lineHeight) };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verdade!! Muito melhor assim
utils/PresetsDocBlock/index.tsx
Outdated
{(Object.keys(tokens) as TokensKeys[]).map((tokenKey) => { | ||
if ( | ||
tokens[tokenKey] === preset[key as keyof typeof preset] && | ||
tokenKey | ||
.toLowerCase() | ||
.replace(/_/g, '') | ||
.includes(key.toLowerCase()) | ||
) { | ||
return tokenKey; | ||
} | ||
return null; | ||
})} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gambiarrinha mas funciona haha
utils/PresetsDocBlock/index.tsx
Outdated
<div className="value"> | ||
{(Object.keys(tokens) as TokensKeys[]).map((tokenKey) => { | ||
if ( | ||
tokens[tokenKey] === preset[key as keyof typeof preset] && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Não era melhor já fazer o cast no Object.keys(preset)
, de onde o key
é originalmente definido, em vez de fazer aqui no uso?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quando mando pra cima, em Object.keys(preset)
, ele solta um erro na linha 42: tokens[tokenKey]
.
Um jeito que ele não me traz erro, é fazer o seguinte:
{(Object.keys(preset) as PresetNames[]).map((key) => {
return (
<div className="token" key={key}>
<div className="key">{key}:</div>
<div className="value">
{(Object.keys(tokens)).map((tokenKey) => {
if (
tokens[tokenKey as keyof typeof tokens] === preset[key as keyof typeof preset] &&
tokenKey
.toLowerCase()
.replace(/_/g, '')
.includes(key.toLowerCase())
) {
return tokenKey;
}
return null;
})}
</div>
</div>
);
})}
token-presets/index.ts
Outdated
export interface Preset { | ||
fontFamily: string; | ||
fontSize: number | string; | ||
fontWeight: string; | ||
letterSpacing: number | string; | ||
lineHeight: number | string; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pensando só na necessidade dessa função, talvez a tipagem dela poderia ser algo como
export const toWebPreset = <Preset extends { lineHeight: number }>(preset: Preset): Preset & { lineHeight: string } => {
return { ...preset, lineHeight: toPx(preset.lineHeight) };
};
se não quiser criar o tipo do Preset
.
Contexto
Esse PR cria um documento para documentar os presets de tipografia. E, para exibir os estilos, cria um helper -
toWebPreset
- para converter valores absolutos em pixel, nos casos de uso web-only.Mudanças
Como testar
Verifique a documentação das pré-definições no storybook;
Verifique que é possível usá-las no SGLearner, em casos web only - com o helper novo.