Skip to content
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

Adds flag to optionally hide title attribute #283

Merged
merged 10 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ So, a relative date phrase is used for up to a month and then the actual date is
| `month` | `month` | `'numeric'\|'2-digit'\|'short'\|'long'\|'narrow'\|undefined` | <sup>***</sup> |
| `year` | `year` | `'numeric'\|'2-digit'\|undefined` | <sup>****</sup> |
| `timeZoneName` | `time-zone-name` | `'long'\|'short'\|'shortOffset'\|'longOffset'` `\|'shortGeneric'\|'longGeneric'\|undefined` | `undefined` |
| `noTitle` | `no-title` | `boolean \| null` | - |

<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'`.

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

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`.

##### noTitle (`boolean`, default: `undefined`)

Passing in `no-title="true"` 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.

## Browser Support

Browsers without native [custom element support][support] require a [polyfill][ce-polyfill].
Expand Down
8 changes: 8 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ <h3>Format DateTime</h3>
</relative-time>
</p>

<p>
No title attribute:
<relative-time datetime="1970-01-01T00:00:00.000Z" format="datetime" hour="numeric" minute="2-digit" second="2-digit"
no-title="true">
Jan 1 1970
</relative-time>
</p>

<p>
Customised options:
<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">
Expand Down
7 changes: 6 additions & 1 deletion src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
'precision',
'format',
'format-style',
'no-title',
'datetime',
'lang',
'title',
Expand Down Expand Up @@ -382,6 +383,10 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
this.setAttribute('format-style', value)
}

get noTitle(): boolean {
return this.hasAttribute('no-title') && this.getAttribute('no-title') === 'true'
lindseywild marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking:

  • Is there any reason to make this private?
  • Is there any reason to add a setter (to support e.g. relativeTime.noTitle = false)?

(I can’t think of any good reasons.)


get datetime() {
return this.getAttribute('datetime') || ''
}
Expand Down Expand Up @@ -431,7 +436,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
return
}
const now = Date.now()
if (!this.#customTitle) {
if (!this.#customTitle && !this.noTitle) {
newTitle = this.#getFormattedTitle(date) || ''
if (newTitle) this.setAttribute('title', newTitle)
}
Expand Down
16 changes: 16 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ suite('relative-time', function () {
assert.equal(el.getAttribute('title'), text)
})

test('does not set title if no-title=true', async () => {
const el = document.createElement('relative-time')
el.setAttribute('datetime', new Date().toISOString())
el.setAttribute('no-title', true)
await Promise.resolve()
assert.equal(el.getAttribute('title'), null)
})

test('sets title if no-title=false', async () => {
const el = document.createElement('relative-time')
el.setAttribute('datetime', new Date().toISOString())
el.setAttribute('no-title', false)
await Promise.resolve()
assert.ok(el.getAttribute('title'))
})

test('shadowDOM reflects textContent with invalid date', async () => {
const el = document.createElement('relative-time')
el.textContent = 'A date string'
Expand Down
Loading