Skip to content

Commit 1bc6d2f

Browse files
committed
Remove JSX and fix .gitignore
1 parent 1d5b7fa commit 1bc6d2f

File tree

4 files changed

+73
-38
lines changed

4 files changed

+73
-38
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
node_modules
22
npm-debug.*
3-
index.js
4-
index.d.ts
3+
/index.js
4+
/index.d.ts
55
.vscode
6-
web-build/
7-
web-report/
86
coverage
97
.DS_Store
108
yarn-error.log

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-themed-stylesheet",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "React Native StyleSheets with Theming Support",
55
"author": "Andre Pedroza",
66
"license": "MIT",

storybook/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useEffect, Fragment, createElement } from 'react'
2+
import addons, { makeDecorator } from '@storybook/addons'
3+
4+
const Wrapper = ({ children, useTheme, channel }) => {
5+
const { mode, setMode } = useTheme()
6+
useEffect(() => {
7+
const onModeChange = newMode => {
8+
setMode(newMode)
9+
}
10+
channel.on('setMode', onModeChange)
11+
return channel.removeListener(onModeChange)
12+
}, [mode])
13+
useEffect(() => {
14+
channel.emit('mode', mode)
15+
}, [mode])
16+
return createElement(Fragment, null, children)
17+
}
18+
19+
export const withThemeHook = makeDecorator({
20+
name: 'withThemeHook',
21+
parameterName: 'useTheme',
22+
skipIfNoParametersOrOptions: true,
23+
allowDeprecatedUsage: true,
24+
wrapper: (getStory, context, { parameters }) => {
25+
const useTheme = parameters || (() => ({ mode: '', setMode: () => {} }))
26+
const channel = addons.getChannel()
27+
return createElement(Wrapper, { useTheme, channel }, getStory(context))
28+
}
29+
})

storybook/register.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
/* eslint-disable indent */
12
/* eslint-disable multiline-ternary */
2-
import React, { useEffect, useState } from 'react'
3+
import { useEffect, useState, createElement } from 'react'
34
import { Text, TouchableOpacity, View } from 'react-native'
45
import addons from '@storybook/addons'
56

67
const ADDON_ID = 'storybook-addon-ondevice-themed-stylesheet'
78
const PANEL_ID = `${ADDON_ID}/panel`
89

9-
const Button = ({ mode, active, setMode }) => (
10-
<TouchableOpacity
11-
style={{
12-
borderRadius: 5,
13-
borderWidth: 1,
14-
borderColor: 'rgba(0,0,0,0.5)',
15-
padding: 10,
16-
marginHorizontal: 10,
17-
marginVertical: 5,
18-
backgroundColor: active ? 'black' : 'white'
19-
}}
20-
onPress={() => setMode(mode)}
21-
>
22-
<Text style={{ textAlign: 'center', color: active ? 'white' : 'black' }}>
23-
{mode.toUpperCase()}
24-
</Text>
25-
</TouchableOpacity>
26-
)
10+
const Button = ({ mode, active, setMode }) =>
11+
createElement(
12+
TouchableOpacity,
13+
{
14+
style: {
15+
borderRadius: 5,
16+
borderWidth: 1,
17+
borderColor: 'rgba(0,0,0,0.5)',
18+
padding: 10,
19+
marginHorizontal: 10,
20+
marginVertical: 5,
21+
backgroundColor: active ? 'black' : 'white'
22+
},
23+
onPress: () => setMode(mode)
24+
},
25+
createElement(
26+
Text,
27+
{ style: { textAlign: 'center', color: active ? 'white' : 'black' } },
28+
mode.toUpperCase()
29+
)
30+
)
2731

2832
const ThemePanel = ({ channel }) => {
2933
const [currentMode, setCurrentMode] = useState(null)
@@ -35,25 +39,29 @@ const ThemePanel = ({ channel }) => {
3539
useEffect(() => {
3640
currentMode && channel.emit('setMode', currentMode)
3741
}, [currentMode])
38-
return currentMode ? (
39-
<View>
40-
{['auto', 'light', 'dark'].map(m => (
41-
<View key={m}>
42-
<Button
43-
mode={m}
44-
active={currentMode === m}
45-
setMode={setCurrentMode}
46-
/>
47-
</View>
48-
))}
49-
</View>
50-
) : null
42+
return currentMode
43+
? createElement(
44+
View,
45+
null,
46+
['auto', 'light', 'dark'].map(m =>
47+
createElement(
48+
View,
49+
{ key: m },
50+
createElement(Button, {
51+
mode: m,
52+
active: currentMode === m,
53+
setMode: setCurrentMode
54+
})
55+
)
56+
)
57+
)
58+
: null
5159
}
5260

5361
addons.register(ADDON_ID, () => {
5462
const channel = addons.getChannel()
5563
addons.addPanel(PANEL_ID, {
5664
title: 'Theme',
57-
render: () => <ThemePanel channel={channel} />
65+
render: () => createElement(ThemePanel, { channel }, null)
5866
})
5967
})

0 commit comments

Comments
 (0)