From 90740eb10a105a166bfc92fe05c56174b645b53c Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Mon, 1 Mar 2021 11:03:36 -0600 Subject: [PATCH] [CSS-in-JS] Support extensions via overrides prop (#4547) * allow custom properties via overrides; type clean up * add some tests * update canopy * overrides -> modify * clean up * Update src/services/theme/README.md Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com> * update readme * Update src/components/common.ts Co-authored-by: Chandler Prall Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com> Co-authored-by: Chandler Prall --- src-docs/src/views/emotion/canopy.tsx | 102 ++++++++++++++++++++++++-- src/components/common.ts | 13 +++- src/services/index.ts | 4 +- src/services/theme/README.md | 27 +++---- src/services/theme/context.ts | 4 +- src/services/theme/hooks.tsx | 10 +-- src/services/theme/index.ts | 4 +- src/services/theme/provider.tsx | 44 ++++++----- src/services/theme/types.ts | 26 ++----- src/services/theme/utils.test.ts | 58 ++++++++++++++- src/services/theme/utils.ts | 41 ++++++----- 11 files changed, 235 insertions(+), 98 deletions(-) diff --git a/src-docs/src/views/emotion/canopy.tsx b/src-docs/src/views/emotion/canopy.tsx index 2b683e320c2..abec3dcdc0c 100644 --- a/src-docs/src/views/emotion/canopy.tsx +++ b/src-docs/src/views/emotion/canopy.tsx @@ -30,6 +30,7 @@ import { computed, euiThemeDefault, buildTheme, + EuiThemeModifications, } from '../../../../src/services'; const View = () => { @@ -76,7 +77,7 @@ const View3 = () => { const overrides = { colors: { light: { euiColorPrimary: '#8A07BD' }, - dark: { euiColorPrimary: '#bd07a5' }, + dark: { euiColorPrimary: '#BD07A5' }, }, }; return ( @@ -84,7 +85,7 @@ const View3 = () => { - + Overriding primary @@ -98,16 +99,16 @@ const View2 = () => { light: { euiColorSecondary: computed( ['colors.euiColorPrimary'], - () => '#85e89d' + () => '#85E89d' ), }, - dark: { euiColorSecondary: '#f0fff4' }, + dark: { euiColorSecondary: '#F0FFF4' }, }, }; return ( <> - + Overriding secondary @@ -176,15 +177,95 @@ export default () => { 'CUSTOM' ); + // Difference is due to automatic colorMode reduction during value computation. + // Makes typing slightly inconvenient, but makes consuming values very convenient. + type ExtensionsUncomputed = { + colors: { light: { myColor: string }; dark: { myColor: string } }; + custom: { + colors: { + light: { customColor: string }; + dark: { customColor: string }; + }; + mySize: number; + }; + }; + type ExtensionsComputed = { + colors: { myColor: string }; + custom: { colors: { customColor: string }; mySize: number }; + }; + + // Type (EuiThemeModifications) only necessary if you want IDE autocomplete support here + const extend: EuiThemeModifications = { + colors: { + light: { + euiColorPrimary: '#F56407', + myColor: computed(['colors.euiColorPrimary'], ([primary]) => primary), + }, + dark: { + euiColorPrimary: '#FA924F', + myColor: computed(['colors.euiColorPrimary'], ([primary]) => primary), + }, + }, + custom: { + colors: { + light: { customColor: '#080AEF' }, + dark: { customColor: '#087EEF' }, + }, + mySize: 5, + }, + }; + + const Extend = () => { + // Generic type (ExtensionsComputed) necessary if accessing extensions/custom properties + const [{ colors, custom }, colorMode] = useEuiTheme(); + return ( +
+
+ {colorMode} +
+            {JSON.stringify({ colors, custom }, null, 2)}
+          
+
+
+

+

+

+

+

+

+
+
+ ); + }; + return ( <> + modify={overrides}>