-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to extend custom element class (#8991)
This should help everyone who has special needs and use cases around custom elements. Since Svelte components are wrapped and only run on connectedCallback, it makes sense to expose the custom element class for modification before that. - fixes #8954 / closes #8955 - use extend to attach the function manually and save possible values to a prop - closes #8473 / closes #4168 - use extend to set the proper static attribute and then call attachInternals in the constructor - closes #8472 - use extend to attach anything custom you need - closes #3091 - pass `this` to a prop of your choice and use it inside your component - add some doc for #8987
- Loading branch information
1 parent
4bbb545
commit 657f113
Showing
8 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': minor | ||
--- | ||
|
||
feat: add ability to extend custom element class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/svelte/test/runtime-browser/custom-elements-samples/custom-class/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<svelte:options | ||
customElement={{ | ||
tag: 'custom-element', | ||
extend: (CeClass) => { | ||
return class extends CeClass { | ||
updateFoo(value) { | ||
this.foo = value; | ||
} | ||
}; | ||
} | ||
}} | ||
/> | ||
|
||
<script> | ||
export function updateFoo(value) { | ||
foo = value; | ||
} | ||
export let foo; | ||
</script> | ||
|
||
<p>{foo}</p> |
14 changes: 14 additions & 0 deletions
14
packages/svelte/test/runtime-browser/custom-elements-samples/custom-class/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as assert from 'assert.js'; | ||
import { tick } from 'svelte'; | ||
import './main.svelte'; | ||
|
||
export default async function (target) { | ||
const element = document.createElement('custom-element'); | ||
element.updateFoo('42'); | ||
target.appendChild(element); | ||
await tick(); | ||
|
||
const el = target.querySelector('custom-element'); | ||
const p = el.shadowRoot.querySelector('p'); | ||
assert.equal(p.textContent, '42'); | ||
} |