Skip to content
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

Preact support #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/number-flow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,22 @@ let styleSheet: CSSStyleSheet | undefined
// doesn't include things like attribute support:
export class NumberFlowLite extends ServerSafeHTMLElement {
static define() {
if (BROWSER) customElements.define('number-flow', this)
if (BROWSER) {
const registeredElement = customElements.get('number-flow')

// If an element is already registered with the same name by another library, warn the user.
if (registeredElement && registeredElement.constructor !== this.constructor) {
console.error(
'An element has already been defined under the name `number-flow`. NumberFlow is not compatible in this case.'
)
return
}

// If the element is not registered, register it.
if (!registeredElement) {
customElements.define('number-flow', this)
}
}
}

transformTiming = defaultTransformTiming
Expand Down Expand Up @@ -91,7 +106,9 @@ export class NumberFlowLite extends ServerSafeHTMLElement {
this.#parts = parts

// Don't check for declarative shadow DOM because we'll recreate it anyway:
this.attachShadow({ mode: 'open' })
if (!this.shadowRoot) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this related to HMR as well? I wonder how a shadow root is already attached 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that was the only fix required for HMR to work without issue. It was in the case that a component is mounted, HMR reloads with that component no longer mounted, then HMR reloads a 2nd time with it mounted once again. Reproduction details are in #38

this.attachShadow({ mode: 'open' })
}

// Add stylesheet
if (typeof CSSStyleSheet !== 'undefined' && this.shadowRoot!.adoptedStyleSheets) {
Expand Down