Skip to content

Commit

Permalink
Merge pull request #257 from github/add-define-static-method
Browse files Browse the repository at this point in the history
add define static method
  • Loading branch information
keithamus authored Apr 18, 2023
2 parents c000266 + 8be4134 commit a3f2192
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/relative-time-element-define.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import RelativeTimeElement from './relative-time-element.js'
import {RelativeTimeElement} from './relative-time-element.js'

const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window
try {
customElements.define('relative-time', RelativeTimeElement)
root.RelativeTimeElement = RelativeTimeElement
root.RelativeTimeElement = RelativeTimeElement.define()
} catch (e: unknown) {
if (
!(root.DOMException && e instanceof DOMException && e.name === 'NotSupportedError') &&
Expand Down
12 changes: 9 additions & 3 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Duration, elapsedTime, getRelativeTimeUnit, isDuration, roundToSingleUnit, Unit, unitNames} from './duration.js'
const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window
const HTMLElement = root.HTMLElement || (null as unknown as typeof window['HTMLElement'])
const HTMLElement = globalThis.HTMLElement || (null as unknown as typeof window['HTMLElement'])

export type DeprecatedFormat = 'auto' | 'micro' | 'elapsed'
export type ResolvedFormat = 'duration' | 'relative' | 'datetime'
Expand Down Expand Up @@ -73,7 +72,12 @@ const dateObserver = new (class {
}
})()

export default class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFormatOptions {
export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFormatOptions {
static define(tag = 'relative-time', registry = customElements) {
registry.define(tag, this)
return this
}

#customTitle = false
#updating: false | Promise<void> = false

Expand Down Expand Up @@ -462,3 +466,5 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
this.#updating = false
}
}

export default RelativeTimeElement

0 comments on commit a3f2192

Please sign in to comment.