Skip to content

[Backdrops] Fix text selection & right-clicks #1702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import type { CustomStyleHookMapping } from '../../utils/getStyleHookProps';
import { popupStateMapping as baseMapping } from '../../utils/popupStateMapping';
import { transitionStatusMapping } from '../../utils/styleHookMapping';
import { useForkRef } from '../../utils/useForkRef';
import { mergeProps } from '../../merge-props';

const customStyleHookMapping: CustomStyleHookMapping<AlertDialogBackdrop.State> = {
...baseMapping,
@@ -43,11 +44,17 @@ const AlertDialogBackdrop = React.forwardRef(function AlertDialogBackdrop(
className,
state,
ref: mergedRef,
extraProps: {
role: 'presentation',
hidden: !mounted,
...other,
},
extraProps: mergeProps(
{
role: 'presentation',
hidden: !mounted,
style: {
userSelect: 'none',
WebkitUserSelect: 'none',
},
},
other,
),
customStyleHookMapping,
});

17 changes: 12 additions & 5 deletions packages/react/src/dialog/backdrop/DialogBackdrop.tsx
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { useDialogRootContext } from '../root/DialogRootContext';
import { mergeProps } from '../../merge-props';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { type TransitionStatus } from '../../utils/useTransitionStatus';
import { type BaseUIComponentProps } from '../../utils/types';
@@ -43,11 +44,17 @@ const DialogBackdrop = React.forwardRef(function DialogBackdrop(
className,
state,
ref: mergedRef,
extraProps: {
role: 'presentation',
hidden: !mounted,
...other,
},
extraProps: mergeProps(
{
role: 'presentation',
hidden: !mounted,
style: {
userSelect: 'none',
WebkitUserSelect: 'none',
},
},
other,
),
customStyleHookMapping,
});

21 changes: 21 additions & 0 deletions packages/react/src/dialog/root/DialogRoot.test.tsx
Original file line number Diff line number Diff line change
@@ -192,6 +192,27 @@ describe('<Dialog.Root />', () => {
expect(handleOpenChange.callCount).to.equal(1);
expect(handleOpenChange.firstCall.args[2]).to.equal('outside-press');
});

it('does not change open state on non-main button clicks', async () => {
const handleOpenChange = spy();

const { user } = await render(
<Dialog.Root defaultOpen onOpenChange={handleOpenChange}>
<Dialog.Trigger>Open</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Backdrop data-backdrop style={{ position: 'fixed', zIndex: 10, inset: 0 }} />
<Dialog.Popup style={{ position: 'fixed', zIndex: 10 }}>
<Dialog.Close>Close</Dialog.Close>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>,
);

const backdrop = document.querySelector('[data-backdrop]') as HTMLElement;
await user.pointer([{ target: backdrop }, { keys: '[MouseRight]', target: backdrop }]);

expect(handleOpenChange.callCount).to.equal(0);
});
});
});

3 changes: 3 additions & 0 deletions packages/react/src/dialog/root/useDialogRoot.ts
Original file line number Diff line number Diff line change
@@ -109,6 +109,9 @@ export function useDialogRoot(params: useDialogRoot.Parameters): useDialogRoot.R
const dismiss = useDismiss(context, {
outsidePressEvent: 'mousedown',
outsidePress(event) {
if (event.button !== 0) {
return false;
}
const target = getTarget(event) as Element | null;
if (isTopmost && dismissible) {
const backdrop = target as HTMLDivElement | null;
6 changes: 5 additions & 1 deletion packages/react/src/menu/backdrop/MenuBackdrop.tsx
Original file line number Diff line number Diff line change
@@ -45,7 +45,11 @@ const MenuBackdrop = React.forwardRef(function MenuBackdrop(
{
role: 'presentation',
hidden: !mounted,
style: openReason === 'hover' ? { pointerEvents: 'none' } : {},
style: {
pointerEvents: openReason === 'hover' ? 'none' : undefined,
userSelect: 'none',
WebkitUserSelect: 'none',
},
},
other,
),
6 changes: 5 additions & 1 deletion packages/react/src/popover/backdrop/PopoverBackdrop.tsx
Original file line number Diff line number Diff line change
@@ -43,7 +43,11 @@ const PopoverBackdrop = React.forwardRef(function PopoverBackdrop(
{
role: 'presentation',
hidden: !mounted,
style: openReason === 'hover' ? { pointerEvents: 'none' } : {},
style: {
pointerEvents: openReason === 'hover' ? 'none' : undefined,
userSelect: 'none',
WebkitUserSelect: 'none',
},
},
elementProps,
],
Original file line number Diff line number Diff line change
@@ -48,6 +48,8 @@ const PreviewCardBackdrop = React.forwardRef(function PreviewCardBackdrop(
hidden: !mounted,
style: {
pointerEvents: 'none',
userSelect: 'none',
WebkitUserSelect: 'none',
},
},
other,
13 changes: 12 additions & 1 deletion packages/react/src/select/backdrop/SelectBackdrop.tsx
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import { popupStateMapping } from '../../utils/popupStateMapping';
import type { CustomStyleHookMapping } from '../../utils/getStyleHookProps';
import type { TransitionStatus } from '../../utils/useTransitionStatus';
import { transitionStatusMapping } from '../../utils/styleHookMapping';
import { mergeProps } from '../../merge-props';

const customStyleHookMapping: CustomStyleHookMapping<SelectBackdrop.State> = {
...popupStateMapping,
@@ -38,7 +39,17 @@ const SelectBackdrop = React.forwardRef(function SelectBackdrop(
className,
state,
ref: forwardedRef,
extraProps: { role: 'presentation', hidden: !mounted, ...other },
extraProps: mergeProps(
{
role: 'presentation',
hidden: !mounted,
style: {
userSelect: 'none',
WebkitUserSelect: 'none',
},
},
other,
),
customStyleHookMapping,
});

2 changes: 2 additions & 0 deletions packages/react/src/utils/InternalBackdrop.tsx
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ const InternalBackdrop = React.forwardRef(function InternalBackdrop(
style={{
position: 'fixed',
inset: 0,
userSelect: 'none',
WebkitUserSelect: 'none',
}}
/>
);