Merge branch 'topic-font-family' into topic-implement-date-picker

This commit is contained in:
Hossein Mehrabi 2023-03-21 03:21:01 +03:30
commit af277559d8
9 changed files with 99 additions and 13 deletions

View File

@ -1,5 +1,9 @@
import { DecoratorFunction, Parameters } from '@storybook/addons' import { DecoratorFunction, Parameters } from '@storybook/addons'
import { defaultThemes } from '../src' import {
storybookDefaultTheme,
storybookDefaultThemeKey,
themes,
} from './themes'
import { withTheme } from './withTheme.decorator' import { withTheme } from './withTheme.decorator'
export const parameters: Parameters = { export const parameters: Parameters = {
@ -12,7 +16,7 @@ export const parameters: Parameters = {
}, },
backgrounds: { backgrounds: {
default: 'light', default: 'light',
values: Object.entries(defaultThemes).map(([name, theme]) => ({ values: Object.entries(themes).map(([name, theme]) => ({
name, name,
value: `rgb(${theme.palette.secondary})`, value: `rgb(${theme.palette.secondary})`,
})), })),
@ -20,7 +24,7 @@ export const parameters: Parameters = {
viewport: { viewport: {
viewports: { viewports: {
...Object.fromEntries( ...Object.fromEntries(
Object.entries(defaultThemes.light.breakpoints).map( Object.entries(storybookDefaultTheme.breakpoints).map(
([key, { width }]) => [ ([key, { width }]) => [
key, key,
{ {
@ -44,13 +48,14 @@ export const globalTypes = {
theme: { theme: {
name: 'Theme', name: 'Theme',
description: 'Theme', description: 'Theme',
defaultValue: 'light', defaultValue: storybookDefaultThemeKey,
toolbar: { toolbar: {
icon: 'circlehollow', icon: 'circlehollow',
items: [ items: Object.entries(themes).map(([name, theme]) => ({
{ value: 'light', icon: 'circlehollow', title: 'light' }, value: name,
{ value: 'dark', icon: 'circle', title: 'dark' }, icon: name.startsWith('Light') ? 'circlehollow' : 'circle',
], title: name,
})),
}, },
}, },
} }

View File

@ -0,0 +1,44 @@
import { createTheme, CreateThemeProps, defaultThemes } from '../src'
const themeProps: CreateThemeProps = {
typography: {
body3: {
fontFamily: 'sans-serif',
},
},
breakpoints: {},
palette: {},
typographyGlobal: {},
}
const typefaceTypes: Record<string, CreateThemeProps['typographyGlobal']> = {
'sans-serif': {
fontFamily: 'Helvetica, sans-serif',
},
serif: {
fontFamily: 'Georgia, serif',
},
monospace: {
fontFamily: 'Courier, monospace',
},
}
export const themesArray = Object.keys(defaultThemes).flatMap((key) =>
Object.entries(typefaceTypes).map(([typeFace, typographyGlobal]) =>
createTheme(
{
name: `${defaultThemes[key].name} (${typeFace})`,
...themeProps,
typographyGlobal,
},
defaultThemes[key],
),
),
)
export const themes = Object.fromEntries(
themesArray.map((theme) => [theme.name, theme]),
)
export const storybookDefaultThemeKey = themesArray[0].name
export const storybookDefaultTheme = themesArray[0]

View File

@ -1,12 +1,13 @@
import { DecoratorFunction, useGlobals } from '@storybook/addons' import { DecoratorFunction, useGlobals } from '@storybook/addons'
import React, { useEffect } from 'react' import React, { useEffect } from 'react'
import { defaultThemes, Theme, ThemeProvider } from '../src' import { Theme, ThemeProvider } from '../src'
import { storybookDefaultThemeKey, themes } from './themes'
export const withTheme: DecoratorFunction = (Story, context) => { export const withTheme: DecoratorFunction = (Story, context) => {
const StoryComponent = Story as any as React.ComponentType const StoryComponent = Story as any as React.ComponentType
const themeName = context.globals?.theme ?? 'light' const themeName = context.globals?.theme ?? storybookDefaultThemeKey
const theme = defaultThemes[themeName] as Theme const theme = themes[themeName] as Theme
const [globals, setGlobals] = useGlobals() const [globals, setGlobals] = useGlobals()

View File

@ -3,6 +3,7 @@ import { createThemeGlobalStyles } from './globalStyles'
import { Theme } from './types' import { Theme } from './types'
export const baseTheme: Theme = { export const baseTheme: Theme = {
name: 'LSD',
breakpoints: { breakpoints: {
xs: { xs: {
width: 0, width: 0,
@ -122,6 +123,7 @@ export const baseTheme: Theme = {
label1: { fontSize: '0.875rem', lineHeight: '1.25rem' }, label1: { fontSize: '0.875rem', lineHeight: '1.25rem' },
label2: { fontSize: '0.75rem', lineHeight: '1rem' }, label2: { fontSize: '0.75rem', lineHeight: '1rem' },
}, },
typographyGlobal: {},
palette: { palette: {
primary: '0, 0, 0', primary: '0, 0, 0',
secondary: '255, 255, 255', secondary: '255, 255, 255',

View File

@ -42,6 +42,7 @@ export const THEME_TYPOGRAPHY_ELEMENTS: Partial<
export const THEME_TYPOGRAPHY_PROPERTIES = [ export const THEME_TYPOGRAPHY_PROPERTIES = [
'fontSize', 'fontSize',
'fontFamily',
'lineHeight', 'lineHeight',
] as TypographyProperties[] ] as TypographyProperties[]

View File

@ -15,6 +15,8 @@ import type {
const createTypographyStyles = (theme: CreateThemeProps, defaultTheme: Theme) => const createTypographyStyles = (theme: CreateThemeProps, defaultTheme: Theme) =>
pairs<TypographyVariants>(THEME_TYPOGRAPHY_VARIANTS, (variant) => ({ pairs<TypographyVariants>(THEME_TYPOGRAPHY_VARIANTS, (variant) => ({
...defaultTheme.typographyGlobal,
...theme.typographyGlobal,
...defaultTheme.typography[variant], ...defaultTheme.typography[variant],
...(theme.typography[variant] ?? {}), ...(theme.typography[variant] ?? {}),
})) }))
@ -105,7 +107,9 @@ export const createTheme = (
from = baseTheme, from = baseTheme,
): Theme => { ): Theme => {
const theme: Theme = { const theme: Theme = {
name: props.name ?? from.name,
typography: createTypographyStyles(props, from), typography: createTypographyStyles(props, from),
typographyGlobal: { ...from.typographyGlobal, ...props.typographyGlobal },
breakpoints: createBreakpointStyles(props, from), breakpoints: createBreakpointStyles(props, from),
palette: createPaletteStyles(props, from), palette: createPaletteStyles(props, from),
globalStyles: css``, globalStyles: css``,

View File

@ -3,8 +3,10 @@ import { createTheme } from './createTheme'
const lightTheme = createTheme( const lightTheme = createTheme(
{ {
name: 'Light',
breakpoints: {}, breakpoints: {},
typography: {}, typography: {},
typographyGlobal: {},
palette: {}, palette: {},
}, },
baseTheme, baseTheme,
@ -12,8 +14,10 @@ const lightTheme = createTheme(
const darkTheme = createTheme( const darkTheme = createTheme(
{ {
name: 'Dark',
breakpoints: {}, breakpoints: {},
typography: {}, typography: {},
typographyGlobal: {},
palette: { palette: {
primary: '255, 255, 255', primary: '255, 255, 255',
secondary: '0, 0, 0', secondary: '0, 0, 0',

View File

@ -23,7 +23,13 @@ export type TypographyVariants =
export type VariantThemeProperties = keyof Pick<Theme, 'typography'> export type VariantThemeProperties = keyof Pick<Theme, 'typography'>
export type TypographyStyles = Pick<CSSProperties, 'fontSize' | 'lineHeight'> export type TypographyStyles = Pick<
CSSProperties,
'fontSize' | 'fontFamily' | 'lineHeight'
> & { fontFamily?: string }
export type GlobalTypographyStyles = {
fontFamily?: string
}
export type TypographyProperties = keyof TypographyStyles export type TypographyProperties = keyof TypographyStyles
export type ThemeTypography<T extends string = TypographyVariants> = { export type ThemeTypography<T extends string = TypographyVariants> = {
[key in T]: TypographyStyles [key in T]: TypographyStyles
@ -61,8 +67,10 @@ export type ThemeBreakpoints = {
} }
export type Theme = { export type Theme = {
name: string
breakpoints: ThemeBreakpoints breakpoints: ThemeBreakpoints
typography: ThemeTypography typography: ThemeTypography
typographyGlobal: GlobalTypographyStyles
palette: ThemePalette palette: ThemePalette
globalStyles: SerializedStyles globalStyles: SerializedStyles
} }
@ -83,8 +91,10 @@ export type ThemeOptionTypography = DeepPartial<ThemeTypography>
export type ThemeOptionPalette = DeepPartial<ThemePalette> export type ThemeOptionPalette = DeepPartial<ThemePalette>
export type CreateThemeProps = { export type CreateThemeProps = {
name?: string
breakpoints: ThemeOptionBreakpoints breakpoints: ThemeOptionBreakpoints
typography: ThemeOptionTypography typography: ThemeOptionTypography
typographyGlobal: GlobalTypographyStyles
palette: ThemeOptionPalette palette: ThemeOptionPalette
} }

View File

@ -32,12 +32,27 @@ export const TypographyStyles = withTheme(
color: rgb(var(--lsd-text-primary)); color: rgb(var(--lsd-text-primary));
font-weight: normal; font-weight: normal;
font-size: var(--lsd-${variant}-fontSize); font-size: var(--lsd-${variant}-fontSize);
font-family: var(--lsd-${variant}-fontFamily);
line-height: var(--lsd-${variant}-lineHeight); line-height: var(--lsd-${variant}-lineHeight);
} }
`, `,
)} )}
h1, h2, h3, h4, h5, h6, p, span { input {
color: rgb(var(--lsd-text-primary));
font-weight: normal;
font-size: var(--lsd-body1-fontSize);
font-family: var(--lsd-body1-fontFamily);
}
h1,
h2,
h3,
h4,
h5,
h6,
p,
span {
margin: 0; margin: 0;
} }
`, `,