Svelte: Allow to specify custom base class for Svelte Components

Created on 26 Dec 2019  路  2Comments  路  Source: sveltejs/svelte

Could we have an option to specify a custom base class for Svelte components?

<script>
import {BaseSvelteComponent} from './base';
</script>

<svelte:options base="BaseSvelteComponent"/>

base.js:

import {SvelteComponent} from 'svelte';

export class BaseSvelteComponent extends SvelteComponent {
 /* or could be completly own custom impl */
}

It would step to make Svelte a more generic compiler and could move many conflict decisions out of the scope of Svelte project.

It allows end-users of the compiler to make own decision. I think it could resolve things like Component inheritance, move towards a solution for Custom element without shadow DOM and Support attachShadow({mode: 'closed'}), handle Native HTML elements and
Form-associated custom elements and minor things like Define some attributes on Custom Elements

And it seems pretty easy to implement - https://github.com/kmmbvnr/svelte/commit/4a7f3171050dea3d676bf928bbfb6644d09e0359

custom element proposal

Most helpful comment

I think commandment about composition vs inheritance more applied to business logic stuff, till there we have mostly technical question.

Svelte may compiles to Web Component, by producing custom subclass of web browser HTMLElement. New Web Component specs includes new form-associated base classes It is the only way to achieve native browser behavior with web component compiled svelte code.

Beyond that, it solves other issues listed by me in the ticket description

Probably, some more compositional approach, would be split svelte and svelte compiler, to allow just compile shadow dom related js code, without other framework-related things

All 2 comments

Why? Inheriting from HTML elements doesn't get us any closer to using vanilla Svelte components inside the shadow dom. Is there any other motivation?

In general, inheritance is bad. We don't want to give people any encouragement to use it. Prefer composition.

I think commandment about composition vs inheritance more applied to business logic stuff, till there we have mostly technical question.

Svelte may compiles to Web Component, by producing custom subclass of web browser HTMLElement. New Web Component specs includes new form-associated base classes It is the only way to achieve native browser behavior with web component compiled svelte code.

Beyond that, it solves other issues listed by me in the ticket description

Probably, some more compositional approach, would be split svelte and svelte compiler, to allow just compile shadow dom related js code, without other framework-related things

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thoughtspile picture thoughtspile  路  3Comments

AntoninBeaufort picture AntoninBeaufort  路  3Comments

st-schneider picture st-schneider  路  3Comments

robnagler picture robnagler  路  3Comments

plumpNation picture plumpNation  路  3Comments