Skip to content

Commit

Permalink
fix: event prevent default
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Sep 2, 2023
1 parent e88edbc commit 88f2f12
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/compiler/react/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ export const transformJSX = (
jsxPath.replaceWith(t.jsxExpressionContainer(id!));
}, [jsx, t.booleanLiteral(unstable)]);

return dynamics;
continue;
}

if (isJSXFragment(child)) {
Expand Down Expand Up @@ -1069,7 +1069,7 @@ export const transformJSX = (
jsx.children[i] = t.jsxExpressionContainer(id!);
}, [expression, t.booleanLiteral(unstable)]);

return dynamics;
continue;
}

const id = createDynamic(
Expand Down
18 changes: 18 additions & 0 deletions packages/kitchen-sink/src/examples/form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { block } from 'million/react';

const Form = block(() => {
const onSubmit = (event: any) => {
event.preventDefault();
alert('submitted');
};

return (
<div>
<form onSubmit={onSubmit}>
<button type="submit">Submit</button>
</form>
</div>
);
});

export default Form;
26 changes: 15 additions & 11 deletions packages/million/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,27 @@ export const createEventListener = (
name: string,
value?: EventListener,
) => {
const event = name.toLowerCase();
let event = name.toLowerCase();
let capture = false;
if (event.endsWith('capture')) {
event = event.slice(0, -7);
capture = true;
}

const key = `$$${event}`;
if (!SetHas$.call(document$[EVENTS_REGISTRY], event)) {
// createEventListener uses a synthetic event handler to capture events
// https://betterprogramming.pub/whats-the-difference-between-synthetic-react-events-and-javascript-events-ba7dbc742294
addEventListener$.call(document$, event, (nativeEvent: Event) => {
nativeEvent.stopPropagation = () => {
nativeEvent.cancelBubble = true;
nativeEvent.stopImmediatePropagation();
};

requestAnimationFrame(() => {
addEventListener$.call(
document$,
event,
(nativeEvent: Event) => {
let el = nativeEvent.target as Node | null;
// Bubble up the DOM tree to find all event listeners
while (el) {
const handler = el[key];
if (handler) {
Object.defineProperty(nativeEvent, 'currentTarget', {
Object$.defineProperty(nativeEvent, 'currentTarget', {
configurable: true,
get() {
return el;
Expand All @@ -121,8 +124,9 @@ export const createEventListener = (
}
el = el.parentNode;
}
});
});
},
{ capture },
);
SetAdd$.call(document$[EVENTS_REGISTRY], event);
}
const patch = (newValue?: EventListener | null) => {
Expand Down
63 changes: 56 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"classnames": "^2.3.2",
"dedent": "^0.7.0",
"framer-motion": "^10.12.4",
"million": "workspace:2.6.0-beta.8",
"million": "workspace:*",
"next": "^13.4.10",
"nextra": "^2.10.0",
"nextra-theme-docs": "^2.10.0",
Expand Down

2 comments on commit 88f2f12

@vercel
Copy link

@vercel vercel bot commented on 88f2f12 Sep 2, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

sink – ./packages/kitchen-sink

million-kitchen-sink-atit.vercel.app
sink-git-main-millionjs.vercel.app
sink-millionjs.vercel.app
sink.million.dev

@vercel
Copy link

@vercel vercel bot commented on 88f2f12 Sep 2, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

million-kitchen-sink – ./packages/kitchen-sink

million-kitchen-sink-git-main-millionjs.vercel.app
million-kitchen-sink.vercel.app
million-kitchen-sink-millionjs.vercel.app

Please # to comment.