Skip to content

Commit

Permalink
Merge pull request #1572 from habx/feature/prepare-dark-theme-
Browse files Browse the repository at this point in the history
APP-16937: Add preset on ThemeProvider
  • Loading branch information
habx-auto-merge[bot] committed Dec 9, 2020
2 parents ddb2fc8 + 777018b commit 17b91cd
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 54 deletions.
8 changes: 2 additions & 6 deletions src/Background/Background.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import styled from 'styled-components'

import { theme } from '../theme'

export const BackgroundContainer = styled.div<{
backgroundColor: string
}>`
export const BackgroundContainer = styled.div`
&:not([data-simulated='true']) {
background-color: ${theme.color('primary', {
valuePropName: 'backgroundColor',
})};
background-color: ${theme.color('background')};
}
`
7 changes: 1 addition & 6 deletions src/Background/Background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ export const Background = React.forwardRef<HTMLDivElement, BackgroundProps>(

return (
<BackgroundThemeProvider backgroundColor={backgroundColorWithOpacity}>
<BackgroundContainer
ref={ref}
{...rest}
backgroundColor={backgroundColorWithOpacity}
data-simulated={simulated}
/>
<BackgroundContainer ref={ref} {...rest} data-simulated={simulated} />
</BackgroundThemeProvider>
)
}
Expand Down
5 changes: 4 additions & 1 deletion src/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import * as React from 'react'

import { useCurrentBackground } from '../useCurrentBackground'

import { CardProps } from './Card.interface'
import { CardContainer } from './Card.style'

export const Card = React.forwardRef<HTMLDivElement, CardProps>(
(props, ref) => {
const defaultBackgroundColor = useCurrentBackground()
const {
animated,
flat,
children,
spacing,
backgroundColor = '#FFFFFF',
backgroundColor = defaultBackgroundColor,
...rest
} = props

Expand Down
3 changes: 2 additions & 1 deletion src/HeaderBar/HeaderBar.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BackgroundProps } from '../Background'
import { Color } from '../color'

export interface HeaderBarProps
extends Omit<BackgroundProps, 'backgroundColor'> {
progress?: number
small?: boolean
large?: boolean
backgroundColor?: string
backgroundColor?: Color
sticky?: boolean
}
3 changes: 2 additions & 1 deletion src/Layout/Layout.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BackgroundProps } from '../Background'
import { Color } from '../color'

export interface LayoutProps extends Omit<BackgroundProps, 'backgroundColor'> {
backgroundColor?: string
backgroundColor?: Color
}

export enum LayoutChild {
Expand Down
11 changes: 7 additions & 4 deletions src/LightBox/LightBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ReactDOM from 'react-dom'
import { isFunction } from '../_internal/data'
import { isClientSide } from '../_internal/ssr'
import { ANIMATION_DURATIONS } from '../animations'
import { useCurrentBackground } from '../useCurrentBackground'
import { withTriggerElement } from '../withTriggerElement'

import { LightBoxInnerProps } from './LightBox.interface'
Expand All @@ -30,10 +31,12 @@ const InnerLightBox = React.forwardRef<HTMLDivElement, LightBoxInnerProps>(
animationDuration: ANIMATION_DURATIONS.l,
})

const backgroundColor = useCurrentBackground({ useRootTheme: true })

const content = (
<LightBoxContainer
ref={ref}
backgroundColor="#FFFFFF"
backgroundColor={backgroundColor}
data-state={modal.state}
data-spacing={spacing}
{...rest}
Expand All @@ -51,6 +54,6 @@ const InnerLightBox = React.forwardRef<HTMLDivElement, LightBoxInnerProps>(
}
)

export const LightBox = withTriggerElement<HTMLDivElement>()<
LightBoxInnerProps
>(InnerLightBox)
export const LightBox = withTriggerElement<HTMLDivElement>()<LightBoxInnerProps>(
InnerLightBox
)
4 changes: 1 addition & 3 deletions src/Menu/Menu.style.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import styled from 'styled-components'

import { Background } from '../Background'
import { theme } from '../theme'

export const FloatingMenuContainer = styled(Background)`
background-color: ${theme.color('background', { useRootTheme: true })};
export const FloatingMenuContainer = styled.div`
box-shadow: ${theme.shadow()};
border-radius: 4px;
`
Expand Down
13 changes: 2 additions & 11 deletions src/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { buildUseOnlyOpenedInstanceHook } from '../_internal/useOnlyOpenedInstan
import { useWindowSize } from '../_internal/useWindowSize'
import { breakpoints } from '../breakpoints'
import { TogglePanel, TogglePanelProps } from '../TogglePanel'
import { useCurrentBackground } from '../useCurrentBackground'
import { withTriggerElement } from '../withTriggerElement'

import { MenuContext } from './Menu.context'
Expand Down Expand Up @@ -38,7 +37,6 @@ export const InnerMenu = React.forwardRef<HTMLDivElement, InnerMenuProps>(
useOnlyOneMenuOpened({ open, onClose })

const size = useWindowSize()
const background = useCurrentBackground({ useRootTheme: true })

const getChildren = React.useCallback(
(modal: Modal<HTMLDivElement>) => {
Expand All @@ -49,7 +47,7 @@ export const InnerMenu = React.forwardRef<HTMLDivElement, InnerMenuProps>(
{fullScreenOnMobile && size.width < breakpoints.raw.phone ? (
<FullScreenMenu>{content}</FullScreenMenu>
) : (
<FloatingMenuContainer backgroundColor={background}>
<FloatingMenuContainer>
<FloatingMenu data-scrollable={scrollable}>
{content}
</FloatingMenu>
Expand All @@ -58,14 +56,7 @@ export const InnerMenu = React.forwardRef<HTMLDivElement, InnerMenuProps>(
</MenuContext.Provider>
)
},
[
background,
children,
fullScreenOnMobile,
onClose,
scrollable,
size.width,
]
[children, fullScreenOnMobile, onClose, scrollable, size.width]
)

const setStyle = React.useCallback<Required<TogglePanelProps>['setStyle']>(
Expand Down
5 changes: 4 additions & 1 deletion src/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ReactDOM from 'react-dom'

import { isFunction } from '../_internal/data'
import { isClientSide } from '../_internal/ssr'
import { useCurrentBackground } from '../useCurrentBackground'
import { withTriggerElement } from '../withTriggerElement'

import { ModalInnerProps } from './Modal.interface'
Expand Down Expand Up @@ -42,10 +43,12 @@ const InnerModal = React.forwardRef<HTMLDivElement, ModalInnerProps>(
animationDuration: ANIMATION_DURATION,
})

const backgroundColor = useCurrentBackground({ useRootTheme: true })

const content = (
<ModalOverlay data-state={modal.state} data-testid="modal-overlay">
<ModalContainer
backgroundColor="#FFFFFF"
backgroundColor={backgroundColor}
ref={modal.ref}
data-testid="modal-container"
data-width={width}
Expand Down
8 changes: 4 additions & 4 deletions src/Select/Select.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const SelectContainer = styled.div`
--select-border-width: 1.5px;
&[data-background='true'] {
--select-background-color: #fff;
--select-border-color: ${theme.neutralColor(200)};
--select-background-color: ${theme.neutralColor(100)};
--select-border-color: ${theme.neutralColor(300)};
}
${({ color }) =>
Expand All @@ -92,7 +92,7 @@ export const SelectContainer = styled.div`
}
&[data-light='true'] {
--select-background-color: #fff;
--select-background-color: ${theme.color('background')};
font-size: ${fontScale.mars.size}px;
padding: 0 12px;
Expand Down Expand Up @@ -143,7 +143,7 @@ export const SelectContainer = styled.div`
}
&:not([data-light='true']) {
background-color: #fff;
background-color: ${theme.neutralColor(100)};
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/TextInput/TextInput.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export const inputStyle = css`
}
&[data-background='true'] {
background-color: #fff;
border-color: #fff;
background-color: ${theme.neutralColor(100)};
border-color: ${theme.neutralColor(300)};
}
&[data-error='true'] {
Expand Down
5 changes: 2 additions & 3 deletions src/ThemeProvider/ThemeProvider.interface.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import * as React from 'react'

import { Color } from '../color'
import { ThemeVariant } from '../theme'

type PartialRecursive<T> = T extends object
? { [K in keyof T]?: PartialRecursive<T[K]> }
: T

export interface DesignSystemThemePatch {
backgroundColor?: Color
light?: PartialRecursive<ThemeVariant>
dark?: PartialRecursive<ThemeVariant>
}

export interface ThemeProviderProps {
theme: DesignSystemThemePatch
theme?: DesignSystemThemePatch
preset?: 'dark' | 'light'
children?: React.ReactNode
}
7 changes: 7 additions & 0 deletions src/ThemeProvider/ThemeProvider.style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styled from 'styled-components'

import { theme } from '../theme'

export const ThemeProviderContainer = styled.div`
background-color: ${theme.color('background')};
`
28 changes: 18 additions & 10 deletions src/ThemeProvider/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ThemeContext,
} from 'styled-components'

import { isColorDark } from '../color'
import {
DesignSystemTheme,
DesignSystemProviderValue,
Expand All @@ -15,9 +14,11 @@ import {
import { ThemeVariant } from '../theme'

import { ThemeProviderProps } from './ThemeProvider.interface'
import { ThemeProviderContainer } from './ThemeProvider.style'

export const ThemeProvider: React.FunctionComponent<ThemeProviderProps> = ({
theme,
preset = 'light',
children,
}) => {
const styledTheme = React.useContext<StyledTheme>(ThemeContext)
Expand All @@ -28,30 +29,37 @@ export const ThemeProvider: React.FunctionComponent<ThemeProviderProps> = ({
const newLightVariant: ThemeVariant = merge.recursive(
true,
currentTheme.light,
theme.light ?? {}
theme?.light ?? {}
)

const newDarkVariant: ThemeVariant = merge.recursive(
true,
currentTheme.dark,
theme.dark ?? {}
theme?.dark ?? {}
)

const newBackgroundColor =
theme.backgroundColor ?? currentTheme.backgroundColor

const newTheme: DesignSystemTheme = {
light: newLightVariant,
dark: newDarkVariant,
isDark: isColorDark(newBackgroundColor),
backgroundColor: newBackgroundColor,
isDark: preset === 'dark',
backgroundColor:
preset === 'dark'
? newDarkVariant.defaultBackground
: newLightVariant.defaultBackground,
}

return {
value: newTheme,
rootValue: newTheme,
}
}, [currentTheme, theme])
}, [
currentTheme.backgroundColor,
currentTheme.dark,
currentTheme.light,
preset,
theme?.dark,
theme?.light,
])

const newStyledTheme = React.useMemo<StyledTheme>(
() => ({
Expand All @@ -63,7 +71,7 @@ export const ThemeProvider: React.FunctionComponent<ThemeProviderProps> = ({

return (
<BaseThemeProvider theme={newStyledTheme}>
<React.Fragment>{children}</React.Fragment>
<ThemeProviderContainer>{children}</ThemeProviderContainer>
</BaseThemeProvider>
)
}
3 changes: 2 additions & 1 deletion src/TogglePanel/TogglePanel.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import styled from 'styled-components'

import { zIndex } from '../_internal/theme/zIndex'
import { animations } from '../animations'
import { Background } from '../Background'

export const Container = styled.div`
export const Container = styled(Background)`
position: fixed;
opacity: 1;
z-index: ${zIndex.dropDowns};
Expand Down
4 changes: 4 additions & 0 deletions src/TogglePanel/TogglePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useWindowSize } from '../_internal/useWindowSize'
import { ANIMATION_DURATIONS } from '../animations'
import { breakpoints } from '../breakpoints'
import { Modal } from '../Modal'
import { useCurrentBackground } from '../useCurrentBackground'
import { withTriggerElement } from '../withTriggerElement'

import { InnerTogglePanelProps } from './TogglePanel.interface'
Expand Down Expand Up @@ -100,6 +101,8 @@ const InnerTogglePanel = React.forwardRef<

const parent = React.useContext(Context)

const backgroundColor = useCurrentBackground({ useRootTheme: true })

if (!isClientSide) {
return null
}
Expand All @@ -121,6 +124,7 @@ const InnerTogglePanel = React.forwardRef<
{withOverlay && modal.state === 'opened' && <Overlay />}

<Container
backgroundColor={backgroundColor}
data-state={modal.state}
ref={modal.ref}
style={customStyle}
Expand Down
3 changes: 3 additions & 0 deletions src/theme/theme.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const LIGHT_THEME_VARIANT: ThemeVariant = {
{ x: 0, y: 16, blur: 36, opacity: 0.15 },
],
},
defaultBackground: palette.neutralWhiteWithIntensityFading[1000],
}

const DARK_THEME_VARIANT: ThemeVariant = {
Expand Down Expand Up @@ -168,6 +169,8 @@ const DARK_THEME_VARIANT: ThemeVariant = {
{ x: 0, y: 16, blur: 36, opacity: 0.25 },
],
},

defaultBackground: palette.neutralBlackWithIntensityFading[900],
}

export const DEFAULT_THEME: DesignSystemTheme = {
Expand Down
1 change: 1 addition & 0 deletions src/theme/theme.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface ThemeVariant {
typography: Typography
neutralColor: NeutralGradients
shadows: Shadows
defaultBackground: Color
}

export interface DesignSystemTheme {
Expand Down

0 comments on commit 17b91cd

Please sign in to comment.