-
-
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
Transitions on WebComponents not working #4735
Comments
Sounds like a duplicate of #1825. |
You are right my bad I serached as Web Component not Custom Element =S |
Thanks for your workaround! |
The ugly workaround can be made a little bit more reusable with this. I lookup the top parent element with while loop as the above fix doesn't always work with nested transitions. // transfix.js
export default function fix(transtion) {
return function(node, params){
if (!node.hasOwnProperty('ownerDocument')) {
Object.defineProperty(node, 'ownerDocument', { get: function() { return node.parentElement; } });
let elem = node
while(elem.parentElement){ elem = elem.parentElement }
node.parentElement.head = elem
}
return transtion(node, params)
}
} Then your transition can be applied with. This should work with any transition. <script>
import { fly } from 'svelte/transition';
import fix from './transfix.js'
</script>
{#if visible}
<p transition:fix(fly)="{{ duration: 3000 }}">
Fly in and out working
</p>
{/if} |
Thanks, this helped! I needed a slightly modified version though: // transfix.js
export default function fix(transtion) {
return function(node, params){
Object.defineProperty(node, 'ownerDocument', { get: function() { return {head: node.parentNode}; } });
return transtion(node, params)
}
} |
Here to get a fix for the following transitions in typescript: fade, scale, blur, fly, slide (I only tested fade...)
|
@yannkost Would it be possible for you (or anyone, really) to quickly explain how to use this "fix"?'' Thanks in advance! |
@OClement So if you want to use it on your button component .. you will have to apply the workaround when you mount the button or use the fixed transitions from the examples .. but you will have to make sure that the "workaround" code return an element that is still inside the web component hope this help |
I created a PR that makes it possible to use svelte inside a shadow dom. All styles and animations will work like in a normal svelte-application. You could install svelte from this branch to use it until it gets merged. |
Great explanation, this clarifies a lot, thanks a bunch! |
@OClement Just so you know how to use the fix, personnally I put the definitions/interfaces in a fix.ts file, import the fix function where needed, then i'm applying the fix when I use a Svelte transition the following way: |
@yannkost your fix works but try using a transition on hover for example on a node that is not a direct child of a |
An alternative method that doesn't require modification of components: interface ExtendedDoc extends Document {
__svelte_stylesheet: StyleShim;
}
class StyleShim {
cssRules: string[] = [];
private _stylesheets: CSSStyleSheet[] = [];
constructor() {
this.register(
document.head.appendChild(document.createElement("style")).sheet
);
}
insertRule(rule: string, index = 0) {
this.cssRules.splice(index, 0, rule);
for (const sheet of this._stylesheets) {
sheet.insertRule(rule, index);
}
}
deleteRule(index: number) {
this.cssRules.splice(index, 1);
for (const sheet of this._stylesheets) {
sheet.deleteRule(index);
}
}
register(sheet: CSSStyleSheet) {
this._stylesheets.push(sheet);
}
unregister(sheet: CSSStyleSheet) {
const i = this._stylesheets.findIndex((s) => s === sheet);
if (i !== -1) this._stylesheets.splice(i, 1);
}
}
const shim = new StyleShim();
(document as ExtendedDoc).__svelte_stylesheet = shim;
export default shim; Then, const styleSheet = element.parentNode.appendChild(
document.createElement("style")
).sheet;
StyleShim.register(styleSheet);
return () => StyleShim.unregister(styleSheet); |
necro post
|
Describe the bug
When you define a transition on an element that is a customElement (web component) the transitions animations dont show since the css for the animation are not applied
Logs
It fails silently
To Reproduce
Check repo but is just the transition example made web component
https://github.com/msaglietto/svelte-transitions-issue
Or just
Expected behavior
Transitions working fine in web compoent with the shadow root restrictions
Information about your Svelte project:
Your browser and the version: Chrome 81.0.4044.103
Your operating system: Chrome OS Version 81.0.4044.10
Svelte version: 3.21
Whether your project uses Webpack or Rollup: Rollup
Severity
It was blocking us from using svelte but we found a work around
Additional context
The issue happen because svelte insert the css in the head of the ownerDocument:
https://github.com/sveltejs/svelte/blob/master/src/runtime/internal/style_manager.ts#L32
In the case of web component the elementes inside of the shadow-root can not read outside of it so the head css dont apply
I think maybe it could be solved if you can configure where svelte insert the css
A quite ugly workaround is to redefine node ownerDocument and its head element on the transition
The text was updated successfully, but these errors were encountered: