@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
1 lines • 4.61 kB
Source Map (JSON)
{"version":3,"file":"Highlight.cjs","names":["polymorphicFactory","useProps","highlighter","Text","Mark"],"sources":["../../../src/components/Highlight/Highlight.tsx"],"sourcesContent":["import {\n MantineColor,\n MantineTheme,\n polymorphicFactory,\n PolymorphicFactory,\n useProps,\n} from '../../core';\nimport { Mark } from '../Mark';\nimport { Text, TextProps, TextStylesNames, TextVariant } from '../Text';\nimport { highlighter } from './highlighter/highlighter';\n\nexport interface HighlightTerm {\n /** Text to highlight */\n text: string;\n\n /** Background color for this specific term. Key of `theme.colors` or any valid CSS color */\n color?: MantineColor | string;\n}\n\nexport interface HighlightProps extends Omit<TextProps, 'color'> {\n /**\n * Substring(s) to highlight in `children`. Can be:\n * - string: single term\n * - string[]: multiple terms with same color\n * - HighlightTerm[]: multiple terms with custom colors per term\n *\n * - Matching is case-insensitive\n * - Regex special characters are automatically escaped\n * - When multiple substrings are provided, longer matches take precedence\n * - Empty strings and whitespace-only strings are ignored\n */\n highlight: string | string[] | HighlightTerm[];\n\n /**\n * Default background color for all highlighted text.\n * Key of `theme.colors` or any valid CSS color, passed to `Mark` component.\n * Can be overridden per term when using HighlightTerm objects.\n * @default 'yellow'\n */\n color?: MantineColor | string;\n\n /** Styles applied to `mark` elements */\n highlightStyles?: React.CSSProperties | ((theme: MantineTheme) => React.CSSProperties);\n\n /** String in which to highlight substrings */\n children: string;\n\n /**\n * Only match whole words (adds word boundaries to regex).\n * When enabled, 'the' will not match 'there'.\n * @default false\n */\n wholeWord?: boolean;\n}\n\nexport type HighlightFactory = PolymorphicFactory<{\n props: HighlightProps;\n defaultRef: HTMLDivElement;\n defaultComponent: 'div';\n stylesNames: TextStylesNames;\n variant: TextVariant;\n}>;\n\nconst defaultProps = {\n color: 'yellow',\n wholeWord: false,\n} satisfies Partial<HighlightProps>;\n\nexport const Highlight = polymorphicFactory<HighlightFactory>((_props) => {\n const { unstyled, children, highlight, highlightStyles, color, wholeWord, ...others } = useProps(\n 'Highlight',\n defaultProps,\n _props\n );\n\n const isTermArray = Array.isArray(highlight) && typeof highlight[0] === 'object';\n const colorMap = new Map<string, string>();\n\n let highlightStrings: string[];\n if (isTermArray) {\n highlightStrings = (highlight as HighlightTerm[]).map((term) => {\n if (term.color) {\n colorMap.set(term.text.toLowerCase(), term.color);\n }\n return term.text;\n });\n } else if (Array.isArray(highlight)) {\n highlightStrings = highlight as string[];\n } else {\n highlightStrings = [highlight as string];\n }\n\n const highlightChunks = highlighter(children, highlightStrings, { wholeWord });\n\n return (\n <Text unstyled={unstyled} {...others} __staticSelector=\"Highlight\">\n {highlightChunks.map(({ chunk, highlighted }, i) =>\n highlighted ? (\n <Mark\n unstyled={unstyled}\n key={i}\n color={colorMap.get(chunk.toLowerCase()) || color}\n style={highlightStyles}\n data-highlight={chunk}\n >\n {chunk}\n </Mark>\n ) : (\n <span key={i}>{chunk}</span>\n )\n )}\n </Text>\n );\n});\n\nHighlight.classes = Text.classes;\nHighlight.displayName = '@mantine/core/Highlight';\n"],"mappings":";;;;;;;;;AA+DA,MAAM,eAAe;CACnB,OAAO;CACP,WAAW;CACZ;AAED,MAAa,YAAYA,4BAAAA,oBAAsC,WAAW;CACxE,MAAM,EAAE,UAAU,UAAU,WAAW,iBAAiB,OAAO,WAAW,GAAG,WAAWC,kBAAAA,SACtF,aACA,cACA,OACD;CAED,MAAM,cAAc,MAAM,QAAQ,UAAU,IAAI,OAAO,UAAU,OAAO;CACxE,MAAM,2BAAW,IAAI,KAAqB;CAE1C,IAAI;AACJ,KAAI,YACF,oBAAoB,UAA8B,KAAK,SAAS;AAC9D,MAAI,KAAK,MACP,UAAS,IAAI,KAAK,KAAK,aAAa,EAAE,KAAK,MAAM;AAEnD,SAAO,KAAK;GACZ;UACO,MAAM,QAAQ,UAAU,CACjC,oBAAmB;KAEnB,oBAAmB,CAAC,UAAoB;CAG1C,MAAM,kBAAkBC,oBAAAA,YAAY,UAAU,kBAAkB,EAAE,WAAW,CAAC;AAE9E,QACE,iBAAA,GAAA,kBAAA,KAACC,aAAAA,MAAD;EAAgB;EAAU,GAAI;EAAQ,kBAAiB;YACpD,gBAAgB,KAAK,EAAE,OAAO,eAAe,MAC5C,cACE,iBAAA,GAAA,kBAAA,KAACC,aAAAA,MAAD;GACY;GAEV,OAAO,SAAS,IAAI,MAAM,aAAa,CAAC,IAAI;GAC5C,OAAO;GACP,kBAAgB;aAEf;GACI,EANA,EAMA,GAEP,iBAAA,GAAA,kBAAA,KAAC,QAAD,EAAA,UAAe,OAAa,EAAjB,EAAiB,CAE/B;EACI,CAAA;EAET;AAEF,UAAU,UAAUD,aAAAA,KAAK;AACzB,UAAU,cAAc"}