Skip to content
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

Wrap custom events dispatching with flushSync #1292

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion packages/react/dismissable-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"react-remove-scroll": "^2.4.0"
},
"peerDependencies": {
"react": "^16.8 || ^17.0"
"react": "^16.8 || ^17.0",
"react-dom": "^16.8 || ^17.0"
},
"homepage": "https://radix-ui.com/primitives",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion packages/react/dismissable-layer/src/DismissableLayer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { composeEventHandlers } from '@radix-ui/primitive';
import { Primitive } from '@radix-ui/react-primitive';
import { useComposedRefs } from '@radix-ui/react-compose-refs';
Expand Down Expand Up @@ -281,7 +282,7 @@ function dispatchCustomEvent<E extends CustomEvent, OriginalEvent extends Event>
const target = detail.originalEvent.target as HTMLElement;
const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail });
if (handler) target.addEventListener(name, handler as EventListener, { once: true });
return !target.dispatchEvent(event);
return ReactDOM.flushSync(() => !target.dispatchEvent(event));
}

const Root = DismissableLayer;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/toast/src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ function dispatchCustomEvent<E extends CustomEvent, ReactEvent extends React.Syn
const currentTarget = detail.originalEvent.currentTarget as HTMLElement;
const event = new CustomEvent(name, { bubbles: true, cancelable: true, detail });
if (handler) currentTarget.addEventListener(name, handler as EventListener, { once: true });
currentTarget.dispatchEvent(event);
ReactDOM.flushSync(() => currentTarget.dispatchEvent(event));
}

const isDeltaInDirection = (
Expand Down
17 changes: 10 additions & 7 deletions packages/react/tooltip/src/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { composeEventHandlers } from '@radix-ui/primitive';
import { useComposedRefs } from '@radix-ui/react-compose-refs';
import { createContextScope } from '@radix-ui/react-context';
Expand Down Expand Up @@ -157,13 +158,15 @@ const Tooltip: React.FC<TooltipProps> = (props: ScopedProps<TooltipProps>) => {
prop: openProp,
defaultProp: defaultOpen,
onChange: (open) => {
if (open) {
// we dispatch here so `TooltipProvider` isn't required to
// ensure other tooltips are aware of this one opening.
document.dispatchEvent(new CustomEvent(TOOLTIP_OPEN));
onOpen();
}
onOpenChange?.(open);
ReactDOM.flushSync(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In here, I've decided to wrap the whole onChange handler to "batch" all updates scheduled by within this callback

if (open) {
// we dispatch here so `TooltipProvider` isn't required to
// ensure other tooltips are aware of this one opening.
document.dispatchEvent(new CustomEvent(TOOLTIP_OPEN));
onOpen();
}
onOpenChange?.(open);
});
},
});
const stateAttribute = React.useMemo(() => {
Expand Down