Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: github/relative-time-element
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.4.0
Choose a base ref
...
head repository: github/relative-time-element
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.4.1
Choose a head ref
  • 15 commits
  • 4 files changed
  • 4 contributors

Commits on May 24, 2024

  1. fix: remove dollar sign from command

    currently it copies the `$` when copying
    o-az authored May 24, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    d970378 View commit details
  2. Merge pull request #282 from o-az/patch-1

    fix: remove dollar sign from command
    keithamus authored May 24, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    4b5442f View commit details

Commits on May 29, 2024

  1. Adds flag to optionally hide title attribute

    lindseywild committed May 29, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    ce767cc View commit details
  2. Update readme

    lindseywild committed May 29, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    fe78512 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    abc079e View commit details
  4. Update README.md

    lindseywild authored May 29, 2024
    Copy the full SHA
    48c1c8e View commit details
  5. Updates examples

    lindseywild committed May 29, 2024
    Copy the full SHA
    e281a47 View commit details
  6. Updates to hasAttribute

    lindseywild committed May 29, 2024
    Copy the full SHA
    519d915 View commit details
  7. Updates docs

    lindseywild committed May 29, 2024
    Copy the full SHA
    409d456 View commit details
  8. Uses toggleAttribute

    lindseywild committed May 29, 2024
    Copy the full SHA
    f7a01aa View commit details

Commits on May 30, 2024

  1. Update src/relative-time-element.ts

    Co-authored-by: Clay Miller <clay@smockle.com>
    lindseywild and smockle authored May 30, 2024
    Copy the full SHA
    4f8eefb View commit details
  2. Update relative-time.js

    lindseywild authored May 30, 2024
    Copy the full SHA
    942896a View commit details
  3. Merge pull request #283 from github/lw/optional-title

    Adds flag to optionally hide title attribute
    lindseywild authored May 30, 2024
    Copy the full SHA
    534ce30 View commit details

Commits on May 31, 2024

  1. fix: Move 'noTitle' conditional.

    Calculate the new title, just don’t update the attribute. This change means the 'relative-time-updated' event will fire even if 'noTitle' is set.
    smockle authored May 31, 2024
    Copy the full SHA
    257f0eb View commit details
  2. Merge pull request #284 from github/smockle/untitled

    fix: Move `noTitle` conditional.
    smockle authored May 31, 2024
    Copy the full SHA
    ed0833b View commit details
Showing with 32 additions and 2 deletions.
  1. +6 −1 README.md
  2. +8 −0 examples/index.html
  3. +10 −1 src/relative-time-element.ts
  4. +8 −0 test/relative-time.js
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ If the browser's JavaScript is disabled, the default text served in the cached m
Available on [npm](https://www.npmjs.com/) as [**@github/relative-time-element**](https://www.npmjs.com/package/@github/relative-time-element).

```
$ npm install @github/relative-time-element
npm install @github/relative-time-element
```

This element uses the `Intl.DateTimeFormat` & `Intl.RelativeTimeFormat` APIs, which are supported by all modern JS engines. If you need to support an older browser, you may need to introduce a polyfill for `Intl.DateTimeFormat` & `Intl.RelativeTimeFormat`.
@@ -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` | `-` | `-` |

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

@@ -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

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.

## Browser Support

Browsers without native [custom element support][support] require a [polyfill][ce-polyfill].
8 changes: 8 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -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>
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">
11 changes: 10 additions & 1 deletion src/relative-time-element.ts
Original file line number Diff line number Diff line change
@@ -107,6 +107,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
'precision',
'format',
'format-style',
'no-title',
'datetime',
'lang',
'title',
@@ -382,6 +383,14 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
this.setAttribute('format-style', value)
}

get noTitle(): boolean {
return this.hasAttribute('no-title')
}

set noTitle(value: boolean | undefined) {
this.toggleAttribute('no-title', value)
}

get datetime() {
return this.getAttribute('datetime') || ''
}
@@ -433,7 +442,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
const now = Date.now()
if (!this.#customTitle) {
newTitle = this.#getFormattedTitle(date) || ''
if (newTitle) this.setAttribute('title', newTitle)
if (newTitle && !this.noTitle) this.setAttribute('title', newTitle)
}

const duration = elapsedTime(date, this.precision, now)
8 changes: 8 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
@@ -83,6 +83,14 @@ suite('relative-time', function () {
assert.equal(el.getAttribute('title'), text)
})

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

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