// @flow strict import * as React from 'react'; import type {ColorTypes} from '../../types/typography'; import {TEXT_COLORS} from '../../types/typography'; import classify from '../../utils/classify'; import {escapeRegExp} from '../../utils/string'; import css from '../../styles/typography.module.css'; export type TextProps = { color?: ColorTypes, children?: React.Node, className?: string, highlightedTextClassName?: string, highlightString?: string | string[], caseSensitiveHighlighting?: boolean, highlightWithBackground?: boolean, ... }; export type HighlightTextProps = { text: string, highlight: string | string[], highlightClassName?: string, caseSensitiveHighlighting?: boolean, highlightWithBackground?: boolean, }; const HighlightText: React$AbstractComponent< HighlightTextProps, HTMLSpanElement, > = React.forwardRef( ( { text, highlight, highlightClassName, caseSensitiveHighlighting, highlightWithBackground, }: HighlightTextProps, ref, ) => { // Split text on highlight term, include term itself into parts, ignore case // Convert highlight to an array if it's not already const highlightArray = [].concat(highlight).filter((item) => item !== ''); const highlights = highlightArray.map(escapeRegExp).join('|'); const parts = text .split( new RegExp(`(${highlights})`, caseSensitiveHighlighting ? '' : 'gi'), ) .filter((part) => part !== ''); return ( {parts.map((part, idx) => { const isHighlighted = highlightArray.some((highlightTerm) => caseSensitiveHighlighting ? escapeRegExp(part).includes(escapeRegExp(highlightTerm)) : escapeRegExp(part) .toLowerCase() .includes(escapeRegExp(highlightTerm).toLowerCase()), ); return isHighlighted ? ( {part} ) : ( part ); })} ); }, ); export const JumboLarge: React$AbstractComponent = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const JumboMedium: React$AbstractComponent = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const JumboSmall: React$AbstractComponent = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const TitleMedium: React$AbstractComponent< TextProps, HTMLHeadingElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const SubTitleLarge: React$AbstractComponent< TextProps, HTMLHeadingElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const SubTitleMedium: React$AbstractComponent< TextProps, HTMLHeadingElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const SubTitleSmall: React$AbstractComponent< TextProps, HTMLHeadingElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const SubTitleExtraSmall: React$AbstractComponent< TextProps, HTMLHeadingElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (
{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}
), ); export const ButtonTextMedium: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const ButtonTextSmall: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const MenuTextMedium: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const MenuTextSmall: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const ButtonTextExtraSmall: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const ButtonTextMediumUnderline: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const ButtonTextSmallUnderline: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const ButtonTextExtraSmallUnderline: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const FormInputMedium: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const FormInputSmall: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const BodyLarge: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const BodyMedium: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const BodySmall: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const BodyLargeBold: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const BodyMediumBold: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const BodySmallBold: React$AbstractComponent< TextProps, HTMLParagraphElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => (

{!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )}

), ); export const FormLabelMedium: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), ); export const FormLabelSmall: React$AbstractComponent< TextProps, HTMLSpanElement, > = React.forwardRef( ( { color = TEXT_COLORS.primary, children, className, highlightedTextClassName, highlightString, caseSensitiveHighlighting, highlightWithBackground, ...props }: TextProps, ref, ): React.Node => ( {!!highlightString?.length && typeof children === 'string' ? ( ) : ( children )} ), );