Skip to content

An open-source, faster Devin alternative

TypeScript

fix(web): handle retargeted dropdown dismissal events

Igor Zalutskib214853810c0
diff --git a/web/src/components/form.tsx b/web/src/components/form.tsx
index 77e7716a..e521b50a 100644
--- a/web/src/components/form.tsx
+++ b/web/src/components/form.tsx
@@ -3,7 +3,10 @@ import { Select as SelectPrimitive } from 'radix-ui'
import { Check, ChevronDown } from 'lucide-react'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
-import { floatingLayerProps } from '@/components/ui/floating-layer'
+import {
+ floatingLayerProps,
+ markFloatingLayerOutsideInteraction,
+} from '@/components/ui/floating-layer'
import { cn } from '@/lib/utils'
// Re-exported so screens import all form primitives from one place.
@@ -52,6 +55,7 @@ export function Select({
{...floatingLayerProps}
position="popper"
sideOffset={4}
+ onPointerDownOutside={markFloatingLayerOutsideInteraction}
className="bg-popover text-popover-foreground ring-foreground/10 shadow-overlay data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 z-50 max-h-(--radix-select-content-available-height) min-w-(--radix-select-trigger-width) origin-(--radix-select-content-transform-origin) overflow-hidden rounded-lg p-1 ring-1 duration-100"
>
<SelectPrimitive.Viewport>
diff --git a/web/src/components/ui/dialog.tsx b/web/src/components/ui/dialog.tsx
index bb045cc5..1dddf779 100644
--- a/web/src/components/ui/dialog.tsx
+++ b/web/src/components/ui/dialog.tsx
@@ -4,10 +4,10 @@ import { Dialog as DialogPrimitive } from 'radix-ui'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import { XIcon } from 'lucide-react'
-import { isEventFromFloatingLayer } from '@/components/ui/floating-layer'
+import { shouldKeepParentOpenForFloatingLayer } from '@/components/ui/floating-layer'
function keepDialogOpenForFloatingLayer(event: Event) {
- if (isEventFromFloatingLayer(event)) event.preventDefault()
+ if (shouldKeepParentOpenForFloatingLayer(event)) event.preventDefault()
}
function Dialog({
diff --git a/web/src/components/ui/dropdown-menu.tsx b/web/src/components/ui/dropdown-menu.tsx
index 9e5be012..d1a1653f 100644
--- a/web/src/components/ui/dropdown-menu.tsx
+++ b/web/src/components/ui/dropdown-menu.tsx
@@ -3,7 +3,10 @@ import { DropdownMenu as DropdownMenuPrimitive } from 'radix-ui'
import { cn } from '@/lib/utils'
import { CheckIcon, ChevronRightIcon } from 'lucide-react'
-import { floatingLayerProps } from '@/components/ui/floating-layer'
+import {
+ floatingLayerProps,
+ markFloatingLayerOutsideInteraction,
+} from '@/components/ui/floating-layer'
function DropdownMenu({
...props
@@ -34,6 +37,9 @@ function DropdownMenuContent({
className,
align = 'start',
sideOffset = 4,
+ onInteractOutside,
+ onPointerDownOutside,
+ onFocusOutside,
...props
}: React.ComponentProps<typeof DropdownMenuPrimitive.Content>) {
return (
@@ -47,6 +53,18 @@ function DropdownMenuContent({
'bg-popover text-popover-foreground ring-foreground/10 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-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95 shadow-overlay z-50 max-h-(--radix-dropdown-menu-content-available-height) w-(--radix-dropdown-menu-trigger-width) min-w-32 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-lg p-1 ring-1 duration-100 data-[state=closed]:overflow-hidden',
className,
)}
+ onInteractOutside={(event) => {
+ markFloatingLayerOutsideInteraction()
+ onInteractOutside?.(event)
+ }}
+ onPointerDownOutside={(event) => {
+ markFloatingLayerOutsideInteraction()
+ onPointerDownOutside?.(event)
+ }}
+ onFocusOutside={(event) => {
+ markFloatingLayerOutsideInteraction()
+ onFocusOutside?.(event)
+ }}
{...props}
/>
</DropdownMenuPrimitive.Portal>
@@ -240,6 +258,9 @@ function DropdownMenuSubTrigger({
function DropdownMenuSubContent({
className,
+ onInteractOutside,
+ onPointerDownOutside,
+ onFocusOutside,
...props
}: React.ComponentProps<typeof DropdownMenuPrimitive.SubContent>) {
return (
@@ -250,6 +271,18 @@ function DropdownMenuSubContent({
'bg-popover text-popover-foreground ring-foreground/10 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-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95 shadow-overlay z-50 min-w-[96px] origin-(--radix-dropdown-menu-content-transform-origin) overflow-hidden rounded-lg p-1 ring-1 duration-100',
className,
)}
+ onInteractOutside={(event) => {
+ markFloatingLayerOutsideInteraction()
+ onInteractOutside?.(event)
+ }}
+ onPointerDownOutside={(event) => {
+ markFloatingLayerOutsideInteraction()
+ onPointerDownOutside?.(event)
+ }}
+ onFocusOutside={(event) => {
+ markFloatingLayerOutsideInteraction()
+ onFocusOutside?.(event)
+ }}
{...props}
/>
)
diff --git a/web/src/components/ui/floating-layer.ts b/web/src/components/ui/floating-layer.ts
index d6bbc8e0..3a16d2f1 100644
--- a/web/src/components/ui/floating-layer.ts
+++ b/web/src/components/ui/floating-layer.ts
@@ -1,5 +1,6 @@
const FLOATING_LAYER_ATTR = 'data-oc-floating-layer'
const FLOATING_LAYER_SELECTOR = `[${FLOATING_LAYER_ATTR}]`
+let floatingLayerOutsideInteractionDepth = 0
type OutsideEventDetail = {
originalEvent?: Event
@@ -9,13 +10,36 @@ export const floatingLayerProps = {
[FLOATING_LAYER_ATTR]: '',
} as const
-export function isEventFromFloatingLayer(event: Event) {
+function originalEventTarget(event: Event) {
const originalEvent = (event as CustomEvent<OutsideEventDetail>).detail
?.originalEvent
- const target = originalEvent?.target ?? event.target
+
+ return originalEvent?.target ?? event.target
+}
+
+function ownerDocumentForEvent(event: Event) {
+ const target = originalEventTarget(event)
+
+ return target instanceof Node ? target.ownerDocument : globalThis.document
+}
+
+export function markFloatingLayerOutsideInteraction() {
+ floatingLayerOutsideInteractionDepth += 1
+ globalThis.setTimeout(() => {
+ floatingLayerOutsideInteractionDepth = Math.max(
+ 0,
+ floatingLayerOutsideInteractionDepth - 1,
+ )
+ }, 0)
+}
+
+export function shouldKeepParentOpenForFloatingLayer(event: Event) {
+ const target = originalEventTarget(event)
return (
- target instanceof Element &&
- target.closest(FLOATING_LAYER_SELECTOR) !== null
+ floatingLayerOutsideInteractionDepth > 0 ||
+ (target instanceof Element &&
+ target.closest(FLOATING_LAYER_SELECTOR) !== null) ||
+ ownerDocumentForEvent(event)?.querySelector(FLOATING_LAYER_SELECTOR) !== null
)
}