UNPKG

@yamada-ui/highlight

Version:

Yamada UI highlight component

90 lines (89 loc) 3.48 kB
"use client" "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/highlight.tsx var highlight_exports = {}; __export(highlight_exports, { Highlight: () => Highlight, Mark: () => Mark, useHighlight: () => useHighlight }); module.exports = __toCommonJS(highlight_exports); var import_core = require("@yamada-ui/core"); var import_typography = require("@yamada-ui/typography"); var import_utils = require("@yamada-ui/utils"); var import_react = require("react"); var import_jsx_runtime = require("react/jsx-runtime"); var escapeRegexp = (term) => term.replace(/[|\\{}()[\]^$+*?.-]/g, (char) => `\\${char}`); var buildRegex = (query) => { query = query.filter(Boolean).map((text) => escapeRegexp(text.trim())); if (query.length) return new RegExp(`(${query.join("|")})`, "ig"); }; var highlightWords = ({ query, text }) => { const regex = buildRegex((0, import_utils.isArray)(query) ? query : [query]); if (!regex) return [{ match: false, text }]; return text.split(regex).filter(Boolean).map((text2) => ({ match: regex.test(text2), text: text2 })); }; var useHighlight = ({ query, text }) => (0, import_react.useMemo)(() => highlightWords({ query, text }), [text, query]); var Highlight = ({ children: text, isFragment = false, fragment = isFragment, lineHeight = "tall", query, markProps, ...rest }) => { if (typeof text !== "string") throw new Error("The children prop of Highlight must be a string"); const chunks = useHighlight({ query, text }); const Component = fragment ? import_react.Fragment : import_typography.Text; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...!fragment ? { lineHeight } : {}, ...rest, children: chunks.map( ({ match, text: text2 }, i) => match ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Mark, { ...markProps, children: text2 }, i) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Fragment, { children: text2 }, i) ) }); }; Highlight.displayName = "Highlight"; Highlight.__ui__ = "Highlight"; var Mark = (0, import_core.forwardRef)((props, ref) => { const [styles, mergedProps] = (0, import_core.useComponentStyle)("Mark", props); const { className, ...rest } = (0, import_core.omitThemeProps)(mergedProps); const css = { bg: "transparent", whiteSpace: "nowrap", ...styles }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_core.ui.mark, { ref, className: (0, import_utils.cx)("ui-mark", className), __css: css, ...rest } ); }); Mark.displayName = "Mark"; Mark.__ui__ = "Mark"; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Highlight, Mark, useHighlight }); //# sourceMappingURL=highlight.js.map