Skip to content

Reference self + $$self is an illegal variable name #6718

Closed
@ronen-e

Description

@ronen-e

Describe the bug

I wanted to get a reference to the svelte component inside of the component file.

I'm using svelte to render customElement (with "tag" option).

The reason for getting a reference was the need for dispatching a composed event which can be picked up by the host page, beyond the web component's shadow DOM.

bind:this

I tried using bind:this on the top level element but that gives a reference to the specific element, not the root web component.

  • I can use bind:this in a container component like in the example but I would prefer to reference the component from inside the component (file) itself.
/* container.svelte */
import Component from "./component.svelte"
let ref;

<Component bind:this={ref}  />

$$self

I saw a variable named $$self in the scope, which references the webComponent but using it gives

ValidationError $$self is an illegal variable name

  • I can turn the error to a warning using the compiler options of errorMode="warn"
    but I would prefer to keep the error mode as it is.

  • I can also use eval('$$self') which works but is not ideal and gives another compiler warning

  • I'm also concerned with using an undocumented variable - I did not find documentation about $$self. Is it meant for internal use only?

I noticed the illegal variable name issue can be resolved by adding $$self to the list of reserved keywords for the svelte compiler

const reserved_keywords = new Set(['$$props', '$$restProps', '$$slots']);

Related

Reproduction

https://svelte.dev/repl/71cb45d247f94ea5896f7de2303cbe8a?version=3.42.4

Logs

No response

System Info

Not system related

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions