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

Svelte 5: Custom Element prop to attribute mapping inconsistent #13785

Closed
oladayo21 opened this issue Oct 22, 2024 · 0 comments · Fixed by #14084
Closed

Svelte 5: Custom Element prop to attribute mapping inconsistent #13785

oladayo21 opened this issue Oct 22, 2024 · 0 comments · Fixed by #14084

Comments

@oladayo21
Copy link

oladayo21 commented Oct 22, 2024

Describe the bug

For custom elements where the props and attributes are defined with <svelte:options/>, there appears to be an inconsistency in how Svelte handles props-to-attributes mapping depending on how the props are accessed.

<svelte:options customElement={{
	tag:"ps-greet",
	props: {
		message: {attribute:"data-external-props-message"}
	}	
}} />

When accessing props directly:

let props = $props();
props.message; // undefined
props["data-external-props-message"]; // works, but requires using attribute name

When using destructuring:

let { message } = $props(); // works as expected

Correct me if I am wrong but I think props should be consistently accessible regardless of the method used to reference them, either through direct reference or destructuring.

Upon initial investigation, the issue seems to originate from here where the prop definition of the custom element is constructed based on the explicitly defined props of the component (see here). This worked in Svelte 4 because the props are always defined with export let message, however in svelte 5 where it is possible to define props like let props = $props(), properties will always be an empty array in this case.

Reproduction

REPL

Logs

No response

System Info

System:
    OS: macOS 14.6.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 58.38 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.13.1 - ~/.local/state/fnm_multishells/69839_1729547059074/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.5.2 - ~/.local/state/fnm_multishells/69839_1729547059074/bin/npm
    pnpm: 9.11.0 - ~/.local/share/pnpm/pnpm
    bun: 1.1.29 - /opt/homebrew/bin/bun
  Browsers:
    Edge: 130.0.2849.46
    Safari: 18.0
  npmPackages:
    svelte: ^5.0.0 => 5.0.4

Severity

annoyance

dummdidumm added a commit that referenced this issue Oct 31, 2024
We didn't account for the `$props` rune being writtin in a way that makes some props unknown, and they would only be visible through the `customElement.props` definition. This changes the iteration to account for that and also adds a note to the documentation that you need to list out the properties explicitly.

fixes #13785
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 a pull request may close this issue.

1 participant