From a5ae7c58bab4389a9b1ec13be03fcb43aeefeb10 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Fri, 13 Sep 2024 12:56:13 -0400 Subject: [PATCH] fix morphing --- src/internal/webawesome-element.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/internal/webawesome-element.ts b/src/internal/webawesome-element.ts index 7bbaf4773..4ef77ee8b 100644 --- a/src/internal/webawesome-element.ts +++ b/src/internal/webawesome-element.ts @@ -10,7 +10,7 @@ export default class WebAwesomeElement extends LitElement { @property({ type: Boolean, reflect: true, attribute: 'did-ssr' }) didSSR = isServer || Boolean(this.shadowRoot); - #hasRecordedInitialProperties = true + #hasRecordedInitialProperties = false // Store the constructor value of all `static properties = {}` initialReflectedProperties: Map = new Map(); @@ -30,6 +30,20 @@ export default class WebAwesomeElement extends LitElement { super.attributeChangedCallback(name, oldValue, newValue) } + protected willUpdate(changedProperties: Parameters[0]): void { + super.willUpdate(changedProperties) + + // Run the morph fixing *after* willUpdate. + this.initialReflectedProperties.forEach((value, prop: string & keyof typeof this) => { + // If a prop changes to `null`, we assume this happens via an attribute changing to `null`. + // eslint-disable-next-line + if (changedProperties.has(prop) && this[prop] == null) { + // Silly type gymnastics to appease the compiler. + (this as Record)[prop] = value; + } + }); + } + protected firstUpdated(changedProperties: Parameters[0]): void { super.firstUpdated(changedProperties);