Skip to content

Commit

Permalink
feat: stopPropagation on click and pointerdown
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVidra committed Oct 30, 2023
1 parent 954487f commit 72d4dd1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rbnd/flows",
"version": "0.0.13",
"version": "0.0.14",
"description": "A better way to onboard users and drive product adoption.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ let handlers: Handler[] = [];

export const addHandlers = (handlersToAdd: Handler[]): void => {
handlers.forEach(({ type, handler }) => {
document.removeEventListener(type, handler);
document.removeEventListener(type, handler, true);
});
handlersToAdd.forEach(({ type, handler }) => {
document.addEventListener(type, handler);
document.addEventListener(type, handler, true);
});
handlers = handlersToAdd;
};
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export const init = (options: FlowsOptions): void => {
const eventTarget = event.target;
if (!eventTarget || !(eventTarget instanceof Element)) return;

if (
Array.from(document.querySelectorAll(".flows-root")).some((root) =>
root.contains(eventTarget),
)
) {
event.stopPropagation();
}

Object.values(context.flowsById ?? {}).forEach((flow) => {
if (!flow.element) return;
if (eventTarget.matches(flow.element)) startFlow(flow.id);
Expand Down Expand Up @@ -153,6 +161,18 @@ export const init = (options: FlowsOptions): void => {
}
});
};
const handlePointerDown = (event: PointerEvent): void => {
const eventTarget = event.target;
if (!eventTarget || !(eventTarget instanceof Element)) return;

if (
Array.from(document.querySelectorAll(".flows-root")).some((root) =>
root.contains(eventTarget),
)
) {
event.stopPropagation();
}
};

observer?.disconnect();
observer = new MutationObserver(() => {
Expand All @@ -169,6 +189,7 @@ export const init = (options: FlowsOptions): void => {
{ type: "click", handler: handleClick },
{ type: "submit", handler: handleSubmit },
{ type: "change", handler: handleChange },
{ type: "pointerdown", handler: handlePointerDown },
]);
};

Expand Down
2 changes: 2 additions & 0 deletions src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export const render = (state: FlowState): void => {

if ("wait" in step) return;
const root = document.createElement("div");
root.className = "flows-root";
root.style.pointerEvents = "auto";
document.body.appendChild(root);
if (isTooltipStep(step)) {
const target = document.querySelector(step.element);
Expand Down

0 comments on commit 72d4dd1

Please sign in to comment.