-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: custom elements rework #8457
Merged
dummdidumm
merged 42 commits into
sveltejs:version-4
from
dummdidumm:custom-elements-rework
May 2, 2023
Merged
feat: custom elements rework #8457
dummdidumm
merged 42 commits into
sveltejs:version-4
from
dummdidumm:custom-elements-rework
May 2, 2023
Conversation
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
@dummdidumm is attempting to deploy a commit to the Svelte Team on Vercel. A member of the Team first needs to authorize it. |
tanhauhau
reviewed
Apr 7, 2023
src/compiler/compile/Component.ts
Outdated
prop.key.name === 'reflect' && typeof prop.value.value !== 'boolean' || | ||
prop.key.name === 'attribute' && typeof prop.value.value !== 'string' | ||
) { | ||
return error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe give specific explanation of why it failed, eg
'cePropsDefinition' must be a statically analyzable object literal of the form '{ prop: { attribute?: string; type?: 'String' | 'Boolean' | 'Number' | 'Array' | 'Object', reflect?: boolean; } }', found key 'xxx'
'cePropsDefinition' must be a statically analyzable object literal of the form '{ prop: { attribute?: string; type?: 'String' | 'Boolean' | 'Number' | 'Array' | 'Object', reflect?: boolean; } }', the value of reflect is not boolean
Instead of "both", which doesn't make sense at that point.
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
- upgrade to TypeScript 5 - upgrade @ampproject/remapping - remove obsolete workarounds --------- Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
bump to rollup 3. Includes reworking the "treat those imports as external" a bit so that Rollup builds correctly but doesn't bundle some of the (now relative) imports --------- Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
This was referenced Jul 1, 2023
nosovk
added a commit
to nosovk/open-wc
that referenced
this pull request
Jul 23, 2023
There was always an option to build custom elements with svelte, but recently there was a [huge update](sveltejs/svelte#8457) and now dev experience is much better that was before. Lots of issues were fixed and now svelte is a pretty solid solution to build reliable custom elements. It would be nice to add it to the list of base libraries. For example lots of svelte libraries were complied to custom elements before, (slidy)[https://github.com/Valexr/slidy/tree/main/packages] for example, and now it's even much easier than befor.
1 task
1 task
Merged
1 task
1 task
1 task
1 task
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an overhaul of custom elements in Svelte. Instead of compiling to a custom element class, the Svelte component class is mostly preserved as-is. Instead a wrapper is introduced which wraps a Svelte component constructor and returns a HTML element constructor. This has a couple of advantages:
connectedCallback
and disconnects it indisconnectedCallback
(but only after one tick, because this could be a element move). Mount/destroy works as expected inside, fixes Action lifecycle in custom elements #5989, fixes Custom elements are not "cleaned up" when the consuming application removes an element. #8191addEventListener
calls tocomponent.$on
, which allows to listen to custom events, fixes Events are not emitted from components compiled to a custom element #3119, closes Custom element dispatch event #4142<svelte:options customElement={..}>
can also take an object to modify such aspects. This option allows to specify whether setting a prop should be reflected back to the attribute (defaultfalse
), what to use when converting the property to the attribute value and vice versa (throughtype
, defaultString
, or whenexport let prop = false
thenBoolean
), and what the corresponding attribute for the property is (attribute
, default lowercased prop name). These options are heavily inspired by lit: https://lit.dev/docs/components/properties. Closes feat: hyphenated attributes in custom elements #7638, fixes Two-Way Binding With Native Components Not In Sync With Attribute #5705shadowdom
option to control whether or not encapsulate the custom element. Closes issue #1748 - Custom elements light dom, open / closed #4330, closes Custom element without shadow DOM #1748Breaking changes:
Wrapped Svelte component now stays as a regular Svelte component (invokeing it like before with
new Component({ target: ..})
won't create a custom element). Its custom element constructor is now a static property namedelement
on the class (Component.element
) and should be regularly invoked through setting it in the html.The timing of mount/destroy/update is different. Mount/destroy/updating a prop all happen after a tick, so
shadowRoot.innerHTML
won't immediately reflect the change (Lit does this too). If you rely on it, you need to await a promise for example:const component = someDiv.innerHTML = '<custom-element-tag foo="bar"></custom-element-tag>'; + await Promise.resolve(); someDiv.querySelector('custom-element-tag').shadowRoot.querySelector('div'); // ...
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests
npm test
and lint the project withnpm run lint