UNPKG

react-konva-grid

Version:

Declarative React Canvas Grid primitive for Data table, Pivot table, Excel Worksheets

74 lines 3.02 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importStar(require("react")); const DefaultTooltipComponent = ({ content, x, y }) => { const offset = 10; return (react_1.default.createElement("div", { style: { position: "absolute", left: 0, top: 0, transform: `translate(${x + offset}px, ${y + offset}px)`, maxWidth: 200, background: "rgba(0,0,0,0.8)", padding: 5, borderRadius: 5, color: "white", } }, content)); }; const useTooltip = ({ getValue, gridRef, component: Component = DefaultTooltipComponent, }) => { const [activeCell, setActiveCell] = react_1.useState(null); const [tooltipPosition, setTooltipPosition] = react_1.useState({ x: 0, y: 0 }); const content = react_1.useMemo(() => { var _a; return (activeCell ? (_a = getValue(activeCell)) !== null && _a !== void 0 ? _a : null : null); }, [activeCell]); const showTooltip = activeCell && content !== null; const tooltipProps = { content, x: tooltipPosition.x, y: tooltipPosition.y, }; const tooltipComponent = showTooltip ? react_1.default.createElement(Component, Object.assign({}, tooltipProps)) : null; const handleMouseMove = react_1.useCallback((e) => { const coords = gridRef.current.getCellCoordsFromOffset(e.clientX, e.clientY); if (!coords) return; const { rowIndex, columnIndex } = coords; setTooltipPosition({ x: e.clientX, y: e.clientY, }); /* Exit if its the same cell */ if (activeCell && activeCell.rowIndex === rowIndex && activeCell.columnIndex === columnIndex) return; setActiveCell({ rowIndex, columnIndex }); }, [activeCell]); const handleMouseLeave = react_1.useCallback((e) => { setActiveCell(null); }, []); return { tooltipComponent, onMouseMove: handleMouseMove, onMouseLeave: handleMouseLeave, }; }; exports.default = useTooltip; //# sourceMappingURL=useTooltip.js.map