Description
This is a proposal to add a $$self
( or else ) reserved keyword that resolves into a reference to a component's own class, such as to make the following output <h1>hello world !</h1>
<script>
export let name;
</script>
{#if !name}
<svelte:component this={$$self} name={'world'} />
{:else}
<h1>hello {name} !</h1>
{/if}
Now that usecase is already covered by svelte:self
, and even then there will always be a way to sneak that reference in to the script block or proxy it through other script modules ( see below ), yet I happen to find myself needing $$self
more than any other supported "escape hatch" $$keyword
, so while the argument for such keyword is definitely not compelling, I do think that it would be a nice to have
Script block workaround at component instantiation
<script>
export let name;
const self = arguments[0].__proto__.constructor;
</script>
{#if !name}
<svelte:component this={self} name={'world'} />
{:else}
<h1>hello {name} !</h1>
{/if}
For typescript users, the only way to use a component in any other way but as inlined in another component's xhtml markup is by exporting it through the script module of a proxy component, as importing from a non .svelte
file is not yet supported by the vscode plugin
<!-- Utils.svelte -->
<script context="module" lang="ts">
import CustomScrollbar from "./CustomScrollbar.svelte";
type SvelteProps<T> = T extends Svelte2TsxComponent<infer U> ? U : never;
export function custom_scrollbar(target, props: SvelteProps<CustomScrollbar>) {
const instance = new CustomScrollbar({ target, props: {...props, parentNode: target} });
return { update: (props) => instance.$set(props), destroy: () => instance.$destroy() };
}
</script>
<!-- MyComponent.svelte -->
<script lang="ts">
import { custom_scrollbar } from "./Utils.svelte";
</script>
<div use:custom_scrollbar={{ /** autocomplete works ! */ }}>
...
</div>