Skip to content

Commit

Permalink
chore: overriding theme stories
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Dec 28, 2024
1 parent dc8eb3d commit 00deb70
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 127 deletions.
12 changes: 7 additions & 5 deletions .storybook/decorators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import {ThemeProvider} from '../src/providers/ThemeProvider';
import {dark, light} from '../src/utils/colors';
import styled from '@emotion/native';
import {CpkProvider} from '../src/providers';
import {ThemeParam} from '../src/utils/theme';

export const withThemeProvider = (Story: React.FC, context: any) => {
export const withThemeProvider = (
Story: React.FC,
context: any,
customTheme?: ThemeParam,
) => {
const isDarkMode = context.globals.theme === 'dark';

return (
<CpkProvider
themeConfig={{
initialThemeType: isDarkMode ? 'dark' : 'light',
customTheme: {
light,
dark,
},
customTheme,
}}
>
<Container>
Expand Down
62 changes: 61 additions & 1 deletion src/components/uis/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {withThemeProvider} from '../../../../.storybook/decorators';

import type {ButtonColorType, ButtonSizeType, ButtonType} from './Button';
import {Button} from './Button';
import {ThemeParam} from '../../../utils/theme';

const buttonTypes: ButtonType[] = ['outlined', 'solid', 'text'];
const buttonSizes: ButtonSizeType[] = ['large', 'medium', 'small'];
Expand Down Expand Up @@ -43,7 +44,15 @@ const meta = {
options: buttonSizes,
},
},
decorators: [withThemeProvider],
decorators: [
(Story, context) =>
withThemeProvider(
Story,
context,
// @ts-ignore
context.args.theme,
),
],
} satisfies Meta<typeof Button>;

export default meta;
Expand Down Expand Up @@ -79,3 +88,54 @@ export const Danger: Story = {
onPress: action('onPress'),
},
};

const disableAllExcept = (allowedField: string) => {
const argTypes = {
text: {table: {disable: true}},
type: {table: {disable: true}},
color: {table: {disable: true}},
size: {table: {disable: true}},
onPress: {table: {disable: true}},
testID: {table: {disable: true}},
disabled: {table: {disable: true}},
loadingElement: {table: {disable: true}},
borderRadius: {table: {disable: true}},
startElement: {table: {disable: true}},
endElement: {table: {disable: true}},
activeOpacity: {table: {disable: true}},
touchableHighlightProps: {table: {disable: true}},
hitSlop: {table: {disable: true}},
theme: {control: 'object'},
};

if (argTypes[allowedField]) {
argTypes[allowedField] = {table: {disable: false}};
}

return argTypes;
};

export const CustomColor: Story = {
args: {
// @ts-ignore
theme: {
light: {
button: {
primary: {
bg: 'red',
text: 'white',
},
},
},
dark: {
button: {
primary: {
bg: 'pink',
text: 'red',
},
},
},
} as ThemeParam,
},
argTypes: disableAllExcept('theme'),
};
14 changes: 7 additions & 7 deletions src/components/uis/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ export function Button({
const LoadingView = loadingElement ?? (
<LoadingIndicator
color={
loadingColor || type === 'solid'
? theme.text.contrast
: theme.text.basic
loadingColor
? loadingColor
: type === 'solid'
? theme.text.contrast
: theme.text.basic
}
size="small"
/>
Expand Down Expand Up @@ -285,13 +287,11 @@ export function Button({
],
);

function resolveStyle<T>(
style: StyleProp<T>
): T | undefined {
function resolveStyle<T>(style: StyleProp<T>): T | undefined {
if (Array.isArray(style)) {
return style.find((s): s is T => !!s);
}
return style as T || undefined;
return (style as T) || undefined;
}

const viewStyle = resolveStyle<ViewStyle>(compositeStyles.container);
Expand Down
58 changes: 58 additions & 0 deletions src/components/uis/Typography/Typography.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type {Meta, StoryObj} from '@storybook/react';
import {Typography} from './Typography';
import {withThemeProvider} from '../../../../.storybook/decorators';
import {ScrollView, View} from 'react-native';
import {ThemeParam} from '../../../utils/theme';

const meta = {
title: 'Typography',
component: Typography.Title,
decorators: [
(Story, context) =>
withThemeProvider(
Story,
context,
(context.args.theme as any) || undefined,
),
],
} satisfies Meta<typeof Typography.Title>;

export default meta;

type Story = StoryObj<typeof meta>;

export const AllTypography: Story = {
render: (args) => (
<ScrollView contentContainerStyle={{padding: 16}}>
{Object.entries(Typography).map(([key, Component]) => (
<View key={key} style={{marginBottom: 16}}>
<Component {...args}>
{key} {args.children}
</Component>
</View>
))}
</ScrollView>
),
args: {
children: 'Hello world',
},
};

export const CustomTitle: Story = {
args: {
children: 'Custom Color',
// @ts-ignore
theme: {
light: {
text: {
basic: 'blue',
},
},
dark: {
text: {
basic: 'skyblue',
},
},
} as ThemeParam,
},
};
Loading

0 comments on commit 00deb70

Please sign in to comment.