@ultraviolet/ui 1.27.1 → 1.27.2

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.
@@ -1,6 +1,7 @@
1
1
  import _styled from '@emotion/styled/base';
2
- import { useState, useRef, useEffect } from 'react';
2
+ import { useRef } from 'react';
3
3
  import recursivelyGetChildrenString from '../../helpers/recursivelyGetChildrenString.js';
4
+ import { useIsOverflowing } from '../../hooks/useIsOverflowing.js';
4
5
  import capitalize from '../../utils/capitalize.js';
5
6
  import { Tooltip } from '../Tooltip/index.js';
6
7
  import { jsx } from '@emotion/react/jsx-runtime';
@@ -79,20 +80,11 @@ const Text = _ref2 => {
79
80
  'data-testid': dataTestId
80
81
  } = _ref2;
81
82
  const computedSentiment = sentiment ?? color;
82
- const [isTruncated, setIsTruncated] = useState(false);
83
83
  const elementRef = useRef(null);
84
+ const isOverflowing = useIsOverflowing(elementRef);
84
85
  const finalStringChildren = recursivelyGetChildrenString(children);
85
- useEffect(() => {
86
- if (oneLine && elementRef?.current) {
87
- const {
88
- offsetWidth,
89
- scrollWidth
90
- } = elementRef.current;
91
- setIsTruncated(offsetWidth < scrollWidth);
92
- }
93
- }, [oneLine]);
94
86
  return jsx(Tooltip, {
95
- text: oneLine && isTruncated ? finalStringChildren : '',
87
+ text: oneLine && isOverflowing ? finalStringChildren : '',
96
88
  children: jsx(StyledText, {
97
89
  ref: elementRef,
98
90
  as: as,
@@ -0,0 +1,34 @@
1
+ import { useState, useEffect } from 'react';
2
+
3
+ /**
4
+ * This hook checks if the element has overflow based on the offsetWidth and scrollWidth of the element.
5
+ */
6
+ const useIsOverflowing = (ref, callback) => {
7
+ const [isOverflowing, setIsOverflowing] = useState(false);
8
+ useEffect(() => {
9
+ const handleResize = () => {
10
+ if (!ref.current) {
11
+ return;
12
+ }
13
+ const element = ref.current;
14
+ const hasOverflow = element.clientWidth < element.scrollWidth;
15
+ setIsOverflowing(hasOverflow);
16
+ if (callback) callback(hasOverflow);
17
+ };
18
+
19
+ // This will add the function into the browser event queue after the DOM is painted
20
+ // which is needed to get the correct offsetWidth and scrollWidth
21
+ setTimeout(() => handleResize(), 0);
22
+
23
+ // Listen for resize events
24
+ window.addEventListener('resize', handleResize);
25
+
26
+ // Cleanup the event listener
27
+ return () => {
28
+ window.removeEventListener('resize', handleResize);
29
+ };
30
+ }, [ref, callback]);
31
+ return isOverflowing;
32
+ };
33
+
34
+ export { useIsOverflowing };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultraviolet/ui",
3
- "version": "1.27.1",
3
+ "version": "1.27.2",
4
4
  "description": "Ultraviolet UI",
5
5
  "homepage": "https://github.com/scaleway/ultraviolet#readme",
6
6
  "repository": {