-
Notifications
You must be signed in to change notification settings - Fork 299
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
fix(overlays): allow Subclassers to teardown at the right moment #2438
base: master
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: a2d8a73 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Would it be possible to run the init
callback when connected
instead of firstUpdated
? I find such symmetry (setting up when connected and tearing down when disconnected) would be cleaner than conditionally tearing things down.
01fdce3
to
3f4f48d
Compare
Hey, good idea. We can probably go back to connectedCallback that waits until first update is completed. See the updated code and especially the tests. |
f632b1f
to
d4343d6
Compare
What do you mean by the Lion base class: #hasSetup = false;
connectedCallback() {
super.connectedCallback();
this.updateComplete.then(() => {
if (this.#hasSetup) return;
this.setup();
this.#hasSetup = true;
});
this.#hasSetup = true;
}
async disconnectedCallback() {
super.disconnectedCallback();
if (await this._isPermanentlyDisconnected()) {
this.teardown();
this.#hasSetup = false;
}
}
/**
* When we're moving around in dom, disconnectedCallback gets called.
* Before we decide to teardown, let's wait to see if we were not just moving nodes around.
* @return {Promise<boolean>}
*/
async _isPermanentlyDisconnected() {
await this.updateComplete;
return !this.isConnected;
}
/**
* Here we are sure all dom (shadow dom and light dom) has rendered.
* We can add event listeners to internal (and/or external) dom and perform related setup logic.
* Also, we do not unnecessarily delay the first lit paint (which is vital for (perceived) performance),
* that we would have otherwise done when setup logic would have been executed in connectedCallback.
*/
setup() {
}
/**
* Here we are sure that a component is permanently disconnected.
* (instead of being moved around in dom, which synchronously triggers disconnected and connectedCallback).
* When dom nodes are just being moved around, we don't unnecessarily hinder performance (INP).
* We make sure that all event listeners are removed (especially the external ones, as they won't be automatically cleaned up).
*/
teardown() {
} In subclassers, we would just use the setup/teardown callbacks |
d4343d6
to
193c9a0
Compare
With Line 23 in fef94cd
This callback is called in a very complex chain: This new interface looks pretty straight-forward!! Do you also plan to simplify the overlay config? |
I was wrong about another thing during the communication. The bug that the teardown callback was called while the element was still on the memory didn't happen due to tabs, but due to a routing library. I believe the router detached the element but kept the reference to the element object hence the next time it was attached to the document again, the constructor was not called. In this scenario, I still think that it would be much simpler to make sure that what is called during |
Imho after teardown #hasSetup would be false again (see code above) and everything would work. Would you mind to make a failing test case for this @ryubro ? The reason we would need to delay the setup is that LitElement didn't render everything yet at connected. Waiting for the first update after connected makes it on par with firstUpdated (https://lit.dev/docs/components/lifecycle/#firstupdated), but also allowing re-attachment. |
It's a great plan indeed. I made a branch like ~4 years ago, with a more readable and modular (separate files for all sub functionality, allowing to load large functionality with dynamic imports (like we do with Popper right now) and treeshaking). But a new proposal is very welcome :) Just like a plan of how we can leverage popover + anchor positioning in a non breaking way |
Sure! |
99a13b8
to
f4af9e6
Compare
f4af9e6
to
f06d795
Compare
147bf7a
to
0c35130
Compare
What I did