@julseb-lib/react 1.0.45 → 1.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2499,48 +2499,75 @@ var usePagination = ({
2499
2499
 
2500
2500
  // src/lib/hooks/useTextLineCount.tsx
2501
2501
  var import_react14 = require("react");
2502
- var useTextLineCount = (text, containerWidth, fontSize = 16, fontFamily = "system-ui") => {
2503
- const [lineCount, setLineCount] = (0, import_react14.useState)(1);
2504
- const canvasRef = (0, import_react14.useRef)(null);
2502
+ var useTextLineCount = (text, fontSize = 16) => {
2503
+ const [visualLines, setVisualLines] = (0, import_react14.useState)(1);
2504
+ const elementRef = (0, import_react14.useRef)(
2505
+ null
2506
+ );
2505
2507
  const measureLines = (0, import_react14.useCallback)(() => {
2506
- if (!canvasRef.current) {
2507
- canvasRef.current = document.createElement("canvas");
2508
+ const element = elementRef.current;
2509
+ if (!element || !text) {
2510
+ setVisualLines(1);
2511
+ return;
2508
2512
  }
2509
- const canvas = canvasRef.current;
2510
- const context = canvas.getContext("2d");
2511
- if (!context || !text || containerWidth <= 0) {
2512
- setLineCount(1);
2513
+ const computedStyle = getComputedStyle(element);
2514
+ const paddingLeft = parseInt(computedStyle.paddingLeft) || 0;
2515
+ const paddingRight = parseInt(computedStyle.paddingRight) || 0;
2516
+ const borderLeft = parseInt(computedStyle.borderLeftWidth) || 0;
2517
+ const borderRight = parseInt(computedStyle.borderRightWidth) || 0;
2518
+ const actualWidth = element.offsetWidth - paddingLeft - paddingRight - borderLeft - borderRight;
2519
+ if (actualWidth <= 0) {
2520
+ setVisualLines(1);
2513
2521
  return;
2514
2522
  }
2515
- context.font = `${fontSize}px ${fontFamily}`;
2516
- const lines = text.split("\n");
2517
- let totalLines = 0;
2518
- lines.forEach((line) => {
2519
- if (line.length === 0) {
2520
- totalLines += 1;
2521
- return;
2522
- }
2523
- const words = line.split(" ");
2524
- let currentLine = "";
2525
- let linesForThisSegment = 1;
2526
- for (const word of words) {
2527
- const testLine = currentLine ? `${currentLine} ${word}` : word;
2528
- const testWidth = context.measureText(testLine).width;
2529
- if (testWidth > containerWidth && currentLine) {
2530
- linesForThisSegment++;
2531
- currentLine = word;
2532
- } else {
2533
- currentLine = testLine;
2534
- }
2535
- }
2536
- totalLines += linesForThisSegment;
2537
- });
2538
- setLineCount(Math.max(1, totalLines));
2539
- }, [text, containerWidth, fontSize, fontFamily]);
2523
+ try {
2524
+ const hiddenDiv = document.createElement("div");
2525
+ hiddenDiv.style.position = "absolute";
2526
+ hiddenDiv.style.visibility = "hidden";
2527
+ hiddenDiv.style.height = "auto";
2528
+ hiddenDiv.style.width = `${actualWidth}px`;
2529
+ hiddenDiv.style.fontSize = computedStyle.fontSize || `${fontSize}px`;
2530
+ hiddenDiv.style.fontFamily = computedStyle.fontFamily || "system-ui";
2531
+ hiddenDiv.style.lineHeight = computedStyle.lineHeight || "1.2";
2532
+ hiddenDiv.style.wordWrap = "break-word";
2533
+ hiddenDiv.style.whiteSpace = "pre-wrap";
2534
+ hiddenDiv.style.padding = "0";
2535
+ hiddenDiv.style.margin = "0";
2536
+ hiddenDiv.style.border = "none";
2537
+ document.body.appendChild(hiddenDiv);
2538
+ hiddenDiv.textContent = text;
2539
+ const elementHeight = hiddenDiv.offsetHeight;
2540
+ const lineHeight = parseInt(getComputedStyle(hiddenDiv).lineHeight) || fontSize * 1.2;
2541
+ const calculatedLines = Math.max(
2542
+ 1,
2543
+ Math.round(elementHeight / lineHeight)
2544
+ );
2545
+ document.body.removeChild(hiddenDiv);
2546
+ setVisualLines(calculatedLines);
2547
+ } catch (error) {
2548
+ console.warn("Element line count measurement failed:", error);
2549
+ setVisualLines(text.split("\n").length);
2550
+ }
2551
+ }, [text, fontSize]);
2540
2552
  (0, import_react14.useEffect)(() => {
2541
- measureLines();
2553
+ const timer = setTimeout(measureLines, 50);
2554
+ return () => clearTimeout(timer);
2542
2555
  }, [measureLines]);
2543
- return lineCount;
2556
+ (0, import_react14.useEffect)(() => {
2557
+ const element = elementRef.current;
2558
+ if (!element) return;
2559
+ const resizeObserver = new ResizeObserver(() => {
2560
+ measureLines();
2561
+ });
2562
+ resizeObserver.observe(element);
2563
+ return () => {
2564
+ resizeObserver.disconnect();
2565
+ };
2566
+ }, [measureLines]);
2567
+ return {
2568
+ visualLines,
2569
+ elementRef
2570
+ };
2544
2571
  };
2545
2572
 
2546
2573
  // src/lib/hooks/useTouchScreen.tsx