feat(webui): truncate long file names with 100ms hover tooltip (#355)

This commit is contained in:
Yiling-J
2026-08-27 10:54:35 +08:00
committed by GitHub
parent 5ec4cd7fcd
commit d9a8b6a191
7 changed files with 324 additions and 24 deletions
+78 -15
View File
@@ -1,33 +1,96 @@
import { Input } from '@/ui/components/ui/input'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/ui/components/ui/tooltip'
import { cn } from '@/ui/lib/utils'
import React from 'react'
import React, { useRef, useState } from 'react'
export type EditableTextProps = React.ComponentPropsWithoutRef<'input'>
export type EditableTextProps = React.ComponentPropsWithoutRef<'input'> & {
tooltipSide?: 'top' | 'right' | 'bottom' | 'left'
tooltipDelayDuration?: number
}
const EditableText = React.forwardRef<HTMLInputElement, EditableTextProps>(
({ className, onClick, onMouseDown, disabled, ...props }, ref) => {
(
{
className,
onClick,
onMouseDown,
disabled,
value,
title: _title,
tooltipSide = 'top',
tooltipDelayDuration = 100,
...props
},
ref,
) => {
const [isOpen, setIsOpen] = useState(false)
const spanRef = useRef<HTMLSpanElement>(null)
const textContent = typeof value === 'string' ? value : (value?.toString() ?? '')
if (disabled) {
return (
<TooltipProvider delayDuration={tooltipDelayDuration}>
<Tooltip
open={isOpen}
onOpenChange={(nextOpen) => {
if (nextOpen) {
if (spanRef.current && spanRef.current.scrollWidth > spanRef.current.clientWidth) {
setIsOpen(true)
} else {
setIsOpen(false)
}
} else {
setIsOpen(false)
}
}}
>
<TooltipTrigger asChild>
<span
ref={spanRef}
className={cn(
'box-border block min-w-0 w-full rounded-sm border border-transparent px-1 py-0.5 text-base shadow-none transition-all outline-none cursor-default opacity-100 select-none truncate',
className,
)}
onClick={onClick}
onMouseDown={onMouseDown}
>
{textContent}
</span>
</TooltipTrigger>
{isOpen && (
<TooltipContent
side={tooltipSide}
className="max-w-md break-all text-wrap [text-wrap:normal] text-left"
>
{textContent}
</TooltipContent>
)}
</Tooltip>
</TooltipProvider>
)
}
return (
<Input
ref={ref}
readOnly={disabled}
tabIndex={disabled ? -1 : undefined}
value={value}
tabIndex={0}
className={cn(
'h-auto min-h-0 w-full rounded-sm border px-1 py-0.5 text-base shadow-none transition-all outline-none focus-visible:ring-0',
!disabled
? 'border-blue-500 !bg-muted/10 ring-1 ring-blue-500'
: 'border-transparent !bg-transparent cursor-default opacity-100 focus-visible:border-transparent focus-visible:ring-0 caret-transparent select-none',
'box-border block min-w-0 h-auto w-full rounded-sm border border-blue-500 !bg-muted/10 ring-1 ring-blue-500 px-1 py-0.5 text-base shadow-none transition-all outline-none focus-visible:ring-0',
className,
)}
onClick={(e) => {
if (!disabled) {
e.stopPropagation()
}
e.stopPropagation()
onClick?.(e)
}}
onMouseDown={(e) => {
if (!disabled) {
e.stopPropagation()
}
e.stopPropagation()
onMouseDown?.(e)
}}
{...props}