From 16ba8153f220dbbb1252be89f9042f07de57f746 Mon Sep 17 00:00:00 2001 From: sky Date: Thu, 18 Mar 2021 14:38:07 -0400 Subject: [PATCH 1/2] Added default theme to useTheme hook --- src/create-use-theme.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/create-use-theme.js b/src/create-use-theme.js index 3a71c71..49648d3 100644 --- a/src/create-use-theme.js +++ b/src/create-use-theme.js @@ -5,9 +5,15 @@ import warning from 'tiny-warning' import isObject from './is-object' export default function createUseTheme(context: Context) { - const useTheme = () => { + const useTheme = (defaultTheme: Theme = null) => { const theme = React.useContext(context) + // Return default theme if not set in the context + // This can be useful if a themed component might not be wrapped in a theme provider + if (!isObject(theme) && isObject(defaultTheme)) { + return defaultTheme; + } + warning(isObject(theme), '[theming] Please use useTheme only with the ThemeProvider') return theme From 61324ef03ec03e556379b2482ed504f7012496c9 Mon Sep 17 00:00:00 2001 From: sky Date: Thu, 18 Mar 2021 14:53:35 -0400 Subject: [PATCH 2/2] Made param nullable --- .size-snapshot.json | 28 ++++++++++++++-------------- src/create-use-theme.js | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.size-snapshot.json b/.size-snapshot.json index e3fe6c5..7eb87c5 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -1,30 +1,30 @@ { "dist/theming.js": { - "bundled": 51727, - "minified": 17238, - "gzipped": 5434 + "bundled": 52050, + "minified": 17275, + "gzipped": 5454 }, "dist/theming.min.js": { - "bundled": 19263, - "minified": 7337, - "gzipped": 2662 + "bundled": 19692, + "minified": 7449, + "gzipped": 2706 }, "dist/theming.cjs.js": { - "bundled": 5974, - "minified": 3855, - "gzipped": 1276 + "bundled": 6283, + "minified": 3907, + "gzipped": 1298 }, "dist/theming.esm.js": { - "bundled": 5502, - "minified": 3457, - "gzipped": 1197, + "bundled": 5811, + "minified": 3509, + "gzipped": 1220, "treeshaked": { "rollup": { - "code": 1573, + "code": 1642, "import_statements": 147 }, "webpack": { - "code": 3069 + "code": 3181 } } } diff --git a/src/create-use-theme.js b/src/create-use-theme.js index 49648d3..b1dc40d 100644 --- a/src/create-use-theme.js +++ b/src/create-use-theme.js @@ -5,7 +5,7 @@ import warning from 'tiny-warning' import isObject from './is-object' export default function createUseTheme(context: Context) { - const useTheme = (defaultTheme: Theme = null) => { + const useTheme = (defaultTheme: ?Theme = null) => { const theme = React.useContext(context) // Return default theme if not set in the context