Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dashboard): Fix hover labels for horizontal overflow native filter dividers #22210

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ test('horizontal mode, title', () => {
orientation={FilterBarOrientation.HORIZONTAL}
title={SAMPLE_TITLE}
description=""
overflow
/>,
);

Expand Down Expand Up @@ -88,9 +89,7 @@ test('horizontal mode, title and description', async () => {
const descriptionIcon = screen.getByTestId('divider-description-icon');
expect(descriptionIcon).toBeVisible();
userEvent.hover(descriptionIcon);
const tooltip = await screen.findByRole('tooltip', {
name: SAMPLE_DESCRIPTION,
});
const tooltip = await screen.findByRole('tooltip');

expect(tooltip).toBeInTheDocument();
expect(tooltip).toHaveTextContent(SAMPLE_DESCRIPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const VerticalDivider = ({ title, description }: FilterDividerProps) => (
const HorizontalDivider = ({ title, description }: FilterDividerProps) => {
const theme = useTheme();
const [titleRef, titleIsTruncated] =
useCSSTextTruncation<HTMLHeadingElement>(title);
useCSSTextTruncation<HTMLHeadingElement>();

const tooltipOverlay = (
<>
Expand Down Expand Up @@ -95,10 +95,10 @@ const HorizontalOverflowDivider = ({
}: FilterDividerProps) => {
const theme = useTheme();
const [titleRef, titleIsTruncated] =
useCSSTextTruncation<HTMLHeadingElement>(title);
useCSSTextTruncation<HTMLHeadingElement>();

const [descriptionRef, descriptionIsTruncated] =
useCSSTextTruncation<HTMLHeadingElement>(description);
useCSSTextTruncation<HTMLHeadingElement>();

return (
<div
Expand Down
25 changes: 18 additions & 7 deletions superset-frontend/src/hooks/useTruncation/useCSSTextTruncation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,27 @@ export const truncationCSS = css`
* to be displayed, this hook returns a ref to attach to the text
* element and a boolean for whether that element is currently truncated.
*/
const useCSSTextTruncation = <T extends HTMLElement>(
text: string,
): [React.RefObject<T>, boolean] => {
const ref = useRef<T>(null);
const useCSSTextTruncation = <T extends HTMLElement>(): [
React.RefObject<T>,
boolean,
] => {
const [isTruncated, setIsTruncated] = useState(true);
const ref = useRef<T>(null);
const { offsetWidth, scrollWidth } = ref.current ?? {};
const prevWidths = useRef({ offsetWidth, scrollWidth });
const { offsetWidth: prevOffsetWidth, scrollWidth: prevScrollWidth } =
prevWidths.current;

useEffect(() => {
if (ref.current) {
setIsTruncated(ref.current.offsetWidth < ref.current.scrollWidth);
if (
offsetWidth &&
scrollWidth &&
(offsetWidth !== prevOffsetWidth || scrollWidth !== prevScrollWidth)
) {
prevWidths.current = { offsetWidth, scrollWidth };
setIsTruncated(offsetWidth < scrollWidth);
}
}, [text]);
}, [offsetWidth, prevOffsetWidth, prevScrollWidth, scrollWidth]);

return [ref, isTruncated];
};
Expand Down