Skip to content

Commit 957d553

Browse files
authored
Merge pull request #341 from silverwind/part
feat: expose `part=root` for external CSS targeting
2 parents 16d01f4 + 9b987de commit 957d553

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ Lang is a [built-in global attribute](https://developer.mozilla.org/en-US/docs/W
284284

285285
Adding the `no-title` attribute will remove the `title` attribute from the `<relative-time>` element. The `title` attribute is inaccessible to screen reader and keyboard users, so not adding a title attribute allows a user to create a custom, accessible tooltip if one is desired.
286286

287+
## Styling
288+
289+
The element renders its text inside a Shadow DOM `<span>` with a [`part="root"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/part) attribute. This allows you to style the element inside the Shadow DOM from outside using the `::part()` CSS pseudo-element:
290+
291+
```css
292+
relative-time::part(root) {
293+
color: rebeccapurple;
294+
font-weight: bold;
295+
}
296+
relative-time::part(root)::selection {
297+
background: lightgreen;
298+
}
299+
```
300+
287301
## Browser Support
288302

289303
Browsers without native [custom element support][support] require a [polyfill][ce-polyfill].

src/relative-time-element.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,13 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
272272
}
273273

274274
#updateRenderRootContent(content: string | null): void {
275+
const span = document.createElement('span')
276+
span.setAttribute('part', 'root')
275277
if (this.hasAttribute('aria-hidden') && this.getAttribute('aria-hidden') === 'true') {
276-
const span = document.createElement('span')
277278
span.setAttribute('aria-hidden', 'true')
278-
span.textContent = content
279-
;(this.#renderRoot as Element).replaceChildren(span)
280-
} else {
281-
this.#renderRoot.textContent = content
282279
}
280+
span.textContent = content
281+
;(this.#renderRoot as Element).replaceChildren(span)
283282
}
284283

285284
#shouldDisplayUserPreferredAbsoluteTime(format: ResolvedFormat): boolean {

test/relative-time.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,6 @@ suite('relative-time', function () {
21222122
await Promise.resolve()
21232123

21242124
assert.isNull(time.shadowRoot.querySelector('[aria-hidden]'), 'Expected no aria-hidden to be present')
2125-
assert.isNull(time.shadowRoot.querySelector('span'), 'Expected no span to be present')
21262125
})
21272126

21282127
test('no aria-hidden applies to shadow root', async () => {
@@ -2132,7 +2131,30 @@ suite('relative-time', function () {
21322131
await Promise.resolve()
21332132

21342133
assert.isNull(time.shadowRoot.querySelector('[aria-hidden]'), 'Expected no aria-hidden to be present')
2135-
assert.isNull(time.shadowRoot.querySelector('span'), 'Expected no span to be present')
2134+
})
2135+
})
2136+
2137+
suite('[part]', () => {
2138+
test('shadow root span has part="root"', async () => {
2139+
const now = new Date().toISOString()
2140+
const time = document.createElement('relative-time')
2141+
time.setAttribute('datetime', now)
2142+
await Promise.resolve()
2143+
2144+
const span = time.shadowRoot.querySelector('span')
2145+
assert.equal(span.getAttribute('part'), 'root')
2146+
})
2147+
2148+
test('shadow root span has part="root" alongside aria-hidden="true"', async () => {
2149+
const now = new Date().toISOString()
2150+
const time = document.createElement('relative-time')
2151+
time.setAttribute('datetime', now)
2152+
time.setAttribute('aria-hidden', 'true')
2153+
await Promise.resolve()
2154+
2155+
const span = time.shadowRoot.querySelector('span')
2156+
assert.equal(span.getAttribute('part'), 'root')
2157+
assert.equal(span.getAttribute('aria-hidden'), 'true')
21362158
})
21372159
})
21382160

0 commit comments

Comments
 (0)