humanjs-core 1.0.0
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/CHANGELOG.md +64 -0
- package/LICENSE +21 -0
- package/README.md +655 -0
- package/examples/api-example/app.js +365 -0
- package/examples/counter/app.js +141 -0
- package/examples/human-counter/app.human +27 -0
- package/examples/human-counter/app.js +77 -0
- package/examples/routing/app.js +129 -0
- package/examples/simple-js/app.js +30 -0
- package/examples/todo-app/app.js +378 -0
- package/examples/user-dashboard/app.js +0 -0
- package/package.json +78 -0
- package/scripts/human-compile.js +43 -0
- package/scripts/humanjs.js +700 -0
- package/src/compiler/human.js +194 -0
- package/src/core/component.js +381 -0
- package/src/core/events.js +130 -0
- package/src/core/render.js +173 -0
- package/src/core/router.js +274 -0
- package/src/core/state.js +114 -0
- package/src/index.js +61 -0
- package/src/plugins/http.js +167 -0
- package/src/plugins/storage.js +181 -0
- package/src/plugins/validator.js +193 -0
- package/src/utils/dom.js +0 -0
- package/src/utils/helpers.js +209 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API INTEGRATION EXAMPLE
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Fetch data from API
|
|
6
|
+
* - Loading states
|
|
7
|
+
* - Error handling
|
|
8
|
+
* - Search and filter
|
|
9
|
+
* - Pagination
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { app, html, each, when } from '../../src/index.js';
|
|
13
|
+
import { createHttp } from '../../src/plugins/http.js';
|
|
14
|
+
import { debounce } from '../../src/core/events.js';
|
|
15
|
+
|
|
16
|
+
// ============================================
|
|
17
|
+
// HTTP CLIENT
|
|
18
|
+
// ============================================
|
|
19
|
+
|
|
20
|
+
const api = createHttp({
|
|
21
|
+
baseURL: 'https://jsonplaceholder.typicode.com',
|
|
22
|
+
onRequest: (config) => {
|
|
23
|
+
console.log('🌐 API Request:', config);
|
|
24
|
+
},
|
|
25
|
+
onError: (error) => {
|
|
26
|
+
console.error('❌ API Error:', error);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// ============================================
|
|
31
|
+
// COMPONENTS
|
|
32
|
+
// ============================================
|
|
33
|
+
|
|
34
|
+
function UserCard(user) {
|
|
35
|
+
return html`
|
|
36
|
+
<div style="
|
|
37
|
+
border: 1px solid #ddd;
|
|
38
|
+
border-radius: 8px;
|
|
39
|
+
padding: 20px;
|
|
40
|
+
margin: 10px;
|
|
41
|
+
background: white;
|
|
42
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
43
|
+
transition: transform 0.2s;
|
|
44
|
+
"
|
|
45
|
+
onmouseover="this.style.transform='translateY(-4px)'"
|
|
46
|
+
onmouseout="this.style.transform='translateY(0)'"
|
|
47
|
+
>
|
|
48
|
+
<h3 style="margin: 0 0 10px 0; color: #2196F3;">
|
|
49
|
+
${user.name}
|
|
50
|
+
</h3>
|
|
51
|
+
<p style="margin: 5px 0; color: #666;">
|
|
52
|
+
📧 ${user.email}
|
|
53
|
+
</p>
|
|
54
|
+
<p style="margin: 5px 0; color: #666;">
|
|
55
|
+
📱 ${user.phone}
|
|
56
|
+
</p>
|
|
57
|
+
<p style="margin: 5px 0; color: #666;">
|
|
58
|
+
🏢 ${user.company.name}
|
|
59
|
+
</p>
|
|
60
|
+
<button
|
|
61
|
+
class="view-posts-btn"
|
|
62
|
+
data-user-id="${user.id}"
|
|
63
|
+
style="
|
|
64
|
+
margin-top: 10px;
|
|
65
|
+
padding: 8px 16px;
|
|
66
|
+
background: #4CAF50;
|
|
67
|
+
color: white;
|
|
68
|
+
border: none;
|
|
69
|
+
border-radius: 4px;
|
|
70
|
+
cursor: pointer;
|
|
71
|
+
"
|
|
72
|
+
>
|
|
73
|
+
View Posts
|
|
74
|
+
</button>
|
|
75
|
+
</div>
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function PostCard(post) {
|
|
80
|
+
return html`
|
|
81
|
+
<div style="
|
|
82
|
+
border-left: 4px solid #2196F3;
|
|
83
|
+
padding: 15px;
|
|
84
|
+
margin: 10px 0;
|
|
85
|
+
background: #f9f9f9;
|
|
86
|
+
border-radius: 4px;
|
|
87
|
+
">
|
|
88
|
+
<h4 style="margin: 0 0 10px 0; color: #333;">
|
|
89
|
+
${post.title}
|
|
90
|
+
</h4>
|
|
91
|
+
<p style="margin: 0; color: #666; line-height: 1.6;">
|
|
92
|
+
${post.body}
|
|
93
|
+
</p>
|
|
94
|
+
</div>
|
|
95
|
+
`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function LoadingSpinner() {
|
|
99
|
+
return html`
|
|
100
|
+
<div style="
|
|
101
|
+
text-align: center;
|
|
102
|
+
padding: 40px;
|
|
103
|
+
">
|
|
104
|
+
<div style="
|
|
105
|
+
display: inline-block;
|
|
106
|
+
width: 40px;
|
|
107
|
+
height: 40px;
|
|
108
|
+
border: 4px solid #f3f3f3;
|
|
109
|
+
border-top: 4px solid #2196F3;
|
|
110
|
+
border-radius: 50%;
|
|
111
|
+
animation: spin 1s linear infinite;
|
|
112
|
+
"></div>
|
|
113
|
+
<style>
|
|
114
|
+
@keyframes spin {
|
|
115
|
+
0% { transform: rotate(0deg); }
|
|
116
|
+
100% { transform: rotate(360deg); }
|
|
117
|
+
}
|
|
118
|
+
</style>
|
|
119
|
+
<p style="margin-top: 20px; color: #666;">Loading...</p>
|
|
120
|
+
</div>
|
|
121
|
+
`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function ErrorMessage(message) {
|
|
125
|
+
return html`
|
|
126
|
+
<div style="
|
|
127
|
+
padding: 20px;
|
|
128
|
+
background: #ffebee;
|
|
129
|
+
border-left: 4px solid #f44336;
|
|
130
|
+
border-radius: 4px;
|
|
131
|
+
margin: 20px 0;
|
|
132
|
+
">
|
|
133
|
+
<h3 style="margin: 0 0 10px 0; color: #c62828;">
|
|
134
|
+
⚠️ Error
|
|
135
|
+
</h3>
|
|
136
|
+
<p style="margin: 0; color: #666;">
|
|
137
|
+
${message}
|
|
138
|
+
</p>
|
|
139
|
+
</div>
|
|
140
|
+
`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ============================================
|
|
144
|
+
// MAIN APP
|
|
145
|
+
// ============================================
|
|
146
|
+
|
|
147
|
+
const apiApp = app.create({
|
|
148
|
+
state: {
|
|
149
|
+
users: [],
|
|
150
|
+
posts: [],
|
|
151
|
+
loading: false,
|
|
152
|
+
error: null,
|
|
153
|
+
view: 'users', // users, posts
|
|
154
|
+
selectedUserId: null,
|
|
155
|
+
searchQuery: ''
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
render: (state) => {
|
|
159
|
+
// Filter users by search query
|
|
160
|
+
const filteredUsers = state.users.filter(user =>
|
|
161
|
+
user.name.toLowerCase().includes(state.searchQuery.toLowerCase()) ||
|
|
162
|
+
user.email.toLowerCase().includes(state.searchQuery.toLowerCase())
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const element = html`
|
|
166
|
+
<div style="
|
|
167
|
+
max-width: 1200px;
|
|
168
|
+
margin: 50px auto;
|
|
169
|
+
padding: 20px;
|
|
170
|
+
font-family: system-ui, sans-serif;
|
|
171
|
+
">
|
|
172
|
+
<h1 style="text-align: center; color: #2196F3;">
|
|
173
|
+
🌐 API Integration Example
|
|
174
|
+
</h1>
|
|
175
|
+
|
|
176
|
+
<!-- Navigation -->
|
|
177
|
+
<div style="
|
|
178
|
+
display: flex;
|
|
179
|
+
justify-content: center;
|
|
180
|
+
gap: 10px;
|
|
181
|
+
margin: 30px 0;
|
|
182
|
+
">
|
|
183
|
+
<button
|
|
184
|
+
id="load-users-btn"
|
|
185
|
+
style="
|
|
186
|
+
padding: 12px 24px;
|
|
187
|
+
background: ${state.view === 'users' ? '#2196F3' : '#fff'};
|
|
188
|
+
color: ${state.view === 'users' ? '#fff' : '#2196F3'};
|
|
189
|
+
border: 2px solid #2196F3;
|
|
190
|
+
border-radius: 4px;
|
|
191
|
+
cursor: pointer;
|
|
192
|
+
font-weight: bold;
|
|
193
|
+
"
|
|
194
|
+
>
|
|
195
|
+
Load Users
|
|
196
|
+
</button>
|
|
197
|
+
${when(
|
|
198
|
+
state.selectedUserId,
|
|
199
|
+
() => html`
|
|
200
|
+
<button
|
|
201
|
+
id="back-to-users-btn"
|
|
202
|
+
style="
|
|
203
|
+
padding: 12px 24px;
|
|
204
|
+
background: #FF9800;
|
|
205
|
+
color: white;
|
|
206
|
+
border: none;
|
|
207
|
+
border-radius: 4px;
|
|
208
|
+
cursor: pointer;
|
|
209
|
+
"
|
|
210
|
+
>
|
|
211
|
+
← Back to Users
|
|
212
|
+
</button>
|
|
213
|
+
`
|
|
214
|
+
)}
|
|
215
|
+
</div>
|
|
216
|
+
|
|
217
|
+
<!-- Search Bar (only for users view) -->
|
|
218
|
+
${when(
|
|
219
|
+
state.view === 'users' && state.users.length > 0,
|
|
220
|
+
() => html`
|
|
221
|
+
<div style="margin: 20px 0;">
|
|
222
|
+
<input
|
|
223
|
+
type="text"
|
|
224
|
+
id="search-input"
|
|
225
|
+
placeholder="Search users by name or email..."
|
|
226
|
+
value="${state.searchQuery}"
|
|
227
|
+
style="
|
|
228
|
+
width: 100%;
|
|
229
|
+
padding: 12px;
|
|
230
|
+
border: 2px solid #ddd;
|
|
231
|
+
border-radius: 4px;
|
|
232
|
+
font-size: 16px;
|
|
233
|
+
"
|
|
234
|
+
/>
|
|
235
|
+
</div>
|
|
236
|
+
`
|
|
237
|
+
)}
|
|
238
|
+
|
|
239
|
+
<!-- Loading State -->
|
|
240
|
+
${when(
|
|
241
|
+
state.loading,
|
|
242
|
+
() => LoadingSpinner()
|
|
243
|
+
)}
|
|
244
|
+
|
|
245
|
+
<!-- Error State -->
|
|
246
|
+
${when(
|
|
247
|
+
state.error,
|
|
248
|
+
() => ErrorMessage(state.error)
|
|
249
|
+
)}
|
|
250
|
+
|
|
251
|
+
<!-- Users Grid -->
|
|
252
|
+
${when(
|
|
253
|
+
state.view === 'users' && !state.loading && !state.error,
|
|
254
|
+
() => html`
|
|
255
|
+
<div>
|
|
256
|
+
${when(
|
|
257
|
+
filteredUsers.length > 0,
|
|
258
|
+
() => html`
|
|
259
|
+
<div style="
|
|
260
|
+
display: grid;
|
|
261
|
+
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
|
262
|
+
gap: 20px;
|
|
263
|
+
">
|
|
264
|
+
${each(filteredUsers, (user) => UserCard(user))}
|
|
265
|
+
</div>
|
|
266
|
+
`,
|
|
267
|
+
() => html`
|
|
268
|
+
<p style="text-align: center; color: #999; padding: 40px;">
|
|
269
|
+
${state.searchQuery ? 'No users found matching your search.' : 'No users loaded. Click "Load Users" to fetch data.'}
|
|
270
|
+
</p>
|
|
271
|
+
`
|
|
272
|
+
)}
|
|
273
|
+
</div>
|
|
274
|
+
`
|
|
275
|
+
)}
|
|
276
|
+
|
|
277
|
+
<!-- Posts View -->
|
|
278
|
+
${when(
|
|
279
|
+
state.view === 'posts' && !state.loading && !state.error,
|
|
280
|
+
() => html`
|
|
281
|
+
<div>
|
|
282
|
+
<h2 style="color: #333; margin-bottom: 20px;">
|
|
283
|
+
User Posts (${state.posts.length})
|
|
284
|
+
</h2>
|
|
285
|
+
${when(
|
|
286
|
+
state.posts.length > 0,
|
|
287
|
+
() => html`
|
|
288
|
+
<div>
|
|
289
|
+
${each(state.posts, (post) => PostCard(post))}
|
|
290
|
+
</div>
|
|
291
|
+
`,
|
|
292
|
+
() => html`
|
|
293
|
+
<p style="text-align: center; color: #999; padding: 40px;">
|
|
294
|
+
No posts found for this user.
|
|
295
|
+
</p>
|
|
296
|
+
`
|
|
297
|
+
)}
|
|
298
|
+
</div>
|
|
299
|
+
`
|
|
300
|
+
)}
|
|
301
|
+
</div>
|
|
302
|
+
`;
|
|
303
|
+
|
|
304
|
+
// Debounced search handler
|
|
305
|
+
const handleSearch = debounce((e) => {
|
|
306
|
+
state.searchQuery = e.target.value;
|
|
307
|
+
}, 300);
|
|
308
|
+
|
|
309
|
+
const events = {
|
|
310
|
+
'#load-users-btn': {
|
|
311
|
+
click: async () => {
|
|
312
|
+
state.loading = true;
|
|
313
|
+
state.error = null;
|
|
314
|
+
state.view = 'users';
|
|
315
|
+
state.selectedUserId = null;
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
const { data } = await api.get('/users');
|
|
319
|
+
state.users = data;
|
|
320
|
+
} catch (error) {
|
|
321
|
+
state.error = 'Failed to load users. Please try again.';
|
|
322
|
+
} finally {
|
|
323
|
+
state.loading = false;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
'.view-posts-btn': {
|
|
328
|
+
click: async (e) => {
|
|
329
|
+
const userId = e.target.dataset.userId;
|
|
330
|
+
state.selectedUserId = userId;
|
|
331
|
+
state.loading = true;
|
|
332
|
+
state.error = null;
|
|
333
|
+
state.view = 'posts';
|
|
334
|
+
|
|
335
|
+
try {
|
|
336
|
+
const { data } = await api.get(`/posts?userId=${userId}`);
|
|
337
|
+
state.posts = data;
|
|
338
|
+
} catch (error) {
|
|
339
|
+
state.error = 'Failed to load posts. Please try again.';
|
|
340
|
+
} finally {
|
|
341
|
+
state.loading = false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
'#back-to-users-btn': {
|
|
346
|
+
click: () => {
|
|
347
|
+
state.view = 'users';
|
|
348
|
+
state.selectedUserId = null;
|
|
349
|
+
state.posts = [];
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
'#search-input': {
|
|
353
|
+
input: handleSearch
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
return { element, events };
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
onMount: (state) => {
|
|
361
|
+
console.log('✅ API Example App mounted!');
|
|
362
|
+
// Auto-load users on mount
|
|
363
|
+
document.getElementById('load-users-btn')?.click();
|
|
364
|
+
}
|
|
365
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { app, html } from '../../src/index.js';
|
|
2
|
+
import { local } from '../../src/plugins/storage.js';
|
|
3
|
+
|
|
4
|
+
const saved = local.get('counterState', {});
|
|
5
|
+
|
|
6
|
+
app.human({
|
|
7
|
+
state: {
|
|
8
|
+
count: saved.count ?? 0,
|
|
9
|
+
step: saved.step ?? 1,
|
|
10
|
+
history: saved.history ?? []
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
derived: {
|
|
14
|
+
doubled: (state) => state.count * 2
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
actions: {
|
|
18
|
+
increment({ state, args }) {
|
|
19
|
+
const value = Number(args[0] ?? state.step);
|
|
20
|
+
state.count += value;
|
|
21
|
+
remember(state, `Added ${value}`, value);
|
|
22
|
+
},
|
|
23
|
+
decrement({ state, args }) {
|
|
24
|
+
const value = Number(args[0] ?? state.step);
|
|
25
|
+
state.count -= value;
|
|
26
|
+
remember(state, `Removed ${value}`, -value);
|
|
27
|
+
},
|
|
28
|
+
setStep({ state, args }) {
|
|
29
|
+
state.step = Number(args[0]);
|
|
30
|
+
persist(state);
|
|
31
|
+
},
|
|
32
|
+
reset({ state }) {
|
|
33
|
+
const previous = state.count;
|
|
34
|
+
state.count = 0;
|
|
35
|
+
remember(state, 'Reset counter', -previous);
|
|
36
|
+
},
|
|
37
|
+
clearHistory({ state }) {
|
|
38
|
+
state.history = [];
|
|
39
|
+
persist(state);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
render: ({ state }) => html`
|
|
44
|
+
<div style="min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at top, #fff7ed 0%, #f8fafc 55%, #e2e8f0 100%);">
|
|
45
|
+
<section style="width: min(100%, 720px); border-radius: 28px; overflow: hidden; background: rgba(255,255,255,0.92); box-shadow: 0 24px 80px rgba(15, 23, 42, 0.14); border: 1px solid rgba(148, 163, 184, 0.2);">
|
|
46
|
+
<div style="padding: 28px; background: linear-gradient(135deg, #0f172a, #1e293b); color: white;">
|
|
47
|
+
<p style="margin: 0; text-transform: uppercase; letter-spacing: 0.18em; font-size: 12px; color: rgba(255,255,255,0.7);">Human Counter</p>
|
|
48
|
+
<h1 style="margin: 12px 0 6px; font-size: clamp(32px, 6vw, 64px); line-height: 1;">${state.count}</h1>
|
|
49
|
+
<p style="margin: 0; color: rgba(255,255,255,0.72);">Double view: ${state.doubled}</p>
|
|
50
|
+
</div>
|
|
51
|
+
|
|
52
|
+
<div style="padding: 28px; display: grid; gap: 22px;">
|
|
53
|
+
<div style="display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px;">
|
|
54
|
+
<button data-click="decrement" style="${button('#e11d48')}">- ${state.step}</button>
|
|
55
|
+
<button data-click="reset" style="${button('#64748b')}">Reset</button>
|
|
56
|
+
<button data-click="increment" style="${button('#16a34a')}">+ ${state.step}</button>
|
|
57
|
+
</div>
|
|
58
|
+
|
|
59
|
+
<div style="display: grid; gap: 10px;">
|
|
60
|
+
<div style="display: flex; justify-content: space-between; align-items: center;">
|
|
61
|
+
<strong style="font-size: 14px; color: #0f172a;">Step size</strong>
|
|
62
|
+
<span style="font-size: 14px; color: #475569;">Current: ${state.step}</span>
|
|
63
|
+
</div>
|
|
64
|
+
<div style="display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px;">
|
|
65
|
+
${[1, 5, 10, 100].map((step) => html`
|
|
66
|
+
<button
|
|
67
|
+
data-click="setStep"
|
|
68
|
+
data-click-args='[${step}]'
|
|
69
|
+
style="${chip(step === state.step)}"
|
|
70
|
+
>
|
|
71
|
+
${step}
|
|
72
|
+
</button>
|
|
73
|
+
`)}
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
|
|
77
|
+
<div style="padding: 18px; border-radius: 18px; background: #f8fafc; border: 1px solid #e2e8f0;">
|
|
78
|
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px;">
|
|
79
|
+
<strong style="color: #0f172a;">Recent moves</strong>
|
|
80
|
+
<button data-click="clearHistory" style="background: transparent; border: none; color: #ea580c; font-weight: 600;">Clear</button>
|
|
81
|
+
</div>
|
|
82
|
+
${state.history.length === 0 ? html`
|
|
83
|
+
<p style="margin: 0; color: #64748b;">No history yet. Tap a button and the framework rerenders automatically.</p>
|
|
84
|
+
` : html`
|
|
85
|
+
<div style="display: grid; gap: 8px;">
|
|
86
|
+
${state.history.slice(-8).reverse().map((item) => html`
|
|
87
|
+
<div style="display: flex; justify-content: space-between; gap: 12px; padding: 10px 12px; border-radius: 14px; background: white;">
|
|
88
|
+
<span style="color: #334155;">${item.action}</span>
|
|
89
|
+
<strong style="color: ${item.value >= 0 ? '#16a34a' : '#e11d48'};">
|
|
90
|
+
${item.value >= 0 ? '+' : ''}${item.value}
|
|
91
|
+
</strong>
|
|
92
|
+
</div>
|
|
93
|
+
`)}
|
|
94
|
+
</div>
|
|
95
|
+
`}
|
|
96
|
+
</div>
|
|
97
|
+
</div>
|
|
98
|
+
</section>
|
|
99
|
+
</div>
|
|
100
|
+
`
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
function remember(state, action, value) {
|
|
104
|
+
state.history = [
|
|
105
|
+
...state.history,
|
|
106
|
+
{ action, value, timestamp: Date.now() }
|
|
107
|
+
].slice(-50);
|
|
108
|
+
|
|
109
|
+
persist(state);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function persist(state) {
|
|
113
|
+
local.set('counterState', {
|
|
114
|
+
count: state.count,
|
|
115
|
+
step: state.step,
|
|
116
|
+
history: state.history
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function button(background) {
|
|
121
|
+
return [
|
|
122
|
+
'padding: 16px 18px',
|
|
123
|
+
'border: none',
|
|
124
|
+
'border-radius: 18px',
|
|
125
|
+
`background: ${background}`,
|
|
126
|
+
'color: white',
|
|
127
|
+
'font-size: 16px',
|
|
128
|
+
'font-weight: 700'
|
|
129
|
+
].join('; ');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function chip(active) {
|
|
133
|
+
return [
|
|
134
|
+
'padding: 12px',
|
|
135
|
+
'border-radius: 16px',
|
|
136
|
+
`border: 1px solid ${active ? '#0f172a' : '#cbd5e1'}`,
|
|
137
|
+
`background: ${active ? '#0f172a' : 'white'}`,
|
|
138
|
+
`color: ${active ? 'white' : '#334155'}`,
|
|
139
|
+
'font-weight: 600'
|
|
140
|
+
].join('; ');
|
|
141
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---state
|
|
2
|
+
{
|
|
3
|
+
count: 0,
|
|
4
|
+
step: 1
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
---template
|
|
8
|
+
<main style="min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(180deg, #f8fafc 0%, #e0f2fe 100%);">
|
|
9
|
+
<section style="width: min(100%, 560px); background: white; border-radius: 28px; padding: 28px; box-shadow: 0 24px 80px rgba(15, 23, 42, 0.12); display: grid; gap: 18px;">
|
|
10
|
+
<p style="margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.18em; color: #64748b;">simple .human app</p>
|
|
11
|
+
<h1 style="margin: 0; font-size: clamp(40px, 10vw, 72px); line-height: 1; color: #0f172a;">{count}</h1>
|
|
12
|
+
<p style="margin: 0; color: #475569;">Step: {step}</p>
|
|
13
|
+
|
|
14
|
+
<div style="display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px;">
|
|
15
|
+
<button @click="count -= step" style="padding: 16px; border: none; border-radius: 18px; background: #e11d48; color: white; font-weight: 700;">-</button>
|
|
16
|
+
<button @click="count = 0" style="padding: 16px; border: none; border-radius: 18px; background: #64748b; color: white; font-weight: 700;">reset</button>
|
|
17
|
+
<button @click="count += step" style="padding: 16px; border: none; border-radius: 18px; background: #2563eb; color: white; font-weight: 700;">+</button>
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
<div style="display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px;">
|
|
21
|
+
<button @click="step = 1" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: {step === 1 ? '#0f172a' : 'white'}; color: {step === 1 ? 'white' : '#334155'};">1</button>
|
|
22
|
+
<button @click="step = 5" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: {step === 5 ? '#0f172a' : 'white'}; color: {step === 5 ? 'white' : '#334155'};">5</button>
|
|
23
|
+
<button @click="step = 10" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: {step === 10 ? '#0f172a' : 'white'}; color: {step === 10 ? 'white' : '#334155'};">10</button>
|
|
24
|
+
<button @click="step = 100" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: {step === 100 ? '#0f172a' : 'white'}; color: {step === 100 ? 'white' : '#334155'};">100</button>
|
|
25
|
+
</div>
|
|
26
|
+
</section>
|
|
27
|
+
</main>
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { app, html } from '../../src/index.js';
|
|
2
|
+
|
|
3
|
+
function __humanExpr(state, actions, code) {
|
|
4
|
+
return new Function('state', 'actions', `with (state) { return (${code}); }`)(state, actions);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function __humanRun(state, actions, event, el, args, code) {
|
|
8
|
+
return new Function(
|
|
9
|
+
'state',
|
|
10
|
+
'actions',
|
|
11
|
+
'event',
|
|
12
|
+
'el',
|
|
13
|
+
'args',
|
|
14
|
+
`with (state) { ${code}; }`
|
|
15
|
+
)(state, actions, event, el, args);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
app.human({
|
|
19
|
+
state: {
|
|
20
|
+
count: 0,
|
|
21
|
+
step: 1
|
|
22
|
+
},
|
|
23
|
+
derived: {},
|
|
24
|
+
actions: {
|
|
25
|
+
|
|
26
|
+
__inline_1({ state, actions, event, el, args }) {
|
|
27
|
+
return __humanRun(state, actions, event, el, args, "count -= step");
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
__inline_2({ state, actions, event, el, args }) {
|
|
31
|
+
return __humanRun(state, actions, event, el, args, "count = 0");
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
__inline_3({ state, actions, event, el, args }) {
|
|
35
|
+
return __humanRun(state, actions, event, el, args, "count += step");
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
__inline_4({ state, actions, event, el, args }) {
|
|
39
|
+
return __humanRun(state, actions, event, el, args, "step = 1");
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
__inline_5({ state, actions, event, el, args }) {
|
|
43
|
+
return __humanRun(state, actions, event, el, args, "step = 5");
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
__inline_6({ state, actions, event, el, args }) {
|
|
47
|
+
return __humanRun(state, actions, event, el, args, "step = 10");
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
__inline_7({ state, actions, event, el, args }) {
|
|
51
|
+
return __humanRun(state, actions, event, el, args, "step = 100");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
},
|
|
55
|
+
render: ({ state, actions }) => html`
|
|
56
|
+
<main style="min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(180deg, #f8fafc 0%, #e0f2fe 100%);">
|
|
57
|
+
<section style="width: min(100%, 560px); background: white; border-radius: 28px; padding: 28px; box-shadow: 0 24px 80px rgba(15, 23, 42, 0.12); display: grid; gap: 18px;">
|
|
58
|
+
<p style="margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.18em; color: #64748b;">simple .human app</p>
|
|
59
|
+
<h1 style="margin: 0; font-size: clamp(40px, 10vw, 72px); line-height: 1; color: #0f172a;">${__humanExpr(state, actions, "count")}</h1>
|
|
60
|
+
<p style="margin: 0; color: #475569;">Step: ${__humanExpr(state, actions, "step")}</p>
|
|
61
|
+
|
|
62
|
+
<div style="display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px;">
|
|
63
|
+
<button data-click="__inline_1" style="padding: 16px; border: none; border-radius: 18px; background: #e11d48; color: white; font-weight: 700;">-</button>
|
|
64
|
+
<button data-click="__inline_2" style="padding: 16px; border: none; border-radius: 18px; background: #64748b; color: white; font-weight: 700;">reset</button>
|
|
65
|
+
<button data-click="__inline_3" style="padding: 16px; border: none; border-radius: 18px; background: #2563eb; color: white; font-weight: 700;">+</button>
|
|
66
|
+
</div>
|
|
67
|
+
|
|
68
|
+
<div style="display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px;">
|
|
69
|
+
<button data-click="__inline_4" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: ${__humanExpr(state, actions, "step === 1 ? '#0f172a' : 'white'")}; color: ${__humanExpr(state, actions, "step === 1 ? 'white' : '#334155'")};">1</button>
|
|
70
|
+
<button data-click="__inline_5" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: ${__humanExpr(state, actions, "step === 5 ? '#0f172a' : 'white'")}; color: ${__humanExpr(state, actions, "step === 5 ? 'white' : '#334155'")};">5</button>
|
|
71
|
+
<button data-click="__inline_6" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: ${__humanExpr(state, actions, "step === 10 ? '#0f172a' : 'white'")}; color: ${__humanExpr(state, actions, "step === 10 ? 'white' : '#334155'")};">10</button>
|
|
72
|
+
<button data-click="__inline_7" style="padding: 12px; border-radius: 16px; border: 1px solid #cbd5e1; background: ${__humanExpr(state, actions, "step === 100 ? '#0f172a' : 'white'")}; color: ${__humanExpr(state, actions, "step === 100 ? 'white' : '#334155'")};">100</button>
|
|
73
|
+
</div>
|
|
74
|
+
</section>
|
|
75
|
+
</main>
|
|
76
|
+
`
|
|
77
|
+
});
|