Skip to content

Commit c973516

Browse files
author
LB
authored
Merge pull request #878 from system-ui/add-options
Let gatsby-plugin-theme-ui take base preset options
2 parents d8b29f9 + 83dbd76 commit c973516

File tree

7 files changed

+123
-12
lines changed

7 files changed

+123
-12
lines changed

jest-preprocess.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = require('babel-jest').createTransformer({
2+
presets: ['babel-preset-gatsby'],
3+
})

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@
9696
],
9797
"setupFiles": [
9898
"jest-canvas-mock"
99-
]
99+
],
100+
"transform": {
101+
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js"
102+
}
100103
},
101104
"husky": {
102105
"hooks": {

packages/gatsby-plugin-theme-ui/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ module.exports = {
1616
In addition to providing context, this plugin will also
1717
prevent a flash of unstyled colors when using color modes.
1818

19+
## Options
20+
21+
| Key | Default value | Description |
22+
| ------------------------ | ---------------- | -------------------------------------------------------------------------------- |
23+
| `preset` | `null` | This can be a JSON theme object or a string package name. Make sure the package you're requiring is installed in your dependencies. |
24+
25+
> Note that this plugin assumes the theme object is exported as `default`.
26+
27+
The theme module you include in options is considered your base theme. Any further customization and shadowing will be merged with it.
28+
29+
### Using options
30+
31+
```js
32+
// gatsby-config.js
33+
module.exports = {
34+
plugins: [
35+
{ resolve: 'gatsby-plugin-theme-ui',
36+
options: {
37+
preset: '@theme-ui/preset-funk'
38+
// or require('@theme-ui/preset-funk')
39+
}
40+
}],
41+
}
42+
```
43+
1944
## Customizing the theme
2045

2146
To customize the theme used in your Gatsby site,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
exports.onPreInit = (__, options) => {
2+
let { preset } = options
3+
4+
if (typeof preset === 'string') {
5+
try {
6+
options.preset = require(preset)
7+
} catch {
8+
reporter.warn(
9+
`It appears your theme dependency is not installed. Only local styles will appear.`
10+
)
11+
}
12+
}
13+
}
14+
15+
exports.createSchemaCustomization = ({ actions }) => {
16+
const { createTypes } = actions
17+
18+
createTypes(`
19+
type ThemeUiConfig implements Node {
20+
preset: JSON,
21+
}
22+
`)
23+
}
24+
25+
exports.sourceNodes = ({ actions, createContentDigest }, { preset = {} }) => {
26+
const { createNode } = actions
27+
28+
const themeUiConfig = {
29+
preset,
30+
}
31+
32+
createNode({
33+
...themeUiConfig,
34+
id: `gatsby-plugin-theme-ui-config`,
35+
parent: null,
36+
children: [],
37+
internal: {
38+
type: `ThemeUiConfig`,
39+
contentDigest: createContentDigest(themeUiConfig),
40+
content: JSON.stringify(themeUiConfig),
41+
description: `Options for gatsby-plugin-theme-ui`,
42+
},
43+
})
44+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useStaticQuery, graphql } from 'gatsby'
2+
3+
const useThemeUiConfig = () => {
4+
const data = useStaticQuery(graphql`
5+
query {
6+
themeUiConfig(id: { eq: "gatsby-plugin-theme-ui-config" }) {
7+
preset
8+
}
9+
}
10+
`)
11+
12+
return data.themeUiConfig
13+
}
14+
15+
export default useThemeUiConfig
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
/** @jsx jsx */
2-
import {
3-
jsx,
4-
ThemeProvider,
5-
} from 'theme-ui'
6-
import theme from './index'
2+
import { jsx, ThemeProvider, merge } from 'theme-ui'
3+
import localTheme from './index'
74
import components from './components'
5+
import useThemeUiConfig from './hooks/configOptions'
86

9-
export const wrapRootElement = ({ element }) =>
10-
jsx(ThemeProvider, {
11-
theme,
12-
components,
13-
},
14-
element,
7+
const Root = ({ children }) => {
8+
const themeUiConfig = useThemeUiConfig()
9+
const { preset } = themeUiConfig
10+
11+
const theme = preset.default || preset
12+
13+
const fullTheme = merge(theme, localTheme)
14+
15+
return (
16+
<ThemeProvider theme={fullTheme} components={components}>
17+
{children}
18+
</ThemeProvider>
1519
)
20+
}
21+
22+
export const wrapRootElement = ({ element }) => {
23+
return <Root>{element}</Root>
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const React = require('react')
2+
const gatsby = jest.requireActual('gatsby')
3+
4+
module.exports = {
5+
...gatsby,
6+
graphql: jest.fn(),
7+
useStaticQuery: jest.fn(() => ({
8+
themeUiConfig: {
9+
preset: {},
10+
},
11+
})),
12+
}

0 commit comments

Comments
 (0)