-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useTheme hook for emotion-theming (#1499)
* Add useTheme hook for emotion-theming * Add changeset * Small fixes to imports and docs * Update theming.mdx
- Loading branch information
Showing
8 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"releases": [{ "name": "emotion-theming", "type": "patch" }], | ||
"dependents": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add useTheme React hook to emotion-theming |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/emotion-theming/__tests__/__snapshots__/use-theme.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Nested useTheme works 1`] = ` | ||
.emotion-1 { | ||
color: green; | ||
} | ||
.emotion-1:hover { | ||
color: darkgreen; | ||
} | ||
.emotion-0 { | ||
color: lawngreen; | ||
} | ||
.emotion-0:hover { | ||
color: seagreen; | ||
} | ||
<div | ||
className="emotion-1" | ||
> | ||
Should be green | ||
<div | ||
className="emotion-0" | ||
> | ||
Should be lawngreen | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`useTheme works 1`] = ` | ||
.emotion-0 { | ||
color: green; | ||
} | ||
.emotion-0:hover { | ||
color: darkgreen; | ||
} | ||
<div | ||
className="emotion-0" | ||
> | ||
Should be green | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @flow | ||
/** @jsx jsx */ | ||
import 'test-utils/next-env' | ||
import * as renderer from 'react-test-renderer' | ||
import { jsx } from '@emotion/core' | ||
import { useTheme, ThemeProvider } from 'emotion-theming' | ||
|
||
test('useTheme works', () => { | ||
function TestComponent(props) { | ||
const theme = useTheme() | ||
return ( | ||
<div | ||
css={{ color: theme.lightGreen, '&:hover': { color: theme.darkGreen } }} | ||
> | ||
Should be green | ||
</div> | ||
) | ||
} | ||
|
||
expect( | ||
renderer | ||
.create( | ||
<ThemeProvider theme={{ lightGreen: 'green', darkGreen: 'darkgreen' }}> | ||
<TestComponent /> | ||
</ThemeProvider> | ||
) | ||
.toJSON() | ||
).toMatchSnapshot() | ||
}) | ||
|
||
test('Nested useTheme works', () => { | ||
function TestComponent1(props) { | ||
const theme = useTheme() | ||
return ( | ||
<div | ||
css={{ color: theme.lightGreen, '&:hover': { color: theme.darkGreen } }} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
function NestedComponent(props) { | ||
const theme = useTheme() | ||
return ( | ||
<div | ||
css={{ | ||
color: theme.lightGreen, | ||
'&:hover': { color: theme.darkGreen } | ||
}} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
function TestComponent2(props) { | ||
return ( | ||
<TestComponent1> | ||
Should be green | ||
<ThemeProvider | ||
theme={{ lightGreen: 'lawngreen', darkGreen: 'seagreen' }} | ||
> | ||
<NestedComponent>Should be lawngreen</NestedComponent> | ||
</ThemeProvider> | ||
</TestComponent1> | ||
) | ||
} | ||
|
||
expect( | ||
renderer | ||
.create( | ||
<ThemeProvider theme={{ lightGreen: 'green', darkGreen: 'darkgreen' }}> | ||
<TestComponent2 /> | ||
</ThemeProvider> | ||
) | ||
.toJSON() | ||
).toMatchSnapshot() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// @flow | ||
export { default as ThemeProvider } from './theme-provider' | ||
export { default as withTheme } from './with-theme' | ||
export { default as useTheme } from './use-theme' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @flow | ||
import React from 'react' | ||
import { ThemeContext } from '@emotion/core' | ||
|
||
export default function useTheme() { | ||
return React.useContext(ThemeContext) | ||
} |