Skip to content

Commit ce767cc

Browse files
committed
Adds flag to optionally hide title attribute
1 parent 4b5442f commit ce767cc

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

examples/index.html

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

24+
<p>
25+
Hide title attribute:
26+
<relative-time datetime="1970-01-01T00:00:00.000Z" format="datetime" hour="numeric" minute="2-digit" second="2-digit" hide-title="true">
27+
Jan 1 1970
28+
</relative-time>
29+
</p>
30+
2431
<p>
2532
Customised options:
2633
<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">
@@ -185,8 +192,8 @@ <h2>Localised Dates</h2>
185192
</relative-time>
186193
</p>
187194

188-
<!-- <script type="module" src="../dist/index.js"></script> -->
189-
<script type="module" src="https://unpkg.com/@github/relative-time-element@latest/dist/bundle.js"></script>
195+
<script type="module" src="../dist/index.js"></script>
196+
<!-- <script type="module" src="https://unpkg.com/@github/relative-time-element@latest/dist/bundle.js"></script> -->
190197
<script>
191198
document.body.addEventListener('relative-time-updated', event => {
192199
console.log('event from', event.target, event)

src/relative-time-element.ts

Lines changed: 6 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+
'hide-title',
110111
'datetime',
111112
'lang',
112113
'title',
@@ -382,6 +383,10 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
382383
this.setAttribute('format-style', value)
383384
}
384385

386+
get hideTitle(): boolean {
387+
return this.getAttribute('hide-title') === 'true'
388+
}
389+
385390
get datetime() {
386391
return this.getAttribute('datetime') || ''
387392
}
@@ -431,7 +436,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
431436
return
432437
}
433438
const now = Date.now()
434-
if (!this.#customTitle) {
439+
if (!this.#customTitle && !this.hideTitle) {
435440
newTitle = this.#getFormattedTitle(date) || ''
436441
if (newTitle) this.setAttribute('title', newTitle)
437442
}

test/relative-time.js

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

86+
test('does not set title if hide-title=true', async () => {
87+
const el = document.createElement('relative-time')
88+
el.setAttribute('datetime', new Date().toISOString())
89+
el.setAttribute('hide-title', true)
90+
await Promise.resolve()
91+
assert.equal(el.getAttribute('title'), null)
92+
})
93+
94+
test('sets title if hide-title=false', async () => {
95+
const el = document.createElement('relative-time')
96+
el.setAttribute('datetime', new Date().toISOString())
97+
el.setAttribute('hide-title', false)
98+
await Promise.resolve()
99+
assert.ok(el.getAttribute('title'))
100+
})
101+
86102
test('shadowDOM reflects textContent with invalid date', async () => {
87103
const el = document.createElement('relative-time')
88104
el.textContent = 'A date string'

0 commit comments

Comments
 (0)