@8wave/ai-elements 0.0.1-beta.1
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 +291 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# Web Component App
|
|
2
|
+
|
|
3
|
+
Applicazione per creare Web Components (Custom Elements) usando Vue 3.
|
|
4
|
+
|
|
5
|
+
## π Descrizione
|
|
6
|
+
|
|
7
|
+
Questa app permette di creare custom elements nativi del browser usando Vue 3. I componenti sono completamente autonomi e possono essere usati in qualsiasi progetto HTML, indipendentemente dal framework utilizzato.
|
|
8
|
+
|
|
9
|
+
## π Caratteristiche
|
|
10
|
+
|
|
11
|
+
- β
Web Components nativi del browser
|
|
12
|
+
- β
Costruiti con Vue 3
|
|
13
|
+
- β
Shadow DOM per incapsulamento degli stili
|
|
14
|
+
- β
Props reattive tramite attributi HTML
|
|
15
|
+
- β
Eventi custom personalizzati
|
|
16
|
+
- β
Bundle ottimizzato (ES modules e UMD)
|
|
17
|
+
- β
TypeScript support
|
|
18
|
+
|
|
19
|
+
## π¦ Struttura
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
web-component/
|
|
23
|
+
βββ src/
|
|
24
|
+
β βββ main.ts # Entry point - registra i custom elements
|
|
25
|
+
β βββ MyElement.ce.vue # Esempio di custom element
|
|
26
|
+
β βββ vite-env.d.ts # Type definitions
|
|
27
|
+
βββ index.html # Pagina demo per testare i componenti
|
|
28
|
+
βββ package.json
|
|
29
|
+
βββ tsconfig.json
|
|
30
|
+
βββ vite.config.ts # Configurazione Vite per custom elements
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## π οΈ Setup
|
|
34
|
+
|
|
35
|
+
### Installazione dipendenze
|
|
36
|
+
|
|
37
|
+
Dalla root del monorepo:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pnpm install
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Sviluppo
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# Dalla root del monorepo
|
|
47
|
+
pnpm --filter web-component dev
|
|
48
|
+
|
|
49
|
+
# Oppure
|
|
50
|
+
cd apps/web-component
|
|
51
|
+
pnpm dev
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Questo avvierΓ un server di sviluppo su `http://localhost:8082` con la pagina di demo.
|
|
55
|
+
|
|
56
|
+
### Build
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Dalla root del monorepo
|
|
60
|
+
pnpm --filter web-component build
|
|
61
|
+
|
|
62
|
+
# Oppure
|
|
63
|
+
cd apps/web-component
|
|
64
|
+
pnpm build
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Questo genererΓ i file ottimizzati nella cartella `dist/`:
|
|
68
|
+
- `web-components.es.js` - ES modules (raccomandato)
|
|
69
|
+
- `web-components.umd.js` - UMD format (per compatibilitΓ legacy)
|
|
70
|
+
|
|
71
|
+
## π Come creare un nuovo Custom Element
|
|
72
|
+
|
|
73
|
+
### 1. Crea un componente Vue con estensione `.ce.vue`
|
|
74
|
+
|
|
75
|
+
```vue
|
|
76
|
+
<!-- src/NewElement.ce.vue -->
|
|
77
|
+
<template>
|
|
78
|
+
<div class="new-element">
|
|
79
|
+
<h3>{{ title }}</h3>
|
|
80
|
+
<slot></slot>
|
|
81
|
+
</div>
|
|
82
|
+
</template>
|
|
83
|
+
|
|
84
|
+
<script setup lang="ts">
|
|
85
|
+
import { toRefs } from 'vue'
|
|
86
|
+
|
|
87
|
+
const props = defineProps<{
|
|
88
|
+
title?: string
|
|
89
|
+
}>()
|
|
90
|
+
|
|
91
|
+
const { title = 'Default Title' } = toRefs(props)
|
|
92
|
+
</script>
|
|
93
|
+
|
|
94
|
+
<style scoped>
|
|
95
|
+
.new-element {
|
|
96
|
+
padding: 20px;
|
|
97
|
+
border: 1px solid #ccc;
|
|
98
|
+
}
|
|
99
|
+
</style>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 2. Registra il custom element in `main.ts`
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { defineCustomElement } from 'vue'
|
|
106
|
+
import NewElement from './NewElement.ce.vue'
|
|
107
|
+
|
|
108
|
+
const NewElementCE = defineCustomElement(NewElement)
|
|
109
|
+
customElements.define('new-element', NewElementCE)
|
|
110
|
+
|
|
111
|
+
export { NewElementCE }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 3. Usa il custom element in HTML
|
|
115
|
+
|
|
116
|
+
```html
|
|
117
|
+
<script type="module" src="./dist/web-components.es.js"></script>
|
|
118
|
+
|
|
119
|
+
<new-element title="Hello World">
|
|
120
|
+
Questo Γ¨ il contenuto dello slot
|
|
121
|
+
</new-element>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## π― Uso nei Progetti
|
|
125
|
+
|
|
126
|
+
### In un progetto HTML statico
|
|
127
|
+
|
|
128
|
+
```html
|
|
129
|
+
<!DOCTYPE html>
|
|
130
|
+
<html>
|
|
131
|
+
<head>
|
|
132
|
+
<meta charset="UTF-8">
|
|
133
|
+
<title>My App</title>
|
|
134
|
+
</head>
|
|
135
|
+
<body>
|
|
136
|
+
<!-- Usa il custom element -->
|
|
137
|
+
<my-element
|
|
138
|
+
title="Benvenuto"
|
|
139
|
+
message="Questo Γ¨ un web component!">
|
|
140
|
+
</my-element>
|
|
141
|
+
|
|
142
|
+
<!-- Importa il bundle -->
|
|
143
|
+
<script type="module" src="./web-components.es.js"></script>
|
|
144
|
+
</body>
|
|
145
|
+
</html>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### In React
|
|
149
|
+
|
|
150
|
+
```jsx
|
|
151
|
+
import { useEffect } from 'react';
|
|
152
|
+
|
|
153
|
+
function App() {
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
// Importa il web component
|
|
156
|
+
import('./web-components.es.js');
|
|
157
|
+
}, []);
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<div>
|
|
161
|
+
<my-element
|
|
162
|
+
title="From React"
|
|
163
|
+
message="Web component in React!"
|
|
164
|
+
/>
|
|
165
|
+
</div>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### In Angular
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
// app.module.ts
|
|
174
|
+
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
|
175
|
+
|
|
176
|
+
@NgModule({
|
|
177
|
+
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
178
|
+
})
|
|
179
|
+
export class AppModule { }
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
```html
|
|
183
|
+
<!-- app.component.html -->
|
|
184
|
+
<my-element
|
|
185
|
+
[title]="'From Angular'"
|
|
186
|
+
[message]="'Web component in Angular!'">
|
|
187
|
+
</my-element>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// app.component.ts
|
|
192
|
+
import './web-components.es.js';
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## π¨ Styling
|
|
196
|
+
|
|
197
|
+
Gli stili definiti nei componenti `.ce.vue` sono incapsulati nel Shadow DOM. Per sovrascriverli dall'esterno, puoi usare CSS custom properties (variabili CSS):
|
|
198
|
+
|
|
199
|
+
```vue
|
|
200
|
+
<!-- Nel componente -->
|
|
201
|
+
<style scoped>
|
|
202
|
+
.my-element {
|
|
203
|
+
background-color: var(--my-element-bg, #f9f9f9);
|
|
204
|
+
color: var(--my-element-color, #333);
|
|
205
|
+
}
|
|
206
|
+
</style>
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
```css
|
|
210
|
+
/* Nel CSS esterno */
|
|
211
|
+
my-element {
|
|
212
|
+
--my-element-bg: #e0e0e0;
|
|
213
|
+
--my-element-color: #000;
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## π‘ Eventi
|
|
218
|
+
|
|
219
|
+
I custom elements possono emettere eventi personalizzati:
|
|
220
|
+
|
|
221
|
+
```vue
|
|
222
|
+
<!-- Nel componente -->
|
|
223
|
+
<script setup lang="ts">
|
|
224
|
+
const emit = defineEmits<{
|
|
225
|
+
customEvent: [data: string]
|
|
226
|
+
}>()
|
|
227
|
+
|
|
228
|
+
const handleClick = () => {
|
|
229
|
+
emit('customEvent', 'Hello from custom element!')
|
|
230
|
+
}
|
|
231
|
+
</script>
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
```javascript
|
|
235
|
+
// Nel JavaScript esterno
|
|
236
|
+
const element = document.querySelector('my-element');
|
|
237
|
+
element.addEventListener('customEvent', (event) => {
|
|
238
|
+
console.log(event.detail); // ['Hello from custom element!']
|
|
239
|
+
});
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## π§ Configurazione Avanzata
|
|
243
|
+
|
|
244
|
+
### Vite Config
|
|
245
|
+
|
|
246
|
+
La configurazione in `vite.config.ts` Γ¨ ottimizzata per la build di custom elements:
|
|
247
|
+
|
|
248
|
+
- **`customElement: true`**: Abilita la modalitΓ custom element in Vue
|
|
249
|
+
- **Library mode**: Crea un bundle ottimizzato per il riutilizzo
|
|
250
|
+
- **Multiple formats**: Genera sia ES modules che UMD
|
|
251
|
+
|
|
252
|
+
### TypeScript
|
|
253
|
+
|
|
254
|
+
Il progetto usa TypeScript con configurazione estesa da `tsconfig/tsconfig.vite.json` del monorepo.
|
|
255
|
+
|
|
256
|
+
## π Risorse
|
|
257
|
+
|
|
258
|
+
- [Vue 3 - Web Components](https://it.vuejs.org/guide/extras/web-components.html)
|
|
259
|
+
- [MDN - Custom Elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)
|
|
260
|
+
- [Vite - Library Mode](https://vitejs.dev/guide/build.html#library-mode)
|
|
261
|
+
|
|
262
|
+
## π Troubleshooting
|
|
263
|
+
|
|
264
|
+
### Il componente non si registra
|
|
265
|
+
|
|
266
|
+
Assicurati che il nome del custom element contenga un trattino (`-`):
|
|
267
|
+
```typescript
|
|
268
|
+
// β
Corretto
|
|
269
|
+
customElements.define('my-element', MyElementCE)
|
|
270
|
+
|
|
271
|
+
// β Errato (manca il trattino)
|
|
272
|
+
customElements.define('myelement', MyElementCE)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Gli stili non vengono applicati
|
|
276
|
+
|
|
277
|
+
Verifica che:
|
|
278
|
+
1. Gli stili siano definiti nel componente `.ce.vue`
|
|
279
|
+
2. Il tag `<style scoped>` sia presente
|
|
280
|
+
3. Gli stili non siano in conflitto con il CSS della pagina host
|
|
281
|
+
|
|
282
|
+
### Eventi non ricevuti
|
|
283
|
+
|
|
284
|
+
Assicurati di:
|
|
285
|
+
1. Emettere eventi con `emit()`
|
|
286
|
+
2. Ascoltare eventi dopo che il DOM Γ¨ caricato
|
|
287
|
+
3. Usare `event.detail` per accedere ai dati dell'evento
|
|
288
|
+
|
|
289
|
+
## π Licenza
|
|
290
|
+
|
|
291
|
+
Questo progetto fa parte del monorepo 8bot-ai e segue la stessa licenza.
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@8wave/ai-elements",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1-beta.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite --port 8082 --open --host",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"build:lib": "vite build --mode lib",
|
|
10
|
+
"preview": "vite preview"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"ability": "workspace:*",
|
|
14
|
+
"auth": "workspace:*",
|
|
15
|
+
"components": "workspace:*",
|
|
16
|
+
"composables": "workspace:*",
|
|
17
|
+
"i18n": "workspace:*",
|
|
18
|
+
"icons": "workspace:*",
|
|
19
|
+
"models": "workspace:*",
|
|
20
|
+
"style": "workspace:*",
|
|
21
|
+
"vue": "^3.5.28"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@intlify/unplugin-vue-i18n": "^11.0.3",
|
|
25
|
+
"@nabla/vite-plugin-eslint": "^2.0.6",
|
|
26
|
+
"@vitejs/plugin-vue": "^6.0.4",
|
|
27
|
+
"tsconfig": "workspace:*",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"unplugin-auto-import": "^21.0.0",
|
|
30
|
+
"unplugin-vue-components": "^31.0.0",
|
|
31
|
+
"vite": "^7.3.1",
|
|
32
|
+
"vite-plugin-stylelint": "^6.0.4",
|
|
33
|
+
"vite-plugin-webfont-dl": "^3.12.0"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
]
|
|
38
|
+
}
|