Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

RFC: enhanceName and replaceName to customize styles #1867

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,86 @@
import * as React from 'react'
import { Button } from '@stardust-ui/react'
import {
Button,
ButtonProps,
ComponentSlotStylesInput,
ComponentVariablesPrepared,
Flex,
Provider,
ThemeInput,
} from '@stardust-ui/react'

const ButtonExample = () => <Button content="Click here" />
import { ButtonVariables } from 'src/themes/teams/components/Button/buttonVariables'

type ButtonTertiaryVariables = {
backgroundColor: string
color: string
colorHover: string
fontWeight: string
fontWeightHover: string
}

type CustomTheme = ThemeInput & {
componentStyles: {
ButtonTertiary: ComponentSlotStylesInput<ButtonProps, ButtonTertiaryVariables>
}
componentVariables: { ButtonTertiary: ComponentVariablesPrepared }
}

const customTheme: CustomTheme = {
componentStyles: {
'Button:Disabled': {
root: { cursor: 'not-allowed' },
},
ButtonTertiary: {
root: ({ variables: v }) => ({
backgroundColor: v.backgroundColor,
border: 0,
color: v.color,
fontWeight: v.fontWeight,
textDecoration: 'underline',

':hover': {
color: v.colorHover,
cursor: 'pointer',
fontWeight: v.fontWeightHover,
},
}),
},
},
componentVariables: {
'Button:Danger': (siteVariables): Partial<ButtonVariables> => ({
backgroundColor: siteVariables.colorScheme.red.background1,
backgroundColorHover: siteVariables.colorScheme.red.backgroundHover,
color: siteVariables.colorScheme.red.foreground,
colorHover: siteVariables.colorScheme.red.foregroundHover,
borderColorHover: siteVariables.colorScheme.red.background3,
}),
ButtonTertiary: (siteVariables): ButtonTertiaryVariables => ({
backgroundColor: 'transparent',
color: siteVariables.colorScheme.brand.foreground,
colorHover: siteVariables.colorScheme.brand.foregroundHover,
fontWeight: siteVariables.fontWeightRegular,
fontWeightHover: siteVariables.fontWeightSemibold,
}),
},
}

const ButtonExample = () => (
<>
<Provider theme={customTheme}>
<Flex gap="gap.medium">
<Button content="Button:Danger" enhanceName="Button:Danger" />
<Button content="Button:Disabled" disabled enhanceName="Button:Disabled" />

<Button content="ButtonTertiary" replaceName="ButtonTertiary" />
</Flex>
</Provider>
<div style={{ margin: 20 }}>
<button onClick={() => window.switchTheme('teams')}>Light theme</button>
<button onClick={() => window.switchTheme('teamsDark')}>Dark theme</button>
<button onClick={() => window.switchTheme('teamsHighContrast')}>HC theme</button>
</div>
</>
)

export default ButtonExample
6 changes: 6 additions & 0 deletions packages/react/src/lib/commonPropInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { ComponentVariablesInput, ComponentSlotStyle, AnimationProp } from '../t
import { ReactChildren } from '../types'

export interface StyledComponentProps<P = any, V = any> {
/** FIX ME */
enhanceName?: string

/** FIX ME */
replaceName?: string

/** Additional CSS styles to apply to the component instance. */
styles?: ComponentSlotStyle<P, V>

Expand Down
20 changes: 18 additions & 2 deletions packages/react/src/lib/renderComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,24 @@ const renderComponent = <P extends {}>(
const ElementType = getElementType({ defaultProps }, props) as React.ReactType<P>
const stateAndProps = { ...state, ...props }

/** FIX ME */
if (props.enhanceName && props.replaceName) {
throw new Error('"enhanceName" and "replaceName" props can not be combined')
}

/** FIX ME */
if (props.enhanceName) {
if (!props.enhanceName.startsWith(`${displayName}:`)) {
throw new Error(
`"enhanceName" prop should start with "displayName" of matching component, i.e. "${displayName}:Variant"`,
)
}
}

// Resolve variables for this component, allow props.variables to override
const resolvedVariables: ComponentVariablesObject = mergeComponentVariables(
theme.componentVariables[displayName],
theme.componentVariables[props.replaceName || displayName],
theme.componentVariables[props.enhanceName],
props.variables,
)(theme.siteVariables)

Expand All @@ -178,7 +193,8 @@ const renderComponent = <P extends {}>(

// Resolve styles using resolved variables, merge results, allow props.styles to override
const mergedStyles: ComponentSlotStylesPrepared = mergeComponentStyles(
theme.componentStyles[displayName],
theme.componentStyles[props.replaceName || displayName],
theme.componentStyles[props.enhanceName],
{ root: props.styles },
{ root: animationCSSProp },
)
Expand Down