Skip to content

Commit b2314e7

Browse files
Merge pull request #119 from commercelayer/fix-minicart
Add connected and disconnected callbacks to better manage minicart lifecycle
2 parents d5bd603 + 4e09b9e commit b2314e7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

packages/drop-in/src/components/cl-cart-link/cl-cart-link.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,23 @@ export class ClCartLink {
2727
@State() minicart: HTMLClCartElement | null = null
2828
@State() href: string | undefined
2929

30-
componentWillLoad(): void {
31-
this.host.setAttribute("cl-hydrated", "")
32-
this.minicart = this.host.querySelector("cl-cart")
30+
connectedCallback(): void {
31+
this.minicart ??= this.host.querySelector("cl-cart")
3332

3433
if (this.minicart !== null) {
35-
document.body.appendChild(this.minicart)
3634
this.minicart.type = "mini"
35+
document.body.appendChild(this.minicart)
36+
}
37+
}
38+
39+
disconnectedCallback(): void {
40+
if (this.minicart !== null) {
41+
this.host.appendChild(this.minicart)
3742
}
43+
}
44+
45+
componentWillLoad(): void {
46+
this.host.setAttribute("cl-hydrated", "")
3847

3948
listenTo("cl-cart-update", async () => {
4049
if (this.href === undefined || !(await isValidUrl(this.href))) {

packages/drop-in/src/components/cl-cart/cl-cart.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ export class ClCart {
189189
}
190190
}
191191

192+
connectedCallback(): void {
193+
if (this.type === "mini") {
194+
document.body.classList.toggle(this.openDirective, this.open)
195+
}
196+
}
197+
198+
disconnectedCallback(): void {
199+
if (this.type === "mini") {
200+
document.body.classList.toggle(this.openDirective, false)
201+
}
202+
}
203+
192204
@Watch("open")
193205
watchOpenHandler(opened: boolean): void {
194206
if (this.type === "mini") {

0 commit comments

Comments
 (0)