mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-24 10:17:14 +02:00
* style(ui): add tooltips for long playlist and album names - 5068 Signed-off-by: Thiago Sfreddo <sfredo@gmail.com> * fix dnd and improve performance Signed-off-by: Thiago Sfreddo <sfredo@gmail.com> * lint fixes Signed-off-by: Thiago Sfreddo <sfredo@gmail.com> * fix(ui): update tooltip styles for improved visibility and consistency Signed-off-by: Deluan <deluan@navidrome.org> * fix(ui): add overflow tooltip to playlist name for better visibility Signed-off-by: Deluan <deluan@navidrome.org> * refactor(ui): simplify OverflowTooltip and improve render performance - Inline styles from useMenuTooltipStyles into OverflowTooltip (single consumer) - Use MUI named colors (grey[700]/grey[300] with alpha) instead of raw rgba - Stabilize ref callback with useCallback to avoid unnecessary ref churn - Memoize Tooltip classes and hoist TransitionProps to module level - Fix useLayoutEffect dependency: observe DOM size, not title string --------- Signed-off-by: Thiago Sfreddo <sfredo@gmail.com> Signed-off-by: Deluan <deluan@navidrome.org> Co-authored-by: Deluan Quintão <deluan@navidrome.org>
90 lines
2.1 KiB
JavaScript
90 lines
2.1 KiB
JavaScript
import React from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import { Tooltip } from '@material-ui/core'
|
|
import { makeStyles, alpha } from '@material-ui/core/styles'
|
|
import grey from '@material-ui/core/colors/grey'
|
|
|
|
const useStyles = makeStyles(
|
|
(theme) => ({
|
|
tooltip: {
|
|
backgroundColor:
|
|
theme.palette.type === 'dark'
|
|
? alpha(grey[700], 0.92)
|
|
: alpha(grey[300], 0.92),
|
|
color:
|
|
theme.palette.type === 'dark'
|
|
? theme.palette.common.white
|
|
: theme.palette.common.black,
|
|
borderRadius: theme.shape.borderRadius,
|
|
...theme.typography.body2,
|
|
padding: theme.spacing(0.5, 1),
|
|
maxWidth: 300,
|
|
},
|
|
}),
|
|
{ name: 'NDOverflowTooltip' },
|
|
)
|
|
|
|
const transitionProps = { timeout: 0 }
|
|
|
|
export const OverflowTooltip = ({
|
|
children,
|
|
title,
|
|
placement = 'bottom-start',
|
|
}) => {
|
|
const classes = useStyles()
|
|
const textRef = React.useRef(null)
|
|
const [isOverflowing, setIsOverflowing] = React.useState(false)
|
|
const tooltipClasses = React.useMemo(
|
|
() => ({ tooltip: classes.tooltip }),
|
|
[classes.tooltip],
|
|
)
|
|
|
|
React.useLayoutEffect(() => {
|
|
const el = textRef.current
|
|
if (!el) return
|
|
|
|
const checkOverflow = () => {
|
|
setIsOverflowing(el.scrollWidth > el.clientWidth)
|
|
}
|
|
|
|
const resizeObserver = new ResizeObserver(checkOverflow)
|
|
resizeObserver.observe(el)
|
|
|
|
checkOverflow()
|
|
|
|
return () => resizeObserver.disconnect()
|
|
}, [])
|
|
|
|
const mergedRef = React.useCallback(
|
|
(el) => {
|
|
textRef.current = el
|
|
|
|
const { ref } = children
|
|
if (typeof ref === 'function') {
|
|
ref(el)
|
|
} else if (ref && typeof ref === 'object') {
|
|
ref.current = el
|
|
}
|
|
},
|
|
[children],
|
|
)
|
|
|
|
return (
|
|
<Tooltip
|
|
title={title}
|
|
disableHoverListener={!isOverflowing}
|
|
disableTouchListener
|
|
placement={placement}
|
|
TransitionProps={transitionProps}
|
|
classes={tooltipClasses}
|
|
>
|
|
{React.cloneElement(children, { ref: mergedRef })}
|
|
</Tooltip>
|
|
)
|
|
}
|
|
|
|
OverflowTooltip.propTypes = {
|
|
children: PropTypes.element.isRequired,
|
|
title: PropTypes.string.isRequired,
|
|
placement: PropTypes.string,
|
|
}
|