Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer committed Jan 16, 2025
1 parent 6998a06 commit f4b255e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/primitives/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function Dialog({
componentTheme,
'fixed inset-0 z-50 flex items-center justify-center',
'bg-black/50 transition-opacity duration-200',
'animate-in fade-in duration-200'
'animate-in fade-in duration-200',
)}
>
<FocusTrap active={isOpen}>
Expand Down
7 changes: 7 additions & 0 deletions src/primitives/DismissableLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function DismissableLayer({
onDismiss,
}: DismissableLayerProps) {
const layerRef = useRef<HTMLDivElement>(null);
// Tracks whether the pointer event originated inside the React component tree
const isPointerInsideReactTreeRef = useRef(false);

useEffect(() => {
Expand All @@ -33,15 +34,19 @@ export function DismissableLayer({
return layerRef.current && !layerRef.current.contains(target);
};

// Handle clicks outside the layer
const handlePointerDown = (event: PointerEvent) => {
// Skip if outside clicks are disabled or if the click started inside the component
if (disableOutsideClick || isPointerInsideReactTreeRef.current) {
isPointerInsideReactTreeRef.current = false;
return;
}

// Dismiss if click is outside the layer
if (shouldDismiss(event.target as Node)) {
onDismiss?.();
}
// Reset the flag after handling the event
isPointerInsideReactTreeRef.current = false;
};

Expand All @@ -57,6 +62,8 @@ export function DismissableLayer({
return (
<div
data-testid="ockDismissableLayer"
// Set flag when pointer event starts inside the component
// This prevents dismissal when dragging from inside to outside
onPointerDownCapture={() => {
isPointerInsideReactTreeRef.current = true;
}}
Expand Down

0 comments on commit f4b255e

Please sign in to comment.