Skip to content

Commit

Permalink
feat(util): createSxComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
mym0404 committed Jun 14, 2024
1 parent 25f177d commit 63acffb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
17 changes: 0 additions & 17 deletions example/src/components/StyledImage.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions example/src/components/StyledView.tsx

This file was deleted.

9 changes: 9 additions & 0 deletions example/src/components/StyledViews.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ComponentProps } from 'react';
import { Image, View } from 'react-native';
import { createSxComponent } from '@react-native-styled-system/core';

export const StyledView = createSxComponent(View)();
export type StyledViewProps = ComponentProps<typeof StyledView>;

export const StyledImage = createSxComponent(Image)();
export type StyledImageProps = ComponentProps<typeof StyledImage>;
3 changes: 1 addition & 2 deletions example/src/screen/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React from 'react';
import ExpoStatusBar from 'expo-status-bar/build/ExpoStatusBar';

import { StyledButton } from '../components/StyledButton';
import { StyledImage } from '../components/StyledImage';
import { StyledScrollView } from '../components/StyledScrollView';
import { StyledView } from '../components/StyledView';
import { StyledImage, StyledView } from '../components/StyledViews';
import { Txt } from '../components/Txt';
import { useDarkTheme } from '../theme/AppThemeProvider';

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './hook/useSxStyle';
export * from './hook/useSxTokens';
export * from './provider/StyledSystemProvider';
export * from './util/propsToThemedStyle';
export * from './util/createSxComponent';
33 changes: 33 additions & 0 deletions packages/core/src/util/createSxComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { ComponentType } from 'react';
import React, { forwardRef } from 'react';

import type { SxProps, TextSxProps } from '../@types/SxProps';
import { useSx } from '../hook/useSx';

export function createSxComponent<Props extends object, Ref>(Base: ComponentType<Props>) {
return () => {
const Transformed = forwardRef<Ref, Props & SxProps>(function (props, ref) {
const { filteredProps, getStyle } = useSx(props);

return <Base {...(filteredProps as any)} style={getStyle()} ref={ref as any} />;
});

Transformed.displayName = `Sx.${Base.displayName || Base.name || 'NoName'}`;

return Transformed;
};
}

export function createSxTextComponent<Props extends object, Ref>(Base: ComponentType<Props>) {
return () => {
const Transformed = forwardRef<Ref, Props & TextSxProps>(function (props, ref) {
const { filteredProps, getStyle } = useSx(props, { styleType: 'TextStyle' });

return <Base {...(filteredProps as any)} style={getStyle()} ref={ref as any} />;
});

Transformed.displayName = `Sx.${Base.displayName || Base.name || 'NoName'}`;

return Transformed;
};
}

0 comments on commit 63acffb

Please sign in to comment.