mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
## For #27667 - Have `TooltipTruncatedText` component use `useCheckTruncatedElement` to track its current state of truncation. - Update `useCheckTruncatedElement` to re-evaluate truncation state based on changes to the width of the element itself as opposed to changes to viewport width. This facilitates truncation when the width of the element is updated due to user interaction / change in UI state other than window resize, e.g. checking a policy in the policy software automations modal (see issue description for details reproduction instructions there). **Truncation with tooltip successful for UI state changes:**  Truncation with tooltip successful for viewport resizing:   - [x] Changes file added for user-visible changes in `changes/⁄ - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
36 lines
991 B
TypeScript
36 lines
991 B
TypeScript
import { useLayoutEffect, useState } from "react";
|
|
|
|
/**
|
|
* This hook checks if an element is truncated and returns a boolean value.
|
|
*/
|
|
// eslint-disable-next-line import/prefer-default-export
|
|
export const useCheckTruncatedElement = <T extends HTMLElement>(
|
|
ref: React.RefObject<T>
|
|
) => {
|
|
const [isTruncated, setIsTruncated] = useState(false);
|
|
|
|
const updateIsTruncated = (element: HTMLElement) => {
|
|
const { scrollWidth, clientWidth } = element;
|
|
setIsTruncated(scrollWidth > clientWidth);
|
|
};
|
|
|
|
useLayoutEffect(() => {
|
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
entries.forEach((entry) => {
|
|
updateIsTruncated(entry.target as HTMLElement);
|
|
});
|
|
});
|
|
const element = ref.current;
|
|
if (element) {
|
|
updateIsTruncated(element);
|
|
resizeObserver.observe(ref.current as HTMLElement);
|
|
}
|
|
return () => {
|
|
if (element) {
|
|
resizeObserver.unobserve(element);
|
|
}
|
|
};
|
|
}, [ref]);
|
|
|
|
return isTruncated;
|
|
};
|