Skip to content

Commit ff9037d

Browse files
Laurie BarthLaurie Barth
authored andcommitted
let gatsby-plugin-theme-ui take base preset options
1 parent 2e296a0 commit ff9037d

File tree

4 files changed

+124
-6
lines changed

4 files changed

+124
-6
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ 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+
| `themeModule` | `null` | JSON theme object, use the `require` syntax to include it in options. Make sure the package you're requiring is installed in your dependencies. |
24+
| `themeModulePath` | `null` | A string package name that the plugin will require for you. Make sure the package you're requiring is installed in your dependencies. |
25+
| `moduleExportName` | `default` | The name of the export from the theme module, applies to `themeModule` or `themeModulePath` resolution depending which one you're using |
26+
27+
> Note that if your theme is exported at the top level, the `moduleExportName` of `default` is bypassed. See [theme-ui/preset-deep](https://github.com/system-ui/theme-ui/blob/master/packages/preset-deep/src/index.ts).
28+
29+
The theme module you include in options is considered your base theme. Any further customization and shadowing will be merged with it.
30+
31+
### Using options
32+
33+
```js
34+
// gatsby-config.js
35+
module.exports = {
36+
plugins: [
37+
{ resolve: 'gatsby-plugin-theme-ui',
38+
options: {
39+
themeModulePath: '@theme-ui/preset-funk'
40+
// or themeModule: require('@theme-ui/preset-funk')
41+
}
42+
}],
43+
}
44+
```
45+
1946
## Customizing the theme
2047

2148
To customize the theme used in your Gatsby site,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
exports.onPreInit = (__, options) => {
2+
let {themeModulePath} = options
3+
if(themeModulePath) {
4+
options.themeModulePath = require(themeModulePath)
5+
}
6+
}
7+
8+
exports.createSchemaCustomization = ({ actions }) => {
9+
const { createTypes } = actions
10+
11+
createTypes(`
12+
type ThemeUiConfig implements Node {
13+
themeModule: JSON,
14+
themeModulePath: JSON,
15+
moduleExportName: String,
16+
}
17+
`)
18+
}
19+
20+
exports.sourceNodes = (
21+
{ actions, createContentDigest },
22+
{ moduleExportName = 'default', themeModule, themeModulePath}
23+
) => {
24+
const { createNode } = actions
25+
26+
const themeUiConfig = {
27+
themeModule,
28+
themeModulePath,
29+
moduleExportName,
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+
}
45+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
themeModule
8+
themeModulePath
9+
moduleExportName
10+
}
11+
}
12+
`)
13+
14+
return data.themeUiConfig
15+
}
16+
17+
export default useThemeUiConfig

packages/gatsby-plugin-theme-ui/src/provider.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@
22
import {
33
jsx,
44
ThemeProvider,
5+
merge
56
} from 'theme-ui'
67
import theme from './index'
78
import components from './components'
9+
import useThemeUiConfig from './hooks/configOptions'
810

9-
export const wrapRootElement = ({ element }) =>
10-
jsx(ThemeProvider, {
11-
theme,
12-
components,
13-
},
14-
element,
11+
12+
const Root = ({children}) => {
13+
const themeUiConfig = useThemeUiConfig()
14+
const {themeModule, themeModulePath, moduleExportName} = themeUiConfig
15+
16+
let themeWrapper
17+
if (themeModule) {
18+
themeWrapper = themeModule
19+
}
20+
21+
if (themeModulePath) {
22+
themeWrapper = themeModulePath
23+
}
24+
25+
if(themeWrapper && (moduleExportName in themeWrapper)) {
26+
themeWrapper = themeWrapper[moduleExportName]
27+
}
28+
29+
themeWrapper = merge(themeWrapper, {
30+
...theme
31+
})
32+
return (
33+
<ThemeProvider theme={themeWrapper} components={components}>
34+
{children}
35+
</ThemeProvider>
1536
)
37+
}
38+
39+
export const wrapRootElement = ({ element }) => {
40+
return (
41+
<Root>{element}</Root>
42+
)
43+
}
44+

0 commit comments

Comments
 (0)