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

feat: introduce $host rune, deprecate createEventDispatcher #11059

Merged
merged 3 commits into from
Apr 8, 2024
Merged

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Apr 4, 2024

closes #11022

$host() returns undefined on the server, but it's not reflected in the types to not make interacting with it more cumbersome than it needs to be - I think that's an acceptable trade-off.

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

Copy link

changeset-bot bot commented Apr 4, 2024

🦋 Changeset detected

Latest commit: 1456278

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

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

*
* https://svelte-5-preview.vercel.app/docs/runes#$host
*/
declare function $host<El extends HTMLElement = HTMLElement>(): El;
Copy link
Member Author

Choose a reason for hiding this comment

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

takes a generic because people could add their own properties to the element when using the extend feature of the customElement option and then want type-safety using it

<button onclick={() => greet('hello')}>say hello</button>
```

> Only available inside custom element components, and only on the client-side
Copy link
Contributor

Choose a reason for hiding this comment

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

Only available within client-side custom element components.

Copy link
Member

Choose a reason for hiding this comment

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

This raises an interesting question — what even is a server-side custom element component? What is supposed to get rendered? Should we be emitting declarative shadow DOM?

Are there any situations where $host() could be reached in a server environment? Instead of undefined, should it be a function call that throws a useful error (perhaps only in dev?)

Copy link
Member Author

@dummdidumm dummdidumm Apr 4, 2024

Choose a reason for hiding this comment

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

Calling $host() doesn't necessarily mean that you access one of its properties right away. You could save this to a variable. So an error could have false positives.

The question what we should be doing on the server is interesting indeed - I'm not even sure what would happen today if you do that. It's a larger/separate conversation IMO

Copy link
Member

Choose a reason for hiding this comment

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

What about a proxy that threw on property access? That way if someone did something like

const parent = $host().parentNode;

they would get a more useful error than Cannot read properties of undefined (reading 'parentNode') or whatever

Copy link
Member Author

@dummdidumm dummdidumm Apr 6, 2024

Choose a reason for hiding this comment

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

That could have unintended consequences in the case of checking if host is defined when writing code that is eagerly executed on the server

<script>
- import { createEventDispatcher } from 'svelte';
- const dispatch = createEventDispatcher();
+ let { onGreet } = $props();
Copy link
Member

Choose a reason for hiding this comment

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

should we use lowercase names so it feels more consistent with DOM events? or is the inconsistency a feature rather than a bug?

Suggested change
+ let { onGreet } = $props();
+ let { ongreet } = $props();

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd say feature - most people will likely separate the words somehow

Copy link
Member

Choose a reason for hiding this comment

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

You know, thinking about this further, I think the idiomatic thing to do would be to pass in greet as a prop, rather than having a deeply weird distinction between DOM events and component 'events'. That also simplifies the code a fair bit — instead of refactoring the greet function defined here to call onGreet instead of dispatch('greet'), we can just remove it altogether.

The way to think of this is that you're not defining some behaviour that happens in response to a 'greet' event, you're defining what greet means

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't quite follow - are you suggesting to call it let { greet } = $props() (fine with me), or are you suggesting to remove the prop in this specific example (not fine, because this is about showing how to do events in Svelte 5)?

Copy link
Member

Choose a reason for hiding this comment

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

the first

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yeah, I kind of like that idea. Props that begin with on don't have special behavior like attributes that begin with on do, so encouraging people to use that naming is probably confusing - not to mention this annoying question of how to capitalize it.

- dispatch('greet');
+ onGreet?.();
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be greet as well?
I have the feeling that this function either needs to be renamed, or can be removed from the example entirely (as greet seems to be given to the directly now)

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 getting removed as part of this PR. We're no longer suggesting switching to onGreet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, I got confused by the double diff syntax 😅

@dummdidumm dummdidumm merged commit 22494be into main Apr 8, 2024
8 checks passed
@dummdidumm dummdidumm deleted the host-rune branch April 8, 2024 15:57
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.

Svelte 5: $host() rune for custom elements
5 participants