Skip to content

Commit 5e17eb5

Browse files
committed
fix: support react 18.3.1
1 parent a9ee989 commit 5e17eb5

File tree

6 files changed

+857
-773
lines changed

6 files changed

+857
-773
lines changed

babel.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset',
3-
4-
],
2+
presets: ['module:@react-native/babel-preset'],
53
plugins: [['./babel-plugin', { imports: [ '../styled' ] }]]
64
}

package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@
1111
},
1212
"dependencies": {
1313
"css-to-react-native": "^3.2.0",
14-
"postcss": "^8.4.32"
14+
"postcss": "^8.4.38"
1515
},
1616
"devDependencies": {
17-
"@babel/cli": "^7.23.4",
18-
"@babel/types": "^7.23.5",
19-
"@tsconfig/react-native": "^3.0.2",
17+
"@babel/cli": "^7.24.5",
18+
"@babel/types": "^7.24.5",
19+
"@react-native/babel-preset": "0.74.83",
20+
"@tsconfig/react-native": "^3.0.5",
2021
"@types/jest": "^29.5.11",
2122
"@types/node": "^20.10.5",
22-
"@types/react": "^18.2.42",
23+
"@types/react": "^18.3.1",
2324
"jest": "^29.7.0",
24-
"metro-react-native-babel-preset": "0.73",
25-
"react": "^18.2.0",
26-
"react-native": "^0.72.7",
27-
"typescript": "^5.3.3"
25+
"react": "^18.3.1",
26+
"react-native": "^0.74.1",
27+
"typescript": "^5.4.5"
28+
},
29+
"peerDependencies": {
30+
"react": ">=18.2.0",
31+
"react-native": ">=0.73.0"
2832
},
2933
"packageManager": "yarn@4.1.1"
3034
}

src/buildPropsFromAttrs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AnyTheme, AttrsFn, BaseObject, Themed, UnknownProps } from './types'
1+
import { AnyTheme, BaseObject, InnerAttrs, Themed, UnknownProps } from './types'
22

33
/**
44
* Combines passed props and props from attrs
@@ -8,14 +8,14 @@ import { AnyTheme, AttrsFn, BaseObject, Themed, UnknownProps } from './types'
88
* Example:
99
* props - { overriddenNumber: 0 }
1010
* attrs - [
11-
* () => { number: 1, overriddenNumber: 1},
11+
* { number: 1, overriddenNumber: 1},
1212
* (props) => { number: props.number + 1, overriddenNumber: props.overriddenNumber + 1 }
1313
* ]
1414
* result - { overriddenNumber: 2, number: 2 }
1515
*/
16-
export function buildPropsFromAttrs<Theme extends BaseObject = AnyTheme>(props: Themed<UnknownProps, Theme>, attrs: AttrsFn[]): Themed<UnknownProps, Theme> {
16+
export function buildPropsFromAttrs<Theme extends BaseObject = AnyTheme>(props: Themed<UnknownProps, Theme>, attrs: InnerAttrs[]): Themed<UnknownProps, Theme> {
1717
for (const attr of attrs) {
18-
props = Object.assign(props, attr(props))
18+
props = Object.assign(props, typeof attr === 'function' ? attr(props) : attr)
1919
}
2020
return props
2121
}

src/splitAttrs.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/styled.tsx

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
import { buildPropsFromAttrs } from './buildPropsFromAttrs'
4343
import { substitute, runtime, mixin } from './parsers'
4444
import { createTheme } from './theme'
45-
import { splitAttrs } from './splitAttrs'
4645
import { buildDynamicStyles } from './buildDynamicStyles'
4746
import { isFunction, isStyledComponent } from './utils'
4847

@@ -66,47 +65,37 @@ export function createStyled<Theme extends AnyTheme>() {
6665
attrs = isStyledComponent(Component) ? [...Component.attrs, ...attrs] : attrs
6766
const hasDynamicStyles = Object.keys(initialStyles).some((key) => isFunction(initialStyles[key]))
6867
const fixedStyle = hasDynamicStyles ? undefined : initialStyles
69-
const { fixedProps, dynamicAttrs } = splitAttrs(attrs)
7068
const origin = isStyledComponent(Component) ? Component.origin : Component
71-
const isDynamic = dynamicAttrs.length > 0 || hasDynamicStyles
7269

7370
// Component
7471
type ThemedProps = Themed<UnknownProps, Theme>
7572
const StyledComponent = React.forwardRef((props: PropsWithChildren<ThemedProps & AnyStyleProps & AsComponentProps>, ref) => {
7673
const theme = useContext(ThemeContext)
7774
const CastedComponent = (props.as ?? origin) as AnyComponent
78-
const styleFromProps = props.style
79-
const hasCustomStyle = fixedStyle !== styleFromProps
80-
// We add fixed props and styles to defaultProps. Thanks to this, we don't need to copy the props object.
81-
// We copy the props object in the following cases:
82-
// 1. ref is not null, meaning the parent has set a ref for the component.
83-
// 2. There are dynamic props that depend on the component's props.
84-
// 3. There are dynamic attributes that depend on the component's props.
85-
const shouldCopyProps = isDynamic || ref || hasCustomStyle
86-
let propsForElement = shouldCopyProps ? Object.assign({}, props, { theme, ref }) : props
75+
let propsForElement: ThemedProps = Object.assign({}, props, { theme, ref })
8776
let style: StyleProp<UnknownStyles> = fixedStyle
8877

89-
if (dynamicAttrs.length > 0) {
90-
propsForElement = buildPropsFromAttrs(propsForElement, dynamicAttrs)
78+
if (attrs.length > 0) {
79+
propsForElement = buildPropsFromAttrs(propsForElement, attrs)
9180
}
9281
if (hasDynamicStyles) {
9382
style = buildDynamicStyles(propsForElement, initialStyles)
9483
}
95-
if (hasCustomStyle) {
84+
85+
const styleFromProps = props.style
86+
if (styleFromProps) {
9687
style = ([style] as Array<StyleProp<UnknownStyles>>).concat(styleFromProps)
9788
}
9889
propsForElement.style = style
99-
if (shouldCopyProps) {
100-
propsForElement.theme = props.theme
101-
}
90+
propsForElement.theme = props.theme
91+
10292
return createElement(CastedComponent, propsForElement)
10393
}) as StyledComponent
10494

10595
StyledComponent.displayName = 'StyledComponent'
10696
StyledComponent.isStyled = true
10797
StyledComponent.styles = initialStyles
10898
StyledComponent.attrs = attrs
109-
StyledComponent.defaultProps = Object.assign({ style: fixedStyle }, fixedProps)
11099
StyledComponent.origin = origin
111100
return StyledComponent
112101
}

0 commit comments

Comments
 (0)