bareui-core 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +209 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,53 +1,245 @@
|
|
|
1
|
-
#
|
|
1
|
+
# bareui-core
|
|
2
2
|
|
|
3
3
|
A minimal reactive UI library for lightweight apps, widgets, and experiments.
|
|
4
4
|
|
|
5
|
+
BareUI is designed to stay small, readable, and easy to reason about. It provides a few focused primitives for reactivity, templating, lifecycle hooks, and DOM mounting.
|
|
6
|
+
|
|
5
7
|
## API
|
|
6
8
|
|
|
7
9
|
### `reactive(object)`
|
|
8
|
-
|
|
10
|
+
|
|
11
|
+
Creates a reactive proxy.
|
|
12
|
+
|
|
13
|
+
When a property changes, any `effect()` or template binding that reads it will update automatically.
|
|
14
|
+
|
|
15
|
+
You can use `reactive()` in two common ways:
|
|
16
|
+
|
|
17
|
+
#### Globally scoped reactive state
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { reactive, effect } from 'bareui-core';
|
|
21
|
+
|
|
22
|
+
const state = reactive({
|
|
23
|
+
count: 0,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
effect(() => {
|
|
27
|
+
console.log('count changed:', state.count);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
state.count++;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### Component‑scoped reactive state
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { component, html, reactive } from 'bareui-core';
|
|
37
|
+
|
|
38
|
+
const Counter = component(() => {
|
|
39
|
+
const state = reactive({
|
|
40
|
+
count: 0,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return html`
|
|
44
|
+
<button @click=${() => state.count++}>
|
|
45
|
+
Count: ${() => state.count}
|
|
46
|
+
</button>
|
|
47
|
+
`;
|
|
48
|
+
});
|
|
49
|
+
```
|
|
9
50
|
|
|
10
51
|
### `effect(fn)`
|
|
11
|
-
Runs a function and tracks the reactive values it reads.
|
|
52
|
+
Runs a function and tracks the reactive values it reads.
|
|
53
|
+
|
|
54
|
+
When those values change, the function runs again.
|
|
55
|
+
|
|
56
|
+
Outside a component, the returned runner can be stopped manually with stop().
|
|
12
57
|
|
|
13
|
-
|
|
14
|
-
|
|
58
|
+
#### Manual effect with `stop()`
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import { effect, reactive } from 'bareui-core';
|
|
62
|
+
|
|
63
|
+
const state = reactive({
|
|
64
|
+
count: 0,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const runner = effect(() => {
|
|
68
|
+
console.log('count:', state.count);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
state.count++;
|
|
72
|
+
runner.stop();
|
|
73
|
+
state.count++;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### Automatic effect inside a component
|
|
77
|
+
If `effect()` is created while a component is rendering, it becomes part of that component’s scope and is cleaned up automatically when the component is unmounted.
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
import { component, effect, html, reactive } from 'bareui-core';
|
|
81
|
+
|
|
82
|
+
const Counter = component(() => {
|
|
83
|
+
const state = reactive({
|
|
84
|
+
count: 0,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
effect(() => {
|
|
88
|
+
console.log('component count:', state.count);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return html`
|
|
92
|
+
<button @click=${() => state.count++}>
|
|
93
|
+
Count: ${() => state.count}
|
|
94
|
+
</button>
|
|
95
|
+
`;
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `html'...'`
|
|
100
|
+
Creates a template result using tagged template literals.
|
|
101
|
+
|
|
102
|
+
#### Use ${...} placeholders for dynamic content, attributes, and event handlers.
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
import { html } from 'bareui-core';
|
|
106
|
+
|
|
107
|
+
const view = html`
|
|
108
|
+
<div class="card">
|
|
109
|
+
<h1>Hello</h1>
|
|
110
|
+
<p>This is a BareUI template.</p>
|
|
111
|
+
</div>
|
|
112
|
+
`;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### Dynamic bindings
|
|
116
|
+
```js
|
|
117
|
+
import { html } from 'bareui-core';
|
|
118
|
+
|
|
119
|
+
const title = 'Hello';
|
|
120
|
+
const isActive = true;
|
|
121
|
+
|
|
122
|
+
const view = html`
|
|
123
|
+
<div class=${isActive ? 'active' : 'idle'}>
|
|
124
|
+
<h1>${title}</h1>
|
|
125
|
+
</div>
|
|
126
|
+
`;
|
|
127
|
+
```
|
|
15
128
|
|
|
16
129
|
### `component(fn)`
|
|
17
|
-
Wraps a render function in a component scope.
|
|
130
|
+
Wraps a render function in a component scope.
|
|
131
|
+
|
|
132
|
+
Use it when you want lifecycle callbacks, scoped effects, and grouped cleanup.
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
import { component, html, reactive } from 'bareui-core';
|
|
136
|
+
|
|
137
|
+
const App = component(() => {
|
|
138
|
+
const state = reactive({
|
|
139
|
+
name: 'BareUI',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return html`
|
|
143
|
+
<h1>Hello ${() => state.name}</h1>
|
|
144
|
+
`;
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
A component is especially useful when you want state, effects, and cleanup to belong to the same UI unit.
|
|
18
148
|
|
|
19
149
|
### `onMounted(fn)`
|
|
20
150
|
Registers a callback that runs after the component is mounted.
|
|
21
151
|
|
|
152
|
+
```js
|
|
153
|
+
import { component, html, onMounted } from 'bareui-core';
|
|
154
|
+
|
|
155
|
+
const App = component(() => {
|
|
156
|
+
onMounted(() => {
|
|
157
|
+
console.log('mounted');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return html`
|
|
161
|
+
<div>Mounted component</div>
|
|
162
|
+
`;
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
22
166
|
### `onUnmounted(fn)`
|
|
23
167
|
Registers a callback that runs when the component is disposed.
|
|
24
168
|
|
|
169
|
+
```js
|
|
170
|
+
import { component, html, onUnmounted } from 'bareui-core';
|
|
171
|
+
|
|
172
|
+
const App = component(() => {
|
|
173
|
+
onUnmounted(() => {
|
|
174
|
+
console.log('unmounted');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return html`
|
|
178
|
+
<div>Cleanup example</div>
|
|
179
|
+
`;
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
25
183
|
### `repeat(items, keyFn, renderItem)`
|
|
26
184
|
Renders a keyed list of items and keeps DOM updates efficient.
|
|
27
185
|
|
|
186
|
+
```js
|
|
187
|
+
import { component, html, reactive, repeat } from 'bareui-core';
|
|
188
|
+
|
|
189
|
+
const App = component(() => {
|
|
190
|
+
const state = reactive({
|
|
191
|
+
items: [
|
|
192
|
+
{ id: 1, label: 'Alpha' },
|
|
193
|
+
{ id: 2, label: 'Beta' },
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return html`
|
|
198
|
+
<ul>
|
|
199
|
+
${repeat(
|
|
200
|
+
() => state.items,
|
|
201
|
+
item => item.id,
|
|
202
|
+
item => html`<li>${item.label}</li>`
|
|
203
|
+
)}
|
|
204
|
+
</ul>
|
|
205
|
+
`;
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
28
209
|
### `mount(view, container)`
|
|
29
210
|
Mounts a template result or render function into a DOM element.
|
|
30
211
|
|
|
31
|
-
|
|
212
|
+
You can mount:
|
|
32
213
|
|
|
33
|
-
|
|
34
|
-
import { component, html, mount, reactive } from '@mintool/bareui-core';
|
|
214
|
+
* a TemplateResult
|
|
35
215
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
216
|
+
* a render function that returns a renderable value
|
|
217
|
+
|
|
218
|
+
#### Mounting a component
|
|
219
|
+
```js
|
|
220
|
+
import { component, html, mount } from 'bareui-core';
|
|
39
221
|
|
|
40
222
|
const App = component(() => html`
|
|
41
|
-
<div>
|
|
42
|
-
<h1>Count: ${() => state.count}</h1>
|
|
43
|
-
<button @click=${() => state.count++}>Increment</button>
|
|
44
|
-
</div>
|
|
223
|
+
<div>Hello from BareUI</div>
|
|
45
224
|
`);
|
|
46
225
|
|
|
47
|
-
mount(App(), '
|
|
226
|
+
mount(App(), document.getElementById('app'));
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
#### Mounting a plain template
|
|
230
|
+
```js
|
|
231
|
+
import { html, mount } from 'bareui-core';
|
|
232
|
+
|
|
233
|
+
const view = html`
|
|
234
|
+
<div>Plain template</div>
|
|
235
|
+
`;
|
|
48
236
|
|
|
237
|
+
mount(view, document.getElementById('app'));
|
|
49
238
|
```
|
|
50
239
|
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
51
243
|
### Notes
|
|
52
244
|
* bareui-core is designed to stay small and readable.
|
|
53
245
|
* It works best for lightweight apps, widgets, demos, and UI experiments.
|