lego-dom 0.0.7 → 0.0.8

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.
Files changed (143) hide show
  1. package/.github/workflows/deploy-docs.yml +56 -0
  2. package/LICENSE +21 -0
  3. package/README.md +122 -0
  4. package/docs/.vitepress/config.js +107 -0
  5. package/docs/.vitepress/dist/404.html +22 -0
  6. package/docs/.vitepress/dist/api/define.html +35 -0
  7. package/docs/.vitepress/dist/api/directives.html +32 -0
  8. package/docs/.vitepress/dist/api/globals.html +27 -0
  9. package/docs/.vitepress/dist/api/index.html +25 -0
  10. package/docs/.vitepress/dist/api/lifecycle.html +38 -0
  11. package/docs/.vitepress/dist/api/route.html +34 -0
  12. package/docs/.vitepress/dist/api/vite-plugin.html +37 -0
  13. package/docs/.vitepress/dist/assets/api_define.md.UA-ygUnQ.js +11 -0
  14. package/docs/.vitepress/dist/assets/api_define.md.UA-ygUnQ.lean.js +1 -0
  15. package/docs/.vitepress/dist/assets/api_directives.md.BV-D251p.js +8 -0
  16. package/docs/.vitepress/dist/assets/api_directives.md.BV-D251p.lean.js +1 -0
  17. package/docs/.vitepress/dist/assets/api_globals.md.DOjt7AV0.js +3 -0
  18. package/docs/.vitepress/dist/assets/api_globals.md.DOjt7AV0.lean.js +1 -0
  19. package/docs/.vitepress/dist/assets/api_index.md.OS6h01ct.js +1 -0
  20. package/docs/.vitepress/dist/assets/api_index.md.OS6h01ct.lean.js +1 -0
  21. package/docs/.vitepress/dist/assets/api_lifecycle.md.Ccm5xw6-.js +14 -0
  22. package/docs/.vitepress/dist/assets/api_lifecycle.md.Ccm5xw6-.lean.js +1 -0
  23. package/docs/.vitepress/dist/assets/api_route.md.CAHf_KNp.js +10 -0
  24. package/docs/.vitepress/dist/assets/api_route.md.CAHf_KNp.lean.js +1 -0
  25. package/docs/.vitepress/dist/assets/api_vite-plugin.md.DNn9VhL5.js +13 -0
  26. package/docs/.vitepress/dist/assets/api_vite-plugin.md.DNn9VhL5.lean.js +1 -0
  27. package/docs/.vitepress/dist/assets/app.BG5s3B0P.js +1 -0
  28. package/docs/.vitepress/dist/assets/chunks/@localSearchIndexroot.DQmuWC2Z.js +1 -0
  29. package/docs/.vitepress/dist/assets/chunks/VPLocalSearchBox.BO-PSxt1.js +9 -0
  30. package/docs/.vitepress/dist/assets/chunks/framework.B7OFBR9X.js +19 -0
  31. package/docs/.vitepress/dist/assets/chunks/theme.DA-iSa9B.js +2 -0
  32. package/docs/.vitepress/dist/assets/examples_form.md.B3stGKbu.js +34 -0
  33. package/docs/.vitepress/dist/assets/examples_form.md.B3stGKbu.lean.js +1 -0
  34. package/docs/.vitepress/dist/assets/examples_index.md.BDEG_D4J.js +30 -0
  35. package/docs/.vitepress/dist/assets/examples_index.md.BDEG_D4J.lean.js +1 -0
  36. package/docs/.vitepress/dist/assets/examples_routing.md.bqZ9DjDK.js +338 -0
  37. package/docs/.vitepress/dist/assets/examples_routing.md.bqZ9DjDK.lean.js +1 -0
  38. package/docs/.vitepress/dist/assets/examples_sfc-showcase.md.DLXaUiop.js +13 -0
  39. package/docs/.vitepress/dist/assets/examples_sfc-showcase.md.DLXaUiop.lean.js +1 -0
  40. package/docs/.vitepress/dist/assets/examples_todo-app.md.D5RhZoo5.js +297 -0
  41. package/docs/.vitepress/dist/assets/examples_todo-app.md.D5RhZoo5.lean.js +1 -0
  42. package/docs/.vitepress/dist/assets/guide_cdn-usage.md.CAjf03Lr.js +182 -0
  43. package/docs/.vitepress/dist/assets/guide_cdn-usage.md.CAjf03Lr.lean.js +1 -0
  44. package/docs/.vitepress/dist/assets/guide_components.md.BIFWF1Hc.js +174 -0
  45. package/docs/.vitepress/dist/assets/guide_components.md.BIFWF1Hc.lean.js +1 -0
  46. package/docs/.vitepress/dist/assets/guide_contributing.md.BgbUN-Mr.js +1 -0
  47. package/docs/.vitepress/dist/assets/guide_contributing.md.BgbUN-Mr.lean.js +1 -0
  48. package/docs/.vitepress/dist/assets/guide_directives.md.Bi3ynu1d.js +140 -0
  49. package/docs/.vitepress/dist/assets/guide_directives.md.Bi3ynu1d.lean.js +1 -0
  50. package/docs/.vitepress/dist/assets/guide_getting-started.md.2Nr1lp2z.js +107 -0
  51. package/docs/.vitepress/dist/assets/guide_getting-started.md.2Nr1lp2z.lean.js +1 -0
  52. package/docs/.vitepress/dist/assets/guide_index.md.GvZq_Yf2.js +2 -0
  53. package/docs/.vitepress/dist/assets/guide_index.md.GvZq_Yf2.lean.js +1 -0
  54. package/docs/.vitepress/dist/assets/guide_lifecycle.md.B28j1OzS.js +304 -0
  55. package/docs/.vitepress/dist/assets/guide_lifecycle.md.B28j1OzS.lean.js +1 -0
  56. package/docs/.vitepress/dist/assets/guide_quick-start.md.CNk3VGTF.js +33 -0
  57. package/docs/.vitepress/dist/assets/guide_quick-start.md.CNk3VGTF.lean.js +1 -0
  58. package/docs/.vitepress/dist/assets/guide_reactivity.md.CVsaMaPv.js +135 -0
  59. package/docs/.vitepress/dist/assets/guide_reactivity.md.CVsaMaPv.lean.js +1 -0
  60. package/docs/.vitepress/dist/assets/guide_routing.md.DSpDP25o.js +193 -0
  61. package/docs/.vitepress/dist/assets/guide_routing.md.DSpDP25o.lean.js +1 -0
  62. package/docs/.vitepress/dist/assets/guide_sfc.md.CVUP66tS.js +187 -0
  63. package/docs/.vitepress/dist/assets/guide_sfc.md.CVUP66tS.lean.js +1 -0
  64. package/docs/.vitepress/dist/assets/guide_templating.md.BgCGe4aa.js +119 -0
  65. package/docs/.vitepress/dist/assets/guide_templating.md.BgCGe4aa.lean.js +1 -0
  66. package/docs/.vitepress/dist/assets/index.md.xV1taCED.js +23 -0
  67. package/docs/.vitepress/dist/assets/index.md.xV1taCED.lean.js +1 -0
  68. package/docs/.vitepress/dist/assets/inter-italic-cyrillic-ext.r48I6akx.woff2 +0 -0
  69. package/docs/.vitepress/dist/assets/inter-italic-cyrillic.By2_1cv3.woff2 +0 -0
  70. package/docs/.vitepress/dist/assets/inter-italic-greek-ext.1u6EdAuj.woff2 +0 -0
  71. package/docs/.vitepress/dist/assets/inter-italic-greek.DJ8dCoTZ.woff2 +0 -0
  72. package/docs/.vitepress/dist/assets/inter-italic-latin-ext.CN1xVJS-.woff2 +0 -0
  73. package/docs/.vitepress/dist/assets/inter-italic-latin.C2AdPX0b.woff2 +0 -0
  74. package/docs/.vitepress/dist/assets/inter-italic-vietnamese.BSbpV94h.woff2 +0 -0
  75. package/docs/.vitepress/dist/assets/inter-roman-cyrillic-ext.BBPuwvHQ.woff2 +0 -0
  76. package/docs/.vitepress/dist/assets/inter-roman-cyrillic.C5lxZ8CY.woff2 +0 -0
  77. package/docs/.vitepress/dist/assets/inter-roman-greek-ext.CqjqNYQ-.woff2 +0 -0
  78. package/docs/.vitepress/dist/assets/inter-roman-greek.BBVDIX6e.woff2 +0 -0
  79. package/docs/.vitepress/dist/assets/inter-roman-latin-ext.4ZJIpNVo.woff2 +0 -0
  80. package/docs/.vitepress/dist/assets/inter-roman-latin.Di8DUHzh.woff2 +0 -0
  81. package/docs/.vitepress/dist/assets/inter-roman-vietnamese.BjW4sHH5.woff2 +0 -0
  82. package/docs/.vitepress/dist/assets/style.eycE2Jhw.css +1 -0
  83. package/docs/.vitepress/dist/examples/form.html +58 -0
  84. package/docs/.vitepress/dist/examples/index.html +368 -0
  85. package/docs/.vitepress/dist/examples/routing.html +362 -0
  86. package/docs/.vitepress/dist/examples/sfc-showcase.html +37 -0
  87. package/docs/.vitepress/dist/examples/todo-app.html +321 -0
  88. package/docs/.vitepress/dist/guide/cdn-usage.html +206 -0
  89. package/docs/.vitepress/dist/guide/components.html +198 -0
  90. package/docs/.vitepress/dist/guide/contributing.html +25 -0
  91. package/docs/.vitepress/dist/guide/directives.html +164 -0
  92. package/docs/.vitepress/dist/guide/getting-started.html +131 -0
  93. package/docs/.vitepress/dist/guide/index.html +26 -0
  94. package/docs/.vitepress/dist/guide/lifecycle.html +328 -0
  95. package/docs/.vitepress/dist/guide/quick-start.html +57 -0
  96. package/docs/.vitepress/dist/guide/reactivity.html +159 -0
  97. package/docs/.vitepress/dist/guide/routing.html +217 -0
  98. package/docs/.vitepress/dist/guide/sfc.html +211 -0
  99. package/docs/.vitepress/dist/guide/templating.html +143 -0
  100. package/docs/.vitepress/dist/hashmap.json +1 -0
  101. package/docs/.vitepress/dist/index.html +47 -0
  102. package/docs/.vitepress/dist/logo.svg +38 -0
  103. package/docs/.vitepress/dist/vp-icons.css +1 -0
  104. package/docs/api/define.md +31 -0
  105. package/docs/api/directives.md +42 -0
  106. package/docs/api/globals.md +29 -0
  107. package/docs/api/index.md +29 -0
  108. package/docs/api/lifecycle.md +40 -0
  109. package/docs/api/route.md +37 -0
  110. package/docs/api/vite-plugin.md +58 -0
  111. package/docs/examples/form.md +42 -0
  112. package/docs/examples/index.md +104 -0
  113. package/docs/examples/routing.md +409 -0
  114. package/docs/examples/sfc-showcase.md +34 -0
  115. package/docs/examples/todo-app.md +383 -0
  116. package/docs/guide/cdn-usage.md +320 -0
  117. package/docs/guide/components.md +394 -0
  118. package/docs/guide/contributing.md +32 -0
  119. package/docs/guide/directives.md +430 -0
  120. package/docs/guide/getting-started.md +233 -0
  121. package/docs/guide/index.md +88 -0
  122. package/docs/guide/lifecycle.md +493 -0
  123. package/docs/guide/quick-start.md +46 -0
  124. package/docs/guide/reactivity.md +394 -0
  125. package/docs/guide/routing.md +373 -0
  126. package/docs/guide/sfc.md +381 -0
  127. package/docs/guide/templating.md +383 -0
  128. package/docs/index.md +126 -0
  129. package/docs/public/logo.svg +38 -0
  130. package/examples/vite-app/README.md +71 -0
  131. package/examples/vite-app/index.html +45 -0
  132. package/examples/vite-app/package.json +16 -0
  133. package/examples/vite-app/src/components/greeting-card.lego +41 -0
  134. package/examples/vite-app/src/components/sample-component.lego +75 -0
  135. package/examples/vite-app/src/main.js +11 -0
  136. package/examples/vite-app/vite.config.js +16 -0
  137. package/examples.js +99 -0
  138. package/package.json +33 -5
  139. package/parse-lego.js +119 -0
  140. package/parse-lego.test.js +107 -0
  141. package/vite-plugin.js +133 -0
  142. package/.ignore/auto.html +0 -135
  143. package/.ignore/test.html +0 -73
@@ -0,0 +1,381 @@
1
+ # Single File Components (.lego)
2
+
3
+ Single File Components (SFCs) let you define components in dedicated `.lego` files when using Vite as your build tool.
4
+
5
+ ## Why SFCs?
6
+
7
+ When your project grows, keeping
8
+
9
+ components in separate files makes your codebase more organized and maintainable.
10
+
11
+ ### Benefits
12
+
13
+ ✅ **Better Organization** - One file per component
14
+ ✅ **Syntax Highlighting** - Proper editor support
15
+ ✅ **Auto-discovery** - Vite plugin finds and registers components automatically
16
+ ✅ **Hot Reload** - Changes reflect instantly during development
17
+ ✅ **Familiar Format** - Similar to Vue SFCs if you've used them
18
+
19
+ ## File Format
20
+
21
+ A `.lego` file contains three optional sections:
22
+
23
+ ```html
24
+ <template>
25
+ <!-- Your component markup -->
26
+ </template>
27
+
28
+ <script>
29
+ // Your component logic
30
+ export default {
31
+ // reactive state and methods
32
+ }
33
+ </script>
34
+
35
+ <style>
36
+ /* Scoped styles */
37
+ </style>
38
+ ```
39
+
40
+ ### Example Component
41
+
42
+ Here's a complete example (`user-card.lego`):
43
+
44
+ ```html
45
+ <template>
46
+ <style>
47
+ self {
48
+ display: block;
49
+ padding: 1.5rem;
50
+ border: 2px solid #ddd;
51
+ border-radius: 8px;
52
+ max-width: 300px;
53
+ }
54
+
55
+ .avatar {
56
+ width: 80px;
57
+ height: 80px;
58
+ border-radius: 50%;
59
+ object-fit: cover;
60
+ }
61
+
62
+ .name {
63
+ font-size: 1.5rem;
64
+ font-weight: bold;
65
+ margin: 0.5rem 0;
66
+ }
67
+
68
+ .bio {
69
+ color: #666;
70
+ margin: 0.5rem 0;
71
+ }
72
+
73
+ button {
74
+ background: #4CAF50;
75
+ color: white;
76
+ border: none;
77
+ padding: 0.5rem 1rem;
78
+ border-radius: 4px;
79
+ cursor: pointer;
80
+ }
81
+ </style>
82
+
83
+ <img class="avatar" src="{{ avatarUrl }}" alt="{{ name }}">
84
+ <h2 class="name">{{ name }}</h2>
85
+ <p class="bio">{{ bio }}</p>
86
+ <p>Followers: {{ followers }}</p>
87
+ <button @click="follow()">
88
+ {{ isFollowing ? 'Unfollow' : 'Follow' }}
89
+ </button>
90
+ </template>
91
+
92
+ <script>
93
+ export default {
94
+ name: 'John Doe',
95
+ bio: 'Web developer & coffee enthusiast',
96
+ avatarUrl: 'https://api.dicebear.com/7.x/avataaars/svg?seed=John',
97
+ followers: 1234,
98
+ isFollowing: false,
99
+
100
+ follow() {
101
+ if (this.isFollowing) {
102
+ this.followers--;
103
+ this.isFollowing = false;
104
+ } else {
105
+ this.followers++;
106
+ this.isFollowing = true;
107
+ }
108
+ }
109
+ }
110
+ </script>
111
+ ```
112
+
113
+ ## Vite Plugin Setup
114
+
115
+ ### Installation
116
+
117
+ ```bash
118
+ npm install -D vite lego-dom
119
+ ```
120
+
121
+ ### Configuration
122
+
123
+ Create or update `vite.config.js`:
124
+
125
+ ```js
126
+ import { defineConfig } from 'vite';
127
+ import legoPlugin from 'lego-dom/vite-plugin';
128
+
129
+ export default defineConfig({
130
+ plugins: [
131
+ legoPlugin({
132
+ componentsDir: './src/components', // Where to look
133
+ include: ['**/*.lego'] // What to match
134
+ })
135
+ ]
136
+ });
137
+ ```
138
+
139
+ ### Project Structure
140
+
141
+ ```
142
+ my-app/
143
+ ├── src/
144
+ │ ├── components/
145
+ │ │ ├── user-card.lego
146
+ │ │ ├── post-list.lego
147
+ │ │ └── comment-item.lego
148
+ │ └── main.js
149
+ ├── index.html
150
+ ├── package.json
151
+ └── vite.config.js
152
+ ```
153
+
154
+ ### Entry Point
155
+
156
+ In your `src/main.js`:
157
+
158
+ ```js
159
+ import { Lego } from 'lego-dom/main.js';
160
+ import registerComponents from 'virtual:lego-components';
161
+
162
+ // Auto-register all discovered components
163
+ registerComponents();
164
+
165
+ // Now all .lego components are available!
166
+ ```
167
+
168
+ ### Use Components
169
+
170
+ In your `index.html`:
171
+
172
+ ```html
173
+ <!DOCTYPE html>
174
+ <html>
175
+ <head>
176
+ <title>My LegoJS App</title>
177
+ </head>
178
+ <body>
179
+ <div id="app">
180
+ <user-card></user-card>
181
+ <post-list></post-list>
182
+ </div>
183
+
184
+ <script type="module" src="/src/main.js"></script>
185
+ </body>
186
+ </html>
187
+ ```
188
+
189
+ ## Component Naming
190
+
191
+ Component names are automatically derived from filenames:
192
+
193
+ | Filename | Component Tag |
194
+ |----------|--------------|
195
+ | `user-card.lego` | `<user-card>` |
196
+ | `post-list.lego` | `<post-list>` |
197
+ | `comment-item.lego` | `<comment-item>` |
198
+
199
+ ::: warning Naming Rules
200
+ Component names **must**:
201
+ - Be kebab-case (lowercase with hyphens)
202
+ - Contain at least one hyphen
203
+ - Match the pattern: `[a-z][a-z0-9]*(-[a-z0-9]+)+`
204
+
205
+ ✅ Good: `user-card`, `post-list`, `nav-menu`
206
+ ❌ Bad: `UserCard`, `postlist`, `card`
207
+ :::
208
+
209
+ ## Section Details
210
+
211
+ ### Template Section
212
+
213
+ Contains your component's HTML markup with LegoJS directives:
214
+
215
+ ```html
216
+ <template>
217
+ <h1>{{ title }}</h1>
218
+ <p b-if="showContent">{{ content }}</p>
219
+ <ul>
220
+ <li b-for="item in items">{{ item }}</li>
221
+ </ul>
222
+ </template>
223
+ ```
224
+
225
+ ### Script Section
226
+
227
+ Exports the component's reactive state and methods:
228
+
229
+ ```html
230
+ <script>
231
+ export default {
232
+ // Reactive properties
233
+ title: 'Welcome',
234
+ count: 0,
235
+ items: ['apple', 'banana'],
236
+
237
+ // Methods
238
+ increment() {
239
+ this.count++;
240
+ },
241
+
242
+ // Lifecycle hooks
243
+ mounted() {
244
+ console.log('Component mounted!');
245
+ }
246
+ }
247
+ </script>
248
+ ```
249
+
250
+ ### Style Section
251
+
252
+ Scoped styles using Shadow DOM. Use `self` to target the component root:
253
+
254
+ ```html
255
+ <style>
256
+ self {
257
+ display: block;
258
+ padding: 1rem;
259
+ }
260
+
261
+ h1 {
262
+ color: #333;
263
+ }
264
+
265
+ button {
266
+ background: blue;
267
+ color: white;
268
+ }
269
+ </style>
270
+ ```
271
+
272
+ Styles are automatically scoped to your component—they won't affect other components or global styles.
273
+
274
+ ## Hot Module Replacement
275
+
276
+ During development, changes to `.lego` files trigger a full page reload. Your changes appear instantly!
277
+
278
+ ```bash
279
+ npm run dev
280
+ ```
281
+
282
+ Edit your component, save, and see the result immediately.
283
+
284
+ ## Passing Props
285
+
286
+ Pass data to components via the `b-data` attribute:
287
+
288
+ ```html
289
+ <user-card b-data="{
290
+ name: 'Jane Smith',
291
+ bio: 'Designer',
292
+ followers: 5678
293
+ }"></user-card>
294
+ ```
295
+
296
+ Or define defaults in the script section and override as needed.
297
+
298
+ ## Best Practices
299
+
300
+ ### 1. Keep Components Small
301
+
302
+ Each `.lego` file should represent a single, focused component.
303
+
304
+ ✅ Good: `user-avatar.lego`, `user-name.lego`, `user-bio.lego`
305
+ ❌ Bad: `entire-profile-page.lego`
306
+
307
+ ### 2. Use Semantic Names
308
+
309
+ Name components after what they represent, not how they look:
310
+
311
+ ✅ Good: `article-preview.lego`, `comment-list.lego`
312
+ ❌ Bad: `blue-box.lego`, `flex-container.lego`
313
+
314
+ ### 3. Organize by Feature
315
+
316
+ ```
317
+ components/
318
+ ├── user/
319
+ │ ├── user-card.lego
320
+ │ ├── user-avatar.lego
321
+ │ └── user-profile.lego
322
+ ├── posts/
323
+ │ ├── post-item.lego
324
+ │ └── post-list.lego
325
+ └── shared/
326
+ ├── app-button.lego
327
+ └── app-modal.lego
328
+ ```
329
+
330
+ ## Limitations
331
+
332
+ - `.lego` files require Vite—they don't work with direct `<script>` tag inclusion
333
+ - Each file creates exactly one component
334
+ - Component name is derived from filename (cannot be customized)
335
+
336
+ ## Comparison: SFC vs Traditional
337
+
338
+ ### Traditional (HTML Template)
339
+
340
+ ```html
341
+ <template b-id="my-component">
342
+ <style>self { padding: 1rem; }</style>
343
+ <h1>{{ title }}</h1>
344
+ </template>
345
+
346
+ <my-component b-data="{ title: 'Hello' }"></my-component>
347
+ ```
348
+
349
+ ### SFC (.lego file)
350
+
351
+ ```html
352
+ <!-- my-component.lego -->
353
+ <template>
354
+ <h1>{{ title }}</h1>
355
+ </template>
356
+
357
+ <style>
358
+ self { padding: 1rem; }
359
+ </style>
360
+
361
+ <script>
362
+ export default {
363
+ title: 'Hello'
364
+ }
365
+ </script>
366
+ ```
367
+
368
+ ```html
369
+ <!-- index.html -->
370
+ <my-component></my-component>
371
+ ```
372
+
373
+ Both work perfectly! Choose based on your project needs:
374
+ - **Small projects / prototypes** → HTML templates
375
+ - **Medium/large projects** → SFCs with Vite
376
+
377
+ ## Next Steps
378
+
379
+ - See [complete SFC examples](/examples/sfc-showcase)
380
+ - Learn about the [Vite plugin API](/api/vite-plugin)
381
+ - Check out the [routing guide](/guide/routing) for building apps