Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .storybook/StorybookShell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ReactNode } from 'react'
import { Appearance, View } from 'react-native'
import { StyleSheet } from 'react-native-unistyles'

import { ThemeContextProvider, ThemeVariant } from '../src/theme'

import { StorybookToolbar } from './StorybookToolbar'

const initialTheme =
Appearance.getColorScheme() === 'dark'
? ThemeVariant.Dark
: ThemeVariant.Light

export const StorybookShell = ({ children }: { children: ReactNode }) => (
<ThemeContextProvider initialTheme={initialTheme}>
<View style={styles.container}>
<StorybookToolbar />
{children}
</View>
</ThemeContextProvider>
)

const styles = StyleSheet.create(({ semantic }, rt) => ({
container: {
flex: 1,
paddingTop: rt.insets.top,
backgroundColor: semantic.colorScheme.color.bg.surface.default.default,
},
}))
68 changes: 68 additions & 0 deletions .storybook/StorybookToolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { IconMoon, IconSun } from '@tabler/icons-react-native'
import { Appearance, Pressable, Text, View } from 'react-native'
import {
StyleSheet,
UnistylesRuntime,
useUnistyles,
} from 'react-native-unistyles'

const THEME_TOGGLE_LABELS = {
light: 'Включить тёмную тему',
dark: 'Включить светлую тему',
}

export const StorybookToolbar = () => {
const { theme, rt } = useUnistyles()
const isDark = rt.themeName === 'dark'
const ThemeIcon = isDark ? IconSun : IconMoon

const toggleTheme = () => {
const nextTheme = isDark ? 'light' : 'dark'
Appearance.setColorScheme(nextTheme)
UnistylesRuntime.setTheme(nextTheme)
}

return (
<View style={styles.toolbar}>
<Text style={styles.title}>Storybook</Text>
<Pressable
accessibilityRole='button'
accessibilityLabel={THEME_TOGGLE_LABELS[isDark ? 'dark' : 'light']}
onPress={toggleTheme}
style={styles.themeButton}
testID='StorybookThemeToggle'
>
<ThemeIcon
accessible={false}
size={20}
color={theme.semantic.colorScheme.color.fg.default}
/>
</Pressable>
</View>
)
}

const styles = StyleSheet.create(({ semantic }) => ({
toolbar: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 16,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: semantic.colorScheme.color.border.neutral.default,
},
title: {
flexShrink: 1,
fontSize: 16,
fontWeight: '600',
color: semantic.colorScheme.color.fg.default,
},
themeButton: {
minWidth: 44,
minHeight: 44,
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 12,
paddingVertical: 8,
},
}))
9 changes: 8 additions & 1 deletion .storybook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '../src/theme/ThemeContext'

import { createAsyncStorage } from '@react-native-async-storage/async-storage'

import { StorybookShell } from './StorybookShell'
import { StorybookStatusUI } from './StorybookStatusUI'
import { view } from './storybook.requires'

Expand All @@ -28,4 +29,10 @@ const StorybookUIRoot = view.getStorybookUI({
},
})

export default StorybookUIRoot
const Storybook = () => (
<StorybookShell>
<StorybookUIRoot />
</StorybookShell>
)

export default Storybook
91 changes: 14 additions & 77 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import type { Preview } from '@storybook/react'
import { useArgs } from '@storybook/preview-api'
import { StyleSheet, UnistylesRuntime } from 'react-native-unistyles'
import { ThemeContextProvider, ThemeVariant } from '../src/theme'
import { View, Appearance } from 'react-native'
import React, {
useEffect,
useLayoutEffect,
useRef,
type FunctionComponent,
type ReactNode,
} from 'react'

let currentTheme =
Appearance.getColorScheme() === 'dark'
? ThemeVariant.Dark
: ThemeVariant.Light
import { StyleSheet } from 'react-native-unistyles'
import { View } from 'react-native'

const preview: Preview = {
decorators: [
(Story, context) => {
const [args, updateArgs] = useArgs<StorybookThemeArgs>()

return (
<Container theme={args.theme} updateArgs={updateArgs}>
<View style={{ padding: 16, flex: 1 }}>
<Story />
</View>
</Container>
)
},
(Story) => (
<View style={styles.container}>
<Story />
</View>
),
],
parameters: {
options: {
Expand All @@ -41,60 +21,17 @@ const preview: Preview = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: { matchers: { color: /(background|color)$/i, date: /Date$/ } },
},
argTypes: {
theme: {
options: [ThemeVariant.Light, ThemeVariant.Dark],
control: { type: 'radio' },
},
},
args: { theme: currentTheme },
}

export default preview

interface StorybookThemeArgs {
theme: ThemeVariant
}

const THEME_NAME_MAP: Record<ThemeVariant, 'light' | 'dark'> = {
[ThemeVariant.Light]: 'light',
[ThemeVariant.Dark]: 'dark',
}

const Container: FunctionComponent<{
children: ReactNode
theme: ThemeVariant
updateArgs: (args: StorybookThemeArgs) => void
}> = ({ children, theme, updateArgs }) => {
const themeSynchronized = useRef(false)

useEffect(() => {
if (!themeSynchronized.current) {
themeSynchronized.current = true
updateArgs({ theme: currentTheme })
}
}, [updateArgs])

useLayoutEffect(() => {
if (themeSynchronized.current) {
const nextTheme = THEME_NAME_MAP[theme]
Appearance.setColorScheme(nextTheme)
UnistylesRuntime.setTheme(nextTheme)
currentTheme = theme
}
}, [theme])

return (
<ThemeContextProvider initialTheme={currentTheme}>
<View style={styles.container}>{children}</View>
</ThemeContextProvider>
)
}

const styles = StyleSheet.create(({ theme }) => ({
const styles = StyleSheet.create(({ semantic }, rt) => ({
container: {
width: '100%',
height: '100%',
backgroundColor: theme.Surface['surface-card'],
flex: 1,
padding: 16,
backgroundColor:
rt.themeName === 'dark'
? semantic.colorScheme.color.bg.surface.canvas.default
: semantic.colorScheme.color.bg.surface.default.default,
},
}))
1 change: 0 additions & 1 deletion src/components/Checkbox/stories/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const meta: Meta<CheckboxProps> = {
parameters: {
controls: {
include: [
'theme',
'checked',
'indeterminate',
'disabled',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const meta: Meta<CheckboxProps> = {
onCheckedChange: { action: 'onCheckedChange' },
onPress: { action: 'onPress' },
},
parameters: { controls: { include: ['theme'] } },
parameters: { controls: { include: [] } },
render: ({ onCheckedChange, onPress }) => (
<ScrollView contentContainerStyle={styles.root}>
<CheckboxGroupExample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const meta: Meta<RadioButtonProps> = {
},
parameters: {
controls: {
include: ['theme', 'checked', 'disabled', 'danger', 'label', 'caption'],
include: ['checked', 'disabled', 'danger', 'label', 'caption'],
},
},
render: ({ label, caption, ...args }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const meta: Meta<RadioButtonProps> = {
tags: ['version:2.0'],
component: RadioButton,
argTypes: { onPress: { action: 'onPress' } },
parameters: { controls: { include: ['theme'] } },
parameters: { controls: { include: [] } },
render: ({ onPress }) => (
<ScrollView contentContainerStyle={styles.root}>
<RadioButtonGroupExample onPress={onPress} />
Expand Down
6 changes: 2 additions & 4 deletions src/components/SelectButton/SelectButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const meta: Meta<typeof SelectButton> = {
disabled: { control: 'boolean' },
onPress: { action: 'onPress' },
},
parameters: { controls: { include: ['theme', 'size', 'disabled'] } },
parameters: { controls: { include: ['size', 'disabled'] } },
}

export default meta
Expand All @@ -64,9 +64,7 @@ export const Controlled: Story = {
control: { type: 'range', min: 0, max: buttons.length - 1, step: 1 },
},
},
parameters: {
controls: { include: ['theme', 'size', 'disabled', 'selectedIndex'] },
},
parameters: { controls: { include: ['size', 'disabled', 'selectedIndex'] } },
render: ({
buttons,
disabled,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ToggleSwitch/ToggleSwitch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const meta: Meta<ToggleSwitchProps> = {
},
parameters: {
controls: {
include: ['theme', 'checked', 'disabled', 'danger', 'label', 'caption'],
include: ['checked', 'disabled', 'danger', 'label', 'caption'],
},
},
render: ({ label, caption, ...args }) => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"jsx": "react-native",
"resolveJsonModule": true
},
"include": ["**/*", ".storybook/**/*"],
"exclude": [
"artifacts",
"scripts",
Expand Down
Loading