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

next: fix dismissable layer #480

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/bits-ui/src/lib/bits/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export * as Toggle from "./toggle/index.js";
export * as ToggleGroup from "./toggle-group/index.js";
export * as Toolbar from "./toolbar/index.js";
export * as Tooltip from "./tooltip/index.js";
export * as DismissableLayer from "./utilities/dismissable-layer/index.js";
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export class DismissableLayerState {
touchend: false,
click: false,
};
#isPointerDown = false;
#isPointerDownInside = false;
#isPointerDownOutside = false;
#isResponsibleLayer = false;
node: Box<HTMLElement | null>;
#documentObj: Document;
Expand All @@ -57,13 +56,15 @@ export class DismissableLayerState {
this.#interactOutsideStartProp = props.onInteractOutsideStart;
this.#interactOutsideProp = props.onInteractOutside;

layers.set(this, this.#behaviorType);
const unsubEvents = this.#addEventListeners();

onDestroy(() => {
unsubEvents();
this.#resetState.destroy();
this.#onInteractOutsideStart.destroy();
this.#onInteractOutside.destroy();
layers.delete(this);
});
}

Expand Down Expand Up @@ -113,7 +114,7 @@ export class DismissableLayerState {
*/
addEventListener(
this.#documentObj,
interactOutsideStartEvents,
interactOutsideEndEvents,
composeHandlers(this.#markNonInterceptedEvent, this.#onInteractOutside)
)
);
Expand All @@ -125,8 +126,7 @@ export class DismissableLayerState {
return;
this.#interactOutsideStartProp.value(e);
if (e.defaultPrevented) return;
this.#isPointerDownInside = true;
this.#isPointerDown = true;
this.#isPointerDownOutside = true;
}, 10);

#onInteractOutside = debounce((e: InteractOutsideEvent) => {
Expand All @@ -135,7 +135,7 @@ export class DismissableLayerState {
if (!this.#isResponsibleLayer || this.#isAnyEventIntercepted() || !isValidEvent(e, node))
return;
if (behaviorType !== "close" && behaviorType !== "defer-otherwise-close") return;
if (!this.#isPointerDown || this.#isPointerDownInside) return;
if (!this.#isPointerDownOutside) return;
this.#interactOutsideProp.value(e);
}, 10);

Expand All @@ -156,8 +156,7 @@ export class DismissableLayerState {
for (const eventType in this.#interceptedEvents) {
this.#interceptedEvents[eventType as InteractOutsideInterceptEventType] = false;
}
this.#isPointerDown = false;
this.#isPointerDownInside = false;
this.#isPointerDownOutside = false;
this.#isResponsibleLayer = false;
}, 20);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as DismissableLayer } from "./dismissable-layer.svelte";
export { default as Root } from "./dismissable-layer.svelte";

export type { DismissableLayerProps as Props } from "./types.js";
Loading