|
1 | | -// eslint-disable-next-line no-restricted-imports |
2 | | -import {defaultStyles} from '@styles/index'; |
3 | | -// eslint-disable-next-line no-restricted-imports |
4 | | -import {DefaultStyleUtils} from '@styles/utils'; |
| 1 | +import styles from '@src/styles'; |
| 2 | +import {defaultTheme} from '@src/styles/theme'; |
| 3 | +import createStyleUtils from '@src/styles/utils'; |
5 | 4 | import type {ThemeStylesActionsContextType, ThemeStylesStateContextType} from './types'; |
6 | 5 |
|
7 | | -const defaultThemeStylesStateContextValue: ThemeStylesStateContextType = { |
8 | | - styles: defaultStyles, |
9 | | -}; |
| 6 | +// Lazy defaults: defers the expensive styles(defaultTheme) call from module import |
| 7 | +// time to first access. In production, ThemeStylesProvider supplies real values so |
| 8 | +// these are never reached. Tests that render without the provider will trigger lazy |
| 9 | +// initialization on first access. |
| 10 | +let cachedState: ThemeStylesStateContextType | undefined; |
| 11 | +let cachedActions: ThemeStylesActionsContextType | undefined; |
10 | 12 |
|
11 | | -const defaultThemeStylesActionsContextValue: ThemeStylesActionsContextType = { |
12 | | - StyleUtils: DefaultStyleUtils, |
13 | | -}; |
| 13 | +const defaultThemeStylesStateContextValue = new Proxy({} as ThemeStylesStateContextType, { |
| 14 | + get(_, prop: keyof ThemeStylesStateContextType) { |
| 15 | + if (!cachedState) { |
| 16 | + cachedState = {styles: styles(defaultTheme)}; |
| 17 | + } |
| 18 | + return cachedState[prop]; |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +const defaultThemeStylesActionsContextValue = new Proxy({} as ThemeStylesActionsContextType, { |
| 23 | + get(_, prop: keyof ThemeStylesActionsContextType) { |
| 24 | + if (!cachedActions) { |
| 25 | + if (!cachedState) { |
| 26 | + cachedState = {styles: styles(defaultTheme)}; |
| 27 | + } |
| 28 | + cachedActions = {StyleUtils: createStyleUtils(defaultTheme, cachedState.styles)}; |
| 29 | + } |
| 30 | + return cachedActions[prop]; |
| 31 | + }, |
| 32 | +}); |
14 | 33 |
|
15 | 34 | export {defaultThemeStylesStateContextValue, defaultThemeStylesActionsContextValue}; |
0 commit comments