diff --git a/client/src/javascript/components/sidebar/SidebarFilter.tsx b/client/src/javascript/components/sidebar/SidebarFilter.tsx index 370e74274..4afc6ad07 100644 --- a/client/src/javascript/components/sidebar/SidebarFilter.tsx +++ b/client/src/javascript/components/sidebar/SidebarFilter.tsx @@ -1,11 +1,24 @@ import classnames from 'classnames'; -import {FC, ReactNode, KeyboardEvent, MouseEvent, TouchEvent, useState} from 'react'; +import {createRef, FC, ReactNode, KeyboardEvent, MouseEvent, RefObject, TouchEvent, useEffect, useState} from 'react'; import {useLingui} from '@lingui/react'; import {Start} from '@client/ui/icons'; import Badge from '../general/Badge'; import Size from '../general/Size'; +const useRefTextOverflowed = (ref: RefObject) => { + const [overflowed, setOverflowed] = useState(false); + + useEffect(() => { + if (ref.current) { + const {current} = ref; + setOverflowed(current.scrollWidth > current.clientWidth); + } + }, [ref, ref?.current?.scrollWidth, ref?.current?.clientWidth]); + + return overflowed; +} + interface SidebarFilterProps { children?: ReactNode[]; name: string; @@ -27,6 +40,9 @@ const SidebarFilter: FC = ({ size, handleClick, }: SidebarFilterProps) => { + const nameSpanRef = createRef(); + const overflowed = useRefTextOverflowed(nameSpanRef); + const {i18n} = useLingui(); const [expanded, setExpanded] = useState(false); @@ -67,17 +83,6 @@ const SidebarFilter: FC = ({ } } - const setTitleForOverflowedName = (event: MouseEvent) => { - const target = event.target as HTMLSpanElement; - const overflowed = target.scrollWidth > target.clientWidth; - target.title = overflowed ? target.textContent || '' : ''; - }; - - const unsetTitle = (event: MouseEvent) => { - const target = event.target as HTMLSpanElement; - target.title = ''; - }; - return (
  • @@ -101,7 +106,7 @@ const SidebarFilter: FC = ({ role="menuitem" > {icon} - + {name} {count}