Skip to content

Commit c3c3849

Browse files
Laurie BarthLaurie Barth
authored andcommitted
refactor and pair down to a singular option
1 parent 66f9d9b commit c3c3849

File tree

4 files changed

+50
-75
lines changed

4 files changed

+50
-75
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ prevent a flash of unstyled colors when using color modes.
2020

2121
| Key | Default value | Description |
2222
| ------------------------ | ---------------- | -------------------------------------------------------------------------------- |
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-
23+
| `themePreset` | `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. |
2624

2725
> Note that this plugin assumes the theme object is exported as `default`.
2826
Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
11
exports.onPreInit = (__, options) => {
2-
let {themeModulePath} = options
3-
4-
if(themeModulePath) {
5-
try {
6-
options.themeModulePath = require(themeModulePath)
7-
} catch {
8-
reporter.error(`It appears your theme dependency is not installed. Try running \`${generateInstallInstructions()} ${themeModulePath}\``)
9-
}
2+
let { themePreset } = options
3+
4+
if (typeof themePreset === 'string') {
5+
try {
6+
options.themePreset = require(themePreset)
7+
} catch {
8+
reporter.error(
9+
`It appears your theme dependency is not installed. Try running \`${generateInstallInstructions()} ${themeModule}\``
10+
)
11+
}
1012
}
1113
}
1214

13-
1415
function generateInstallInstructions() {
1516
const { getConfigStore } = require(`gatsby-core-utils`)
1617

1718
const packageMangerConfigKey = `cli.packageManager`
1819
const PACKAGE_MANGER = getConfigStore().get(packageMangerConfigKey) || `yarn`
1920

20-
const installKeyWord = PACKAGE_MANGER === `yarn` ? "add" : "install"
21+
const installKeyWord = PACKAGE_MANGER === `yarn` ? 'add' : 'install'
2122

2223
return `${PACKAGE_MANGER} ${installKeyWord}`
2324
}
2425

2526
exports.createSchemaCustomization = ({ actions }) => {
26-
const { createTypes } = actions
27-
28-
createTypes(`
27+
const { createTypes } = actions
28+
29+
createTypes(`
2930
type ThemeUiConfig implements Node {
30-
themeModule: JSON,
31-
themeModulePath: JSON,
31+
themePreset: JSON,
3232
}
3333
`)
34+
}
35+
36+
exports.sourceNodes = (
37+
{ actions, createContentDigest },
38+
{ themePreset = {} }
39+
) => {
40+
const { createNode } = actions
41+
42+
const themeUiConfig = {
43+
themePreset,
3444
}
35-
36-
exports.sourceNodes = (
37-
{ actions, createContentDigest },
38-
{ themeModule, themeModulePath}
39-
) => {
40-
const { createNode } = actions
41-
42-
const themeUiConfig = {
43-
themeModule,
44-
themeModulePath,
45-
}
46-
47-
createNode({
48-
...themeUiConfig,
49-
id: `gatsby-plugin-theme-ui-config`,
50-
parent: null,
51-
children: [],
52-
internal: {
53-
type: `ThemeUiConfig`,
54-
contentDigest: createContentDigest(themeUiConfig),
55-
content: JSON.stringify(themeUiConfig),
56-
description: `Options for gatsby-plugin-theme-ui`,
57-
},
58-
})
59-
}
6045

46+
createNode({
47+
...themeUiConfig,
48+
id: `gatsby-plugin-theme-ui-config`,
49+
parent: null,
50+
children: [],
51+
internal: {
52+
type: `ThemeUiConfig`,
53+
contentDigest: createContentDigest(themeUiConfig),
54+
content: JSON.stringify(themeUiConfig),
55+
description: `Options for gatsby-plugin-theme-ui`,
56+
},
57+
})
58+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { useStaticQuery, graphql } from "gatsby"
1+
import { useStaticQuery, graphql } from 'gatsby'
22

33
const useThemeUiConfig = () => {
44
const data = useStaticQuery(graphql`
55
query {
66
themeUiConfig(id: { eq: "gatsby-plugin-theme-ui-config" }) {
7-
themeModule
8-
themeModulePath
7+
themePreset
98
}
109
}
1110
`)
1211

1312
return data.themeUiConfig
1413
}
1514

16-
export default useThemeUiConfig
15+
export default useThemeUiConfig
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,24 @@
11
/** @jsx jsx */
2-
import {
3-
jsx,
4-
ThemeProvider,
5-
merge
6-
} from 'theme-ui'
2+
import { jsx, ThemeProvider, merge } from 'theme-ui'
73
import theme from './index'
84
import components from './components'
95
import useThemeUiConfig from './hooks/configOptions'
106

11-
12-
const Root = ({children}) => {
7+
const Root = ({ children }) => {
138
const themeUiConfig = useThemeUiConfig()
14-
const {themeModule, themeModulePath} = themeUiConfig
9+
const { themePreset } = themeUiConfig
1510

16-
let themeWrapper
17-
if (themeModule) {
18-
themeWrapper = themeModule
19-
}
11+
const theme = themePreset.default || themePreset
2012

21-
if (themeModulePath) {
22-
themeWrapper = themeModulePath
23-
}
24-
25-
if(themeWrapper && ('default' in themeWrapper)) {
26-
themeWrapper = themeWrapper.default
27-
}
13+
const fullTheme = merge(theme, localTheme)
2814

29-
themeWrapper = merge(themeWrapper, {
30-
...theme
31-
})
3215
return (
33-
<ThemeProvider theme={themeWrapper} components={components}>
34-
{children}
16+
<ThemeProvider theme={fullTheme} components={components}>
17+
{children}
3518
</ThemeProvider>
3619
)
3720
}
3821

3922
export const wrapRootElement = ({ element }) => {
40-
return (
41-
<Root>{element}</Root>
42-
)
23+
return <Root>{element}</Root>
4324
}
44-

0 commit comments

Comments
 (0)