@telescopic-text/react
Version:
A React component for telescopic text.
121 lines (120 loc) • 4.37 kB
JavaScript
"use client";
// index.tsx
import React, { useState, useEffect } from "react";
function useTelescopicState(persistenceKey, enablePersistence) {
const [expandedKeys, setExpandedKeys] = useState([]);
const [clickedKeys, setClickedKeys] = useState([]);
useEffect(() => {
if (!enablePersistence || typeof window === "undefined" || !persistenceKey) return;
try {
const savedState = localStorage.getItem(persistenceKey);
if (savedState) {
const { expanded, clicked } = JSON.parse(savedState);
setExpandedKeys(expanded || []);
setClickedKeys(clicked || []);
}
} catch (error) {
console.error("Error loading telescopic text state:", error);
localStorage.removeItem(persistenceKey);
}
}, [persistenceKey, enablePersistence]);
useEffect(() => {
if (!enablePersistence || !persistenceKey) return;
localStorage.setItem(
persistenceKey,
JSON.stringify({ expanded: expandedKeys, clicked: clickedKeys })
);
}, [expandedKeys, clickedKeys, persistenceKey, enablePersistence]);
const handleExpand = (key) => {
if (!clickedKeys.includes(key)) {
setExpandedKeys((prev) => [...prev, key]);
setClickedKeys((prev) => [...prev, key]);
}
};
return { expandedKeys, clickedKeys, handleExpand };
}
var TelescopicText = ({
nodes,
persistenceKey = "telescopic-text",
enablePersistence = true,
baseClassName = "",
linkClassName = "",
expandableClassName = ""
}) => {
const { expandedKeys, clickedKeys, handleExpand } = useTelescopicState(
persistenceKey,
enablePersistence
);
function generateNodeKey(parentKey, index, node) {
return parentKey ? `${parentKey}-${node.id || index}` : `${node.id || index}`;
}
function renderItems(items, parentKey = null) {
return items.map((item, index) => {
if (typeof item === "object" && !React.isValidElement(item)) {
const data = item;
const itemKey = generateNodeKey(parentKey, index, data);
return renderItem(item, itemKey);
} else {
const itemKey = parentKey ? `${parentKey}-${index}` : `${index}`;
return renderItem(item, itemKey);
}
});
}
function renderItem(item, key) {
if (typeof item === "string") {
return /* @__PURE__ */ React.createElement("span", { key, className: baseClassName }, item, " ");
}
if (React.isValidElement(item)) {
return /* @__PURE__ */ React.createElement("span", { key, className: baseClassName }, item);
}
if (typeof item === "object" && !Array.isArray(item)) {
return renderDataNode(item, key);
}
return /* @__PURE__ */ React.createElement("span", { key, className: baseClassName }, String(item), " ");
}
function renderDataNode(item, key) {
const isExpanded = expandedKeys.includes(key);
const isClicked = clickedKeys.includes(key);
const currentClassName = item.link ? item.linkClassName || linkClassName : item.expandTo ? item.expandableClassName || expandableClassName : item.baseClassName || baseClassName;
const content = item.word || "";
if (item.expandTo) {
return /* @__PURE__ */ React.createElement("span", { key }, !isClicked && /* @__PURE__ */ React.createElement(
"span",
{
role: "button",
tabIndex: 0,
"aria-expanded": isExpanded,
className: currentClassName,
onClick: () => handleExpand(key),
onKeyDown: (e) => {
if (e.key === "Enter" || e.key === " ") {
handleExpand(key);
}
}
},
content,
" "
), isExpanded && /* @__PURE__ */ React.createElement("span", { key: `${key}-expanded`, className: `${baseClassName} inline` }, renderItems(item.expandTo, key)));
}
if (item.link) {
return /* @__PURE__ */ React.createElement(
"a",
{
key,
href: item.link,
target: "_blank",
rel: "noopener noreferrer",
className: currentClassName
},
content
);
}
return /* @__PURE__ */ React.createElement("span", { key, className: baseClassName }, content);
}
return /* @__PURE__ */ React.createElement(React.Fragment, null, renderItems(nodes));
};
var index_default = TelescopicText;
export {
index_default as default
};
//# sourceMappingURL=index.mjs.map