Get rid of all IFs
I know that this question is pretty old but I would like to contribute with one suggestion
Lately I've come up with a pattern to compute the value using an object, I think this ended up being pretty interesting!
// types.ts
export type CheckboxSize: 'Small' | 'Large'
export interface ICheckbox {
Size: CheckboxSize;
}
// styles.ts
import styled, { css } from 'styled-components'
import type { CheckboxSize } from './types.ts'
interface GetBoxSizeProps {
size: CheckboxSize
}
function getBoxSize ({ size }: GetBoxSizeProps) => ({
'Small': css`
height: 16px;
width: 16px;
`,
'Large': css`
height: 20px;
width: 20px;
`,
}[size])
interface BoxProps {
size: CheckboxSize
}
/*
Just by placing the function here, it will be automatically
called by styled components and computed
*/
const Box = styled.div<BoxProps>`
${getBoxSize}
//other styles...
`
// usage
<Box size="Small" />
<Box size="Large" />
My problem with the old "if" and "switch" styles was that after some props, the styles logic often times grow up into something that's quite verbose and hard to mantain
I'm not clamming this pattern as my invention, but I wasn't able to find something similar anywhere!
Hope this help someone :)
Ps: if you use themes, is possible to pass the property to the function and keep it pure!