mirror of https://github.com/acid-info/lsd.git
feat: make the injection of theme CSS vars optional
This commit is contained in:
parent
9d2253bd2b
commit
9a56d59c35
|
@ -7,18 +7,20 @@ import { Theme } from './types'
|
||||||
|
|
||||||
export type ThemeProviderProps = React.PropsWithChildren<{
|
export type ThemeProviderProps = React.PropsWithChildren<{
|
||||||
theme: Theme
|
theme: Theme
|
||||||
|
injectCssVars?: boolean
|
||||||
}>
|
}>
|
||||||
|
|
||||||
export const ThemeProvider: React.FC<ThemeProviderProps> = ({
|
export const ThemeProvider: React.FC<ThemeProviderProps> = ({
|
||||||
theme,
|
theme,
|
||||||
children,
|
children,
|
||||||
|
injectCssVars = true,
|
||||||
}) => {
|
}) => {
|
||||||
return (
|
return (
|
||||||
<ResizeObserverProvider>
|
<ResizeObserverProvider>
|
||||||
<PortalProvider>
|
<PortalProvider>
|
||||||
<ThemeContext.Provider value={{ theme }}>
|
<ThemeContext.Provider value={{ theme }}>
|
||||||
<CSSBaseline theme={theme} />
|
<CSSBaseline theme={theme} />
|
||||||
<Global styles={theme.globalStyles} />
|
{injectCssVars && <Global styles={theme.globalStyles} />}
|
||||||
<EmotionThemeProvider theme={theme}>{children}</EmotionThemeProvider>
|
<EmotionThemeProvider theme={theme}>{children}</EmotionThemeProvider>
|
||||||
</ThemeContext.Provider>
|
</ThemeContext.Provider>
|
||||||
</PortalProvider>
|
</PortalProvider>
|
||||||
|
|
|
@ -148,6 +148,10 @@ export const baseTheme: Theme = {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
globalStyles: css``,
|
globalStyles: css``,
|
||||||
|
cssVars: '',
|
||||||
}
|
}
|
||||||
|
|
||||||
baseTheme.globalStyles = createThemeGlobalStyles(baseTheme)
|
const { cssVars, globalStyles } = createThemeGlobalStyles(baseTheme)
|
||||||
|
|
||||||
|
baseTheme.cssVars = cssVars
|
||||||
|
baseTheme.globalStyles = globalStyles
|
||||||
|
|
|
@ -136,9 +136,13 @@ export const createTheme = (
|
||||||
breakpoints: createBreakpointStyles(props, from),
|
breakpoints: createBreakpointStyles(props, from),
|
||||||
palette: createPaletteStyles(props, from),
|
palette: createPaletteStyles(props, from),
|
||||||
globalStyles: css``,
|
globalStyles: css``,
|
||||||
|
cssVars: '',
|
||||||
}
|
}
|
||||||
|
|
||||||
theme.globalStyles = createThemeGlobalStyles(theme)
|
const { cssVars, globalStyles } = createThemeGlobalStyles(theme)
|
||||||
|
|
||||||
|
theme.cssVars = cssVars
|
||||||
|
theme.globalStyles = globalStyles
|
||||||
|
|
||||||
return theme
|
return theme
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import {
|
||||||
THEME_TYPOGRAPHY_VARIANTS,
|
THEME_TYPOGRAPHY_VARIANTS,
|
||||||
} from './constants'
|
} from './constants'
|
||||||
import { Theme, TypographyProperties, TypographyVariants } from './types'
|
import { Theme, TypographyProperties, TypographyVariants } from './types'
|
||||||
import { withTheme } from './withTheme'
|
|
||||||
|
|
||||||
const cssUtils = {
|
const cssUtils = {
|
||||||
vars: {
|
vars: {
|
||||||
|
@ -22,7 +21,7 @@ const cssUtils = {
|
||||||
define: (name: string, value: string) => `${name}: ${value};`,
|
define: (name: string, value: string) => `${name}: ${value};`,
|
||||||
}
|
}
|
||||||
|
|
||||||
const generateThemeGlobalStyles = withTheme((theme) => {
|
const generateThemeGlobalStyles = (theme: Theme) => {
|
||||||
const vars: Array<string | string[]> = []
|
const vars: Array<string | string[]> = []
|
||||||
const styles: Array<string | string[]> = []
|
const styles: Array<string | string[]> = []
|
||||||
const breakpointStyles: string[][] = THEME_BREAKPOINTS.map(() => [])
|
const breakpointStyles: string[][] = THEME_BREAKPOINTS.map(() => [])
|
||||||
|
@ -98,18 +97,26 @@ const generateThemeGlobalStyles = withTheme((theme) => {
|
||||||
}`)
|
}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
return css`
|
const cssVars = `
|
||||||
:root {
|
${vars.join('\n')}
|
||||||
${vars.join('\n')}
|
|
||||||
}
|
|
||||||
|
|
||||||
${styles.join('\n')}
|
${styles.join('\n')}
|
||||||
`
|
`
|
||||||
})
|
|
||||||
|
return {
|
||||||
|
cssVars,
|
||||||
|
globalStyles: css`
|
||||||
|
:root {
|
||||||
|
${cssVars}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const createThemeGlobalStyles = (() => {
|
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
|
const key = theme as any as string
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -73,6 +73,7 @@ export type Theme = {
|
||||||
typographyGlobal: GlobalTypographyStyles
|
typographyGlobal: GlobalTypographyStyles
|
||||||
palette: ThemePalette
|
palette: ThemePalette
|
||||||
globalStyles: SerializedStyles
|
globalStyles: SerializedStyles
|
||||||
|
cssVars: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ThemeOptionBreakpointStyles = Partial<
|
export type ThemeOptionBreakpointStyles = Partial<
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import { css } from '@emotion/react'
|
import { css } from '@emotion/react'
|
||||||
import {
|
import { THEME_TYPOGRAPHY_VARIANTS, TypographyVariants } from '../Theme'
|
||||||
THEME_TYPOGRAPHY_VARIANTS,
|
|
||||||
TypographyVariants,
|
|
||||||
withTheme,
|
|
||||||
} from '../Theme'
|
|
||||||
import { THEME_TYPOGRAPHY_ELEMENTS } from '../Theme/constants'
|
import { THEME_TYPOGRAPHY_ELEMENTS } from '../Theme/constants'
|
||||||
import { typographyClasses } from './Typography.classes'
|
import { typographyClasses } from './Typography.classes'
|
||||||
|
|
||||||
|
@ -13,70 +9,68 @@ const selectors = (variant: TypographyVariants) =>
|
||||||
`.${typographyClasses[variant]}`,
|
`.${typographyClasses[variant]}`,
|
||||||
].join(', ')
|
].join(', ')
|
||||||
|
|
||||||
export const TypographyStyles = withTheme(
|
export const TypographyStyles = css`
|
||||||
(theme) => css`
|
body * {
|
||||||
body * {
|
font-family: var(--lsd-typography-generic-font-family);
|
||||||
font-family: var(--lsd-typography-generic-font-family);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.${typographyClasses.root} {
|
.${typographyClasses.root} {
|
||||||
}
|
}
|
||||||
|
|
||||||
.${typographyClasses.primary} {
|
.${typographyClasses.primary} {
|
||||||
color: rgb(var(--lsd-text-primary));
|
color: rgb(var(--lsd-text-primary));
|
||||||
}
|
}
|
||||||
|
|
||||||
.${typographyClasses.secondary} {
|
.${typographyClasses.secondary} {
|
||||||
color: rgb(var(--lsd-text-secondary));
|
color: rgb(var(--lsd-text-secondary));
|
||||||
}
|
}
|
||||||
|
|
||||||
.${typographyClasses.sansSerif} {
|
.${typographyClasses.sansSerif} {
|
||||||
&,
|
&,
|
||||||
* {
|
* {
|
||||||
font-family: sans-serif;
|
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} {
|
input {
|
||||||
&,
|
color: rgb(var(--lsd-text-primary));
|
||||||
* {
|
font-size: var(--lsd-body1-fontSize);
|
||||||
font-family: serif;
|
font-weight: normal;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.${typographyClasses.monospace} {
|
h1,
|
||||||
&,
|
h2,
|
||||||
* {
|
h3,
|
||||||
font-family: monospace;
|
h4,
|
||||||
}
|
h5,
|
||||||
}
|
h6,
|
||||||
|
p,
|
||||||
${THEME_TYPOGRAPHY_VARIANTS.map(
|
span {
|
||||||
(variant) => css`
|
margin: 0;
|
||||||
${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;
|
|
||||||
}
|
|
||||||
`,
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue