Skip to content

Commit 87492d8

Browse files
khiga8zaataylor
andcommitted
Spike on adding support for user preferring absolute time
Co-authored-by: Zachary Taylor <zaataylor@github.com>
1 parent c524e4f commit 87492d8

1 file changed

Lines changed: 41 additions & 5 deletions

File tree

src/relative-time-element.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
122122
'title',
123123
'aria-hidden',
124124
'time-zone',
125+
'enable-user-time-preference'
125126
]
126127
}
127128

@@ -217,6 +218,21 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
217218
return `${this.prefix} ${formatter.format(date)}`.trim()
218219
}
219220

221+
#getUserPreferredFormat(date: Date): string {
222+
const formatter = new Intl.DateTimeFormat(this.#lang, {
223+
second: undefined,
224+
minute: '2-digit',
225+
year: '2-digit',
226+
month: 'numeric',
227+
day: 'numeric',
228+
hour: 'numeric',
229+
weekday: undefined,
230+
timeZoneName: undefined,
231+
timeZone: undefined,
232+
})
233+
return `${formatter.format(date)}`.trim()
234+
}
235+
220236
#updateRenderRootContent(content: string | null): void {
221237
if (this.hasAttribute('aria-hidden') && this.getAttribute('aria-hidden') === 'true') {
222238
const span = document.createElement('span')
@@ -341,6 +357,14 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
341357
this.setAttribute('time-zone-name', value || '')
342358
}
343359

360+
get enableUserTimePreference(): boolean {
361+
return this.hasAttribute('enable-user-time-preference')
362+
}
363+
364+
set enableUserTimePreference(value: boolean | undefined) {
365+
this.toggleAttribute('enable-user-time-preference', value)
366+
}
367+
344368
/** @deprecated */
345369
get prefix(): string {
346370
return this.getAttribute('prefix') ?? (this.format === 'datetime' ? '' : 'on')
@@ -477,12 +501,24 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
477501
const duration = elapsedTime(date, this.precision, now)
478502
const format = this.#resolveFormat(duration)
479503
let newText = oldText
480-
if (format === 'duration') {
481-
newText = this.#getDurationFormat(duration)
482-
} else if (format === 'relative') {
483-
newText = this.#getRelativeFormat(duration)
504+
505+
// Check if user preference is enabled.
506+
const userPreferenceElement = document.querySelector('[data-prefers-absolute-time]')
507+
let userPrefersAbsoluteTime = false
508+
if (userPreferenceElement) {
509+
userPrefersAbsoluteTime = userPreferenceElement.getAttribute('data-prefers-absolute-time') === 'true'
510+
}
511+
if (this.enableUserTimePreference && userPrefersAbsoluteTime) {
512+
// return format
513+
newText = this.#getUserPreferredFormat(date)
484514
} else {
485-
newText = this.#getDateTimeFormat(date)
515+
if (format === 'duration') {
516+
newText = this.#getDurationFormat(duration)
517+
} else if (format === 'relative') {
518+
newText = this.#getRelativeFormat(duration)
519+
} else {
520+
newText = this.#getDateTimeFormat(date)
521+
}
486522
}
487523

488524
if (newText) {

0 commit comments

Comments
 (0)