Skip to content

Commit d2e2b7f

Browse files
keithamusElliott Marquez
andcommitted
initial implementation
Co-authored-by: Elliott Marquez <emarquez@google.com>
1 parent f856f4b commit d2e2b7f

File tree

5 files changed

+98
-42
lines changed

5 files changed

+98
-42
lines changed

examples/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
<title>custom-element demo</title>
66
</head>
77
<body>
8-
<custom-element></custom-element>
8+
<hacker-typer src="https://raw.githubusercontent.com/facebook/react/main/packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js">
9+
</hacker-typer>
910

1011
<script type="module">
1112
// import 'https://unpkg.com/@github/custom-element-boilerplate@latest/dist/custom-element.js'
12-
import '../src/custom-element.ts'
13+
import '../src/hacker-typer-element.ts'
1314
</script>
1415
</body>
1516
</html>

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"name": "@github/custom-element-element",
2+
"name": "@github/hacker-typer-element",
33
"version": "0.0.1",
44
"description": "Boilerplate for creating a custom element.",
5-
"main": "dist/custom-element.js",
6-
"module": "dist/custom-element.js",
5+
"main": "dist/hacker-typer-element.js",
6+
"module": "dist/hacker-typer-element.js",
77
"type": "module",
8-
"types": "dist/custom-elements.d.ts",
8+
"types": "dist/hacker-typer-elements.d.ts",
99
"license": "MIT",
10-
"repository": "github/custom-element-boilerplate",
10+
"repository": "hacker-typer-element",
1111
"files": [
1212
"dist"
1313
],

src/custom-element.ts

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

src/hacker-typer-element.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
* <hacker-typer></hacker-typer>
10+
* ```
11+
*/
12+
class HackerTyperElement extends HTMLElement {
13+
#renderRoot!: ShadowRoot
14+
#position = 0
15+
#content = ''
16+
17+
get src() {
18+
return this.getAttribute('src')
19+
}
20+
21+
set src(value: string) {
22+
this.setAttribute('src', value)
23+
}
24+
25+
get templateEl() {
26+
return this.querySelector('template')
27+
}
28+
29+
get displayAreaEl() {
30+
return this.#renderRoot.querySelector('code')
31+
}
32+
33+
async connectedCallback(): void {
34+
await this.load()
35+
36+
this.#renderRoot = this.attachShadow({mode: 'open'})
37+
this.#renderRoot.innerHTML = html`
38+
<style>
39+
:host {
40+
height: 100vw;
41+
display: block;
42+
background: black;
43+
color: limegreen;
44+
}
45+
</style>
46+
<pre><code></code></pre>`
47+
48+
this.ownerDocument.addEventListener('keypress', this)
49+
}
50+
51+
handleEvent() {
52+
if (this.#content.length <= this.#position) {
53+
this.restart()
54+
}
55+
const char = this.#content.slice(this.#position, this.#position + 10)
56+
const node = this.ownerDocument.createTextNode(char)
57+
this.displayAreaEl?.append(node)
58+
this.#position += 10
59+
}
60+
61+
async load() {
62+
const res = await fetch(this.src)
63+
const text = await res.text()
64+
this.#content = text
65+
}
66+
67+
restart() {
68+
this.#position = 0
69+
}
70+
}
71+
72+
declare global {
73+
interface Window {
74+
HackerTyperElement: typeof HackerTyperElement
75+
}
76+
}
77+
78+
export default HackerTyperElement
79+
80+
if (!window.customElements.get('hacker-typer')) {
81+
window.HackerTyperElement = HackerTyperElement
82+
window.customElements.define('hacker-typer', HackerTyperElement)
83+
}

test/test.ts

Lines changed: 7 additions & 7 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/hacker-typer'
33

4-
describe('custom-element', function () {
4+
describe('hacker-typer', 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('hacker-typer')
8+
assert.equal('HACKER-TYPER', el.nodeName)
99
})
1010

1111
it('creates from constructor', function () {
1212
const el = new window.CustomElementElement()
13-
assert.equal('CUSTOM-ELEMENT', el.nodeName)
13+
assert.equal('HACKER-TYPER', 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` <hacker-typer></hacker-typer>`)
2020
})
2121

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

0 commit comments

Comments
 (0)