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

feat(Typography): Support fontWeightBase tokens #7549

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/vkui/src/components/Typography/Typography.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,35 @@

/* Мы утяжеляем селектор, чтобы перебить другие селекторы */
.Typography--weight-1.Typography--weight-1.Typography--weight-1 {
font-weight: var(--vkui--font_weight_base1);
}

.Typography--accent.Typography--weight-1.Typography--weight-1.Typography--weight-1 {
font-weight: var(--vkui--font_weight_accent1);
}

.Typography--weight-2.Typography--weight-2.Typography--weight-2 {
font-weight: var(--vkui--font_weight_base2);
}

.Typography--accent.Typography--weight-2.Typography--weight-2.Typography--weight-2 {
font-weight: var(--vkui--font_weight_accent2);
}

.Typography--weight-3.Typography--weight-3.Typography--weight-3 {
font-weight: var(--vkui--font_weight_base3);
}

.Typography--accent.Typography--weight-3.Typography--weight-3.Typography--weight-3 {
font-weight: var(--vkui--font_weight_accent3);
}

/* stylelint-disable-next-line selector-max-type */
.Typography b {
font-weight: var(--vkui--font_weight_base1);
}

/* stylelint-disable-next-line selector-max-type */
.Typography--accent.Typography b {
font-weight: var(--vkui--font_weight_accent1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Playground as TitleStory } from './Title/Title.stories';

interface TypographyOverview {
weight: '1' | '2' | '3';
accent: true;
}

const story: Meta<TypographyOverview> = {
Expand All @@ -29,6 +30,7 @@ const story: Meta<TypographyOverview> = {
control: 'inline-radio',
options: ['1', '2', '3'],
},
accent: { control: 'boolean' },
},
decorators: [withCartesian],
};
Expand Down
64 changes: 63 additions & 1 deletion packages/vkui/src/components/Typography/Typography.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
import { render, screen } from '@testing-library/react';
import { baselineComponent } from '../../testing/utils';
import { Typography } from './Typography';
import { Typography, type TypographyProps } from './Typography';
import styles from './Typography.module.css';
import rootComponentStyles from '../RootComponent/RootComponent.module.css';

describe('Typography', () => {
baselineComponent(Typography);

it.each<{
props: Partial<TypographyProps>;
hasClassName: string;
}>([
{
props: {},
hasClassName: '', // по умолчанию Typography
},
{
props: { weight: '1' },
hasClassName: `${styles['Typography--weight-1']} ${styles['Typography--accent']}`,
},
{
props: { weight: '2' },
hasClassName: `${styles['Typography--weight-2']} ${styles['Typography--accent']}`,
},
{
props: { weight: '3' },
hasClassName: `${styles['Typography--weight-3']} ${styles['Typography--accent']}`,
},
{
props: { weight: '1', useAccentWeight: true },
hasClassName: `${styles['Typography--weight-1']} ${styles['Typography--accent']}`,
},
{
props: { weight: '2', useAccentWeight: true },
hasClassName: `${styles['Typography--weight-2']} ${styles['Typography--accent']}`,
},
{
props: { weight: '3', useAccentWeight: true },
hasClassName: `${styles['Typography--weight-3']} ${styles['Typography--accent']}`,
},
{
props: { weight: '1', useAccentWeight: false },
hasClassName: `${styles['Typography--weight-1']}`,
},
{
props: { weight: '2', useAccentWeight: false },
hasClassName: `${styles['Typography--weight-2']}`,
},
{
props: { weight: '3', useAccentWeight: false },
hasClassName: `${styles['Typography--weight-3']}`,
},
{
props: { normalize: true, inline: true },
hasClassName: `${styles['Typography--normalize']} ${styles['Typography--inline']}`,
},
])('it should have className $hasClassName with props $props', ({ props, hasClassName }) => {
render(<Typography {...props}>Test</Typography>);

expect(screen.getByText('Test')).toHaveClass(
`${styles['Typography']} ${hasClassName} ${rootComponentStyles['RootComponent']}`,
{
exact: true,
},
);
});
});
10 changes: 10 additions & 0 deletions packages/vkui/src/components/Typography/Typography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export interface TypographyProps
* Задаёт начертание шрифта, отличное от стандартного.
*/
weight?: '1' | '2' | '3';
/**
* Включает акцентный тип начертания шрифта.
* Используются токены fontWeightAccent[1, 2, 3]
* Используется только вместе с `weight`
*/
useAccentWeight?: boolean;
/**
* Убирает внешние отступы
*/
Expand All @@ -34,6 +40,9 @@ export interface TypographyProps

export const Typography = ({
weight,

// TODO [>=7]: сделать по умолчанию false (нужен будет кодмод)
useAccentWeight = true,
Component = 'span',
normalize,
inline,
Expand All @@ -46,6 +55,7 @@ export const Typography = ({
normalize && styles['Typography--normalize'],
inline && styles['Typography--inline'],
weight && stylesWeight[weight],
weight && useAccentWeight && styles['Typography--accent'],
)}
{...restProps}
/>
Expand Down
Loading