From 72417653408a9bbfbf9e8aa7a37620edbaee931a Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 11:42:25 -0700 Subject: [PATCH] Cancel popover wheel frames from content ref (#3466) * fix: cancel popover wheel frames from content ref * fix: preserve popover ref cleanup Co-authored-by: Orca --------- Co-authored-by: Jinwoo-H Co-authored-by: Orca --- .../src/components/ui/popover-content-ref.ts | 44 +++++++++++++++ .../src/components/ui/popover.test.tsx | 56 +++++++++++++++++++ src/renderer/src/components/ui/popover.tsx | 23 +++++--- 3 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 src/renderer/src/components/ui/popover-content-ref.ts create mode 100644 src/renderer/src/components/ui/popover.test.tsx diff --git a/src/renderer/src/components/ui/popover-content-ref.ts b/src/renderer/src/components/ui/popover-content-ref.ts new file mode 100644 index 000000000..cbdf78347 --- /dev/null +++ b/src/renderer/src/components/ui/popover-content-ref.ts @@ -0,0 +1,44 @@ +import * as React from 'react' +import type { Popover as PopoverPrimitive } from 'radix-ui' + +type PopoverContentRef = React.ComponentProps['ref'] + +export function updatePopoverContentRef( + forwardedRef: PopoverContentRef | undefined, + node: HTMLDivElement | null, + cancelWheelFrames: () => void +): (() => void) | undefined { + if (node === null) { + cancelWheelFrames() + if (typeof forwardedRef === 'function') { + forwardedRef(null) + } else if (forwardedRef) { + forwardedRef.current = null + } + return undefined + } + + if (typeof forwardedRef === 'function') { + const cleanup = forwardedRef(node) + // Why: React callback refs may return cleanup; wrapping the Radix ref must + // preserve that cleanup instead of replacing it with a null callback. + return () => { + cancelWheelFrames() + if (typeof cleanup === 'function') { + cleanup() + } else { + forwardedRef(null) + } + } + } + + if (forwardedRef) { + forwardedRef.current = node + } + return () => { + cancelWheelFrames() + if (forwardedRef) { + forwardedRef.current = null + } + } +} diff --git a/src/renderer/src/components/ui/popover.test.tsx b/src/renderer/src/components/ui/popover.test.tsx new file mode 100644 index 000000000..3d84a1f8e --- /dev/null +++ b/src/renderer/src/components/ui/popover.test.tsx @@ -0,0 +1,56 @@ +import { describe, expect, it, vi } from 'vitest' +import { updatePopoverContentRef } from './popover-content-ref' + +describe('updatePopoverContentRef', () => { + it('preserves callback ref cleanup when content detaches', () => { + const node = {} as HTMLDivElement + const cancelWheelFrames = vi.fn() + const cleanup = vi.fn() + const ref = vi.fn(() => cleanup) + + const detach = updatePopoverContentRef(ref, node, cancelWheelFrames) + detach?.() + + expect(ref).toHaveBeenCalledTimes(1) + expect(ref).toHaveBeenCalledWith(node) + expect(cancelWheelFrames).toHaveBeenCalledTimes(1) + expect(cleanup).toHaveBeenCalledTimes(1) + }) + + it('clears callback refs that do not return cleanup', () => { + const node = {} as HTMLDivElement + const cancelWheelFrames = vi.fn() + const ref = vi.fn() + + const detach = updatePopoverContentRef(ref, node, cancelWheelFrames) + detach?.() + + expect(ref).toHaveBeenNthCalledWith(1, node) + expect(ref).toHaveBeenNthCalledWith(2, null) + expect(cancelWheelFrames).toHaveBeenCalledTimes(1) + }) + + it('clears object refs when content detaches', () => { + const node = {} as HTMLDivElement + const cancelWheelFrames = vi.fn() + const ref: { current: HTMLDivElement | null } = { current: null } + + const detach = updatePopoverContentRef(ref, node, cancelWheelFrames) + expect(ref.current).toBe(node) + + detach?.() + + expect(ref.current).toBeNull() + expect(cancelWheelFrames).toHaveBeenCalledTimes(1) + }) + + it('cancels pending frames when React supplies a null node', () => { + const cancelWheelFrames = vi.fn() + const ref: { current: HTMLDivElement | null } = { current: {} as HTMLDivElement } + + updatePopoverContentRef(ref, null, cancelWheelFrames) + + expect(ref.current).toBeNull() + expect(cancelWheelFrames).toHaveBeenCalledTimes(1) + }) +}) diff --git a/src/renderer/src/components/ui/popover.tsx b/src/renderer/src/components/ui/popover.tsx index a1df95870..8befd2e65 100644 --- a/src/renderer/src/components/ui/popover.tsx +++ b/src/renderer/src/components/ui/popover.tsx @@ -4,6 +4,7 @@ import * as React from 'react' import { Popover as PopoverPrimitive } from 'radix-ui' import { cn } from '@/lib/utils' +import { updatePopoverContentRef } from './popover-content-ref' function Popover(props: React.ComponentProps) { return @@ -24,20 +25,27 @@ function PopoverContent({ portalContainer, style, onWheel, + ref: forwardedRef, ...props }: React.ComponentProps & { portalContainer?: HTMLElement | null }) { const wheelFrameIdsRef = React.useRef>(new Set()) - React.useEffect( - () => () => { - for (const frameId of wheelFrameIdsRef.current) { - cancelAnimationFrame(frameId) - } - wheelFrameIdsRef.current.clear() + const cancelWheelFrames = React.useCallback(() => { + for (const frameId of wheelFrameIdsRef.current) { + cancelAnimationFrame(frameId) + } + wheelFrameIdsRef.current.clear() + }, []) + + const setContentRef = React.useCallback( + (node: HTMLDivElement | null) => { + // Why: the wheel shim schedules frames against the content node; cancel + // them when Radix removes that node instead of from a passive Effect. + return updatePopoverContentRef(forwardedRef, node, cancelWheelFrames) }, - [] + [cancelWheelFrames, forwardedRef] ) const handleWheel = React.useCallback( @@ -89,6 +97,7 @@ function PopoverContent({ 'z-[60] overflow-hidden rounded-md border border-border/50 bg-popover text-popover-foreground shadow-md outline-none data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95', className )} + ref={setContentRef} // Why: Electron's -webkit-app-region: drag on the titlebar captures // clicks at the OS level regardless of z-index. Without no-drag, // popovers that visually overlap the titlebar are unclickable.