Skip to content

Commit a2a8ffb

Browse files
committed
refactor(emotion): add new way of using parent tokens
1 parent 0912c48 commit a2a8ffb

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/__docs__/webpack.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const config = merge(baseConfig, {
5959
directory: outputPath,
6060
},
6161
host: '0.0.0.0',
62+
historyApiFallback: true,
6263
client: {
6364
overlay: false,
6465
},

packages/emotion/src/useStyle.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type UseStyleParamsNew<
7474
params?: SecondParameter<P>
7575
componentId: keyof NewComponentTypes
7676
displayName?: string
77+
//in case of a child component needed to use it's parent's tokens, provide parent's name
78+
useTokensFrom?: keyof NewComponentTypes
7779
}
7880

7981
const isNewThemeObject = (obj: BaseThemeOrOverride): obj is Theme => {
@@ -89,6 +91,7 @@ const useStyle = <
8991
| UseStyleParamsNew<P>
9092
): ReturnType<P> => {
9193
const { generateStyle, params, componentId, displayName } = useStyleParams
94+
const useTokensFrom = (useStyleParams as UseStyleParamsNew<P>).useTokensFrom
9295
const generateComponentTheme: GenerateComponentTheme = (
9396
useStyleParams as UseStyleParamsWithTheme<P>
9497
)?.generateComponentTheme
@@ -98,18 +101,21 @@ const useStyle = <
98101
typeof generateComponentTheme === 'function'
99102
? generateComponentTheme(theme as Theme)
100103
: {}
104+
const componentWithTokensId = useTokensFrom ?? componentId
101105

102106
if (
103107
isNewThemeObject(theme) &&
104-
theme.newTheme.components[componentId as keyof NewComponentTypes]
108+
theme.newTheme.components[componentWithTokensId as keyof NewComponentTypes]
105109
) {
106110
baseComponentTheme =
107-
theme.newTheme.components[componentId as keyof NewComponentTypes]
111+
theme.newTheme.components[
112+
componentWithTokensId as keyof NewComponentTypes
113+
]
108114
}
109115
const themeOverride = getComponentThemeOverride(
110116
theme,
111-
displayName ? displayName : componentId || '',
112-
componentId,
117+
useTokensFrom ?? displayName ?? componentId ?? '',
118+
componentWithTokensId,
113119
params,
114120
baseComponentTheme
115121
)

0 commit comments

Comments
 (0)