forked from trezor/trezor-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(suite): Refactor UI variant, sizes and alignments (trezor#11280
- Loading branch information
Showing
26 changed files
with
394 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,61 @@ | ||
import { ReactNode, HTMLAttributes } from 'react'; | ||
import styled, { DefaultTheme, css } from 'styled-components'; | ||
import { borders, spacingsPx } from '@trezor/theme'; | ||
|
||
// TODO: since the fate of Box is unclear, decouple it from the input state | ||
// later those "state" styles should be unified across components | ||
export const getBoxStateBorderColor = ( | ||
state: BoxProps['state'] | undefined, | ||
theme: DefaultTheme, | ||
) => { | ||
switch (state) { | ||
case 'warning': | ||
return theme.textAlertYellow; | ||
case 'error': | ||
return theme.borderAlertRed; | ||
case 'success': | ||
return theme.borderSecondary; | ||
default: | ||
return 'transparent'; | ||
} | ||
import styled, { DefaultTheme } from 'styled-components'; | ||
import { | ||
CSSColor, | ||
Color, | ||
Elevation, | ||
borders, | ||
mapElevationToBackground, | ||
mapElevationToBorder, | ||
spacingsPx, | ||
} from '@trezor/theme'; | ||
import { useElevation } from '../ElevationContext/ElevationContext'; | ||
import { ElevationContext } from '../ElevationContext/ElevationContext'; | ||
import { UIVariant } from '../../config/types'; | ||
|
||
type BoxVariant = Extract<UIVariant, 'primary' | 'warning' | 'destructive' | 'info'>; | ||
|
||
type MapArgs = { | ||
variant: BoxVariant; | ||
theme: DefaultTheme; | ||
}; | ||
|
||
const mapVariantToBackgroundColor = ({ variant, theme }: MapArgs): CSSColor => { | ||
const colorMap: Record<BoxVariant, Color> = { | ||
primary: 'borderSecondary', | ||
info: 'textAlertBlue', | ||
warning: 'textAlertYellow', | ||
destructive: 'borderAlertRed', | ||
}; | ||
|
||
return theme[colorMap[variant]]; | ||
}; | ||
|
||
export interface BoxProps extends HTMLAttributes<HTMLDivElement> { | ||
state?: 'warning' | 'error' | 'success'; | ||
variant?: BoxVariant; | ||
children: ReactNode; | ||
} | ||
|
||
const Wrapper = styled.div<{ state: BoxProps['state'] }>` | ||
const Wrapper = styled.div<{ variant?: BoxVariant; elevation: Elevation }>` | ||
display: flex; | ||
flex: 1; | ||
border-radius: ${borders.radii.sm}; | ||
padding: ${spacingsPx.md}; | ||
border: solid 1px ${({ theme }) => theme.borderOnElevation0}; | ||
${({ state, theme }) => | ||
state && | ||
css` | ||
border-left: 6px solid ${getBoxStateBorderColor(state, theme)}; | ||
`} | ||
${({ state }) => | ||
!state && | ||
css` | ||
padding-left: ${spacingsPx.lg}; | ||
`} | ||
background: ${mapElevationToBackground}; | ||
border: solid 1px ${mapElevationToBorder}; | ||
${({ variant, theme }) => | ||
variant === undefined | ||
? `padding-left: ${spacingsPx.lg};` | ||
: `border-left: 6px solid ${mapVariantToBackgroundColor({ variant, theme })};`} | ||
`; | ||
|
||
const Box = ({ state, children, ...rest }: BoxProps) => ( | ||
<Wrapper state={state} {...rest}> | ||
{children} | ||
</Wrapper> | ||
); | ||
export const Box = ({ variant, children, ...rest }: BoxProps) => { | ||
const { elevation } = useElevation(); | ||
|
||
export { Box }; | ||
return ( | ||
<Wrapper variant={variant} elevation={elevation} {...rest}> | ||
<ElevationContext baseElevation={elevation}>{children}</ElevationContext> | ||
</Wrapper> | ||
); | ||
}; |
Oops, something went wrong.