parent
1c0ddd59b0
commit
74495758e0
4 changed files with 122 additions and 63 deletions
@ -1,78 +1,108 @@ |
|||||||
import { styled, Typography, TypographyProps } from '@mui/material'; |
import { FC, ReactNode } from 'react'; |
||||||
|
import { |
||||||
|
Typography as MUITypography, |
||||||
|
TypographyProps as MUITypographyProps, |
||||||
|
} from '@mui/material'; |
||||||
|
|
||||||
import { BLACK, TEXT, UNSELECTED } from '../../lib/consts/DEFAULT_THEME'; |
import { BLACK, TEXT, UNSELECTED } from '../../lib/consts/DEFAULT_THEME'; |
||||||
|
|
||||||
const PREFIX = 'BodyText'; |
type BodyTextOptionalProps = { |
||||||
|
inverted?: boolean; |
||||||
|
monospaced?: boolean; |
||||||
|
selected?: boolean; |
||||||
|
text?: null | ReactNode | string; |
||||||
|
}; |
||||||
|
|
||||||
|
type BodyTextProps = MUITypographyProps & BodyTextOptionalProps; |
||||||
|
|
||||||
|
const BODY_TEXT_CLASS_PREFIX = 'BodyText'; |
||||||
|
|
||||||
const classes = { |
const BODY_TEXT_DEFAULT_PROPS: Required<BodyTextOptionalProps> = { |
||||||
inverted: `${PREFIX}-inverted`, |
inverted: false, |
||||||
selected: `${PREFIX}-selected`, |
monospaced: false, |
||||||
unselected: `${PREFIX}-unselected`, |
selected: true, |
||||||
|
text: null, |
||||||
}; |
}; |
||||||
|
|
||||||
const StyledTypography = styled(Typography)(() => ({ |
const BODY_TEXT_CLASSES = { |
||||||
[`&.${classes.inverted}`]: { |
inverted: `${BODY_TEXT_CLASS_PREFIX}-inverted`, |
||||||
color: BLACK, |
monospaced: `${BODY_TEXT_CLASS_PREFIX}-monospaced`, |
||||||
}, |
selected: `${BODY_TEXT_CLASS_PREFIX}-selected`, |
||||||
|
unselected: `${BODY_TEXT_CLASS_PREFIX}-unselected`, |
||||||
|
}; |
||||||
|
|
||||||
[`&.${classes.selected}`]: { |
const buildBodyTextClasses = ({ |
||||||
color: TEXT, |
isInvert, |
||||||
}, |
isMonospace, |
||||||
|
isSelect, |
||||||
|
}: { |
||||||
|
isInvert?: boolean; |
||||||
|
isMonospace?: boolean; |
||||||
|
isSelect?: boolean; |
||||||
|
}) => { |
||||||
|
const bodyTextClasses: string[] = []; |
||||||
|
|
||||||
[`&.${classes.unselected}`]: { |
if (isInvert) { |
||||||
color: UNSELECTED, |
bodyTextClasses.push(BODY_TEXT_CLASSES.inverted); |
||||||
}, |
} else if (isSelect) { |
||||||
})); |
bodyTextClasses.push(BODY_TEXT_CLASSES.selected); |
||||||
|
} else { |
||||||
|
bodyTextClasses.push(BODY_TEXT_CLASSES.unselected); |
||||||
|
} |
||||||
|
|
||||||
type BodyTextProps = TypographyProps & { |
if (isMonospace) { |
||||||
inverted?: boolean; |
bodyTextClasses.push(BODY_TEXT_CLASSES.monospaced); |
||||||
selected?: boolean; |
} |
||||||
text: string; |
|
||||||
|
return bodyTextClasses.join(' '); |
||||||
}; |
}; |
||||||
|
|
||||||
const BodyText = ({ |
const BodyText: FC<BodyTextProps> = ({ |
||||||
inverted, |
children, |
||||||
selected, |
inverted = BODY_TEXT_DEFAULT_PROPS.inverted, |
||||||
|
monospaced = BODY_TEXT_DEFAULT_PROPS.monospaced, |
||||||
|
selected = BODY_TEXT_DEFAULT_PROPS.selected, |
||||||
sx, |
sx, |
||||||
text, |
text = BODY_TEXT_DEFAULT_PROPS.text, |
||||||
}: BodyTextProps): JSX.Element => { |
...muiTypographyRestProps |
||||||
const buildBodyTextClasses = ({ |
}) => ( |
||||||
isInvert, |
<MUITypography |
||||||
isSelect, |
{...{ |
||||||
}: { |
className: buildBodyTextClasses({ |
||||||
isInvert?: boolean; |
|
||||||
isSelect?: boolean; |
|
||||||
}) => { |
|
||||||
let bodyTextClasses = ''; |
|
||||||
|
|
||||||
if (isInvert) { |
|
||||||
bodyTextClasses += classes.inverted; |
|
||||||
} else if (isSelect) { |
|
||||||
bodyTextClasses += classes.selected; |
|
||||||
} else { |
|
||||||
bodyTextClasses += classes.unselected; |
|
||||||
} |
|
||||||
|
|
||||||
return bodyTextClasses; |
|
||||||
}; |
|
||||||
|
|
||||||
return ( |
|
||||||
<StyledTypography |
|
||||||
{...{ sx }} |
|
||||||
className={buildBodyTextClasses({ |
|
||||||
isInvert: inverted, |
isInvert: inverted, |
||||||
|
isMonospace: monospaced, |
||||||
isSelect: selected, |
isSelect: selected, |
||||||
})} |
}), |
||||||
variant="subtitle1" |
variant: 'subtitle1', |
||||||
> |
...muiTypographyRestProps, |
||||||
{text} |
sx: { |
||||||
</StyledTypography> |
[`&.${BODY_TEXT_CLASSES.inverted}`]: { |
||||||
); |
color: BLACK, |
||||||
}; |
}, |
||||||
|
|
||||||
BodyText.defaultProps = { |
[`&.${BODY_TEXT_CLASSES.monospaced}`]: { |
||||||
inverted: false, |
fontFamily: 'Source Code Pro', |
||||||
selected: true, |
fontWeight: 400, |
||||||
}; |
}, |
||||||
|
|
||||||
|
[`&.${BODY_TEXT_CLASSES.selected}`]: { |
||||||
|
color: TEXT, |
||||||
|
}, |
||||||
|
|
||||||
|
[`&.${BODY_TEXT_CLASSES.unselected}`]: { |
||||||
|
color: UNSELECTED, |
||||||
|
}, |
||||||
|
|
||||||
|
...sx, |
||||||
|
}, |
||||||
|
}} |
||||||
|
> |
||||||
|
{text ?? children} |
||||||
|
</MUITypography> |
||||||
|
); |
||||||
|
|
||||||
|
BodyText.defaultProps = BODY_TEXT_DEFAULT_PROPS; |
||||||
|
|
||||||
|
export type { BodyTextProps }; |
||||||
|
|
||||||
export default BodyText; |
export default BodyText; |
||||||
|
@ -0,0 +1,27 @@ |
|||||||
|
import { FC } from 'react'; |
||||||
|
|
||||||
|
import { |
||||||
|
BORDER_RADIUS, |
||||||
|
MONOSPACE_BACKGROUND, |
||||||
|
} from '../../lib/consts/DEFAULT_THEME'; |
||||||
|
|
||||||
|
import BodyText, { BodyTextProps } from './BodyText'; |
||||||
|
|
||||||
|
const Monospace: FC<BodyTextProps> = ({ sx, ...bodyTextRestProps }) => ( |
||||||
|
<BodyText |
||||||
|
{...{ |
||||||
|
...bodyTextRestProps, |
||||||
|
monospaced: true, |
||||||
|
sx: { |
||||||
|
backgroundColor: MONOSPACE_BACKGROUND, |
||||||
|
borderRadius: BORDER_RADIUS, |
||||||
|
display: 'inline', |
||||||
|
padding: '.1rem .3rem', |
||||||
|
|
||||||
|
...sx, |
||||||
|
}, |
||||||
|
}} |
||||||
|
/> |
||||||
|
); |
||||||
|
|
||||||
|
export default Monospace; |
@ -1,4 +1,5 @@ |
|||||||
import HeaderText from './HeaderText'; |
|
||||||
import BodyText from './BodyText'; |
import BodyText from './BodyText'; |
||||||
|
import HeaderText from './HeaderText'; |
||||||
|
import Monospace from './Monospace'; |
||||||
|
|
||||||
export { HeaderText, BodyText }; |
export { BodyText, HeaderText, Monospace }; |
||||||
|
Loading…
Reference in new issue