Comments (3)
Additional info...
This FAILS (meaning, the slots DON'T pass as props and therefore aren't rendered):
<html>
<head><script src="test.js"></script></head>
<body><div id="wrapper">
<text-section>
<span slot="heading">Nice heading</span>
<span slot="content">Great content</span>
</text-section>
</div></body>
</html>
This also FAILS:
<html>
<head><script src="test.js"></script></head>
<body><div id="wrapper">
<script>
document.write(`
<text-section>
<span slot="heading">Nice heading</span>
<span slot="content">Great content</span>
</text-section>
`)
</script>
</div></body>
</html>
However, this PASSES (meaning, the slots DO get passed as props and are rendered):
<html>
<head><script src="test.js"></script></head>
<body>
<div id="wrapper"></div>
<script>
wrapper.innerHTML = `
<text-section>
<span slot="heading">Nice heading</span>
<span slot="content">Great content</span>
</text-section>
`
</script>
</body>
</html>
from preact-custom-element.
I've just run into this as well when I took defer
out of the script
tag in my head
section. To be fair, I don't need it at this point since all my custom elements are there when the page loads, but this is surprising behavior. Aside from slots, everything else seemed to work.
Edit: For completeness, I'm using { shadow: false }
when registering.
Edit2: The issue is the same for { shadow: true }
from preact-custom-element.
Looks like this is a documented issue with Custom Elements: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements
Note that in this case we must ensure that the script defining our custom element is executed after the DOM has been fully parsed, because connectedCallback() is called as soon as the expanding list is added to the DOM, and at that point its children have not been added yet, so the querySelectorAll() calls will not find any items. One way to ensure this is to add the defer attribute to the line that includes the script.
Looks like if the Custom Element is added to the DOM after the first pass then it works fine. The issue is when an element is already registered, then connectedCallback
is triggered immediately when the Custom Element tag is found, but before the children are parsed. This is why setting innerHTML
on an element that was already parsed works, but using document.write
to append a CE before the first DOM pass happens, fails.
Since we should all be using defer
in our script tags to avoid blocking the DOM anyway, this feels like a non-issue to me. Maybe it's worth documenting though? One suggestion I saw was to put a setTimeout(0)
in connectedCallback
to slightly defer until the element's children were parsed, but I'm not sure if that actually works.
from preact-custom-element.
Related Issues (20)
- In Firefox, The Custom Element gets cloned when calling member function of props, like toUpperCase()
- Hydration with shadow root doesn't work HOT 2
- Hyphenated attributes and camelCased properties
- How do I use with unpkg? HOT 1
- Document behavior for multi-word attributes/props?
- I can't use hook HOT 3
- package does not work HOT 1
- Observed attributes break context providers
- Feature Request: Use the "render" function of preact/compat
- Preact Component's constructor is not called when custom element is created via document.createElement HOT 1
- It resets complex property to undefined when provided as second or later prop
- Expose methods HOT 2
- Interop problems with Svelte
- Add support for {mode: 'closed'}
- Pass the created HTML node to the preact component HOT 1
- Generating preact-ce.module.js HOT 1
- Extend element instead of creating new custom element
- [Improvment] Add documentation or template for vite HOT 5
- Publish to npm 🙏 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from preact-custom-element.