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

docs: clarify on:event usage #10088

Open
wants to merge 2 commits into
base: svelte-4
Choose a base branch
from

Conversation

cloudymeatball
Copy link

@cloudymeatball cloudymeatball commented Jan 4, 2024

Problem 1: The events emitted/forwarded were not listened for.
Fix 1: Changed event names to show consistency amongst examples

Problem 2: Why events need to be forwarded is unclear (on this page). createEventDispatcher docs does mention custom events dont bubble. But components also cannot listen to DOM events that do bubble.
Fix 2: Clarified that components only listen to event from their direct children

Please close #10087 as I tried to merge svelte-4 into main (my bad)

Svelte 5 rewrite

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Problem 1: The events emitted/forwarded were not listened for.
Fix 1: Changed event names to show consistency amongst examples

Problem 2: Why events need to be forwarded is unclear (on this page). `createEventDispatcher` docs does mention custom events dont bubble. But components also cannot listen to DOM events that do bubble. 
Fix 2: Clarified that components only listen to event from their direct children
Copy link

changeset-bot bot commented Jan 4, 2024

⚠️ No Changeset found

Latest commit: ce69130

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

```

> Components can only listen to events forwarded or emitted by their direct child components or elements.
Copy link
Member

Choose a reason for hiding this comment

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

This is not completly accurate, if you have something like

<div on:click={foo}
  <Button />
</div>

and you click on the button then the foo handler would also get notified. I'm not sure right now how to bring this nuance across.

Copy link
Author

Choose a reason for hiding this comment

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

I think in this particular case, it works because DOM events bubble through DOM tree and "bypasses" the <Button> component. So imo this would be an element directive not component directive.

But I noticed this doesn't trigger. https://svelte.dev/repl/404bf36cde154bceb7ac7b0852e89d66?version=4.2.8

<Wrapper on:input={foo}>
  <Input on:input />
</Wrapper>

while if placeholder slot is <Input on:input /> then it works: https://svelte.dev/repl/31fdd56cc6754245a463033f910f14bb?version=4.2.8

Also just double checking when we use the word parent, do we refer to the component/page in which a component is instantiated, or do we simply mean in terms of hierarchy. Like in my REPL, is the parent of <Input> the <App> or <Wrapper>?

@benmccann benmccann changed the title docs: Update 06-component-directives.md on:event clarification docs: clarify on:event usage Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants