@wordpress/components
Version:
UI components for WordPress.
36 lines (35 loc) • 1.42 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_element = require("@wordpress/element");
let ___utils_strings_ts = require("../utils/strings.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/components/src/text-highlight/index.tsx
/**
* Highlights occurrences of a given string within another string of text. Wraps
* each match with a `<mark>` tag which provides browser default styling.
*
* ```jsx
* import { TextHighlight } from '@wordpress/components';
*
* const MyTextHighlight = () => (
* <TextHighlight
* text="Why do we like Gutenberg? Because Gutenberg is the best!"
* highlight="Gutenberg"
* />
* );
* ```
*/
const TextHighlight = (props) => {
const { text = "", highlight = "" } = props;
const trimmedHighlightText = highlight.trim();
if (!trimmedHighlightText) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: text });
const regex = new RegExp(`(${(0, ___utils_strings_ts.escapeRegExp)(trimmedHighlightText)})`, "gi");
return (0, _wordpress_element.createInterpolateElement)(text.replace(regex, "<mark>$&</mark>"), { mark: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("mark", {}) });
};
TextHighlight.displayName = "TextHighlight";
exports.TextHighlight = TextHighlight;
exports.default = TextHighlight;
//# sourceMappingURL=index.cjs.map