Skip to content

Commit 534ce30

Browse files
authored
Merge pull request #283 from github/lw/optional-title
Adds flag to optionally hide title attribute
2 parents 4b5442f + 942896a commit 534ce30

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ So, a relative date phrase is used for up to a month and then the actual date is
8080
| `month` | `month` | `'numeric'\|'2-digit'\|'short'\|'long'\|'narrow'\|undefined` | <sup>***</sup> |
8181
| `year` | `year` | `'numeric'\|'2-digit'\|undefined` | <sup>****</sup> |
8282
| `timeZoneName` | `time-zone-name` | `'long'\|'short'\|'shortOffset'\|'longOffset'` `\|'shortGeneric'\|'longGeneric'\|undefined` | `undefined` |
83+
| `noTitle` | `no-title` | `-` | `-` |
8384

8485
<sup>*</sup>: If unspecified, `formatStyle` will return `'narrow'` if `format` is `'elapsed'` or `'micro'`, `'short'` if the format is `'relative'` or `'datetime'`, otherwise it will be `'long'`.
8586

@@ -268,6 +269,10 @@ For dates outside of the specified `threshold`, the formatting of the date can b
268269

269270
Lang is a [built-in global attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). Relative Time will use this to provide an applicable language to the `Intl` APIs. If the individual element does not have a `lang` attribute then it will traverse upwards in the tree to find the closest element that does, or default the lang to `en`.
270271

272+
##### noTitle
273+
274+
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.
275+
271276
## Browser Support
272277

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

examples/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ <h3>Format DateTime</h3>
2121
</relative-time>
2222
</p>
2323

24+
<p>
25+
No title attribute:
26+
<relative-time datetime="1970-01-01T00:00:00.000Z" format="datetime" hour="numeric" minute="2-digit" second="2-digit"
27+
no-title>
28+
Jan 1 1970
29+
</relative-time>
30+
</p>
31+
2432
<p>
2533
Customised options:
2634
<relative-time datetime="1970-01-01T00:00:00.000Z" format="datetime" weekday="narrow" year="2-digit" month="narrow" day="numeric" hour="numeric" minute="2-digit" second="2-digit">

src/relative-time-element.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
107107
'precision',
108108
'format',
109109
'format-style',
110+
'no-title',
110111
'datetime',
111112
'lang',
112113
'title',
@@ -382,6 +383,14 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
382383
this.setAttribute('format-style', value)
383384
}
384385

386+
get noTitle(): boolean {
387+
return this.hasAttribute('no-title')
388+
}
389+
390+
set noTitle(value: boolean | undefined) {
391+
this.toggleAttribute('no-title', value)
392+
}
393+
385394
get datetime() {
386395
return this.getAttribute('datetime') || ''
387396
}
@@ -431,7 +440,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
431440
return
432441
}
433442
const now = Date.now()
434-
if (!this.#customTitle) {
443+
if (!this.#customTitle && !this.noTitle) {
435444
newTitle = this.#getFormattedTitle(date) || ''
436445
if (newTitle) this.setAttribute('title', newTitle)
437446
}

test/relative-time.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ suite('relative-time', function () {
8383
assert.equal(el.getAttribute('title'), text)
8484
})
8585

86+
test('does not set title if no-title attribute is present', async () => {
87+
const el = document.createElement('relative-time')
88+
el.setAttribute('datetime', new Date().toISOString())
89+
el.setAttribute('no-title', '')
90+
await Promise.resolve()
91+
assert.equal(el.getAttribute('title'), null)
92+
})
93+
8694
test('shadowDOM reflects textContent with invalid date', async () => {
8795
const el = document.createElement('relative-time')
8896
el.textContent = 'A date string'

0 commit comments

Comments
 (0)