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

Switch element to defining content within ShadowDOM #193

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
this.datetime = value?.toISOString() || ''
}

constructor() {
super()
if (!this.shadowRoot) this.attachShadow({mode: 'open'})
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the guard for declarative shadow doms! <3

}

connectedCallback(): void {
this.update()
}
Expand All @@ -273,7 +278,7 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
}

update() {
const oldText: string = this.textContent || ''
const oldText: string = this.shadowRoot!.textContent || ''
const oldTitle: string = this.getAttribute('title') || ''
let newTitle: string = oldTitle
let newText: string = oldText
Expand All @@ -288,7 +293,7 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat

newText = this.getFormattedDate(now) || ''
if (newText) {
this.textContent = newText
this.shadowRoot!.textContent = newText
}

if (newText !== oldText || newTitle !== oldTitle) {
Expand Down
18 changes: 9 additions & 9 deletions test/local-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ suite('local-time', function () {
test('ignores contents if datetime attribute is missing', function () {
const time = document.createElement('local-time')
time.setAttribute('year', 'numeric')
assert.equal(time.textContent, '')
assert.equal(time.shadowRoot.textContent, '')
})

test('sets formatted contents to format attribute', function () {
const time = document.createElement('local-time')
time.setAttribute('datetime', '1970-01-01T00:00:00.000Z')
time.setAttribute('year', 'numeric')
assert.include(['1969', '1970'], time.textContent)
assert.include(['1969', '1970'], time.shadowRoot.textContent)
})

test('updates format when attributes change', function () {
const time = document.createElement('local-time')
time.setAttribute('datetime', '1970-01-01T00:00:00.000Z')

time.setAttribute('year', 'numeric')
assert.include(['1969', '1970'], time.textContent)
assert.include(['1969', '1970'], time.shadowRoot.textContent)

time.setAttribute('year', '2-digit')
assert.include(['69', '70'], time.textContent)
assert.include(['69', '70'], time.shadowRoot.textContent)
})

test('sets formatted contents when parsed element is upgraded', function () {
Expand All @@ -89,7 +89,7 @@ suite('local-time', function () {
if ('CustomElements' in window) {
window.CustomElements.upgradeSubtree(root)
}
assert.include(['1969', '1970'], root.children[0].textContent)
assert.include(['1969', '1970'], root.children[0].shadowRoot.textContent)
})
;('Intl' in window ? test : test.skip)('displays time zone name', function () {
const root = document.createElement('div')
Expand All @@ -98,8 +98,8 @@ suite('local-time', function () {
if ('CustomElements' in window) {
window.CustomElements.upgradeSubtree(root)
}
assert.match(root.children[0].textContent, /^\d{1,2} (\w+([+-]\d+)?)$/)
assert.equal(root.children[0].textContent, '0 GMT+4')
assert.match(root.children[0].shadowRoot.textContent, /^\d{1,2} (\w+([+-]\d+)?)$/)
assert.equal(root.children[0].shadowRoot.textContent, '0 GMT+4')
})

test('updates time zone when the `time-zone-name` attribute changes', function () {
Expand All @@ -109,10 +109,10 @@ suite('local-time', function () {
el.setAttribute('time-zone-name', 'short')

fixture.appendChild(el)
assert.equal(el.textContent, '1/1/1970, GMT+4')
assert.equal(el.shadowRoot.textContent, '1/1/1970, GMT+4')

el.setAttribute('time-zone-name', 'long')

assert.equal(el.textContent, '1/1/1970, Gulf Standard Time')
assert.equal(el.shadowRoot.textContent, '1/1/1970, Gulf Standard Time')
})
})
Loading