@@ -5,26 +5,12 @@ import { string } from '@xstyled/util'
55import { StyleGenerator , StyleGeneratorProps , Theme } from '@xstyled/system'
66import {
77 StyledConfig ,
8+ StyledInterface ,
89 ThemedBaseStyledInterface ,
910 ThemedStyledFunction ,
1011} from 'styled-components'
11- import { scStyled } from './scStyled'
1212import { createCssFunction , XCSSFunction } from './createCssFunction'
13-
14- const getCreateStyle = (
15- baseCreateStyle : ThemedStyledFunction < any , any > ,
16- css : XCSSFunction ,
17- generator ?: StyleGenerator ,
18- ) => {
19- const createStyle = ( ...args : Parameters < typeof css > ) =>
20- // @ts -ignore
21- baseCreateStyle `${ css ( ...args ) } ${ generator } `
22- createStyle . attrs = ( attrs : Parameters < typeof baseCreateStyle . attrs > [ 0 ] ) =>
23- getCreateStyle ( baseCreateStyle . attrs ( attrs ) , css , generator )
24- createStyle . withConfig = ( config : StyledConfig < any > ) =>
25- getCreateStyle ( baseCreateStyle . withConfig ( config ) , css , generator )
26- return createStyle
27- }
13+ import { ReactNativeStyledInterface } from 'styled-components/native'
2814
2915type BoxStyledTags < TProps extends object > = {
3016 [ Key in keyof BoxElements ] : ThemedStyledFunction <
@@ -38,6 +24,13 @@ export interface XStyled<TGen extends StyleGenerator>
3824 extends ThemedBaseStyledInterface < Theme > ,
3925 BoxStyledTags < StyleGeneratorProps < TGen > > { }
4026
27+ export type StyledFunctions < XObj > = {
28+ scStyled : StyledInterface | ReactNativeStyledInterface < Theme >
29+ styled ?: XObj
30+ xstyled ?: XObj
31+ x ?: XObj
32+ }
33+
4134const createShouldForwardProp = (
4235 generator : StyleGenerator ,
4336) : ( (
@@ -67,37 +60,85 @@ const createShouldForwardProp = (
6760 }
6861}
6962
70- export const createBaseStyled = < TGen extends StyleGenerator > (
63+ const getCreateStyle = (
64+ baseCreateStyle : ThemedStyledFunction < any , any > ,
65+ css : XCSSFunction ,
66+ generator ?: StyleGenerator ,
67+ ) => {
68+ const createStyle = ( ...args : Parameters < typeof css > ) =>
69+ // @ts -ignore
70+ baseCreateStyle `${ css ( ...args ) } ${ generator } `
71+ createStyle . attrs = ( attrs : Parameters < typeof baseCreateStyle . attrs > [ 0 ] ) =>
72+ getCreateStyle ( baseCreateStyle . attrs ( attrs ) , css , generator )
73+ createStyle . withConfig = ( config : StyledConfig < any > ) =>
74+ getCreateStyle ( baseCreateStyle . withConfig ( config ) , css , generator )
75+ return createStyle
76+ }
77+
78+ export const createBaseStyled = < XObj , TGen extends StyleGenerator > (
79+ scStyled : StyledInterface | ReactNativeStyledInterface < Theme > ,
7180 css : XCSSFunction ,
7281 generator ?: TGen ,
73- ) : XStyled < TGen > => {
82+ ) : XObj => {
7483 const config = generator
7584 ? {
7685 shouldForwardProp : createShouldForwardProp ( generator ) ,
7786 }
7887 : { }
7988 return ( ( component : Parameters < typeof scStyled > [ 0 ] ) => {
80- const baseStyled = scStyled ( component )
89+ const baseStyled = ( scStyled as StyledInterface ) ( component )
8190 return getCreateStyle (
8291 config ? baseStyled . withConfig ( config ) : baseStyled ,
8392 css ,
8493 generator ,
8594 )
86- } ) as XStyled < TGen >
95+ } ) as unknown as XObj
8796}
8897
89- export const createStyled = < TGen extends StyleGenerator > (
98+ export const createStyled = <
99+ SCFN extends StyledInterface | ReactNativeStyledInterface < Theme > ,
100+ SCCSSFN extends ReturnType < typeof createCssFunction > ,
101+ XObj ,
102+ TGen extends StyleGenerator ,
103+ > (
104+ scStyled : SCFN ,
105+ cssFunction : SCCSSFN ,
90106 generator : TGen ,
91- ) : XStyled < TGen > => {
92- const css = createCssFunction ( generator )
93- const styled = createBaseStyled ( css )
94- const xstyled = createBaseStyled ( css , generator )
107+ ) : StyledFunctions < XObj > => {
108+ const styled = createBaseStyled < XObj , TGen > ( scStyled , cssFunction )
109+ const xstyled = createBaseStyled < XObj , TGen > ( scStyled , cssFunction , generator )
110+
111+ return { scStyled, styled, xstyled }
112+ }
113+
114+ export const defineStyledInterface = < XObj > ( {
115+ scStyled,
116+ styled,
117+ xstyled,
118+ } : StyledFunctions < XObj > ) : XObj => {
119+ //@ts -ignore
95120 styled . box = xstyled ( 'div' )
96- Object . keys ( scStyled ) . forEach ( ( key ) => {
97- // @ts -ignore
98- styled [ key ] = styled ( key )
99- // @ts -ignore
100- styled [ `${ key } Box` ] = xstyled ( key )
121+
122+ Object . keys ( scStyled ) . forEach ( ( tag ) => {
123+ //@ts -ignore
124+ styled [ tag ] = styled ( tag )
125+
126+ //@ts -ignore
127+ styled [ `${ tag } Box` ] = xstyled ( tag )
101128 } )
102- return styled
129+
130+ return styled as XObj
131+ }
132+
133+ export const defineXStyledInterface = < XObj > ( {
134+ scStyled,
135+ styled,
136+ xstyled,
137+ } : StyledFunctions < XObj > ) : XObj => {
138+ Object . keys ( scStyled ) . forEach ( ( tag ) => {
139+ //@ts -ignore
140+ styled [ tag ] = xstyled ( tag ) ``
141+ } )
142+
143+ return styled as XObj
103144}
0 commit comments