Skip to content

Commit 66ac294

Browse files
authored
feat(style): improve mixin (#225)
1 parent 1c35ef6 commit 66ac294

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

packages/system/src/style.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,26 @@ describe('#style', () => {
175175
fontFamily: 'arial',
176176
})
177177
})
178+
179+
describe('mixins', () => {
180+
const fontSize = style({
181+
prop: 'fontSize',
182+
key: 'fontSizes',
183+
})
184+
const text = style({
185+
prop: 'text',
186+
key: 'texts',
187+
cssProperty: (value) => fontSize.apply({ fontSize: value }),
188+
})
189+
190+
it('supports functions', () => {
191+
const theme = {
192+
fontSizes: { xs: '0.8rem' },
193+
texts: { xs: 2 },
194+
}
195+
196+
expect(text({ theme, text: 'xs' })).toEqual({ fontSize: 2 })
197+
})
198+
})
178199
})
179200
})

packages/system/src/style.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
identity,
1111
merge,
1212
assign,
13+
cascade,
1314
} from '@xstyled/util'
1415
import { getBreakpoints, getBreakpointMin, mediaMinWidth } from './media'
1516
import { defaultStates } from './defaultStates'
@@ -193,7 +194,7 @@ function styleFromValue(
193194
if (obj(value)) return null
194195
if (cache.has(value)) return cache.get(value)
195196
const computedValue = themeGet(value)(props)
196-
const style = mixin(computedValue)
197+
const style = cascade(mixin(computedValue), props)
197198
cache.set(value, style)
198199
return style
199200
}

packages/system/src/styles/backgrounds.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface BackgroundProps<T extends ITheme = Theme> {
3636
export const background = style({
3737
prop: 'background',
3838
cssProperty: (value) => ({
39-
background: gradientBackgrounds[value] || value,
39+
background: gradientBackgrounds[value as string] || value,
4040
}),
4141
})
4242

@@ -112,7 +112,7 @@ export interface BackgroundImageProps<T extends ITheme = Theme> {
112112
export const backgroundImage = style({
113113
prop: 'backgroundImage',
114114
cssProperty: (value) => ({
115-
backgroundImage: gradientBackgrounds[value] || value,
115+
backgroundImage: gradientBackgrounds[value as string] || value,
116116
}),
117117
})
118118

packages/system/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ export type IStyles = Record<string, unknown>
3838
export type IBreakpoints = Record<string | number, number>
3939
export type IPropsWithTheme<TTheme extends ITheme> = IProps & { theme: TTheme }
4040

41-
export type Mixin = (value: any) => IStyles | null | undefined
42-
4341
export interface StyleGetter {
4442
(props: IProps): any
4543
}
4644

45+
export type Mixin = (value: unknown) => IStyles | null | undefined | StyleGetter
46+
4747
export type Breakpoints<TTheme extends ITheme> = TTheme extends {
4848
breakpoints: IBreakpoints
4949
}

0 commit comments

Comments
 (0)