Skip to content

Commit

Permalink
feat: make the injection of theme CSS vars optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jeangovil committed Jun 11, 2023
1 parent 9d2253b commit 9a56d59
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 72 deletions.
4 changes: 3 additions & 1 deletion packages/lsd-react/src/components/Theme/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 5 additions & 1 deletion packages/lsd-react/src/components/Theme/baseTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion packages/lsd-react/src/components/Theme/createTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 16 additions & 9 deletions packages/lsd-react/src/components/Theme/globalStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
THEME_TYPOGRAPHY_VARIANTS,
} from './constants'
import { Theme, TypographyProperties, TypographyVariants } from './types'
import { withTheme } from './withTheme'

const cssUtils = {
vars: {
Expand All @@ -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(() => [])
Expand Down Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions packages/lsd-react/src/components/Theme/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type Theme = {
typographyGlobal: GlobalTypographyStyles
palette: ThemePalette
globalStyles: SerializedStyles
cssVars: string
}

export type ThemeOptionBreakpointStyles = Partial<
Expand Down
114 changes: 54 additions & 60 deletions packages/lsd-react/src/components/Typography/Typography.styles.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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.serif} {
&,
* {
font-family: serif;
}
}
.${typographyClasses.monospace} {
&,
* {
font-family: monospace;
}
.${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);
}
`,
)}
${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;
}
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;
}
`

0 comments on commit 9a56d59

Please sign in to comment.