Changesets · Branches · Tags · Compare
| Changeset | 4a5fab4dde7e084a44924e48ab6b9d483a20ebae6e417bdc1db0567266b655d3 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-05-23 15:48 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-05-23 15:48 -07:00 |
| Parents | 6aa3615da72b |
5 files changed
deleted gui/src/components/find/FindWidget.test.tsx +0 −105
| @@ -1,105 +0,0 @@ | ||
| 1 | -import { act, fireEvent, render, screen } from "@testing-library/react"; | |
| 2 | -import { useRef } from "react"; | |
| 3 | -import { useFindWidget } from "./FindWidget"; | |
| 4 | - | |
| 5 | -// Test component that uses the find widget | |
| 6 | -const TestComponent = () => { | |
| 7 | - const searchRef = useRef<HTMLDivElement>(null); | |
| 8 | - const headerRef = useRef<HTMLDivElement>(null); | |
| 9 | - | |
| 10 | - const { widget, highlights, open, setOpen } = useFindWidget( | |
| 11 | - searchRef, | |
| 12 | - headerRef, | |
| 13 | - [], | |
| 14 | - false, | |
| 15 | - ); | |
| 16 | - | |
| 17 | - return ( | |
| 18 | - <div> | |
| 19 | - {widget} | |
| 20 | - <div ref={headerRef} className="header"> | |
| 21 | - Header Content | |
| 22 | - </div> | |
| 23 | - <div | |
| 24 | - ref={searchRef} | |
| 25 | - className="relative" | |
| 26 | - style={{ height: "400px", overflow: "auto" }} | |
| 27 | - > | |
| 28 | - <p>This is a test paragraph</p> | |
| 29 | - <p>We can search for content here</p> | |
| 30 | - <p>Multiple instances of test should work</p> | |
| 31 | - {highlights} | |
| 32 | - </div> | |
| 33 | - <button onClick={() => setOpen(true)}>Open Search</button> | |
| 34 | - </div> | |
| 35 | - ); | |
| 36 | -}; | |
| 37 | - | |
| 38 | -describe("FindWidget", () => { | |
| 39 | - beforeEach(() => { | |
| 40 | - // Reset window size and scroll position | |
| 41 | - window.innerHeight = 768; | |
| 42 | - window.innerWidth = 1024; | |
| 43 | - }); | |
| 44 | - | |
| 45 | - it("should open search widget and find matches", async () => { | |
| 46 | - render(<TestComponent />); | |
| 47 | - | |
| 48 | - // Open search widget | |
| 49 | - const openButton = screen.getByText("Open Search"); | |
| 50 | - fireEvent.click(openButton); | |
| 51 | - | |
| 52 | - // Find search input | |
| 53 | - const searchInput = screen.getByPlaceholderText("Search..."); | |
| 54 | - expect(searchInput).toBeInTheDocument(); | |
| 55 | - | |
| 56 | - // Type search term | |
| 57 | - await act(async () => { | |
| 58 | - fireEvent.change(searchInput, { target: { value: "test" } }); | |
| 59 | - // Wait for debounce | |
| 60 | - await new Promise((resolve) => setTimeout(resolve, 350)); | |
| 61 | - }); | |
| 62 | - | |
| 63 | - // Check if matches are found (should find 2 instances of "test") | |
| 64 | - const matchCount = screen.getByText("1 of 2"); | |
| 65 | - expect(matchCount).toBeInTheDocument(); | |
| 66 | - | |
| 67 | - // Test case sensitivity toggle | |
| 68 | - const caseSensitiveButton = screen.getByText("Aa"); | |
| 69 | - fireEvent.click(caseSensitiveButton); | |
| 70 | - | |
| 71 | - // Check highlight elements | |
| 72 | - const highlights = document.querySelectorAll( | |
| 73 | - ".bg-findMatch\\/50, .bg-findMatch-selected\\/50", | |
| 74 | - ); | |
| 75 | - expect(highlights.length).toBe(2); | |
| 76 | - }); | |
| 77 | - | |
| 78 | - it("should navigate between matches", async () => { | |
| 79 | - render( | |
| 80 | - // <Provider store={store}> | |
| 81 | - <TestComponent />, | |
| 82 | - // </Provider> | |
| 83 | - ); | |
| 84 | - | |
| 85 | - // Open search and type | |
| 86 | - fireEvent.click(screen.getByText("Open Search")); | |
| 87 | - const searchInput = screen.getByPlaceholderText("Search..."); | |
| 88 | - | |
| 89 | - await act(async () => { | |
| 90 | - fireEvent.change(searchInput, { target: { value: "test" } }); | |
| 91 | - await new Promise((resolve) => setTimeout(resolve, 350)); | |
| 92 | - }); | |
| 93 | - | |
| 94 | - // Find navigation buttons | |
| 95 | - const nextButton = screen.getByText("Next Match"); | |
| 96 | - const prevButton = screen.getByText("Previous Match"); | |
| 97 | - | |
| 98 | - // Navigate through matches | |
| 99 | - fireEvent.click(nextButton); | |
| 100 | - expect(screen.getByText("2 of 2")).toBeInTheDocument(); | |
| 101 | - | |
| 102 | - fireEvent.click(prevButton); | |
| 103 | - expect(screen.getByText("1 of 2")).toBeInTheDocument(); | |
| 104 | - }); | |
| 105 | -}); |
modified gui/src/components/find/FindWidget.tsx +118 −311
| @@ -3,44 +3,27 @@ | ||
| 3 | 3 | ArrowUpIcon, |
| 4 | 4 | XMarkIcon, |
| 5 | 5 | } from "@heroicons/react/24/outline"; |
| 6 | -import { | |
| 7 | - RefObject, | |
| 8 | - useCallback, | |
| 9 | - useEffect, | |
| 10 | - useMemo, | |
| 11 | - useRef, | |
| 12 | - useState, | |
| 13 | -} from "react"; | |
| 6 | +import { RefObject, useCallback, useEffect, useMemo, useState } from "react"; | |
| 14 | 7 | import { HeaderButton, Input } from ".."; |
| 15 | 8 | import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; |
| 16 | - | |
| 17 | -interface SearchMatch { | |
| 18 | - index: number; | |
| 19 | - textNode: Text; | |
| 20 | - overlayRectangle: Rectangle; | |
| 21 | -} | |
| 22 | - | |
| 23 | -type ScrollToMatchOption = "closest" | "first" | "none"; | |
| 24 | - | |
| 25 | -const SEARCH_DEBOUNCE = 300; | |
| 26 | -const RESIZE_DEBOUNCE = 200; | |
| 27 | - | |
| 28 | -interface Rectangle { | |
| 29 | - top: number; | |
| 30 | - left: number; | |
| 31 | - width: number; | |
| 32 | - height: number; | |
| 33 | -} | |
| 34 | - | |
| 35 | -interface HighlightOverlayProps extends Rectangle { | |
| 9 | +import { | |
| 10 | + Rectangle, | |
| 11 | + SearchMatch, | |
| 12 | + searchWithinContainer, | |
| 13 | +} from "./findWidgetSearch"; | |
| 14 | +import { useDebouncedInput } from "./useDebouncedInput"; | |
| 15 | +import { useElementSize } from "./useElementDims"; | |
| 16 | + | |
| 17 | +interface HighlightOverlayProps { | |
| 18 | + rectangle: Rectangle; | |
| 36 | 19 | isCurrent: boolean; |
| 37 | 20 | } |
| 38 | 21 | |
| 39 | 22 | const HighlightOverlay = (props: HighlightOverlayProps) => { |
| 40 | - const { isCurrent, top, left, width, height } = props; | |
| 23 | + const { top, left, width, height } = props.rectangle; | |
| 41 | 24 | return ( |
| 42 | 25 | <div |
| 43 | - className={isCurrent ? "bg-findMatch-selected/50" : "bg-findMatch/50"} | |
| 26 | + className={props.isCurrent ? "bg-findMatch-selected" : "bg-findMatch"} | |
| 44 | 27 | key={`highlight-${top}-${left}`} |
| 45 | 28 | style={{ |
| 46 | 29 | position: "absolute", |
| @@ -55,6 +38,8 @@ | ||
| 55 | 38 | ); |
| 56 | 39 | }; |
| 57 | 40 | |
| 41 | +type ScrollToMatchOption = "closest" | "first" | "none"; | |
| 42 | + | |
| 58 | 43 | /* |
| 59 | 44 | useFindWidget takes a container ref and returns |
| 60 | 45 | 1. A widget that can be placed anywhere to search the contents of that container |
| @@ -70,16 +55,18 @@ | ||
| 70 | 55 | disabled: boolean, |
| 71 | 56 | ) => { |
| 72 | 57 | // Search input, debounced |
| 73 | - const [input, setInput] = useState<string>(""); | |
| 74 | - const debouncedInput = useRef<string>(""); | |
| 75 | - const inputRef = useRef<HTMLInputElement>(null); | |
| 58 | + const { | |
| 59 | + inputRef, | |
| 60 | + debouncedValue: searchTerm, | |
| 61 | + currentValue, | |
| 62 | + } = useDebouncedInput(); | |
| 76 | 63 | |
| 77 | 64 | // Widget open/closed state |
| 78 | 65 | const [open, setOpen] = useState<boolean>(false); |
| 79 | 66 | const openWidget = useCallback(() => { |
| 80 | 67 | setOpen(true); |
| 81 | 68 | inputRef?.current?.select(); |
| 82 | - }, [setOpen, inputRef]); | |
| 69 | + }, [inputRef]); | |
| 83 | 70 | |
| 84 | 71 | // Search settings and results |
| 85 | 72 | const [caseSensitive, setCaseSensitive] = useState<boolean>(false); |
| @@ -147,221 +134,75 @@ | ||
| 147 | 134 | }, [inputRef, matches, nextMatch]); |
| 148 | 135 | |
| 149 | 136 | // Handle container resize changes - highlight positions must adjust |
| 150 | - const [isResizing, setIsResizing] = useState(false); | |
| 151 | - useEffect(() => { | |
| 152 | - let timeoutId: NodeJS.Timeout | null = null; | |
| 153 | - if (!searchRef?.current) return; | |
| 154 | - | |
| 155 | - const resizeObserver = new ResizeObserver((entries) => { | |
| 156 | - if (timeoutId) { | |
| 157 | - clearTimeout(timeoutId); | |
| 158 | - } | |
| 159 | - setIsResizing(true); | |
| 160 | - timeoutId = setTimeout(() => { | |
| 161 | - setIsResizing(false); | |
| 162 | - }, RESIZE_DEBOUNCE); | |
| 163 | - }); | |
| 164 | - | |
| 165 | - resizeObserver.observe(searchRef.current); | |
| 166 | - if (headerRef.current) { | |
| 167 | - resizeObserver.observe(headerRef.current); | |
| 168 | - } | |
| 169 | - return () => { | |
| 170 | - if (timeoutId) { | |
| 171 | - clearTimeout(timeoutId); | |
| 172 | - } | |
| 173 | - if (searchRef.current) { | |
| 174 | - resizeObserver.unobserve(searchRef.current); | |
| 175 | - } | |
| 176 | - if (headerRef.current) { | |
| 177 | - resizeObserver.unobserve(headerRef.current); | |
| 178 | - } | |
| 179 | - }; | |
| 180 | - }, [searchRef, headerRef]); | |
| 137 | + const headerSize = useElementSize(headerRef); | |
| 138 | + const containerSize = useElementSize(searchRef); | |
| 139 | + const isResizing = useMemo(() => { | |
| 140 | + return containerSize.isResizing || headerSize.isResizing; | |
| 141 | + }, [containerSize, headerSize]); | |
| 142 | + const headerHeight = useMemo(() => { | |
| 143 | + return headerSize.size.height; | |
| 144 | + }, [headerSize]); | |
| 181 | 145 | |
| 182 | 146 | // Main function for finding matches and generating highlight overlays |
| 183 | 147 | const refreshSearch = useCallback( |
| 184 | - (scrollTo: ScrollToMatchOption = "none", clearFirst = false) => { | |
| 185 | - if (clearFirst) setMatches([]); | |
| 186 | - | |
| 187 | - const searchContainer = searchRef.current; | |
| 188 | - const header = headerRef.current; | |
| 189 | - | |
| 190 | - const _query = debouncedInput.current; // trimStart - decided no because spaces should be fully searchable | |
| 191 | - if (!searchContainer || !_query) { | |
| 192 | - setMatches([]); | |
| 193 | - return; | |
| 194 | - } | |
| 195 | - const query = caseSensitive ? _query : _query.toLowerCase(); | |
| 196 | - | |
| 197 | - // First grab all text nodes | |
| 198 | - // Skips any elements with the "find-widget-skip" class | |
| 199 | - const textNodes: Text[] = []; | |
| 200 | - const walker = document.createTreeWalker( | |
| 201 | - searchRef.current, | |
| 202 | - NodeFilter.SHOW_ALL, | |
| 148 | + (scrollTo: ScrollToMatchOption = "none") => { | |
| 149 | + const { results, closestToMiddle } = searchWithinContainer( | |
| 150 | + searchRef, | |
| 151 | + searchTerm, | |
| 203 | 152 | { |
| 204 | - acceptNode: (node) => { | |
| 205 | - if (node.nodeType === Node.ELEMENT_NODE) { | |
| 206 | - if ((node as Element).classList.contains("find-widget-skip")) | |
| 207 | - return NodeFilter.FILTER_REJECT; | |
| 208 | - return NodeFilter.FILTER_ACCEPT; | |
| 209 | - } else if (node.nodeType === Node.TEXT_NODE) { | |
| 210 | - if (!node.nodeValue) return NodeFilter.FILTER_REJECT; | |
| 211 | - const nodeValue = caseSensitive | |
| 212 | - ? node.nodeValue | |
| 213 | - : node.nodeValue.toLowerCase(); | |
| 214 | - if (nodeValue.includes(query)) return NodeFilter.FILTER_ACCEPT; | |
| 215 | - } | |
| 216 | - return NodeFilter.FILTER_REJECT; | |
| 217 | - }, | |
| 153 | + caseSensitive, | |
| 154 | + useRegex, | |
| 155 | + offsetHeight: headerHeight, | |
| 218 | 156 | }, |
| 219 | 157 | ); |
| 220 | - | |
| 221 | - while (walker.nextNode()) { | |
| 222 | - if (walker.currentNode.nodeType === Node.ELEMENT_NODE) continue; | |
| 223 | - textNodes.push(walker.currentNode as Text); | |
| 224 | - } | |
| 225 | - | |
| 226 | - // Now walk through each node match and extract search results | |
| 227 | - // One node can have several matches | |
| 228 | - const newMatches: SearchMatch[] = []; | |
| 229 | - textNodes.forEach((textNode, idx) => { | |
| 230 | - // Hacky way to detect code blocks that be wider than client and cause absolute positioning to fail | |
| 231 | - const highlightFullLine = | |
| 232 | - textNode.parentElement?.className.includes("hljs"); | |
| 233 | - | |
| 234 | - let nodeTextValue = caseSensitive | |
| 235 | - ? textNode.nodeValue | |
| 236 | - : textNode.nodeValue.toLowerCase(); | |
| 237 | - let startIndex = 0; | |
| 238 | - while ((startIndex = nodeTextValue.indexOf(query, startIndex)) !== -1) { | |
| 239 | - // Create a range to measure the size and position of the match | |
| 240 | - const range = document.createRange(); | |
| 241 | - range.setStart(textNode, startIndex); | |
| 242 | - const endIndex = startIndex + query.length; | |
| 243 | - range.setEnd(textNode, endIndex); | |
| 244 | - const rect = range.getBoundingClientRect(); | |
| 245 | - range.detach(); | |
| 246 | - startIndex = endIndex; | |
| 247 | - | |
| 248 | - const headerHeight = header?.clientHeight ?? 0; | |
| 249 | - | |
| 250 | - const top = | |
| 251 | - rect.top + | |
| 252 | - searchContainer.clientTop + | |
| 253 | - searchContainer.scrollTop - | |
| 254 | - headerHeight; | |
| 255 | - const left = | |
| 256 | - rect.left + searchContainer.clientLeft + searchContainer.scrollLeft; | |
| 257 | - | |
| 258 | - // Build a match result and push to matches | |
| 259 | - const newMatch: SearchMatch = { | |
| 260 | - index: 0, // will set later | |
| 261 | - textNode, | |
| 262 | - overlayRectangle: { | |
| 263 | - top, | |
| 264 | - left: highlightFullLine ? 2 : left, | |
| 265 | - width: highlightFullLine | |
| 266 | - ? searchContainer.clientWidth - 4 | |
| 267 | - : rect.width, // equivalent of adding 2 px x padding | |
| 268 | - height: rect.height, | |
| 269 | - }, | |
| 270 | - }; | |
| 271 | - newMatches.push(newMatch); | |
| 272 | - | |
| 273 | - if (highlightFullLine) { | |
| 274 | - break; // Since highlighting full line no need for multiple overlays, will cause darker highlight | |
| 275 | - } | |
| 276 | - } | |
| 277 | - }); | |
| 278 | - | |
| 279 | - // There will still be duplicate full lines when multiple text nodes are in the same line (e.g. Code highlights) | |
| 280 | - // Filter them out by using the overlay rectangle as a hash key | |
| 281 | - const matchHash = Object.fromEntries( | |
| 282 | - newMatches.map((match) => [ | |
| 283 | - JSON.stringify(match.overlayRectangle), | |
| 284 | - match, | |
| 285 | - ]), | |
| 286 | - ); | |
| 287 | - const filteredMatches = Object.values(matchHash).map((match, index) => ({ | |
| 288 | - ...match, | |
| 289 | - index, | |
| 290 | - })); | |
| 291 | - | |
| 158 | + setMatches(results); | |
| 292 | 159 | // Find match closest to the middle of the view |
| 293 | - const verticalMiddle = | |
| 294 | - searchRef.current.scrollTop + searchRef.current.clientHeight / 2; | |
| 295 | - let closestDist = Infinity; | |
| 296 | - let closestMatchToMiddle: SearchMatch | null = null; | |
| 297 | - filteredMatches.forEach((match) => { | |
| 298 | - const dist = Math.abs(verticalMiddle - match.overlayRectangle.top); | |
| 299 | - if (dist < closestDist) { | |
| 300 | - closestDist = dist; | |
| 301 | - closestMatchToMiddle = match; | |
| 302 | - } | |
| 303 | - }); | |
| 304 | - | |
| 305 | - // Update matches and scroll to the closest or first match | |
| 306 | - setMatches(filteredMatches); | |
| 307 | - if (query.length > 1 && filteredMatches.length) { | |
| 160 | + if (searchTerm.length > 1 && results.length) { | |
| 308 | 161 | if (scrollTo === "first") { |
| 309 | - scrollToMatch(filteredMatches[0]); | |
| 162 | + scrollToMatch(results[0]); | |
| 310 | 163 | } |
| 311 | 164 | if (scrollTo === "closest") { |
| 312 | - if (closestMatchToMiddle) { | |
| 313 | - scrollToMatch(closestMatchToMiddle); | |
| 165 | + if (closestToMiddle) { | |
| 166 | + scrollToMatch(closestToMiddle); | |
| 314 | 167 | } |
| 315 | 168 | } |
| 316 | 169 | if (scrollTo === "none") { |
| 317 | - if (closestMatchToMiddle) { | |
| 318 | - setCurrentMatch(closestMatchToMiddle); | |
| 170 | + if (closestToMiddle) { | |
| 171 | + setCurrentMatch(closestToMiddle); | |
| 319 | 172 | } else { |
| 320 | - setCurrentMatch(filteredMatches[0]); | |
| 173 | + setCurrentMatch(results[0]); | |
| 321 | 174 | } |
| 322 | 175 | } |
| 323 | 176 | } |
| 324 | 177 | }, |
| 325 | 178 | [ |
| 326 | - headerRef, | |
| 327 | - searchRef, | |
| 328 | - debouncedInput, | |
| 329 | - scrollToMatch, | |
| 179 | + searchTerm, | |
| 330 | 180 | caseSensitive, |
| 331 | 181 | useRegex, |
| 332 | - ...refreshDependencies, | |
| 182 | + searchRef, | |
| 183 | + headerHeight, | |
| 184 | + scrollToMatch, | |
| 333 | 185 | ], |
| 334 | 186 | ); |
| 335 | 187 | |
| 336 | 188 | // Triggers that should cause immediate refresh of results to closest search value: |
| 337 | - // Input change (debounced) and window click | |
| 338 | - const lastUpdateRef = useRef(0); | |
| 339 | 189 | useEffect(() => { |
| 340 | - const debounce = () => { | |
| 341 | - debouncedInput.current = input; | |
| 342 | - lastUpdateRef.current = Date.now(); | |
| 343 | - refreshSearch("closest"); | |
| 344 | - }; | |
| 345 | - const timeSinceLastUpdate = Date.now() - lastUpdateRef.current; | |
| 346 | - if (timeSinceLastUpdate >= SEARCH_DEBOUNCE) { | |
| 347 | - debounce(); | |
| 190 | + if (disabled || isResizing || !open) { | |
| 191 | + setMatches([]); | |
| 348 | 192 | } else { |
| 349 | - const handler = setTimeout(() => { | |
| 350 | - debounce(); | |
| 351 | - }, SEARCH_DEBOUNCE - timeSinceLastUpdate); | |
| 352 | - return () => { | |
| 353 | - clearTimeout(handler); | |
| 354 | - }; | |
| 193 | + refreshSearch("closest"); | |
| 355 | 194 | } |
| 356 | - }, [refreshSearch, input]); | |
| 195 | + }, [refreshSearch, open, disabled, isResizing, ...refreshDependencies]); | |
| 357 | 196 | |
| 358 | - // Could consider doing any window click but I only handled search div here | |
| 359 | - // Since usually only clicks in search div will cause content changes in search div | |
| 197 | + // Clicks in search div can cause content changes that for some reason don't trigger resize | |
| 198 | + // Refresh clicking within container | |
| 360 | 199 | useEffect(() => { |
| 361 | 200 | const searchContainer = searchRef.current; |
| 362 | 201 | if (!open || !searchContainer) return; |
| 363 | 202 | const handleSearchRefClick = () => { |
| 364 | - refreshSearch("none"); | |
| 203 | + setTimeout(() => { | |
| 204 | + refreshSearch("none"); | |
| 205 | + }, 150); | |
| 365 | 206 | }; |
| 366 | 207 | searchContainer.addEventListener("click", handleSearchRefClick); |
| 367 | 208 | return () => { |
| @@ -369,123 +210,89 @@ | ||
| 369 | 210 | }; |
| 370 | 211 | }, [searchRef, refreshSearch, open]); |
| 371 | 212 | |
| 372 | - useEffect(() => { | |
| 373 | - if (disabled || isResizing) setMatches([]); | |
| 374 | - else setTimeout(() => refreshSearch("none"), 300); // this gives plenty of time for resizing to finish | |
| 375 | - }, [refreshSearch, disabled, isResizing]); | |
| 376 | - | |
| 377 | - useEffect(() => { | |
| 378 | - if (!open) setMatches([]); | |
| 379 | - else refreshSearch("closest"); | |
| 380 | - }, [refreshSearch, open]); | |
| 381 | - | |
| 382 | - useEffect(() => { | |
| 383 | - refreshSearch("closest"); | |
| 384 | - }, [refreshSearch, caseSensitive, useRegex]); | |
| 385 | - | |
| 386 | 213 | // Find widget component |
| 387 | - const widget = useMemo(() => { | |
| 388 | - return ( | |
| 389 | - <div | |
| 390 | - className={`fixed top-0 z-50 transition-all ${open ? "" : "-translate-y-full"} bg-vsc-background right-0 flex flex-row items-center gap-1.5 rounded-bl-lg border-0 border-b border-l border-solid border-zinc-700 pl-[3px] pr-3 sm:gap-2`} | |
| 391 | - > | |
| 392 | - <Input | |
| 393 | - disabled={disabled} | |
| 394 | - type="text" | |
| 395 | - ref={inputRef} | |
| 396 | - value={input} | |
| 397 | - onChange={(e) => { | |
| 398 | - setInput(e.target.value); | |
| 399 | - }} | |
| 400 | - placeholder="Search..." | |
| 401 | - /> | |
| 402 | - <p className="xs:block hidden min-w-12 whitespace-nowrap px-1 text-center text-xs"> | |
| 403 | - {matches.length === 0 | |
| 404 | - ? "No results" | |
| 405 | - : `${(currentMatch?.index ?? 0) + 1} of ${matches.length}`} | |
| 406 | - </p> | |
| 407 | - <div className="hidden flex-row gap-0.5 sm:flex"> | |
| 408 | - <HeaderButtonWithToolTip | |
| 409 | - tooltipPlacement="top-end" | |
| 410 | - text={"Previous Match"} | |
| 411 | - onClick={(e) => { | |
| 412 | - e.stopPropagation(); | |
| 413 | - previousMatch(); | |
| 414 | - }} | |
| 415 | - className="h-4 w-4 focus:ring" | |
| 416 | - disabled={matches.length < 2 || disabled} | |
| 417 | - > | |
| 418 | - <ArrowUpIcon className="h-4 w-4" /> | |
| 419 | - </HeaderButtonWithToolTip> | |
| 420 | - <HeaderButtonWithToolTip | |
| 421 | - tooltipPlacement="top-end" | |
| 422 | - text={"Next Match"} | |
| 423 | - onClick={(e) => { | |
| 424 | - e.stopPropagation(); | |
| 425 | - nextMatch(); | |
| 426 | - }} | |
| 427 | - className="h-4 w-4 focus:ring" | |
| 428 | - disabled={matches.length < 2 || disabled} | |
| 429 | - > | |
| 430 | - <ArrowDownIcon className="h-4 w-4" /> | |
| 431 | - </HeaderButtonWithToolTip> | |
| 432 | - </div> | |
| 214 | + const widget = ( | |
| 215 | + <div | |
| 216 | + className={`fixed top-0 z-50 transition-all ${open ? "" : "-translate-y-full"} bg-vsc-background right-0 flex flex-row items-center gap-1.5 rounded-bl-lg border-0 border-b border-l border-solid border-zinc-700 pl-[3px] pr-3 sm:gap-2`} | |
| 217 | + > | |
| 218 | + <Input | |
| 219 | + disabled={disabled} | |
| 220 | + type="text" | |
| 221 | + ref={inputRef} | |
| 222 | + value={currentValue} | |
| 223 | + placeholder="Search..." | |
| 224 | + /> | |
| 225 | + <p className="xs:block hidden min-w-12 whitespace-nowrap px-1 text-center text-xs"> | |
| 226 | + {matches.length === 0 | |
| 227 | + ? "No results" | |
| 228 | + : `${(currentMatch?.index ?? 0) + 1} of ${matches.length}`} | |
| 229 | + </p> | |
| 230 | + <div className="hidden flex-row gap-0.5 sm:flex"> | |
| 433 | 231 | <HeaderButtonWithToolTip |
| 434 | - disabled={disabled} | |
| 435 | - inverted={caseSensitive} | |
| 436 | 232 | tooltipPlacement="top-end" |
| 437 | - text={ | |
| 438 | - caseSensitive | |
| 439 | - ? "Turn off case sensitivity" | |
| 440 | - : "Turn on case sensitivity" | |
| 441 | - } | |
| 233 | + text={"Previous Match"} | |
| 442 | 234 | onClick={(e) => { |
| 443 | 235 | e.stopPropagation(); |
| 444 | - setCaseSensitive((curr) => !curr); | |
| 236 | + previousMatch(); | |
| 445 | 237 | }} |
| 446 | - className="h-5 w-6 rounded-full border text-xs focus:outline-none focus:ring" | |
| 238 | + className="h-4 w-4 focus:ring" | |
| 239 | + disabled={matches.length < 2 || disabled} | |
| 447 | 240 | > |
| 448 | - Aa | |
| 241 | + <ArrowUpIcon className="h-4 w-4" /> | |
| 449 | 242 | </HeaderButtonWithToolTip> |
| 450 | - {/* TODO - add useRegex functionality */} | |
| 451 | - <HeaderButton | |
| 452 | - inverted={false} | |
| 453 | - onClick={() => setOpen(false)} | |
| 454 | - className="focus:ring" | |
| 243 | + <HeaderButtonWithToolTip | |
| 244 | + tooltipPlacement="top-end" | |
| 245 | + text={"Next Match"} | |
| 246 | + onClick={(e) => { | |
| 247 | + e.stopPropagation(); | |
| 248 | + nextMatch(); | |
| 249 | + }} | |
| 250 | + className="h-4 w-4 focus:ring" | |
| 251 | + disabled={matches.length < 2 || disabled} | |
| 455 | 252 | > |
| 456 | - <XMarkIcon className="h-4 w-4" /> | |
| 457 | - </HeaderButton> | |
| 253 | + <ArrowDownIcon className="h-4 w-4" /> | |
| 254 | + </HeaderButtonWithToolTip> | |
| 458 | 255 | </div> |
| 459 | - ); | |
| 460 | - }, [ | |
| 461 | - open, | |
| 462 | - input, | |
| 463 | - inputRef, | |
| 464 | - caseSensitive, | |
| 465 | - matches, | |
| 466 | - currentMatch, | |
| 467 | - previousMatch, | |
| 468 | - nextMatch, | |
| 469 | - ]); | |
| 256 | + <HeaderButtonWithToolTip | |
| 257 | + disabled={disabled} | |
| 258 | + inverted={caseSensitive} | |
| 259 | + tooltipPlacement="top-end" | |
| 260 | + text={ | |
| 261 | + caseSensitive | |
| 262 | + ? "Turn off case sensitivity" | |
| 263 | + : "Turn on case sensitivity" | |
| 264 | + } | |
| 265 | + onClick={(e) => { | |
| 266 | + e.stopPropagation(); | |
| 267 | + setCaseSensitive((curr) => !curr); | |
| 268 | + }} | |
| 269 | + className="h-5 w-6 rounded-full border text-xs focus:outline-none focus:ring" | |
| 270 | + > | |
| 271 | + Aa | |
| 272 | + </HeaderButtonWithToolTip> | |
| 273 | + {/* TODO - add useRegex functionality */} | |
| 274 | + <HeaderButton | |
| 275 | + inverted={false} | |
| 276 | + onClick={() => setOpen(false)} | |
| 277 | + className="focus:ring" | |
| 278 | + > | |
| 279 | + <XMarkIcon className="h-4 w-4" /> | |
| 280 | + </HeaderButton> | |
| 281 | + </div> | |
| 282 | + ); | |
| 470 | 283 | |
| 471 | 284 | // Generate the highlight overlay elements |
| 472 | 285 | const highlights = useMemo(() => { |
| 473 | 286 | return matches.map((match) => ( |
| 474 | 287 | <HighlightOverlay |
| 475 | - {...match.overlayRectangle} | |
| 288 | + rectangle={match.overlayRectangle} | |
| 476 | 289 | isCurrent={currentMatch?.index === match.index} |
| 477 | 290 | /> |
| 478 | 291 | )); |
| 479 | 292 | }, [matches, currentMatch]); |
| 480 | 293 | |
| 481 | 294 | return { |
| 482 | - matches, | |
| 483 | 295 | highlights, |
| 484 | - inputRef, | |
| 485 | - input: debouncedInput, | |
| 486 | - setInput, | |
| 487 | - open, | |
| 488 | - setOpen, | |
| 489 | 296 | widget, |
| 490 | 297 | }; |
| 491 | 298 | }; |
modified gui/src/components/find/findWidgetSearch.ts +125 −138
| @@ -1,158 +1,145 @@ | ||
| 1 | +export interface Rectangle { | |
| 2 | + top: number; | |
| 3 | + left: number; | |
| 4 | + width: number; | |
| 5 | + height: number; | |
| 6 | +} | |
| 7 | + | |
| 1 | 8 | export interface SearchMatch { |
| 2 | 9 | index: number; |
| 3 | 10 | textNode: Text; |
| 4 | 11 | overlayRectangle: Rectangle; |
| 5 | 12 | } |
| 6 | 13 | |
| 14 | +interface SearchOptions { | |
| 15 | + caseSensitive: boolean; | |
| 16 | + useRegex: boolean; | |
| 17 | + offsetHeight: number; | |
| 18 | +} | |
| 7 | 19 | |
| 8 | -const refreshSearch = (scrollTo: ScrollToMatchOption = "none", clearFirst = false) => { | |
| 9 | - if (clearFirst) setMatches([]); | |
| 10 | - | |
| 11 | - const searchContainer = searchRef.current; | |
| 12 | - const header = headerRef.current; | |
| 20 | +export const searchWithinContainer = ( | |
| 21 | + containerRef: React.RefObject<HTMLDivElement>, | |
| 22 | + searchQuery: string, | |
| 23 | + options: SearchOptions, | |
| 24 | +): { | |
| 25 | + results: SearchMatch[]; | |
| 26 | + closestToMiddle: SearchMatch | null; | |
| 27 | +} => { | |
| 28 | + const searchContainer = containerRef.current; | |
| 13 | 29 | |
| 14 | - const _query = debouncedInput.current; // trimStart - decided no because spaces should be fully searchable | |
| 15 | - if (!searchContainer || !_query) { | |
| 16 | - setMatches([]); | |
| 17 | - return; | |
| 18 | - } | |
| 19 | - const query = caseSensitive ? _query : _query.toLowerCase(); | |
| 30 | + if (!searchContainer || !searchQuery) { | |
| 31 | + return { | |
| 32 | + results: [], | |
| 33 | + closestToMiddle: null, | |
| 34 | + }; | |
| 35 | + } | |
| 36 | + const query = options.caseSensitive ? searchQuery : searchQuery.toLowerCase(); | |
| 20 | 37 | |
| 21 | - // First grab all text nodes | |
| 22 | - // Skips any elements with the "find-widget-skip" class | |
| 23 | - const textNodes: Text[] = []; | |
| 24 | - const walker = document.createTreeWalker( | |
| 25 | - searchRef.current, | |
| 26 | - NodeFilter.SHOW_ALL, | |
| 27 | - { | |
| 28 | - acceptNode: (node) => { | |
| 29 | - if (node.nodeType === Node.ELEMENT_NODE) { | |
| 30 | - if ((node as Element).classList.contains("find-widget-skip")) | |
| 31 | - return NodeFilter.FILTER_REJECT; | |
| 32 | - return NodeFilter.FILTER_ACCEPT; | |
| 33 | - } else if (node.nodeType === Node.TEXT_NODE) { | |
| 34 | - if (!node.nodeValue) return NodeFilter.FILTER_REJECT; | |
| 35 | - const nodeValue = caseSensitive | |
| 36 | - ? node.nodeValue | |
| 37 | - : node.nodeValue.toLowerCase(); | |
| 38 | - if (nodeValue.includes(query)) return NodeFilter.FILTER_ACCEPT; | |
| 39 | - } | |
| 38 | + // First grab all text nodes | |
| 39 | + // Skips any elements with the "find-widget-skip" class | |
| 40 | + const textNodes: Text[] = []; | |
| 41 | + const walker = document.createTreeWalker( | |
| 42 | + searchContainer, | |
| 43 | + NodeFilter.SHOW_ALL, | |
| 44 | + { | |
| 45 | + acceptNode: (node) => { | |
| 46 | + if (node.nodeType === Node.ELEMENT_NODE) { | |
| 47 | + if ((node as Element).classList.contains("find-widget-skip")) | |
| 40 | 48 | return NodeFilter.FILTER_REJECT; |
| 41 | - }, | |
| 42 | - }, | |
| 43 | - ); | |
| 49 | + return NodeFilter.FILTER_ACCEPT; | |
| 50 | + } else if (node.nodeType === Node.TEXT_NODE) { | |
| 51 | + if (!node.nodeValue) return NodeFilter.FILTER_REJECT; | |
| 52 | + const nodeValue = options.caseSensitive | |
| 53 | + ? node.nodeValue | |
| 54 | + : node.nodeValue.toLowerCase(); | |
| 55 | + if (nodeValue.includes(query)) return NodeFilter.FILTER_ACCEPT; | |
| 56 | + } | |
| 57 | + return NodeFilter.FILTER_REJECT; | |
| 58 | + }, | |
| 59 | + }, | |
| 60 | + ); | |
| 44 | 61 | |
| 45 | - while (walker.nextNode()) { | |
| 46 | - if (walker.currentNode.nodeType === Node.ELEMENT_NODE) continue; | |
| 47 | - textNodes.push(walker.currentNode as Text); | |
| 48 | - } | |
| 62 | + while (walker.nextNode()) { | |
| 63 | + if (walker.currentNode.nodeType === Node.ELEMENT_NODE) continue; | |
| 64 | + textNodes.push(walker.currentNode as Text); | |
| 65 | + } | |
| 49 | 66 | |
| 50 | - // Now walk through each node match and extract search results | |
| 51 | - // One node can have several matches | |
| 52 | - const newMatches: SearchMatch[] = []; | |
| 53 | - textNodes.forEach((textNode, idx) => { | |
| 54 | - // Hacky way to detect code blocks that be wider than client and cause absolute positioning to fail | |
| 55 | - const highlightFullLine = | |
| 56 | - textNode.parentElement?.className.includes("hljs"); | |
| 67 | + // Now walk through each node match and extract search results | |
| 68 | + // One node can have several matches | |
| 69 | + const newMatches: SearchMatch[] = []; | |
| 70 | + textNodes.forEach((textNode, idx) => { | |
| 71 | + // Hacky way to detect code blocks that be wider than client and cause absolute positioning to fail | |
| 72 | + const highlightFullLine = | |
| 73 | + textNode.parentElement?.className.includes("hljs"); | |
| 57 | 74 | |
| 58 | - let nodeTextValue = caseSensitive | |
| 59 | - ? textNode.nodeValue | |
| 60 | - : textNode.nodeValue.toLowerCase(); | |
| 61 | - let startIndex = 0; | |
| 62 | - while ((startIndex = nodeTextValue.indexOf(query, startIndex)) !== -1) { | |
| 63 | - // Create a range to measure the size and position of the match | |
| 64 | - const range = document.createRange(); | |
| 65 | - range.setStart(textNode, startIndex); | |
| 66 | - const endIndex = startIndex + query.length; | |
| 67 | - range.setEnd(textNode, endIndex); | |
| 68 | - const rect = range.getBoundingClientRect(); | |
| 69 | - range.detach(); | |
| 70 | - startIndex = endIndex; | |
| 75 | + let nodeTextValue = options.caseSensitive | |
| 76 | + ? textNode.nodeValue | |
| 77 | + : textNode.nodeValue.toLowerCase(); | |
| 78 | + let startIndex = 0; | |
| 79 | + while ((startIndex = nodeTextValue.indexOf(query, startIndex)) !== -1) { | |
| 80 | + // Create a range to measure the size and position of the match | |
| 81 | + const range = document.createRange(); | |
| 82 | + range.setStart(textNode, startIndex); | |
| 83 | + const endIndex = startIndex + query.length; | |
| 84 | + range.setEnd(textNode, endIndex); | |
| 85 | + const rect = range.getBoundingClientRect(); | |
| 86 | + range.detach(); | |
| 87 | + startIndex = endIndex; | |
| 71 | 88 | |
| 72 | - const headerHeight = header?.clientHeight ?? 0; | |
| 89 | + const top = | |
| 90 | + rect.top + searchContainer.clientTop + searchContainer.scrollTop; | |
| 91 | + // - | |
| 92 | + // options.offsetHeight; | |
| 73 | 93 | |
| 74 | - const top = | |
| 75 | - rect.top + | |
| 76 | - searchContainer.clientTop + | |
| 77 | - searchContainer.scrollTop - | |
| 78 | - headerHeight; | |
| 79 | - const left = | |
| 80 | - rect.left + searchContainer.clientLeft + searchContainer.scrollLeft; | |
| 94 | + const left = | |
| 95 | + rect.left + searchContainer.clientLeft + searchContainer.scrollLeft; | |
| 81 | 96 | |
| 82 | - // Build a match result and push to matches | |
| 83 | - const newMatch: SearchMatch = { | |
| 84 | - index: 0, // will set later | |
| 85 | - textNode, | |
| 86 | - overlayRectangle: { | |
| 87 | - top, | |
| 88 | - left: highlightFullLine ? 2 : left, | |
| 89 | - width: highlightFullLine | |
| 90 | - ? searchContainer.clientWidth - 4 | |
| 91 | - : rect.width, // equivalent of adding 2 px x padding | |
| 92 | - height: rect.height, | |
| 93 | - }, | |
| 94 | - }; | |
| 95 | - newMatches.push(newMatch); | |
| 97 | + // Build a match result and push to matches | |
| 98 | + const newMatch: SearchMatch = { | |
| 99 | + index: 0, // will set later | |
| 100 | + textNode, | |
| 101 | + overlayRectangle: { | |
| 102 | + top, | |
| 103 | + left: highlightFullLine ? 2 : left, | |
| 104 | + width: highlightFullLine | |
| 105 | + ? searchContainer.clientWidth - 4 | |
| 106 | + : rect.width, // equivalent of adding 2 px x padding | |
| 107 | + height: rect.height, | |
| 108 | + }, | |
| 109 | + }; | |
| 110 | + newMatches.push(newMatch); | |
| 96 | 111 | |
| 97 | - if (highlightFullLine) { | |
| 98 | - break; // Since highlighting full line no need for multiple overlays, will cause darker highlight | |
| 99 | - } | |
| 100 | - } | |
| 101 | - }); | |
| 112 | + if (highlightFullLine) { | |
| 113 | + break; // Since highlighting full line no need for multiple overlays, will cause darker highlight | |
| 114 | + } | |
| 115 | + } | |
| 116 | + }); | |
| 102 | 117 | |
| 103 | - // There will still be duplicate full lines when multiple text nodes are in the same line (e.g. Code highlights) | |
| 104 | - // Filter them out by using the overlay rectangle as a hash key | |
| 105 | - const matchHash = Object.fromEntries( | |
| 106 | - newMatches.map((match) => [ | |
| 107 | - JSON.stringify(match.overlayRectangle), | |
| 108 | - match, | |
| 109 | - ]), | |
| 110 | - ); | |
| 111 | - const filteredMatches = Object.values(matchHash).map((match, index) => ({ | |
| 112 | - ...match, | |
| 113 | - index, | |
| 114 | - })); | |
| 118 | + // There will still be duplicate full lines when multiple text nodes are in the same line (e.g. Code highlights) | |
| 119 | + // Filter them out by using the overlay rectangle as a hash key | |
| 120 | + const matchHash = Object.fromEntries( | |
| 121 | + newMatches.map((match) => [JSON.stringify(match.overlayRectangle), match]), | |
| 122 | + ); | |
| 123 | + const filteredMatches = Object.values(matchHash).map((match, index) => ({ | |
| 124 | + ...match, | |
| 125 | + index, | |
| 126 | + })); | |
| 115 | 127 | |
| 116 | - // Find match closest to the middle of the view | |
| 117 | - const verticalMiddle = | |
| 118 | - searchRef.current.scrollTop + searchRef.current.clientHeight / 2; | |
| 119 | - let closestDist = Infinity; | |
| 120 | - let closestMatchToMiddle: SearchMatch | null = null; | |
| 121 | - filteredMatches.forEach((match) => { | |
| 122 | - const dist = Math.abs(verticalMiddle - match.overlayRectangle.top); | |
| 123 | - if (dist < closestDist) { | |
| 124 | - closestDist = dist; | |
| 125 | - closestMatchToMiddle = match; | |
| 126 | - } | |
| 127 | - }); | |
| 128 | + // Find the match closest to the vertical middle of the container | |
| 129 | + const verticalMiddle = | |
| 130 | + searchContainer.scrollTop + searchContainer.clientHeight / 2; | |
| 131 | + let closestDist = Infinity; | |
| 132 | + let closestMatchToMiddle: SearchMatch | null = null; | |
| 133 | + filteredMatches.forEach((match) => { | |
| 134 | + const dist = Math.abs(verticalMiddle - match.overlayRectangle.top); | |
| 135 | + if (dist < closestDist) { | |
| 136 | + closestDist = dist; | |
| 137 | + closestMatchToMiddle = match; | |
| 138 | + } | |
| 139 | + }); | |
| 128 | 140 | |
| 129 | - // Update matches and scroll to the closest or first match | |
| 130 | - setMatches(filteredMatches); | |
| 131 | - if (query.length > 1 && filteredMatches.length) { | |
| 132 | - if (scrollTo === "first") { | |
| 133 | - scrollToMatch(filteredMatches[0]); | |
| 134 | - } | |
| 135 | - if (scrollTo === "closest") { | |
| 136 | - if (closestMatchToMiddle) { | |
| 137 | - scrollToMatch(closestMatchToMiddle); | |
| 138 | - } | |
| 139 | - } | |
| 140 | - if (scrollTo === "none") { | |
| 141 | - if (closestMatchToMiddle) { | |
| 142 | - setCurrentMatch(closestMatchToMiddle); | |
| 143 | - } else { | |
| 144 | - setCurrentMatch(filteredMatches[0]); | |
| 145 | - } | |
| 146 | - } | |
| 147 | - } | |
| 148 | - }, | |
| 149 | - [ | |
| 150 | - headerRef, | |
| 151 | - searchRef, | |
| 152 | - debouncedInput, | |
| 153 | - scrollToMatch, | |
| 154 | - caseSensitive, | |
| 155 | - useRegex, | |
| 156 | - ...refreshDependencies, | |
| 157 | - ], | |
| 158 | -} | |
| No newline at the end of the file | ||
| 141 | + return { | |
| 142 | + results: filteredMatches, | |
| 143 | + closestToMiddle: closestMatchToMiddle, | |
| 144 | + }; | |
| 145 | +}; |
added gui/src/components/find/useDebouncedInput.ts +48 −0
| @@ -0,0 +1,48 @@ | ||
| 1 | +import { debounce } from "lodash"; | |
| 2 | +import { useEffect, useRef, useState } from "react"; | |
| 3 | + | |
| 4 | +interface UseDebouncedInputResult { | |
| 5 | + inputRef: React.RefObject<HTMLInputElement>; | |
| 6 | + debouncedValue: string; | |
| 7 | + currentValue: string; | |
| 8 | +} | |
| 9 | + | |
| 10 | +export const useDebouncedInput = ( | |
| 11 | + debounceDelay: number = 300, | |
| 12 | +): UseDebouncedInputResult => { | |
| 13 | + const [debouncedValue, setDebouncedValue] = useState(""); | |
| 14 | + const [currentValue, setCurrentValue] = useState(""); | |
| 15 | + const inputRef = useRef<HTMLInputElement>(null); | |
| 16 | + | |
| 17 | + const debouncedSetValue = useRef( | |
| 18 | + debounce((value: string) => { | |
| 19 | + setDebouncedValue(value); | |
| 20 | + }, debounceDelay), | |
| 21 | + ).current; | |
| 22 | + | |
| 23 | + useEffect(() => { | |
| 24 | + debouncedSetValue(currentValue); | |
| 25 | + // Initial value is set immediately | |
| 26 | + if (currentValue === "") { | |
| 27 | + setDebouncedValue(""); | |
| 28 | + } | |
| 29 | + | |
| 30 | + return () => { | |
| 31 | + debouncedSetValue.cancel(); | |
| 32 | + }; | |
| 33 | + }, [currentValue, debouncedSetValue]); | |
| 34 | + | |
| 35 | + useEffect(() => { | |
| 36 | + const input = inputRef.current; | |
| 37 | + if (!input) return; | |
| 38 | + | |
| 39 | + const handleChange = (event: Event) => { | |
| 40 | + setCurrentValue((event.target as HTMLInputElement).value); | |
| 41 | + }; | |
| 42 | + | |
| 43 | + input.addEventListener("input", handleChange); | |
| 44 | + return () => input.removeEventListener("input", handleChange); | |
| 45 | + }, []); | |
| 46 | + | |
| 47 | + return { inputRef, debouncedValue, currentValue }; | |
| 48 | +}; |
added gui/src/components/find/useElementDims.ts +42 −0
| @@ -0,0 +1,42 @@ | ||
| 1 | +import { debounce } from "lodash"; | |
| 2 | +import { useEffect, useState } from "react"; | |
| 3 | + | |
| 4 | +export function useElementSize( | |
| 5 | + ref: React.RefObject<HTMLElement>, | |
| 6 | + resizeDebounce = 200, | |
| 7 | +) { | |
| 8 | + const [size, setSize] = useState({ | |
| 9 | + height: 0, | |
| 10 | + width: 0, | |
| 11 | + }); | |
| 12 | + const [isResizing, setIsResizing] = useState(false); | |
| 13 | + | |
| 14 | + useEffect(() => { | |
| 15 | + if (!ref.current) return; | |
| 16 | + | |
| 17 | + const element = ref.current; | |
| 18 | + const debouncedSetSize = debounce((entries: ResizeObserverEntry[]) => { | |
| 19 | + if (!entries.length) return; | |
| 20 | + | |
| 21 | + setIsResizing(false); | |
| 22 | + setSize({ | |
| 23 | + height: entries[0].contentRect.height, | |
| 24 | + width: entries[0].contentRect.width, | |
| 25 | + }); | |
| 26 | + }, resizeDebounce); | |
| 27 | + | |
| 28 | + const resizeObserver = new ResizeObserver((entries) => { | |
| 29 | + setIsResizing(true); | |
| 30 | + debouncedSetSize(entries); | |
| 31 | + }); | |
| 32 | + resizeObserver.observe(element); | |
| 33 | + | |
| 34 | + return () => { | |
| 35 | + debouncedSetSize.cancel(); | |
| 36 | + resizeObserver.unobserve(element); | |
| 37 | + resizeObserver.disconnect(); | |
| 38 | + }; | |
| 39 | + }, [ref, resizeDebounce]); | |
| 40 | + | |
| 41 | + return { ref, size, isResizing }; | |
| 42 | +} |