Skip to content

Commit eabfaa1

Browse files
committed
feat(use theme): implement react hook to get the current theme)
1 parent a65a786 commit eabfaa1

File tree

7 files changed

+91
-18
lines changed

7 files changed

+91
-18
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ const ThemeColorText = styl(Text)(({ theme }) => ({
9898

9999
</details>
100100

101+
<details>
102+
<summary><strong>useTheme:</strong></summary>
103+
104+
The `useTheme` hook let you access the currently active theme.
105+
106+
```jsx
107+
import { useTheme, Provider as StyleProvider } from "react-native-styl"
108+
109+
const Main = ({ children }) => {
110+
const theme = useTheme()
111+
112+
return <Text style={{ color: theme.brand }}>Foo</Text>
113+
}
114+
115+
const App = () => {
116+
117+
return (
118+
<StyleProvider theme={{ color: { brand: "blue" }}}>
119+
<Main />
120+
</StyleProvider>
121+
)
122+
}
123+
```
124+
125+
</details>
126+
101127
<details>
102128
<summary><strong>Extends:</strong></summary>
103129

lib/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { styl, Provider, DefaultTheme } from './styl'
1+
import { styl, Provider, DefaultTheme, useTheme } from './styl'
22

3-
export { styl, Provider, DefaultTheme }
3+
export { styl, Provider, DefaultTheme, useTheme }
44
export default styl

lib/styl.test.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { useRef } from 'react'
22
import { Text, TouchableWithoutFeedback } from 'react-native'
33
import renderer from 'react-test-renderer'
4+
import { renderHook } from '@testing-library/react-hooks'
45

5-
import { styl, Provider } from '.'
6+
import { styl, Provider, useTheme } from '.'
67

78
describe('styl', () => {
89
describe('render', () => {
@@ -164,6 +165,17 @@ describe('styl', () => {
164165
expect(render?.props.style.color).toBe(GOAL)
165166
expect(render).toMatchSnapshot()
166167
})
168+
169+
it('useTheme', () => {
170+
const theme = { color: 'red' }
171+
const wrapper: React.FC = ({ children }) => (
172+
<Provider theme={theme}>{children}</Provider>
173+
)
174+
175+
const { result } = renderHook(() => useTheme(), { wrapper })
176+
177+
expect((result.current as typeof theme).color).toBe(theme.color)
178+
})
167179
})
168180

169181
describe('ref', () => {

lib/styl.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ const Context = createContext({ theme: {} })
7777
const Provider: React.FC<{ theme: DefaultTheme }> = ({ children, theme }) =>
7878
createElement(Context.Provider, { value: { theme }, children })
7979

80+
/**
81+
* useTheme
82+
*
83+
* Expose the `theme` as a React hook
84+
*/
85+
const useTheme = (): DefaultTheme => {
86+
const { theme } = useContext(Context)
87+
88+
return theme
89+
}
90+
8091
/**
8192
* styl
8293
*
@@ -124,4 +135,4 @@ const styl = <Comp extends ComponentType<any>>(Component: Comp) => <
124135
)
125136
}
126137

127-
export { styl, Provider }
138+
export { styl, Provider, useTheme }

package-lock.json

Lines changed: 36 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@significa/eslint-config": "0.0.17",
3030
"@significa/prettier-config": "0.0.17",
3131
"@significa/tsconfig-config": "0.0.17",
32+
"@testing-library/react-hooks": "^4.0.0",
3233
"@types/jest": "^25.2.2",
3334
"@types/react": "16.9.35",
3435
"@types/react-native": "0.62.9",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"isolatedModules": false,
1313
"removeComments": true
1414
},
15-
"exclude": ["node_modules", "examples"]
15+
"exclude": ["node_modules", "examples", "jest.config.js", "commitlint.config.js"]
1616
}

0 commit comments

Comments
 (0)