-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat: add svelte/events package and export on function #11912
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
Changes from 7 commits
b51fcdd
f170e00
d45bc77
0a05476
fab3739
0a4ad0c
bf422e4
1009d93
1c3637d
022be97
f878745
1c5fbfd
5da0081
baafaff
38dbd78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "svelte": patch | ||
| --- | ||
|
|
||
| feat: add svelte/events package and export attach function |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { attach } from '../internal/client/dom/elements/events'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,24 @@ export function create_event(event_name, dom, handler, options) { | |
| return target_handler; | ||
| } | ||
|
|
||
| /** | ||
| * Attaches a DOM event handler to an element and returns a function that detaches the event. The event handler | ||
| * will be processed through Svelte's internal event delegation system and is the preferred way to imperatively | ||
| * attach event handlers instead of using `addEventListener`. | ||
| * | ||
| * @param {Element} dom | ||
| * @param {string} event_name | ||
| * @param {EventListener} handler | ||
| * @param {AddEventListenerOptions} [options] | ||
| */ | ||
| export function attach(dom, event_name, handler, options = {}) { | ||
| var target_handler = create_event(event_name, dom, handler, options); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses the target function which ensures any delegated events run as expected, whilst still attaching the event to the target manually.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. The documentation is a bit misleading in that case, will rewrite it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. |
||
|
|
||
| return () => { | ||
| dom.removeEventListener(event_name, target_handler, options); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} event_name | ||
| * @param {Element} dom | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { flushSync } from 'svelte'; | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| mode: ['client'], | ||
|
|
||
| test({ assert, target, logs }) { | ||
| const [b1] = target.querySelectorAll('button'); | ||
|
|
||
| b1?.click(); | ||
| b1?.click(); | ||
| b1?.click(); | ||
| flushSync(); | ||
| assert.htmlEqual(target.innerHTML, '<section><button>clicks: 3</button></section>'); | ||
| assert.deepEqual(logs, []); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <script> | ||
| import {attach} from 'svelte/events'; | ||
|
|
||
| let count = $state(0); | ||
|
|
||
| function increment(e) { | ||
| e.stopPropagation(); | ||
| count += 1; | ||
| } | ||
|
|
||
| let sectionEl | ||
| $effect(() => { | ||
| return attach(sectionEl, 'click', () => { | ||
| console.log('logged from addEventListener'); | ||
| }); | ||
| }); | ||
| </script> | ||
|
|
||
| <section bind:this={sectionEl} onclick={() => console.log('logged from onclick')}> | ||
| <button onclick={increment}> | ||
| clicks: {count} | ||
| </button> | ||
| </section> |
Uh oh!
There was an error while loading. Please reload this page.