-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathlocation-element.ts
More file actions
100 lines (82 loc) · 2.49 KB
/
location-element.ts
File metadata and controls
100 lines (82 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import RelativeTime from '@yaireo/relative-time'
import { getLocation } from '../api/get-location'
import type { UseLocation } from '../types/types'
function createFlag(countryCode: string, countryName: string): string {
if (!countryName || !countryCode) return ''
const offset = 127397
const emoji = countryCode.replace(/./g, (char) =>
String.fromCodePoint(char.charCodeAt(0) + offset)
)
return `<span role="img" aria-label="${countryName}" class="flag">${emoji}</span>`
}
export class LocationElement extends HTMLElement {
private location: UseLocation | null = null
private relativeTime = new RelativeTime({ locale: 'en' })
private timeUpdateInterval: number | null = null
connectedCallback() {
this.render()
this.loadLocation()
}
disconnectedCallback() {
if (this.timeUpdateInterval) {
clearInterval(this.timeUpdateInterval)
}
}
private async loadLocation() {
this.location = await getLocation()
this.render()
this.startTimeUpdates()
}
private startTimeUpdates() {
if (!this.location?.now?.city) return
if (this.timeUpdateInterval) {
clearInterval(this.timeUpdateInterval)
}
}
private render() {
this.setAttribute('aria-label', 'Location')
if (!this.location) {
this.innerHTML = ''
return
}
let html = ''
// Current location
if (this.location.now?.city) {
const nowFlag = createFlag(
this.location.now.countryCode,
this.location.now.country
)
html += `
${nowFlag}
${this.location.now.city} <span>Now</span>
`
}
// Next location
if (this.location.next?.city) {
const isDifferentCountry =
this.location.now?.country !== this.location.next.country
const nextFlag = isDifferentCountry
? createFlag(this.location.next.countryCode, this.location.next.country)
: ''
const relativeTimeText = this.relativeTime.from(
new Date(this.location.next.startDate)
)
html += `
<div class="next">
${nextFlag}
${this.location.next.city}
<span>${relativeTimeText}</span>
</div>
`
}
this.innerHTML = html
this.classList.add('animation', 'animation--fade-in')
// Set animation delay based on the number of elements before
// (networks + availability)
this.style.setProperty(
'animation-delay',
'calc(var(--animation-delay-step) * 6)'
)
}
}
customElements.define('location-element', LocationElement)