Skip to content
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

Merged
merged 10 commits into from
Aug 25, 2023

Conversation

leocpadua7
Copy link
Contributor

@leocpadua7 leocpadua7 commented Aug 24, 2023

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

Captura de Tela 2023-08-24 às 15 03 03
Captura de Tela 2023-08-24 às 15 03 09
Captura de Tela 2023-08-24 às 15 03 15

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.

@leocpadua7 leocpadua7 changed the title [WIP] Documenta presets e adiciona helper para transformar presets para web Documenta presets e adiciona helper para transformar presets para web Aug 24, 2023
Comment on lines 5 to 11
export interface Preset {
fontFamily: string;
fontSize: number | string;
fontWeight: string;
letterSpacing: number | string;
lineHeight: number | string;
}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Comment on lines 16 to 22
(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);
}
});
Copy link
Contributor

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) };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdade!! Muito melhor assim

Comment on lines 40 to 51
{(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;
})}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gambiarrinha mas funciona haha

<div className="value">
{(Object.keys(tokens) as TokensKeys[]).map((tokenKey) => {
if (
tokens[tokenKey] === preset[key as keyof typeof preset] &&
Copy link
Contributor

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?

Copy link
Contributor Author

@leocpadua7 leocpadua7 Aug 25, 2023

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>
          );
        })}

utils/PresetsDocBlock/index.tsx Outdated Show resolved Hide resolved
utils/PresetsDocBlock/index.tsx Outdated Show resolved Hide resolved
utils/PresetsDocBlock/index.tsx Outdated Show resolved Hide resolved
utils/PresetsDocBlock/index.tsx Outdated Show resolved Hide resolved
Comment on lines 5 to 11
export interface Preset {
fontFamily: string;
fontSize: number | string;
fontWeight: string;
letterSpacing: number | string;
lineHeight: number | string;
}
Copy link
Contributor

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 Show resolved Hide resolved
utils/PresetsDocBlock/index.tsx Outdated Show resolved Hide resolved
@leocpadua7 leocpadua7 merged commit 1a90216 into master Aug 25, 2023
1 check passed
@leocpadua7 leocpadua7 deleted the presets-docs branch August 25, 2023 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants