Skip to content

Commit 286238f

Browse files
committed
Optimize useStyleProps for native
1 parent 57cc38c commit 286238f

1 file changed

Lines changed: 71 additions & 57 deletions

File tree

packages/react-strict-dom/src/native/modules/useStyleProps.js

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
*/
99

1010
import type { CustomProperties, Styles, Style } from '../../types/styles';
11-
import type {
12-
ReactNativeProps,
13-
ReactNativeStyle
14-
} from '../../types/renderer.native';
11+
import type { ReactNativeProps } from '../../types/renderer.native';
12+
import type { ReactNativeStyle } from '../../types/renderer.native';
1513

1614
import * as css from '../css';
1715
import * as ReactNative from '../react-native';
@@ -22,6 +20,38 @@ import { usePseudoStates } from './usePseudoStates';
2220
import { useStyleTransition } from './useStyleTransition';
2321
import { useViewportScale } from './ContextViewportScale';
2422

23+
const inheritedProperties = [
24+
'color',
25+
'cursor',
26+
'fontFamily',
27+
'fontSize',
28+
'fontStyle',
29+
'fontVariant',
30+
'fontWeight',
31+
'letterSpacing',
32+
'lineHeight',
33+
'textAlign',
34+
'textDecorationColor',
35+
'textDecorationLine',
36+
'textDecorationStyle',
37+
'textIndent',
38+
'textTransform',
39+
'whiteSpace',
40+
'writingDirection'
41+
];
42+
43+
const eventHandlerNames = [
44+
'onBlur',
45+
'onFocus',
46+
'onMouseEnter',
47+
'onMouseLeave',
48+
'onPointerCancel',
49+
'onPointerDown',
50+
'onPointerEnter',
51+
'onPointerLeave',
52+
'onPointerUp'
53+
];
54+
2555
type StyleOptions = {
2656
customProperties: ?CustomProperties,
2757
provideInheritableStyle: boolean,
@@ -113,19 +143,10 @@ export function useStyleProps(
113143
);
114144

115145
if (handlers != null) {
116-
for (const handler of [
117-
'onBlur',
118-
'onFocus',
119-
'onMouseEnter',
120-
'onMouseLeave',
121-
'onPointerCancel',
122-
'onPointerDown',
123-
'onPointerEnter',
124-
'onPointerLeave',
125-
'onPointerUp'
126-
]) {
127-
if (handler != null) {
128-
styleProps[handler] = handlers[handler];
146+
for (const handler of eventHandlerNames) {
147+
const handlerValue = handlers[handler];
148+
if (handlerValue != null) {
149+
styleProps[handler] = handlerValue;
129150
}
130151
}
131152
}
@@ -146,48 +167,34 @@ export function useStyleProps(
146167
styleProps.style = styleWithAnimations;
147168
}
148169

149-
const {
150-
color,
151-
cursor,
152-
fontFamily,
153-
fontSize,
154-
fontStyle,
155-
fontVariant,
156-
fontWeight,
157-
letterSpacing,
158-
lineHeight,
159-
textAlign,
160-
textDecorationColor,
161-
textDecorationLine,
162-
textDecorationStyle,
163-
textIndent,
164-
textTransform,
165-
whiteSpace,
166-
writingDirection,
167-
...viewStyle
168-
} = styleProps.style;
170+
// Create inherited values lookup for performance
171+
const inheritedValues = {
172+
color: inheritedColor,
173+
cursor: inheritedCursor,
174+
fontFamily: inheritedFontFamily,
175+
fontSize: inheritedFontSize,
176+
fontStyle: inheritedFontStyle,
177+
fontVariant: inheritedFontVariant,
178+
fontWeight: inheritedFontWeight,
179+
letterSpacing: inheritedLetterSpacing,
180+
lineHeight: inheritedLineHeight,
181+
textAlign: inheritedTextAlign,
182+
textDecorationColor: inheritedTextDecorationColor,
183+
textDecorationLine: inheritedTextDecorationLine,
184+
textDecorationStyle: inheritedTextDecorationStyle,
185+
textIndent: inheritedTextIndent,
186+
textTransform: inheritedTextTransform,
187+
whiteSpace: inheritedWhiteSpace,
188+
writingDirection: inheritedWritingDirection
189+
};
169190

170191
const inheritableStyle = {} as $FlowFixMe;
192+
const viewStyle = {} as $FlowFixMe;
193+
194+
for (const key of inheritedProperties) {
195+
const value = styleProps.style[key];
196+
const inheritedValue = inheritedValues[key];
171197

172-
[
173-
['color', color, inheritedColor],
174-
['cursor', cursor, inheritedCursor],
175-
['fontFamily', fontFamily, inheritedFontFamily],
176-
['fontSize', fontSize, inheritedFontSize],
177-
['fontStyle', fontStyle, inheritedFontStyle],
178-
['fontVariant', fontVariant, inheritedFontVariant],
179-
['fontWeight', fontWeight, inheritedFontWeight],
180-
['letterSpacing', letterSpacing, inheritedLetterSpacing],
181-
['lineHeight', lineHeight, inheritedLineHeight],
182-
['textAlign', textAlign, inheritedTextAlign],
183-
['textDecorationColor', textDecorationColor, inheritedTextDecorationColor],
184-
['textDecorationLine', textDecorationLine, inheritedTextDecorationLine],
185-
['textDecorationStyle', textDecorationStyle, inheritedTextDecorationStyle],
186-
['textIndent', textIndent, inheritedTextIndent],
187-
['textTransform', textTransform, inheritedTextTransform],
188-
['whiteSpace', whiteSpace, inheritedWhiteSpace],
189-
['writingDirection', writingDirection, inheritedWritingDirection]
190-
].forEach(([key, value, inheritedValue]) => {
191198
let val = value;
192199
if (
193200
(withInheritedStyle && value == null) ||
@@ -200,7 +207,14 @@ export function useStyleProps(
200207
inheritableStyle[key] = val;
201208
styleProps.style[key] = val;
202209
}
203-
});
210+
}
211+
212+
// Copy non-inherited properties to viewStyle
213+
for (const key in styleProps.style) {
214+
if (!inheritedValues.hasOwnProperty(key)) {
215+
viewStyle[key] = styleProps.style[key];
216+
}
217+
}
204218

205219
if (withTextStyle === true) {
206220
const textStyle =

0 commit comments

Comments
 (0)