Skip to content

Commit 1641e09

Browse files
committed
feat: useStyled for web
1 parent 006671c commit 1641e09

5 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/components/Text/Text.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Text as RNText, type TextProps } from 'react-native';
22
import { copyComponentProperties } from '../../utils';
3+
import { useStyled } from '../../web/useStyled';
34

4-
export const Text = copyComponentProperties(RNText, (props: TextProps) => {
5-
return <RNText {...props} />;
6-
});
5+
export const Text = copyComponentProperties(
6+
RNText,
7+
({ className, style, ...props }: TextProps) => {
8+
const nextStyle = useStyled(className, style);
9+
return <RNText {...props} style={nextStyle} />;
10+
}
11+
);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { View as RNView, type ViewProps } from 'react-native';
2-
import { copyComponentProperties, getDeepKeys } from '../utils';
3-
import { useStyled } from '../native/useStyled';
2+
import { copyComponentProperties, getDeepKeys } from '../../utils';
3+
import { useStyled } from '../../native/useStyled';
44
import { useId } from 'react';
5-
import { useRef } from '../native/useRef';
6-
import { StyleRegistry } from '../specs/StyleRegistry';
5+
import { useRef } from '../../native/useRef';
6+
import { StyleRegistry } from '../../specs/StyleRegistry';
77

88
export const View = copyComponentProperties(RNView, (props: ViewProps) => {
99
const componentId = useId();

src/components/View/View.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { View as RNView, type ViewProps } from 'react-native';
2+
import { copyComponentProperties } from '../../utils';
3+
import { useStyled } from '../../web/useStyled';
4+
5+
export const View = copyComponentProperties(
6+
RNView,
7+
({ className, style, ...props }: ViewProps) => {
8+
const nextStyle = useStyled(className, style);
9+
return <RNView {...props} style={nextStyle} />;
10+
}
11+
);

src/components/View/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { View } from '../View.native';
2-
export * from '../View.native';
1+
import { View } from './View';
2+
export * from './View';
33

44
export default View;

src/web/useStyled.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useMemo } from 'react';
2+
3+
export function useStyled(className?: string, style?: any): any {
4+
return useMemo(() => {
5+
if (className && style) {
6+
return [{ $$css: true, className }, style];
7+
} else if (className) {
8+
return { $$css: true, className };
9+
} else if (style) {
10+
return style;
11+
} else {
12+
return undefined;
13+
}
14+
}, [className, style]);
15+
}

0 commit comments

Comments
 (0)