Skip to content

Commit 78db0c5

Browse files
committed
tweak index example to follow code style
1 parent eeb2705 commit 78db0c5

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

index.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@ styles.replaceSync(`
4848

4949
// Define the StopWatch element Class
5050
class StopWatchElement extends HTMLElement {
51-
// Private state
51+
static define(tag = "stop-watch") {
52+
customElements.define(tag, this)
53+
}
54+
55+
// Give this element its own encapsulated DOM
56+
shadowRoot = this.attachShadow({ mode: "open" })
57+
58+
// Initialize private state
5259
#start = Date.now()
5360

5461
connectedCallback() {
5562
// Add the shared styles
56-
this.attachShadow({ mode: "open" }).adoptedStyleSheets = [styles]
63+
this.shadowRoot.adoptedStyleSheets = [styles]
5764

5865
// Start the timer
5966
this.#tick()
@@ -64,15 +71,12 @@ class StopWatchElement extends HTMLElement {
6471
const minutes = String(Math.floor(milliseconds / (1000 * 60))).padStart(2, "0")
6572
const seconds = String(Math.floor((milliseconds / 1000) % 60)).padStart(2, "0")
6673
const hundredths = String(Math.floor((milliseconds % 1000) / 10)).padStart(2, "0")
67-
this.shadowRoot.innerHTML = `${minutes}:${seconds}:${hundredths}`
74+
75+
this.shadowRoot.replaceChildren(`${minutes}:${seconds}:${hundredths}`)
6876

6977
// Schedule next update
7078
requestAnimationFrame(() => this.#tick())
7179
}
72-
73-
static define(tag = "stop-watch") {
74-
customElements.define(tag, this)
75-
}
7680
}
7781

7882
// Register the element with the Custom Element Registry

learn/index.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ styles.replaceSync(`
3939

4040
// Define the StopWatch element Class
4141
class StopWatchElement extends HTMLElement {
42-
// Private state
42+
static define(tag = "stop-watch") {
43+
customElements.define(tag, this)
44+
}
45+
46+
// Give this element its own encapsulated DOM
47+
shadowRoot = this.attachShadow({ mode: "open" })
48+
49+
// Initialize private state
4350
#start = Date.now()
4451

4552
connectedCallback() {
4653
// Add the shared styles
47-
this.attachShadow({ mode: "open" }).adoptedStyleSheets = [styles]
54+
this.shadowRoot.adoptedStyleSheets = [styles]
4855

4956
// Start the timer
5057
this.#tick()
@@ -55,15 +62,12 @@ class StopWatchElement extends HTMLElement {
5562
const minutes = String(Math.floor(milliseconds / (1000 * 60))).padStart(2, "0")
5663
const seconds = String(Math.floor((milliseconds / 1000) % 60)).padStart(2, "0")
5764
const hundredths = String(Math.floor((milliseconds % 1000) / 10)).padStart(2, "0")
58-
this.shadowRoot.innerHTML = `${minutes}:${seconds}:${hundredths}`
65+
66+
this.shadowRoot.replaceChildren(`${minutes}:${seconds}:${hundredths}`)
5967

6068
// Schedule next update
6169
requestAnimationFrame(() => this.#tick())
6270
}
63-
64-
static define(tag = "stop-watch") {
65-
customElements.define(tag, this)
66-
}
6771
}
6872

6973
// Register the element with the Custom Element Registry

0 commit comments

Comments
 (0)