fix(striker-ui): add inline type SensitiveText and BodyText

main
Tsu-ba-me 2 years ago committed by digimer
parent aa2dbff079
commit 472f634a00
  1. 54
      striker-ui/components/Text/BodyText.tsx
  2. 87
      striker-ui/components/Text/SensitiveText.tsx
  3. 1
      striker-ui/lib/consts/DEFAULT_THEME.ts
  4. 1
      striker-ui/types/SensitiveText.d.ts

@ -8,6 +8,7 @@ import { BLACK, TEXT, UNSELECTED } from '../../lib/consts/DEFAULT_THEME';
type BodyTextOptionalProps = { type BodyTextOptionalProps = {
inheritColour?: boolean; inheritColour?: boolean;
inline?: boolean;
inverted?: boolean; inverted?: boolean;
monospaced?: boolean; monospaced?: boolean;
selected?: boolean; selected?: boolean;
@ -20,6 +21,7 @@ const BODY_TEXT_CLASS_PREFIX = 'BodyText';
const BODY_TEXT_DEFAULT_PROPS: Required<BodyTextOptionalProps> = { const BODY_TEXT_DEFAULT_PROPS: Required<BodyTextOptionalProps> = {
inheritColour: false, inheritColour: false,
inline: false,
inverted: false, inverted: false,
monospaced: false, monospaced: false,
selected: true, selected: true,
@ -68,6 +70,7 @@ const BodyText: FC<BodyTextProps> = ({
children, children,
className, className,
inheritColour: isInheritColour = BODY_TEXT_DEFAULT_PROPS.inheritColour, inheritColour: isInheritColour = BODY_TEXT_DEFAULT_PROPS.inheritColour,
inline: isInline = BODY_TEXT_DEFAULT_PROPS.inline,
inverted: isInvert = BODY_TEXT_DEFAULT_PROPS.inverted, inverted: isInvert = BODY_TEXT_DEFAULT_PROPS.inverted,
monospaced: isMonospace = BODY_TEXT_DEFAULT_PROPS.monospaced, monospaced: isMonospace = BODY_TEXT_DEFAULT_PROPS.monospaced,
selected: isSelect = BODY_TEXT_DEFAULT_PROPS.selected, selected: isSelect = BODY_TEXT_DEFAULT_PROPS.selected,
@ -75,6 +78,11 @@ const BodyText: FC<BodyTextProps> = ({
text = BODY_TEXT_DEFAULT_PROPS.text, text = BODY_TEXT_DEFAULT_PROPS.text,
...muiTypographyRestProps ...muiTypographyRestProps
}) => { }) => {
const sxDisplay = useMemo<string | undefined>(
() => (isInline ? 'inline' : undefined),
[isInline],
);
const baseClassName = useMemo( const baseClassName = useMemo(
() => () =>
buildBodyTextClasses({ buildBodyTextClasses({
@ -89,30 +97,30 @@ const BodyText: FC<BodyTextProps> = ({
return ( return (
<MUITypography <MUITypography
{...{ className={`${baseClassName} ${className}`}
className: `${baseClassName} ${className}`, variant="subtitle1"
variant: 'subtitle1', {...muiTypographyRestProps}
...muiTypographyRestProps, sx={{
sx: { display: sxDisplay,
[`&.${BODY_TEXT_CLASSES.inverted}`]: {
color: BLACK, [`&.${BODY_TEXT_CLASSES.inverted}`]: {
}, color: BLACK,
[`&.${BODY_TEXT_CLASSES.monospaced}`]: {
fontFamily: 'Source Code Pro',
fontWeight: 400,
},
[`&.${BODY_TEXT_CLASSES.selected}`]: {
color: TEXT,
},
[`&.${BODY_TEXT_CLASSES.unselected}`]: {
color: UNSELECTED,
},
...sx,
}, },
[`&.${BODY_TEXT_CLASSES.monospaced}`]: {
fontFamily: 'Source Code Pro',
fontWeight: 400,
},
[`&.${BODY_TEXT_CLASSES.selected}`]: {
color: TEXT,
},
[`&.${BODY_TEXT_CLASSES.unselected}`]: {
color: UNSELECTED,
},
...sx,
}} }}
> >
{content} {content}

@ -1,18 +1,51 @@
import { createElement, FC, ReactNode, useMemo, useState } from 'react'; import { Button, styled } from '@mui/material';
import {
createElement,
FC,
ReactElement,
ReactNode,
useCallback,
useMemo,
useState,
} from 'react';
import { BORDER_RADIUS, EERIE_BLACK } from '../../lib/consts/DEFAULT_THEME';
import BodyText from './BodyText'; import BodyText from './BodyText';
import FlexBox from '../FlexBox'; import FlexBox from '../FlexBox';
import IconButton from '../IconButton'; import IconButton from '../IconButton';
import MonoText from './MonoText'; import MonoText from './MonoText';
const InlineButton = styled(Button)({
backgroundColor: EERIE_BLACK,
borderRadius: BORDER_RADIUS,
minWidth: 'initial',
padding: '0 .6em',
textTransform: 'none',
':hover': {
backgroundColor: `${EERIE_BLACK}F0`,
},
});
const SensitiveText: FC<SensitiveTextProps> = ({ const SensitiveText: FC<SensitiveTextProps> = ({
children, children,
inline: isInline = false,
monospaced: isMonospaced = false, monospaced: isMonospaced = false,
revealInitially: isRevealInitially = false, revealInitially: isRevealInitially = false,
textProps, textProps,
}) => { }) => {
const [isReveal, setIsReveal] = useState<boolean>(isRevealInitially); const [isReveal, setIsReveal] = useState<boolean>(isRevealInitially);
const clickEventHandler = useCallback(() => {
setIsReveal((previous) => !previous);
}, []);
const textSxLineHeight = useMemo<number | string | undefined>(
() => (isInline ? undefined : 2.8),
[isInline],
);
const textElementType = useMemo( const textElementType = useMemo(
() => (isMonospaced ? MonoText : BodyText), () => (isMonospaced ? MonoText : BodyText),
[isMonospaced], [isMonospaced],
@ -27,7 +60,7 @@ const SensitiveText: FC<SensitiveTextProps> = ({
textElementType, textElementType,
{ {
sx: { sx: {
lineHeight: 2.8, lineHeight: textSxLineHeight,
maxWidth: '20em', maxWidth: '20em',
overflowY: 'scroll', overflowY: 'scroll',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
@ -38,27 +71,43 @@ const SensitiveText: FC<SensitiveTextProps> = ({
) )
: children; : children;
} else { } else {
content = createElement(textElementType, textProps, '*****'); content = createElement(
textElementType,
{
sx: {
lineHeight: textSxLineHeight,
},
...textProps,
},
'*****',
);
} }
return content; return content;
}, [children, isReveal, textElementType, textProps]); }, [children, isReveal, textElementType, textProps, textSxLineHeight]);
const rootElement = useMemo<ReactElement>(
return ( () =>
<FlexBox row spacing=".5em"> isInline ? (
{contentElement} <InlineButton onClick={clickEventHandler}>
<IconButton {contentElement}
edge="end" </InlineButton>
mapPreset="visibility" ) : (
onClick={() => { <FlexBox row spacing=".5em">
setIsReveal((previous) => !previous); {contentElement}
}} <IconButton
state={String(isReveal)} edge="end"
sx={{ marginRight: '-.2em', padding: '.2em' }} mapPreset="visibility"
variant="normal" onClick={clickEventHandler}
/> state={String(isReveal)}
</FlexBox> sx={{ marginRight: '-.2em', padding: '.2em' }}
variant="normal"
/>
</FlexBox>
),
[clickEventHandler, contentElement, isInline, isReveal],
); );
return rootElement;
}; };
export default SensitiveText; export default SensitiveText;

@ -13,6 +13,7 @@ export const DIVIDER = '#888';
export const SELECTED_ANVIL = '#00ff00'; export const SELECTED_ANVIL = '#00ff00';
export const DISABLED = '#AAA'; export const DISABLED = '#AAA';
export const BLACK = '#343434'; export const BLACK = '#343434';
export const EERIE_BLACK = '#1F1F1F';
// TODO: remove when old icons are completely replaced. // TODO: remove when old icons are completely replaced.
export const OLD_ICON = '#9da2a7'; export const OLD_ICON = '#9da2a7';

@ -1,4 +1,5 @@
type SensitiveTextOptionalProps = { type SensitiveTextOptionalProps = {
inline?: boolean;
monospaced?: boolean; monospaced?: boolean;
revealInitially?: boolean; revealInitially?: boolean;
textProps?: import('../components/Text').BodyTextProps; textProps?: import('../components/Text').BodyTextProps;

Loading…
Cancel
Save