Skip to content

Commit 6d0dec6

Browse files
committed
initial implementation
1 parent 845f33b commit 6d0dec6

File tree

4 files changed

+101
-38
lines changed

4 files changed

+101
-38
lines changed

examples/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
<title>custom-element demo</title>
66
</head>
77
<body>
8-
<custom-element></custom-element>
8+
<typing-test id="a">the quick brown fox jumped over the lazy dog</typing-test>
9+
10+
11+
<button onclick="a.started ? a.stop() : a.start()">Start</button>
912

1013
<script type="module">
1114
// import 'https://unpkg.com/@github/custom-element-boilerplate@latest/dist/custom-element.js'
12-
import '../src/custom-element.ts'
15+
import '../src/typing-test-element.ts'
1316
</script>
1417
</body>
1518
</html>

src/custom-element.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/typing-test-element.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const html = String.raw
2+
/**
3+
* An example Custom Element. This documentation ends up in the
4+
* README so describe how this elements works here.
5+
*
6+
* You can event add examples on the element is used with Markdown.
7+
*
8+
* ```
9+
* <typing-test></typing-test>
10+
* ```
11+
*/
12+
class TypingTestElement extends HTMLElement {
13+
#renderRoot = this.attachShadow({mode: 'open'})
14+
#started = false
15+
#position = 0
16+
#feedback!: HTMLElement
17+
18+
get started() {
19+
return this.#started
20+
}
21+
22+
connectedCallback(): void {
23+
// eslint-disable-next-line github/no-inner-html
24+
this.#renderRoot.innerHTML = html`
25+
<style>
26+
p span {
27+
color: green;
28+
}
29+
p del {
30+
color: red;
31+
}
32+
</style>
33+
<slot></slot>
34+
<p></p>
35+
`
36+
this.ownerDocument.addEventListener('keypress', this)
37+
// eslint-disable-next-line custom-elements/no-dom-traversal-in-connectedcallback
38+
this.#feedback = this.#renderRoot.querySelector<HTMLElement>('p')!
39+
}
40+
41+
start() {
42+
this.#started = true
43+
this.#position = 0
44+
}
45+
46+
stop() {
47+
this.#started = false
48+
}
49+
50+
#continue() {
51+
this.#feedback.querySelectorAll('del').forEach(e => e.remove())
52+
const span = this.ownerDocument.createElement('span')
53+
span.textContent = this.textContent?.[this.#position] || ''
54+
this.#feedback.append(span)
55+
this.#position += 1
56+
}
57+
58+
#error(character: string) {
59+
this.#feedback.querySelectorAll('del').forEach(e => e.remove())
60+
const del = this.ownerDocument.createElement('del')
61+
del.textContent = character
62+
this.#feedback.append(del)
63+
}
64+
65+
handleEvent(event) {
66+
if (!this.#started) return
67+
const character = this.textContent?.[this.#position]
68+
console.log(event.key, character)
69+
if (event.key === character) {
70+
this.#continue()
71+
} else {
72+
this.#error(event.key)
73+
}
74+
}
75+
}
76+
77+
declare global {
78+
interface Window {
79+
TypingTestElement: typeof TypingTestElement
80+
}
81+
}
82+
83+
export default TypingTestElement
84+
85+
if (!window.customElements.get('typing-test')) {
86+
window.TypingTestElement = TypingTestElement
87+
window.customElements.define('typing-test', TypingTestElement)
88+
}

test/test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import {assert, fixture, html} from '@open-wc/testing'
2-
import '../src/custom-element'
2+
import '../src/typing-test-element'
33

4-
describe('custom-element', function () {
4+
describe('typing-test', function () {
55
describe('element creation', function () {
66
it('creates from document.createElement', function () {
7-
const el = document.createElement('custom-element')
8-
assert.equal('CUSTOM-ELEMENT', el.nodeName)
7+
const el = document.createElement('typing-test')
8+
assert.equal('TYPING-TEST', el.nodeName)
99
})
1010

1111
it('creates from constructor', function () {
12-
const el = new window.CustomElementElement()
13-
assert.equal('CUSTOM-ELEMENT', el.nodeName)
12+
const el = new window.TypingTestElement()
13+
assert.equal('TYPING-TEST', el.nodeName)
1414
})
1515
})
1616

1717
describe('after tree insertion', function () {
1818
beforeEach(async function () {
19-
await fixture(html` <custom-element></custom-element>`)
19+
await fixture(html` <typing-test></typing-test>`)
2020
})
2121

2222
it('initiates', function () {
23-
const ce = document.querySelector('custom-element')
23+
const ce = document.querySelector('typing-test')
2424
assert.equal(ce?.textContent, ':wave:')
2525
})
2626
})

0 commit comments

Comments
 (0)