|
1 | 1 | # js-element |
2 | 2 |
|
3 | 3 | A R&D project to evaluate an alternative approach to develop custom elements. |
4 | | -The package `js-element` uses a patched version of [superfine](https://github.com/jorgebucaran/superfine), a very lightweight virtual DOM library. |
5 | | -The subpackages `js-element/lit` and `js-element/uhtml` use [lit-html](https://lit-html.polymer-project.org/) respective [uhtml](https://github.com/WebReflection/uhtml) instead. |
6 | 4 |
|
7 | 5 | #### Disclaimer: |
8 | 6 |
|
9 | 7 | Project is in an early state ... |
10 | 8 | and it is currently not meant to ever be used in production. |
11 | 9 |
|
12 | | -## Examples |
| 10 | +## Example (simple counter) |
13 | 11 |
|
14 | | -### Example 1 (using JSX) |
15 | | - |
16 | | -```jsx |
17 | | -import { define, h, render } from 'js-element' |
| 12 | +```tsx |
| 13 | +import { component, elem, prop, setMethods, Attrs } from 'js-element' |
18 | 14 | import { useState } from 'js-element/hooks' |
19 | | - |
20 | | -const Counter = define('demo-counter', () => { |
21 | | - const [state, setState] = useState({ count: 0 }) |
22 | | - const onClick = () => setState('count', it => it + 1) |
23 | | - |
24 | | - return () => ( |
25 | | - <button onclick={onClick}> |
26 | | - Count: {state.count} |
27 | | - </button> |
28 | | - ) |
29 | | -}) |
30 | | - |
31 | | -render(<Counter />, '#app') |
32 | | -``` |
33 | | - |
34 | | -### Example 2 (using lit-html) |
35 | | - |
36 | | -```js |
37 | | -import { define, html, render } from 'js-element/lit' |
38 | | -import { useState } from 'js-element/hooks' |
39 | | - |
40 | | -define('demo-counter', () => { |
41 | | - const [state, setState] = useState({ count: 0 }) |
42 | | - const onClick = () => setState('count', it => it + 1) |
43 | | - |
44 | | - return () => html` |
45 | | - <button @click=${onClick}> |
46 | | - Count: ${state.count} |
47 | | - </button> |
48 | | - ` |
49 | | -}) |
50 | | - |
51 | | -render(html`<demo-counter></demo-counter>`, '#app') |
52 | | -``` |
53 | | - |
54 | | -### Example 3 (using uhtml) |
55 | | - |
56 | | -```js |
57 | | -import { define, html, render } from 'js-element/uhtml' |
58 | | -import { useState } from 'js-element/hooks' |
59 | | - |
60 | | -define('demo-counter', () => { |
61 | | - const [state, setState] = useState({ count: 0 }) |
62 | | - const onClick = () => setState('count', it => it + 1) |
63 | | - |
64 | | - return () => html` |
65 | | - <button @click=${onClick}> |
66 | | - Count: ${state.count} |
67 | | - </button> |
68 | | - ` |
69 | | -}) |
70 | | - |
71 | | -render(html`<demo-counter />`, '#app') |
72 | | -``` |
73 | | - |
74 | | -### Example 4 |
75 | | - |
76 | | -```jsx |
77 | | -import { define, h, render } from 'js-element' |
78 | | - |
79 | | -class SayHelloProps { |
80 | | - salutation = 'Hello' |
81 | | - name = 'World' |
| 15 | +import { html } from 'lit-html' |
| 16 | +import { createRef, ref } from 'lit-html/directives/ref' |
| 17 | +import counterStyles from './counter.css' |
| 18 | + |
| 19 | +@elem({ |
| 20 | + tag: 'x-counter', |
| 21 | + styles: counterStyles, |
| 22 | + impl: counterImpl |
| 23 | +}) |
| 24 | +class Counter extends component<{ |
| 25 | + reset(): void |
| 26 | + increment(): void |
| 27 | + decrement(): void |
| 28 | +}>() { |
| 29 | + @prop({ attr: Attrs.string, refl: true }) |
| 30 | + labelText = 'Counter' |
| 31 | + |
| 32 | + @prop({ attr: Attrs.boolean, refl: true }) |
| 33 | + disabled = false |
82 | 34 | } |
83 | 35 |
|
84 | | -const SayHello = define('say-hello', SayHelloProps, (props) => { |
85 | | - return () => ( |
86 | | - <div> |
87 | | - {props.salutation}, {props.name}! |
88 | | - </div> |
89 | | - ) |
90 | | -}) |
91 | | - |
92 | | -render(<SayHello salutation="Hi" name="Jane Doe" />, '#app') |
93 | | -``` |
94 | | - |
95 | | -### Example 5 |
96 | | - |
97 | | -```jsx |
98 | | -// the author's preferred syntax and naming convention - |
99 | | -// may look a bit odd first, but you'll get used to it ;-) |
100 | | - |
101 | | -import { attr, define, h, render, Attr } from 'js-element' |
102 | | -import { useEffect, useAfterMount, useState } from 'js-element/hooks' |
103 | | -import counterStyles from './counter.scss' |
104 | | - |
105 | | -class CounterProps { |
106 | | - @attr(Attr.number, true) // true as 2nd argument means: reflect attribute |
107 | | - initialCount = 0 |
108 | | - |
109 | | - @attr(Attr.string, true) |
110 | | - label = 'Counter' |
111 | | -} |
112 | | - |
113 | | -const Counter = define({ |
114 | | - tag: 'demo-counter', |
115 | | - props: CounterProps, |
116 | | - styles: counterStyles |
117 | | -}).bind((p) => { |
| 36 | +function counterImpl(self: Counter) { |
118 | 37 | const [state, setState] = useState({ |
119 | | - count: p.initialCount |
| 38 | + count: 0 |
120 | 39 | }) |
121 | 40 |
|
122 | | - const onClick = () => setState('count', it => it + 1) |
123 | | - |
124 | | - useAfterMount(() => { |
125 | | - console.log(`"${p.label}" has been mounted`) |
| 41 | + const onClick = () => setState('count', (it) => it + 1) |
126 | 42 |
|
127 | | - return () => console.log(`Unmounting "${p.label}"`) |
| 43 | + setMethods(self, { |
| 44 | + reset: () => setState('count', 0), |
| 45 | + increment: () => setState('count', (it) => it + 1), |
| 46 | + decrement: () => setState('count', (it) => it - 1) |
128 | 47 | }) |
129 | 48 |
|
130 | | - useEffect( |
131 | | - () => console.log(`Value of "${p.label}": ${s.count}`), |
132 | | - () => [s.count] |
133 | | - ) |
134 | | - |
135 | | - return () => ( |
136 | | - <div class="counter"> |
137 | | - <label class="label">{p.label}: </label> |
138 | | - <button class="button" onclick={onClick}> |
139 | | - {s.count} |
140 | | - </button> |
141 | | - </div> |
142 | | - ) |
143 | | -}) |
144 | | - |
145 | | -render(<Counter />, '#app') |
146 | | -``` |
147 | | - |
148 | | -### Example 6 |
149 | | - |
150 | | -```jsx |
151 | | -import { define, h, render } from 'js-element' |
152 | | -import { useTimer } from 'js-element/hooks' |
153 | | - |
154 | | -const Clock = define('demo-clock', () => { |
155 | | - const getTime = useTimer(1000, () => new Date().toLocaleTimeString()) |
156 | | - return () => <div>Current time: {getTime()}</div> |
157 | | -}) |
158 | | - |
159 | | -render(<Clock />, '#app') |
| 49 | + return () => html` |
| 50 | + <button ?disabled=${self.disabled} @click=${onClick}> |
| 51 | + ${self.labelText}: ${state.count} |
| 52 | + </button> |
| 53 | + ` |
| 54 | +} |
160 | 55 | ``` |
161 | 56 |
|
162 | | -### Example 7 (using context) |
163 | | - |
164 | | -```jsx |
165 | | -import { createCtx, define, defineProvider, h, render } from 'js-element' |
166 | | -import { useCtx, useInterval, useState } from 'js-element/hooks' |
167 | | - |
168 | | -const ThemeCtx = createCtx('light') |
169 | | -const ThemeProvider = defineProvider('theme-provider', ThemeCtx) |
170 | | - |
171 | | -const ContextDemo = define('context-demo', () => { |
172 | | - const [state, setState] = useState({ theme: 'light' }) |
173 | | - |
174 | | - useInterval(() => { |
175 | | - setState('theme', state.theme === 'light' ? 'dark' : 'light') |
176 | | - }, 1000) |
177 | | - |
178 | | - return () => ( |
179 | | - <div> |
180 | | - <b>Value for theme will change every second:</b> |
181 | | - <br /> |
182 | | - <ThemeProvider value={state.theme}> |
183 | | - <ThemeInfo /> |
184 | | - </ThemeProvider> |
185 | | - </div> |
186 | | - ) |
187 | | -}) |
188 | | - |
189 | | -const ThemeInfo = define('theme-info', () => { |
190 | | - const ctx = useCtx({ theme: ThemeCtx }) |
191 | | - return () => <div>Current theme: {ctx.theme}</div> |
192 | | -}) |
193 | | - |
194 | | -render(<ContextDemo/>, '#app') |
195 | | -``` |
0 commit comments