Files
Reims-Go/frontend/src/ui/controls.tsx
T
Dnyo666 371eb11629 fix(ui): prevent titlebar menu switch flicker and overlay focus-tooltip races
- Suppress deferred Radix `onCloseAutoFocus` focus restoration in `TitleMenus` when switching between sibling menus (or opening a dialog) so the closing menu's `setTimeout(0)` refocus no longer triggers `onFocusOutside` on the newly opened menu
- Guard `TitleMenus` `onOpenChange` by active menu ownership, block sibling-trigger `onPointerDownOutside` races, add 300ms hover-click guard (primary button without Ctrl), RTL-aware `ArrowLeft`/`ArrowRight` navigation, `--wails-draggable: no-drag` on `.titlebar-menus`, and instant lateral menu-switch transitions
- Keep `Tip` permanently controlled in `controls.tsx` to eliminate uncontrolled/controlled `useControllableState` warnings and stuck ghost tooltips when toggled via `disabled`
- Guard both VMPage `⋯` menus (toolbar + SSH) and Sidebar `ContextMenu` against pointer-close tooltip pops and dialog focus fighting, and reset menu/dialog state on VM switch
- Improve `Modal` `onOpenAutoFocus` to skip disabled/hidden/unchecked-radio inputs with `preventScroll`, fall back to the dialog container instead of the `✕` close tooltip, and exclude `.dialog`/`.menu` containers from `:focus-visible` ring overrides in `app.css`
2026-11-30 17:14:23 +08:00

104 lines
3.4 KiB
TypeScript

import * as RTooltip from '@radix-ui/react-tooltip'
import { LoaderCircle } from 'lucide-react'
import React from 'react'
export const TooltipProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<RTooltip.Provider delayDuration={400} skipDelayDuration={200}>{children}</RTooltip.Provider>
)
interface TipProps {
label: string
shortcut?: string
side?: 'top' | 'bottom' | 'left' | 'right'
disabled?: boolean
children: React.ReactElement
}
/** Tooltip around a single focusable child. */
export const Tip: React.FC<TipProps> = ({ label, shortcut, side = 'bottom', disabled, children }) => {
const [open, setOpen] = React.useState(false)
React.useEffect(() => {
if (disabled) setOpen(false)
}, [disabled])
return (
<RTooltip.Root open={disabled ? false : open} onOpenChange={(next) => setOpen(disabled ? false : next)}>
<RTooltip.Trigger asChild>{children}</RTooltip.Trigger>
<RTooltip.Portal>
<RTooltip.Content className="tooltip" side={side} sideOffset={6} collisionPadding={8}>
{label}
{shortcut && <kbd>{shortcut}</kbd>}
</RTooltip.Content>
</RTooltip.Portal>
</RTooltip.Root>
)
}
type ButtonVariant = 'secondary' | 'primary' | 'danger' | 'ghost'
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: ButtonVariant
busy?: boolean
icon?: React.ReactNode
}
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ variant = 'secondary', busy, icon, children, disabled, className, ...rest }, ref) => (
<button
ref={ref}
type="button"
className={`btn${variant === 'secondary' ? '' : ` btn-${variant}`}${className ? ` ${className}` : ''}`}
disabled={disabled || busy}
aria-busy={busy || undefined}
{...rest}
>
{busy ? <LoaderCircle size={14} className="spin" aria-hidden="true" /> : icon}
{children}
</button>
),
)
Button.displayName = 'Button'
interface IconButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'children'> {
label: string
shortcut?: string
icon: React.ReactNode
busy?: boolean
small?: boolean
tipSide?: TipProps['side']
}
/** Icon-only button: always carries a tooltip and an accessible name (§4.5). */
export const IconButton = React.forwardRef<HTMLButtonElement, IconButtonProps>(
({ label, shortcut, icon, busy, small, disabled, className, tipSide, ...rest }, ref) => (
<Tip label={label} shortcut={shortcut} side={tipSide}>
<button
ref={ref}
type="button"
aria-label={label}
className={`icon-btn${small ? ' sm' : ''}${className ? ` ${className}` : ''}`}
disabled={disabled || busy}
aria-busy={busy || undefined}
{...rest}
>
{busy ? <LoaderCircle size={16} className="spin" aria-hidden="true" /> : icon}
</button>
</Tip>
),
)
IconButton.displayName = 'IconButton'
export type StatusKind = 'running' | 'stopped' | 'busy' | 'broken' | 'warn'
export function statusKind(state: string): StatusKind {
if (state === 'running') return 'running'
if (state === 'starting' || state === 'stopping' || state === 'creating') return 'busy'
if (state === 'driver_unavailable') return 'warn'
if (state === 'broken') return 'broken'
return 'stopped'
}
/** Status shape: differs by shape as well as colour. */
export const StatusShape: React.FC<{ kind: StatusKind }> = ({ kind }) => (
<span className={`status status-${kind}`} aria-hidden="true" />
)