// @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 = React.forwardRef( ({ text, highlight, highlightClassName, caseSensitiveHighlighting, highlightWithBackground, }: HighlightTextProps, ref) => { // Split text on highlight term, include term itself into parts, ignore case let highlights = ''; if (Array.isArray(highlight)) { highlights = highlight.map(escapeRegExp).join('|'); } else { highlights = escapeRegExp(highlight); } const parts = text .split(new RegExp(`(${highlights})`, caseSensitiveHighlighting ? '' : 'gi')) .filter((part) => part !== ''); return ( {parts.map((part, idx) => highlights.toLowerCase().includes(escapeRegExp(part).toLowerCase()) ? ( {part} ) : ( part ), )} ); } ) 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 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 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 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 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 )} ), );