Skip to content

Commit e50de7f

Browse files
committed
update render root content
1 parent 6357519 commit e50de7f

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/relative-time-element.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
202202
return `${this.prefix} ${formatter.format(date)}`.trim()
203203
}
204204

205+
#updateRenderRootContent(content: string | null): void {
206+
if (this.hasAttribute('aria-hidden') && this.getAttribute('aria-hidden') === 'true') {
207+
const span = document.createElement('span')
208+
span.setAttribute('aria-hidden', 'true')
209+
span.textContent = content
210+
;(this.#renderRoot as Element).replaceChildren(span)
211+
} else {
212+
this.#renderRoot.textContent = content
213+
}
214+
}
215+
205216
#onRelativeTimeUpdated: ((event: RelativeTimeUpdatedEvent) => void) | null = null
206217
get onRelativeTimeUpdated() {
207218
return this.#onRelativeTimeUpdated
@@ -460,17 +471,10 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
460471
}
461472

462473
if (newText) {
463-
if (this.hasAttribute('aria-hidden') && this.getAttribute('aria-hidden') === 'true') {
464-
const span = document.createElement('span')
465-
span.setAttribute('aria-hidden', 'true')
466-
span.textContent = newText
467-
;(this.#renderRoot as Element).replaceChildren(span)
468-
} else {
469-
this.#renderRoot.textContent = newText
470-
}
474+
this.#updateRenderRootContent(newText)
471475
} else if (this.shadowRoot === this.#renderRoot && this.textContent) {
472476
// Ensure invalid dates fall back to lightDOM text content
473-
this.#renderRoot.textContent = this.textContent
477+
this.#updateRenderRootContent(this.textContent)
474478
}
475479

476480
if (newText !== oldText || newTitle !== oldTitle) {

0 commit comments

Comments
 (0)