Skip to content

Commit 768b7d1

Browse files
committed
feat: use ReactNative to create elements
1 parent db98bc8 commit 768b7d1

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

packages/styled-components-native/src/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { createStyled, XStyled } from './createStyled'
66
interface XStyledSet<TGen extends StyleGenerator> {
77
css: XCSSFunction
88
x: X<TGen>
9-
styled: XStyled
9+
styled: XStyled<TGen>
1010
}
1111

1212
export const createCss = <TGen extends StyleGenerator>(
Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
/* eslint-disable no-continue, no-loop-func, no-cond-assign */
22
import type { ElementType } from 'react'
33
import type { ReactNativeThemedStyledFunction } from 'styled-components/native'
4+
import type {
5+
StyleGenerator,
6+
StyleGeneratorProps,
7+
Theme,
8+
} from '@xstyled/system'
9+
import type { StyledConfig, ThemedStyledFunction } from 'styled-components'
10+
import type {
11+
ReactNativeBoxElements,
12+
ReactNativeElement,
13+
ReactNativeElements,
14+
} from './types'
15+
import * as ReactNative from 'react-native'
416
import { string } from '@xstyled/util'
5-
import { StyleGenerator, Theme } from '@xstyled/system'
6-
import { StyledConfig } from 'styled-components'
7-
import * as RN from 'react-native'
817
import { scStyled } from './scStyled'
918
import { createCssFunction, XCSSFunction } from './createCssFunction'
10-
import type { ReactNativeThemedBaseStyledInterface } from './types'
1119

1220
const getCreateStyle = (
1321
baseCreateStyle: ReactNativeThemedStyledFunction<any, any>,
@@ -25,7 +33,17 @@ const getCreateStyle = (
2533
return createStyle
2634
}
2735

28-
export type XStyled = ReactNativeThemedBaseStyledInterface<Theme>
36+
type BoxStyledTags<TProps extends object> = {
37+
[Key in keyof ReactNativeBoxElements]: ThemedStyledFunction<
38+
ReactNativeElement<ReactNativeBoxElements[Key], Theme>,
39+
Theme,
40+
TProps
41+
>
42+
}
43+
44+
export interface XStyled<TGen extends StyleGenerator>
45+
extends ReactNativeElements,
46+
BoxStyledTags<StyleGeneratorProps<TGen>> {}
2947

3048
const createShouldForwardProp = (
3149
generator: StyleGenerator,
@@ -36,31 +54,19 @@ const createShouldForwardProp = (
3654
) => boolean) => {
3755
const propSet = new Set<string>(generator.meta.props)
3856

39-
return (
40-
prop: string | number | symbol,
41-
defaultValidatorFn: (prop: string | number | symbol) => boolean,
42-
elementToBeCreated?: ElementType,
43-
) => {
57+
return (prop: string | number | symbol) => {
4458
if (string(prop) && propSet.has(prop)) {
4559
return false
4660
}
47-
if (typeof elementToBeCreated === 'string') {
48-
// We must test elementToBeCreated so we can pass through props for <x.div
49-
// as={Component} />. However elementToBeCreated isn't available until
50-
// styled-components 5.2.4 or 6, and in the meantime will be undefined.
51-
// This means that HTML elements could get unwanted props, but ultimately
52-
// this is a bug in the caller, because why are they passing unwanted
53-
// props?
54-
return defaultValidatorFn(prop)
55-
}
61+
5662
return true
5763
}
5864
}
5965

6066
export const createBaseStyled = <TGen extends StyleGenerator>(
6167
css: XCSSFunction,
6268
generator?: TGen,
63-
): XStyled => {
69+
): XStyled<TGen> => {
6470
const config = generator
6571
? {
6672
shouldForwardProp: createShouldForwardProp(generator),
@@ -74,26 +80,35 @@ export const createBaseStyled = <TGen extends StyleGenerator>(
7480
css,
7581
generator,
7682
)
77-
}) as XStyled
83+
}) as XStyled<TGen>
7884
}
7985

8086
export const createStyled = <TGen extends StyleGenerator>(
8187
generator: TGen,
82-
): XStyled => {
88+
): XStyled<TGen> => {
8389
const css = createCssFunction(generator)
84-
// const styled = createBaseStyled(css)
90+
const styled = createBaseStyled(css)
8591
const xstyled = createBaseStyled(css, generator)
8692

8793
Object.keys(scStyled).forEach((tag) => {
88-
Object.defineProperty(xstyled, tag, {
94+
Object.defineProperty(styled, tag, {
95+
enumerable: true,
96+
configurable: false,
97+
get() {
98+
// @ts-ignore
99+
return styled(ReactNative[tag])
100+
},
101+
})
102+
103+
Object.defineProperty(styled, `${tag}Box`, {
89104
enumerable: true,
90105
configurable: false,
91106
get() {
92107
// @ts-ignore
93-
return xstyled(RN[tag])
108+
return xstyled(ReactNative[tag])
94109
},
95110
})
96111
})
97112

98-
return xstyled
113+
return styled
99114
}

packages/styled-components-native/src/createX.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/* eslint-disable no-continue, no-loop-func, no-cond-assign */
2+
import * as ReactNative from 'react-native'
23
import { StyledComponent } from 'styled-components'
34
import { DefaultTheme } from 'styled-components/native'
4-
import { scStyled } from './scStyled'
55
import { StyleGenerator, StyleGeneratorProps } from '@xstyled/system'
6+
import { scStyled } from './scStyled'
67
import { createBaseStyled } from './createStyled'
78
import { createCssFunction } from './createCssFunction'
8-
import { NativeElement, NativeElementsKeys } from './types'
9-
import * as RN from 'react-native'
9+
import { ReactNativeElement, ReactNativeElementsKeys } from './types'
1010

1111
export type X<TGen extends StyleGenerator> = {
12-
[Key in NativeElementsKeys]: StyledComponent<
13-
NativeElement<Key>,
12+
[Key in ReactNativeElementsKeys]: StyledComponent<
13+
ReactNativeElement<Key, DefaultTheme>,
1414
DefaultTheme,
1515
StyleGeneratorProps<TGen>,
1616
'color'
@@ -30,7 +30,7 @@ export const createX = <TGen extends StyleGenerator>(
3030
configurable: false,
3131
get() {
3232
// @ts-ignore
33-
return xstyled(RN[tag])``
33+
return xstyled(ReactNative[tag])``
3434
},
3535
})
3636
})

0 commit comments

Comments
 (0)