From 9b2086fc551b683570a78994b931861350134858 Mon Sep 17 00:00:00 2001 From: Alex Nicholls Date: Fri, 18 Oct 2019 13:45:17 -0700 Subject: [PATCH] feat(core): Add helper to get theme if ThemeProvider doesn't exist --- modules/avatar/react/lib/Test.tsx | 13 +++++---- modules/banner/react/lib/Banner.tsx | 36 ++++++++++++++++--------- modules/core/react/lib/theming/theme.ts | 11 ++++++++ 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/modules/avatar/react/lib/Test.tsx b/modules/avatar/react/lib/Test.tsx index 29c9f53d9b..0d5ca0fd73 100644 --- a/modules/avatar/react/lib/Test.tsx +++ b/modules/avatar/react/lib/Test.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import styled from '@emotion/styled'; -import {Themeable, defaultCanvasTheme} from '@workday/canvas-kit-react-core'; +import {Themeable, getTheme} from '@workday/canvas-kit-react-core'; export interface TestProps extends Themeable {} @@ -8,10 +8,13 @@ const SomeText = styled('div')( { boxSizing: 'border-box', }, - ({theme}) => ({ - background: theme.palette.primary.main, - color: theme.palette.primary.contrast, - }) + ({theme}) => { + theme = getTheme(theme); // eslint-disable-line no-param-reassign + return { + background: theme.palette.primary.main, + color: theme.palette.primary.contrast, + }; + } ); class Test extends React.Component { diff --git a/modules/banner/react/lib/Banner.tsx b/modules/banner/react/lib/Banner.tsx index a09d05835b..650889272d 100644 --- a/modules/banner/react/lib/Banner.tsx +++ b/modules/banner/react/lib/Banner.tsx @@ -1,6 +1,13 @@ import * as React from 'react'; import styled from '@emotion/styled'; -import {colors, spacing, borderRadius, type, Themeable} from '@workday/canvas-kit-react-core'; +import { + colors, + spacing, + borderRadius, + type, + Themeable, + getTheme, +} from '@workday/canvas-kit-react-core'; import {SystemIcon} from '@workday/canvas-kit-react-icon'; import {exclamationCircleIcon, exclamationTriangleIcon} from '@workday/canvas-system-icons-web'; import {ErrorType, focusRing} from '@workday/canvas-kit-react-common'; @@ -51,19 +58,22 @@ const BannerWrapper = styled('button')( cursor: 'pointer', }, }, - ({error, theme}) => ({ - backgroundColor: - error === ErrorType.Error ? theme.palette.error.main : theme.palette.alert.main, - color: error === ErrorType.Error ? theme.palette.error.contrast : colors.blackPepper400, - '&:hover': { + ({error, theme}) => { + theme = getTheme(theme); // eslint-disable-line no-param-reassign + return { backgroundColor: - error === ErrorType.Error ? theme.palette.error.dark : theme.palette.alert.dark, - }, - '&:focus': { - outline: 'none', - ...focusRing(2, 2, undefined, undefined, undefined, theme.palette.common.focusOutline), - }, - }), + error === ErrorType.Error ? theme.palette.error.main : theme.palette.alert.main, + color: error === ErrorType.Error ? theme.palette.error.contrast : colors.blackPepper400, + '&:hover': { + backgroundColor: + error === ErrorType.Error ? theme.palette.error.dark : theme.palette.alert.dark, + }, + '&:focus': { + outline: 'none', + ...focusRing(2, 2, undefined, undefined, undefined, theme.palette.common.focusOutline), + }, + }; + }, ({variant}) => ({ borderRadius: variant === BannerVariant.Sticky ? `${borderRadius.m} 0 0 ${borderRadius.m}` : borderRadius.m, diff --git a/modules/core/react/lib/theming/theme.ts b/modules/core/react/lib/theming/theme.ts index 2c327b9bc3..d699b956fc 100644 --- a/modules/core/react/lib/theming/theme.ts +++ b/modules/core/react/lib/theming/theme.ts @@ -46,6 +46,17 @@ export const defaultCanvasTheme: CanvasTheme = { // Depth? }; +/** + * Currently a work around for a default theme if no ThemeProvider exists. + * Tracked on https://github.com/emotion-js/emotion/issues/1193. + */ +export function getTheme(theme: Object): CanvasTheme { + if (Object.entries(theme).length === 0) { + return defaultCanvasTheme; + } + return theme as CanvasTheme; +} + // TODO: Should we use PartialCanvasTheme here? export function createCanvasTheme(partialTheme: Object): CanvasTheme { return deepmerge(defaultCanvasTheme, partialTheme);