UNPKG

@redocly/theme

Version:

Shared UI components lib

81 lines 3.41 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HighlightContext = void 0; exports.CatalogClassicHighlight = CatalogClassicHighlight; const React = __importStar(require("react")); const highlight_words_core_1 = require("highlight-words-core"); const styled_components_1 = __importDefault(require("styled-components")); exports.HighlightContext = React.createContext([]); function CatalogClassicHighlight(props) { const { children } = props; const searchWords = React.useContext(exports.HighlightContext); if (!searchWords.length) { return children ? React.createElement(React.Fragment, null, children) : null; } function highlight(str, childIdx = 0) { const chunks = (0, highlight_words_core_1.findAll)({ searchWords, textToHighlight: str, }); return (React.createElement("span", { "data-component-name": "CatalogClassic/CatalogClassicHighlight", key: childIdx }, chunks.map((chunk, idx) => { const { end, highlight, start } = chunk; const text = str.substr(start, end - start); if (highlight) { return React.createElement(HighlightedText, { key: idx }, text); } else { return text; } }))); } if (typeof children === 'string') { return highlight(children); } else if (Array.isArray(children)) { return (React.createElement(React.Fragment, null, children.map((child, idx) => typeof children === 'string' ? highlight(child, idx) : child || null))); } else { return React.createElement(React.Fragment, null, children); } } const HighlightedText = styled_components_1.default.span ` background-color: var(--search-highlight-bg-color); color: var(--search-highlight-text-color); `; //# sourceMappingURL=CatalogClassicHighlight.js.map