Skip to content

Commit 11b35f5

Browse files
authored
Merge pull request #209 from github/move-customelements-define-calls-to-index-js-and-refactor
move customElements define calls to index.js and refactor
2 parents 265a9db + a885458 commit 11b35f5

File tree

11 files changed

+50
-73
lines changed

11 files changed

+50
-73
lines changed

src/index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,48 @@ import RelativeTimeElement from './relative-time-element.js'
33
import TimeAgoElement from './time-ago-element.js'
44
import TimeUntilElement from './time-until-element.js'
55

6+
const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window
7+
try {
8+
customElements.define('relative-time', RelativeTimeElement)
9+
root.RelativeTimeElement = RelativeTimeElement
10+
} catch (e: unknown) {
11+
if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e
12+
}
13+
14+
try {
15+
customElements.define('local-time', LocalTimeElement)
16+
root.LocalTimeElement = LocalTimeElement
17+
} catch (e: unknown) {
18+
if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e
19+
}
20+
21+
try {
22+
customElements.define('time-ago', TimeAgoElement)
23+
root.TimeAgoElement = TimeAgoElement
24+
} catch (e: unknown) {
25+
if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e
26+
}
27+
28+
try {
29+
customElements.define('time-until', TimeUntilElement)
30+
root.TimeUntilElement = TimeUntilElement
31+
} catch (e: unknown) {
32+
if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e
33+
}
34+
35+
declare global {
36+
interface Window {
37+
RelativeTimeElement: typeof RelativeTimeElement
38+
LocalTimeElement: typeof LocalTimeElement
39+
TimeAgoElement: typeof TimeAgoElement
40+
TimeUntilElement: typeof TimeUntilElement
41+
}
42+
interface HTMLElementTagNameMap {
43+
'relative-time': RelativeTimeElement
44+
'local-time': LocalTimeElement
45+
'time-ago': TimeAgoElement
46+
'time-until': TimeUntilElement
47+
}
48+
}
49+
650
export {LocalTimeElement, RelativeTimeElement, TimeAgoElement, TimeUntilElement}

src/local-time-element.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,3 @@ export default class LocalTimeElement extends RelativeTimeElement {
2828
if (year === 'numeric' || year === '2-digit') return year
2929
}
3030
}
31-
32-
// Public: LocalTimeElement constructor.
33-
//
34-
// var time = new LocalTimeElement()
35-
// # => <local-time></local-time>
36-
//
37-
if (!window.customElements.get('local-time')) {
38-
window.LocalTimeElement = LocalTimeElement
39-
window.customElements.define('local-time', LocalTimeElement)
40-
}
41-
42-
declare global {
43-
interface Window {
44-
LocalTimeElement: typeof LocalTimeElement
45-
}
46-
interface HTMLElementTagNameMap {
47-
'local-time': LocalTimeElement
48-
}
49-
}

src/relative-time-element.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,3 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
374374
}
375375
}
376376
}
377-
378-
// Public: RelativeTimeElement constructor.
379-
//
380-
// var time = new RelativeTimeElement()
381-
// # => <relative-time></relative-time>
382-
//
383-
if (!window.customElements.get('relative-time')) {
384-
window.RelativeTimeElement = RelativeTimeElement
385-
window.customElements.define('relative-time', RelativeTimeElement)
386-
}
387-
388-
declare global {
389-
interface Window {
390-
RelativeTimeElement: typeof RelativeTimeElement
391-
}
392-
interface HTMLElementTagNameMap {
393-
'relative-time': RelativeTimeElement
394-
}
395-
}

src/time-ago-element.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,3 @@ export default class TimeAgoElement extends RelativeTimeElement {
66
return 'past'
77
}
88
}
9-
10-
if (!window.customElements.get('time-ago')) {
11-
window.TimeAgoElement = TimeAgoElement
12-
window.customElements.define('time-ago', TimeAgoElement)
13-
}
14-
15-
declare global {
16-
interface Window {
17-
TimeAgoElement: typeof TimeAgoElement
18-
}
19-
interface HTMLElementTagNameMap {
20-
'time-ago': TimeAgoElement
21-
}
22-
}

src/time-until-element.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,3 @@ export default class TimeUntilElement extends RelativeTimeElement {
66
return 'future'
77
}
88
}
9-
10-
if (!window.customElements.get('time-until')) {
11-
window.TimeUntilElement = TimeUntilElement
12-
window.customElements.define('time-until', TimeUntilElement)
13-
}
14-
15-
declare global {
16-
interface Window {
17-
TimeUntilElement: typeof TimeUntilElement
18-
}
19-
interface HTMLElementTagNameMap {
20-
'time-until': TimeUntilElement
21-
}
22-
}

test/constructor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {assert} from '@open-wc/testing'
2-
import '../src/local-time-element.ts'
3-
import '../src/relative-time-element.ts'
2+
import '../src/index.ts'
43

54
suite('constructor', function () {
65
test('create local-time from document.createElement', function () {

test/local-time.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {assert} from '@open-wc/testing'
2-
import '../src/local-time-element.ts'
2+
import '../src/index.ts'
33

44
suite('local-time', function () {
55
let fixture

test/relative-time.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {assert} from '@open-wc/testing'
2-
import RelativeTimeElement from '../src/relative-time-element.ts'
2+
import {RelativeTimeElement} from '../src/index.ts'
33

44
suite('relative-time', function () {
55
let dateNow

test/time-ago.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {assert} from '@open-wc/testing'
2-
import '../src/time-ago-element.ts'
2+
import '../src/index.ts'
33

44
suite('time-ago', function () {
55
let dateNow

test/time-until.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {assert} from '@open-wc/testing'
2-
import '../src/time-until-element.ts'
2+
import '../src/index.ts'
33

44
suite('time-until', function () {
55
test('always uses relative dates', function () {

0 commit comments

Comments
 (0)