Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 192
Experimental feature: Support absolute time format#327
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c524e4f87492d8a7322b3edb5667efe321b0a7b9bcf09d922856c888File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -217,6 +217,18 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor | ||||||||||
| return `${this.prefix} ${formatter.format(date)}`.trim() | ||||||||||
| } | ||||||||||
| #getUserPreferredAbsoluteTimeFormat(date: Date): string { | ||||||||||
| return new Intl.DateTimeFormat(this.#lang, { | ||||||||||
| day: 'numeric', | ||||||||||
| month: 'short', | ||||||||||
| year: 'numeric', | ||||||||||
| hour: 'numeric', | ||||||||||
| minute: '2-digit', | ||||||||||
| timeZoneName: 'short', | ||||||||||
| timeZone: this.timeZone, | ||||||||||
| }).format(date) | ||||||||||
| } | ||||||||||
| #updateRenderRootContent(content: string | null): void { | ||||||||||
| if (this.hasAttribute('aria-hidden') && this.getAttribute('aria-hidden') === 'true') { | ||||||||||
| const span = document.createElement('span') | ||||||||||
| @@ -228,6 +240,16 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor | ||||||||||
| } | ||||||||||
| } | ||||||||||
| #shouldDisplayUserPreferredAbsoluteTime(format: ResolvedFormat): boolean { | ||||||||||
| // Never override duration format with absolute format. | ||||||||||
| if (format === 'duration') return false | ||||||||||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think... this makes sense? @zaataylor check me on this :D Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense! 🫡 This will also work for relative-time-element/src/relative-time-element.ts Lines 150 to 151 in a917f04
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and for relative-time-element/src/relative-time-element.ts Lines 152 to 153 in a917f04
| ||||||||||
| return ( | ||||||||||
| this.ownerDocument.documentElement.getAttribute('data-prefers-absolute-time') === 'true' || | ||||||||||
| this.ownerDocument.body?.getAttribute('data-prefers-absolute-time') === 'true' | ||||||||||
Comment on lines
+248
to
+249
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding support for | ||||||||||
| ) | ||||||||||
| } | ||||||||||
khiga8 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||||||||||
| #onRelativeTimeUpdated: ((event: RelativeTimeUpdatedEvent) => void) | null = null | ||||||||||
| get onRelativeTimeUpdated() { | ||||||||||
| return this.#onRelativeTimeUpdated | ||||||||||
| @@ -477,12 +499,19 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor | ||||||||||
| const duration = elapsedTime(date, this.precision, now) | ||||||||||
| const format = this.#resolveFormat(duration) | ||||||||||
| let newText = oldText | ||||||||||
| if (format === 'duration') { | ||||||||||
| newText = this.#getDurationFormat(duration) | ||||||||||
| } else if (format === 'relative') { | ||||||||||
| newText = this.#getRelativeFormat(duration) | ||||||||||
| // Experimental: Enable absolute time if users prefers it, but never for `duration` format | ||||||||||
| const displayUserPreferredAbsoluteTime = this.#shouldDisplayUserPreferredAbsoluteTime(format) | ||||||||||
| if (displayUserPreferredAbsoluteTime) { | ||||||||||
| newText = this.#getUserPreferredAbsoluteTimeFormat(date) | ||||||||||
| } else { | ||||||||||
| newText = this.#getDateTimeFormat(date) | ||||||||||
| if (format === 'duration') { | ||||||||||
| newText = this.#getDurationFormat(duration) | ||||||||||
| } else if (format === 'relative') { | ||||||||||
| newText = this.#getRelativeFormat(duration) | ||||||||||
| } else { | ||||||||||
| newText = this.#getDateTimeFormat(date) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| if (newText) { | ||||||||||
| @@ -496,7 +525,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor | ||||||||||
| this.dispatchEvent(new RelativeTimeUpdatedEvent(oldText, newText, oldTitle, newTitle)) | ||||||||||
| } | ||||||||||
| if (format === 'relative' || format === 'duration') { | ||||||||||
| if ((format === 'relative' || format === 'duration') && !displayUserPreferredAbsoluteTime) { | ||||||||||
| dateObserver.observe(this) | ||||||||||
| } else { | ||||||||||
| dateObserver.unobserve(this) | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, matching the
titleformat.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For other potential reviewers, here's an example of what this looks like:
Oct 23, 2025 6:59 AM EDT