@@ -7,13 +7,12 @@ import {
77 obj ,
88 getThemeValue ,
99 warn ,
10- identity ,
1110 merge ,
1211 assign ,
1312 cascade ,
1413} from '@xstyled/util'
1514import { getBreakpoints , getBreakpointMin , mediaMinWidth } from './media'
16- import { defaultStates } from './defaultStates '
15+ import { getThemeStates } from './states '
1716import {
1817 IProps ,
1918 IStyles ,
@@ -26,10 +25,6 @@ import {
2625 Mixin ,
2726} from './types'
2827
29- const defaultStateKeys = Object . keys (
30- defaultStates ,
31- ) as ( keyof typeof defaultStates ) [ ]
32-
3328const cacheSupported =
3429 typeof Map !== 'undefined' && typeof WeakMap !== 'undefined'
3530
@@ -143,42 +138,43 @@ export function createStyleGenerator(
143138 return generator
144139}
145140
146- function getMedias ( props : IProps ) {
141+ function getStates ( props : IProps ) {
147142 const breakpoints = getBreakpoints ( props )
148143 const medias : { [ key : string ] : string | null } = { }
149144 for ( const breakpoint in breakpoints ) {
150145 medias [ breakpoint ] = mediaMinWidth (
151146 getBreakpointMin ( breakpoints , breakpoint ) ,
152147 )
153148 }
154- return medias
149+ return { ... medias , ... getThemeStates ( props ) }
155150}
156151
157- function getCachedMedias ( props : IProps , cache : ThemeCache ) {
158- if ( cache . has ( '_medias ' ) ) {
159- return cache . get ( '_medias ' )
152+ function getCachedStates ( props : IProps , cache : ThemeCache ) {
153+ if ( cache . has ( '_states ' ) ) {
154+ return cache . get ( '_states ' )
160155 }
161- const medias = getMedias ( props )
162- cache . set ( '_medias ' , medias )
163- return medias
156+ const states = getStates ( props )
157+ cache . set ( '_states ' , states )
158+ return states
164159}
165160
166- export function reduceBreakpoints (
161+ export function reduceStates (
167162 props : IProps ,
168163 values : { [ key : string ] : any } ,
169- getStyle : ( value : any ) => IStyles | null = identity ,
164+ getStyle : ( value : any ) => IStyles | null ,
170165 cache ?: ThemeCache ,
171- ) {
172- const medias = cache ? getCachedMedias ( props , cache ) : getMedias ( props )
166+ ) : IStyles {
167+ const states = cache ? getCachedStates ( props , cache ) : getStates ( props )
173168 let styles : IStyles = { }
174- for ( const breakpoint in values ) {
175- const style = getStyle ( values [ breakpoint ] )
169+ for ( const value in values ) {
170+ const style = getStyle ( values [ value ] )
176171 if ( style === null ) continue
177- const media = medias [ breakpoint ]
178- if ( media === null ) {
172+ const state = states [ value ]
173+ if ( state === undefined ) continue
174+ if ( state === null ) {
179175 styles = merge ( styles , style )
180176 } else {
181- styles [ media ] = styles [ media ] ? assign ( styles [ media ] , style ) : style
177+ styles [ state ] = styles [ state ] ? assign ( styles [ state ] , style ) : style
182178 }
183179 }
184180 return styles
@@ -205,32 +201,18 @@ function getStyleFactory(
205201 themeGet : ThemeGetter ,
206202) : StyleGetter {
207203 return function getStyle ( props : IProps ) {
208- const value = props [ prop ]
209- if ( ! is ( value ) ) return null
210- const cache = getCacheNamespace ( props . theme , prop )
211-
212- if ( obj ( value ) ) {
213- return reduceBreakpoints (
214- props ,
215- value ,
216- ( breakpointValue ) =>
217- styleFromValue ( mixin , breakpointValue , props , themeGet , cache ) ,
218- cache ,
219- )
220- }
204+ const fromValue = ( value : any ) => {
205+ if ( ! is ( value ) ) return null
206+ const cache = getCacheNamespace ( props . theme , prop )
221207
222- return styleFromValue ( mixin , value , props , themeGet , cache )
223- }
224- }
208+ if ( obj ( value ) ) {
209+ return reduceStates ( props , value , fromValue , cache )
210+ }
225211
226- function scopeStyleGetter (
227- selector : string ,
228- getStyle : StyleGetter ,
229- ) : StyleGetter {
230- return ( props : IProps ) => {
231- const result = getStyle ( props )
232- if ( result === null ) return result
233- return { [ selector ] : result }
212+ return styleFromValue ( mixin , value , props , themeGet , cache )
213+ }
214+
215+ return fromValue ( props [ prop ] )
234216 }
235217}
236218
@@ -289,9 +271,9 @@ export function compose(...generators: StyleGenerator[]): StyleGenerator {
289271
290272 if ( ! sort ) return styles
291273
292- const medias = getCachedMedias (
274+ const medias = getCachedStates (
293275 props ,
294- getCacheNamespace ( props . theme , '__medias ' ) ,
276+ getCacheNamespace ( props . theme , '__states ' ) ,
295277 )
296278 return sortStyles ( styles , medias )
297279 }
@@ -329,7 +311,6 @@ export function style({
329311 key,
330312 transform,
331313 themeGet,
332- states = defaultStates ,
333314} : {
334315 prop : string | string [ ]
335316 cssProperty ?: CSSProperty
@@ -355,20 +336,7 @@ export function style({
355336
356337 themeGet = themeGet || themeGetter ( { key, transform } )
357338
358- const capitalizedProp = prop . charAt ( 0 ) . toUpperCase ( ) + prop . slice ( 1 )
359339 const generators : StyleGenerator [ ] = [ ]
360- const stateNames =
361- states === defaultStates ? defaultStateKeys : Object . keys ( states )
362- for ( let i = 0 ; i < stateNames . length ; i ++ ) {
363- const stateName = stateNames [ i ]
364- const stateProp = `${ stateName } ${ capitalizedProp } `
365- const getStyle = scopeStyleGetter (
366- states [ stateName ] ,
367- getStyleFactory ( stateProp , mixin , themeGet ) ,
368- )
369- const generator = createStyleGenerator ( getStyle , [ stateProp ] )
370- generators . push ( generator )
371- }
372340 const getStyle = getStyleFactory ( prop , mixin , themeGet )
373341 const generator = createStyleGenerator ( getStyle , [ prop ] )
374342 generators . push ( generator )
0 commit comments