From f384a66984b8648abd5cdf372e2e01d368605a7d Mon Sep 17 00:00:00 2001 From: Vansh Date: Sat, 21 Dec 2024 15:53:35 +0530 Subject: [PATCH] Implemented tooltip system for keyboard navigation --- src/components/ExcelClone.js | 93 ++++++++++++++++++++++++------------ src/components/Tooltip.css | 31 ++++++++++++ src/components/Tooltip.js | 24 ++++++++++ 3 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 src/components/Tooltip.css create mode 100644 src/components/Tooltip.js diff --git a/src/components/ExcelClone.js b/src/components/ExcelClone.js index d392f3b..bf7cfcf 100644 --- a/src/components/ExcelClone.js +++ b/src/components/ExcelClone.js @@ -1,30 +1,29 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; +import './Tooltip.css'; // Add tooltip styles -const ROWS = 20; -const COLS = 26; // A to Z +const Tooltip = ({ text, position }) => { + return ( +
+ {text} +
+ ); +}; const ExcelClone = () => { - // State for cell data - const [data, setData] = useState( - Array(ROWS).fill().map(() => Array(COLS).fill('')) - ); - - // State for selected cell + const ROWS = 20; + const COLS = 26; // A to Z + const [data, setData] = useState(Array(ROWS).fill().map(() => Array(COLS).fill(''))); const [selectedCell, setSelectedCell] = useState(null); - - // State for formula bar const [formulaBarValue, setFormulaBarValue] = useState(''); + const [tooltipText, setTooltipText] = useState(''); // State for dynamic tooltip - // Convert column index to letter (0 = A, 1 = B, etc.) const getColumnLabel = (index) => String.fromCharCode(65 + index); - // Handle cell selection const handleCellSelect = (rowIndex, colIndex) => { setSelectedCell({ row: rowIndex, col: colIndex }); setFormulaBarValue(data[rowIndex][colIndex]); }; - // Handle cell value change const handleCellChange = (rowIndex, colIndex, value) => { const newData = [...data]; newData[rowIndex][colIndex] = value; @@ -32,6 +31,50 @@ const ExcelClone = () => { setFormulaBarValue(value); }; + const handleKeyDown = (e) => { + const { row, col } = selectedCell || {}; + + if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') { + setTooltipText("Use Arrow Keys to Navigate"); + } else if (e.key === 'Tab') { + setTooltipText("Use Tab to Move to the Next Cell"); + } else if (e.key === 'Enter') { + setTooltipText("Press Enter to Confirm"); + } + }; + + useEffect(() => { + window.addEventListener('keydown', handleKeyDown); + return () => { + window.removeEventListener('keydown', handleKeyDown); + }; + }, [selectedCell]); + + const Cell = ({ value, rowIndex, colIndex }) => { + const [showTooltip, setShowTooltip] = useState(false); + + return ( + +
setShowTooltip(true)} + onMouseLeave={() => setShowTooltip(false)} + > + handleCellChange(rowIndex, colIndex, e.target.value)} + onClick={() => handleCellSelect(rowIndex, colIndex)} + /> + {showTooltip && } +
+ + ); + }; + return (
{/* Top Bar */} @@ -74,22 +117,12 @@ const ExcelClone = () => { {rowIndex + 1} {Array(COLS).fill().map((_, colIndex) => ( - - handleCellChange(rowIndex, colIndex, e.target.value)} - onClick={() => handleCellSelect(rowIndex, colIndex)} - /> - + value={data[rowIndex][colIndex]} + rowIndex={rowIndex} + colIndex={colIndex} + /> ))} ))} @@ -100,4 +133,4 @@ const ExcelClone = () => { ); }; -export default ExcelClone; \ No newline at end of file +export default ExcelClone; diff --git a/src/components/Tooltip.css b/src/components/Tooltip.css new file mode 100644 index 0000000..2599e03 --- /dev/null +++ b/src/components/Tooltip.css @@ -0,0 +1,31 @@ +.tooltip { + position: absolute; + background-color: rgba(0, 0, 0, 0.75); + color: white; + padding: 5px 10px; + border-radius: 5px; + font-size: 12px; + pointer-events: none; + transition: opacity 0.3s ease; + } + + .tooltip.bottom { + transform: translateY(100%); + top: 100%; + } + + .tooltip.top { + transform: translateY(-100%); + bottom: 100%; + } + + .tooltip.left { + transform: translateX(-100%); + right: 100%; + } + + .tooltip.right { + transform: translateX(100%); + left: 100%; + } + \ No newline at end of file diff --git a/src/components/Tooltip.js b/src/components/Tooltip.js new file mode 100644 index 0000000..ac331e2 --- /dev/null +++ b/src/components/Tooltip.js @@ -0,0 +1,24 @@ +import React, { useState, useEffect } from 'react'; +import './Tooltip.css'; // Add tooltip styles + +const Tooltip = ({ text, target }) => { + const [style, setStyle] = useState({}); + + useEffect(() => { + if (target) { + const rect = target.getBoundingClientRect(); + setStyle({ + top: rect.top + window.scrollY - 40, // Adjust top offset + left: rect.left + window.scrollX + rect.width / 2, // Adjust left offset + }); + } + }, [target]); + + return ( +
+ {text} +
+ ); +}; + +export default Tooltip;