Replies: 3 comments 2 replies
-
Actually I've found html-parsed-element which addresses this problem (it's a nice abstraction over import HTMLParsedElement from 'html-parsed-element';
import { controller, attr, target } from '@github/catalyst'
@controller
export default class TextEditorElement extends HTMLParsedElement {
@target area: HTMLInputElement
parsedCallback() {
this.area.value = 'Hello World!'
}
} PS. TypeScript might complain about |
Beta Was this translation helpful? Give feedback.
-
PR got merged, and new version has been released so Should I add such info to the guides? |
Beta Was this translation helpful? Give feedback.
-
Thanks for this discussion @lowski!
Totally fine! I have set up discussions for this repo and transferred your issue to Discussions, hopefully we can continue the chat here!
The short and blunt answer to this is, in my opinion, "don't". That's not very helpful though 😅, so I'll try to expand: Generally speaking, I think the whole concept of children traversal during initialisation is misguided. Child elements can be swapped out from underneath a component at any point and so any one time initialisation such as Consider the children of an element to be a piece of state. It is initialized and can be mutated outside of the control of the element, and as such the element should only query for its state at the time of need. If an element has private state to initialize it can do so in import { controller, attr, target } from '@github/catalyst'
@controller
export default class TextEditorElement extends HTMLParsedElement {
@target area: HTMLInputElement
#currentArea: HTMLInputElement
connectedCallback() {
new MutationObserver(() => {
if (this.area !== this.#currentArea) {
// this.area has changed so we should re-initialize it
this.area.value = 'Hello World!'
this.#area = this.area
}
}).observe(this, { subtree: true, childList: true })
}
} This code is a little tricky to properly read through but I think it's falling victim to contrived code. I think it's quite rare to want to instantiate values once upon initialization, perhaps some more common scenarios with solutions:
It is not just parser race conditions that cause const editor = document.createElement('text-editor')
document.body.appendChild(editor)
// ^ connectedCallback has now been fired
editor.appendChild(el) The const editor = document.createElement('text-editor')
document.body.appendChild(editor)
// ^ connectedCallback has now been fired
// parsedCallback should be fired around here
setTimeout(() => editor.appendChild(el), 100) |
Beta Was this translation helpful? Give feedback.
-
Hey!
First of all, sorry for using GitHub issues for asking questions but not sure what's the best place to ask for help. I want to ask what's the best way to access targets during the initialization process (according to docs,
connectedCallback
might be too early to query element from the DOM)? Let's say I'd like to setup / start dropzone on a specific target when component is initialized. When / how should I do it?Beta Was this translation helpful? Give feedback.
All reactions