feat: make the injection of theme CSS vars optional

This commit is contained in:
Hossein Mehrabi 2023-06-12 02:31:39 +03:30
parent 9d2253bd2b
commit 9a56d59c35
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
6 changed files with 89 additions and 77 deletions

View File

@ -7,18 +7,20 @@ import { Theme } from './types'
export type ThemeProviderProps = React.PropsWithChildren<{
theme: Theme
injectCssVars?: boolean
}>
export const ThemeProvider: React.FC<ThemeProviderProps> = ({
theme,
children,
injectCssVars = true,
}) => {
return (
<ResizeObserverProvider>
<PortalProvider>
<ThemeContext.Provider value={{ theme }}>
<CSSBaseline theme={theme} />
<Global styles={theme.globalStyles} />
{injectCssVars && <Global styles={theme.globalStyles} />}
<EmotionThemeProvider theme={theme}>{children}</EmotionThemeProvider>
</ThemeContext.Provider>
</PortalProvider>

View File

@ -148,6 +148,10 @@ export const baseTheme: Theme = {
},
},
globalStyles: css``,
cssVars: '',
}
baseTheme.globalStyles = createThemeGlobalStyles(baseTheme)
const { cssVars, globalStyles } = createThemeGlobalStyles(baseTheme)
baseTheme.cssVars = cssVars
baseTheme.globalStyles = globalStyles

View File

@ -136,9 +136,13 @@ export const createTheme = (
breakpoints: createBreakpointStyles(props, from),
palette: createPaletteStyles(props, from),
globalStyles: css``,
cssVars: '',
}
theme.globalStyles = createThemeGlobalStyles(theme)
const { cssVars, globalStyles } = createThemeGlobalStyles(theme)
theme.cssVars = cssVars
theme.globalStyles = globalStyles
return theme
}

View File

@ -5,7 +5,6 @@ import {
THEME_TYPOGRAPHY_VARIANTS,
} from './constants'
import { Theme, TypographyProperties, TypographyVariants } from './types'
import { withTheme } from './withTheme'
const cssUtils = {
vars: {
@ -22,7 +21,7 @@ const cssUtils = {
define: (name: string, value: string) => `${name}: ${value};`,
}
const generateThemeGlobalStyles = withTheme((theme) => {
const generateThemeGlobalStyles = (theme: Theme) => {
const vars: Array<string | string[]> = []
const styles: Array<string | string[]> = []
const breakpointStyles: string[][] = THEME_BREAKPOINTS.map(() => [])
@ -98,18 +97,26 @@ const generateThemeGlobalStyles = withTheme((theme) => {
}`)
})
return css`
:root {
${vars.join('\n')}
}
const cssVars = `
${vars.join('\n')}
${styles.join('\n')}
`
})
return {
cssVars,
globalStyles: css`
:root {
${cssVars}
}
`,
}
}
export const createThemeGlobalStyles = (() => {
return (theme: Theme) => {
const cache: any = {}
const cache: any = {}
return (theme: Theme): ReturnType<typeof generateThemeGlobalStyles> => {
const key = theme as any as string
if (

View File

@ -73,6 +73,7 @@ export type Theme = {
typographyGlobal: GlobalTypographyStyles
palette: ThemePalette
globalStyles: SerializedStyles
cssVars: string
}
export type ThemeOptionBreakpointStyles = Partial<

View File

@ -1,9 +1,5 @@
import { css } from '@emotion/react'
import {
THEME_TYPOGRAPHY_VARIANTS,
TypographyVariants,
withTheme,
} from '../Theme'
import { THEME_TYPOGRAPHY_VARIANTS, TypographyVariants } from '../Theme'
import { THEME_TYPOGRAPHY_ELEMENTS } from '../Theme/constants'
import { typographyClasses } from './Typography.classes'
@ -13,70 +9,68 @@ const selectors = (variant: TypographyVariants) =>
`.${typographyClasses[variant]}`,
].join(', ')
export const TypographyStyles = withTheme(
(theme) => css`
body * {
font-family: var(--lsd-typography-generic-font-family);
}
export const TypographyStyles = css`
body * {
font-family: var(--lsd-typography-generic-font-family);
}
.${typographyClasses.root} {
}
.${typographyClasses.root} {
}
.${typographyClasses.primary} {
color: rgb(var(--lsd-text-primary));
}
.${typographyClasses.primary} {
color: rgb(var(--lsd-text-primary));
}
.${typographyClasses.secondary} {
color: rgb(var(--lsd-text-secondary));
}
.${typographyClasses.secondary} {
color: rgb(var(--lsd-text-secondary));
}
.${typographyClasses.sansSerif} {
&,
* {
font-family: sans-serif;
.${typographyClasses.sansSerif} {
&,
* {
font-family: sans-serif;
}
}
.${typographyClasses.serif} {
&,
* {
font-family: serif;
}
}
.${typographyClasses.monospace} {
&,
* {
font-family: monospace;
}
}
${THEME_TYPOGRAPHY_VARIANTS.map(
(variant) => css`
${selectors(variant)} {
color: rgb(var(--lsd-text-primary));
font-weight: normal;
font-size: var(--lsd-${variant}-fontSize);
line-height: var(--lsd-${variant}-lineHeight);
}
}
`,
)}
.${typographyClasses.serif} {
&,
* {
font-family: serif;
}
}
input {
color: rgb(var(--lsd-text-primary));
font-size: var(--lsd-body1-fontSize);
font-weight: normal;
}
.${typographyClasses.monospace} {
&,
* {
font-family: monospace;
}
}
${THEME_TYPOGRAPHY_VARIANTS.map(
(variant) => css`
${selectors(variant)} {
color: rgb(var(--lsd-text-primary));
font-weight: normal;
font-size: var(--lsd-${variant}-fontSize);
line-height: var(--lsd-${variant}-lineHeight);
}
`,
)}
input {
color: rgb(var(--lsd-text-primary));
font-size: var(--lsd-body1-fontSize);
font-weight: normal;
}
h1,
h2,
h3,
h4,
h5,
h6,
p,
span {
margin: 0;
}
`,
)
h1,
h2,
h3,
h4,
h5,
h6,
p,
span {
margin: 0;
}
`