redgin 0.1.18 → 0.1.19
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 +222 -93
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,126 +1,256 @@
|
|
|
1
|
-
#
|
|
2
|
-
# ~5.3kb Simplified library for building [Web Components](https://developer.mozilla.org/en-US/docs/Web/Web_Components), works on Vanilla JS / all JS framework
|
|
1
|
+
# Redgin
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
* Rerender element with [`watch`](https://stackblitz.com/edit/typescript-t3fqo8?file=sampleWatch.ts)
|
|
6
|
-
* Create getter/setters with [`getset`](https://stackblitz.com/edit/typescript-t3fqo8?file=sampleWatch.ts)
|
|
7
|
-
* Create Property reflection with [`propReflect`](https://stackblitz.com/edit/typescript-hlms7u?file=index.html)
|
|
8
|
-
* Create Inline Events with [`event`](https://stackblitz.com/edit/typescript-t3fqo8?file=sampleWatch.ts)
|
|
9
|
-
* Create custom events with [`emit`](https://stackblitz.com/edit/redgin-childtoparent?file=index.ts)
|
|
10
|
-
* Inject Global Styles with [`injectStyles`](https://stackblitz.com/edit/redgin-bootstrap?file=index.ts)
|
|
11
|
-
* [Support Typescript](https://stackblitz.com/edit/typescript-ue61k6?file=index.ts)
|
|
3
|
+
A lightweight (~5.3kb) library for building Web Components, compatible with Vanilla JS and all JavaScript frameworks. This library simplifies the creation of Web Components and offers features such as using JavaScript template literals for template syntax, rerendering elements with watch, creating getter/setters with getset, property reflection with propReflect, inline events with event, custom events with emit, injecting global styles with injectStyles, and support for TypeScript.
|
|
12
4
|
|
|
13
5
|
|
|
14
|
-
## Install
|
|
15
6
|
|
|
16
|
-
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
- **JavaScript Template Literals for Template Syntax**: Simplify the creation of templates using JavaScript template literals. [Template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
- **Rerender Element with Watch**: Easily trigger a rerender of an element by watching for changes.[`watch`](https://stackblitz.com/edit/typescript-t3fqo8?file=sampleWatch.ts)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
- **Create Getter/Setters with getset**: Define getter and setter functions for your properties.[`getset`](https://stackblitz.com/edit/typescript-t3fqo8?file=sampleWatch.ts)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
- **Create Property Reflection with propReflect**: Reflect property changes to corresponding attributes.[`propReflect`](https://stackblitz.com/edit/typescript-hlms7u?file=index.html)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
- **Create Inline Events with event**: Attach events directly in your component's template.[`event`](https://stackblitz.com/edit/typescript-t3fqo8?file=sampleWatch.ts)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
- **Create Custom Events with emit**: Emit custom events from your components.[`emit`](https://stackblitz.com/edit/redgin-childtoparent?file=index.ts)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
- **Inject Global Styles with injectStyles**: Apply global styles for your components.[`injectStyles`](https://stackblitz.com/edit/redgin-bootstrap?file=index.ts)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
- **Support for TypeScript**: Enjoy type safety when using Redgin with TypeScript.[Support Typescript](https://stackblitz.com/edit/typescript-ue61k6?file=index.ts)
|
|
40
|
+
|
|
41
|
+
|
|
17
42
|
|
|
18
|
-
```js
|
|
19
|
-
// latest
|
|
20
|
-
import { Redgin } from 'https://cdn.jsdelivr.net/npm/redgin@latest/dist/redgin.min.js'
|
|
21
43
|
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
- **Build SPAs (Single-Page Applications)**: Simplify the development of SPAs using Redgin.[Single Page Application](https://stackblitz.com/edit/typescript-ezsw6j)
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
Include the Redgin library in your project.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
// via html
|
|
55
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/redgin@latest/dist/redgin.min.js"></script>
|
|
24
56
|
|
|
25
57
|
```
|
|
26
58
|
|
|
27
|
-
### Or Install using NPM
|
|
28
59
|
|
|
29
|
-
|
|
30
|
-
|
|
60
|
+
Or install it via npm:
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
|
|
66
|
+
npm i redgin
|
|
67
|
+
|
|
31
68
|
```
|
|
69
|
+
## Usage
|
|
32
70
|
|
|
33
|
-
#### then import the library, helpers
|
|
34
71
|
|
|
35
|
-
|
|
36
|
-
|
|
72
|
+
|
|
73
|
+
1. **Import the Library:**
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
|
|
79
|
+
// via js
|
|
80
|
+
|
|
81
|
+
import { Redgin, watch, getset, html } from 'https://cdn.jsdelivr.net/npm/redgin@latest/dist/redgin.min.js';
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
// via npm
|
|
86
|
+
|
|
87
|
+
import { Redgin, watch, getset, html } from 'redgin';
|
|
88
|
+
|
|
37
89
|
```
|
|
38
90
|
|
|
39
91
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
92
|
+
|
|
93
|
+
2. **Use the Features:**
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
|
|
99
|
+
// FetchApiComponent.ts
|
|
100
|
+
|
|
101
|
+
// Creating a Fetch Api Component that displays Todos using Getset, Watch
|
|
102
|
+
class FetchApi extends RedGin {
|
|
103
|
+
// Reactive properties using getset
|
|
104
|
+
ready = getset<boolean>(false);
|
|
105
|
+
todos: any;
|
|
106
|
+
|
|
107
|
+
// Initialize data from the API in the onInit lifecycle method
|
|
108
|
+
onInit() {
|
|
109
|
+
fetch('https://jsonplaceholder.typicode.com/todos/1')
|
|
110
|
+
.then(response => response.json())
|
|
111
|
+
.then(json => {
|
|
112
|
+
this.todos = json;
|
|
113
|
+
this.ready = true;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Render method for displaying the fetched data
|
|
118
|
+
render() {
|
|
119
|
+
return html`
|
|
120
|
+
${watch(['ready'],
|
|
121
|
+
() => this.ready ? JSON.stringify(this.todos) : html`Loading...`
|
|
122
|
+
)}`;
|
|
123
|
+
}
|
|
50
124
|
}
|
|
51
|
-
|
|
125
|
+
|
|
126
|
+
// Define the custom element 'fetch-api'
|
|
127
|
+
customElements.define('fetch-api', FetchApi);
|
|
128
|
+
|
|
52
129
|
```
|
|
53
130
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
131
|
+
|
|
132
|
+
3. **Passing data from Parent to Child component**
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
|
|
138
|
+
// ParentToChildComponents.ts
|
|
139
|
+
|
|
140
|
+
class ParentComp extends RedGin {
|
|
141
|
+
currentItem: string = 'Laptop';
|
|
142
|
+
|
|
143
|
+
// Initialize child component with data using properties or attributes
|
|
144
|
+
onInit() {
|
|
145
|
+
// Option 1: Send data to child component using properties
|
|
146
|
+
const child: IChild = this.shadowRoot?.querySelector('child-comp')!;
|
|
147
|
+
child.item = this.currentItem;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Render method for the parent component
|
|
151
|
+
render() {
|
|
152
|
+
return html`
|
|
153
|
+
<child-comp></child-comp>
|
|
154
|
+
|
|
155
|
+
<!-- Option 2: Send data to child component using attributes -->
|
|
156
|
+
<child2-comp item="${this.currentItem}"></child2-comp>
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
69
159
|
}
|
|
70
|
-
|
|
160
|
+
|
|
161
|
+
|
|
71
162
|
```
|
|
72
163
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
164
|
+
3. **Passing data from Child to Parent component**
|
|
165
|
+
``` javascript
|
|
166
|
+
|
|
167
|
+
// ParentChildComponents.ts
|
|
168
|
+
|
|
169
|
+
// Child component for emitting a custom event
|
|
170
|
+
class ChildComp extends RedGin {
|
|
171
|
+
render() {
|
|
172
|
+
return html`
|
|
173
|
+
<button ${event('click', () => emit.call(this, 'newItem', 'added New Item?'))}>
|
|
174
|
+
<slot>Add to parent's list</slot>
|
|
175
|
+
</button>
|
|
176
|
+
`;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
80
179
|
|
|
180
|
+
// Parent component for receiving the custom event
|
|
181
|
+
class ParentComp extends RedGin {
|
|
81
182
|
render() {
|
|
82
|
-
return `
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
`
|
|
183
|
+
return html`
|
|
184
|
+
<child-comp
|
|
185
|
+
${event('newItem', (e: CustomEvent) => console.log(`Received child data: ${e.detail}`))}>
|
|
186
|
+
Get Child Data?
|
|
187
|
+
</child-comp>
|
|
188
|
+
`;
|
|
90
189
|
}
|
|
91
190
|
}
|
|
92
|
-
|
|
191
|
+
|
|
93
192
|
```
|
|
94
193
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
194
|
+
4. Creating a Reactive button
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
|
|
198
|
+
// ReactiveButton.ts
|
|
199
|
+
|
|
200
|
+
class ReactiveButton extends RedGin {
|
|
201
|
+
// Reactive property using propReflect
|
|
202
|
+
message = propReflect<string>('Hello, World!');
|
|
203
|
+
|
|
204
|
+
// Observed attributes for the component
|
|
205
|
+
static observedAttributes = ['message'];
|
|
206
|
+
|
|
207
|
+
// Render method for the component
|
|
208
|
+
render() {
|
|
209
|
+
// Use watch to trigger a rerender when 'message' changes
|
|
210
|
+
return html`${watch(['message'], () => html`
|
|
211
|
+
<button type="button">${this.message}</button>
|
|
212
|
+
`)}
|
|
213
|
+
`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
102
216
|
|
|
103
|
-
|
|
104
|
-
render() {
|
|
105
|
-
return `${ watch(['obj'], () =>
|
|
106
|
-
html`<div>${ this.obj.id }</div>
|
|
107
|
-
<div>${ this.obj.name }</div>`
|
|
108
|
-
) }`
|
|
109
|
-
}
|
|
110
217
|
```
|
|
111
218
|
|
|
112
|
-
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
219
|
+
5. For Loop through the list of Products
|
|
220
|
+
``` javascript
|
|
221
|
+
|
|
222
|
+
// ProductListRenderer.ts
|
|
223
|
+
|
|
224
|
+
// For Loop through the List of Products
|
|
225
|
+
class ProductListRenderer extends RedGin {
|
|
226
|
+
// Reactive property using getset
|
|
227
|
+
products = getset<IProduct[]>([
|
|
228
|
+
{ id: 1, name: 'Laptop' },
|
|
229
|
+
{ id: 2, name: 'Camera' },
|
|
230
|
+
]);
|
|
231
|
+
|
|
232
|
+
// Render method for displaying the list of products
|
|
233
|
+
render() {
|
|
234
|
+
return html`
|
|
235
|
+
<ul>
|
|
236
|
+
${watch(['products'], () =>
|
|
237
|
+
this.products.map(product =>
|
|
238
|
+
html`<li>${product.id} - ${product.name}</li>`
|
|
239
|
+
)
|
|
240
|
+
)}
|
|
241
|
+
</ul>`;
|
|
242
|
+
}
|
|
122
243
|
}
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
123
247
|
```
|
|
248
|
+
More
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
124
254
|
|
|
125
255
|
## For VSCode Syntax Highlight template literals
|
|
126
256
|
|
|
@@ -140,9 +270,9 @@ https://web.dev/custom-elements-best-practices/
|
|
|
140
270
|
https://web.dev/shadowdom-v1/
|
|
141
271
|
|
|
142
272
|
|
|
143
|
-
## How to run development server?
|
|
273
|
+
## How to run development server?
|
|
144
274
|
```
|
|
145
|
-
git clone git@github.com:josnin/redgin.git
|
|
275
|
+
git clone git@github.com:josnin/redgin.git
|
|
146
276
|
cd ~/Documents/redgin/
|
|
147
277
|
npm install
|
|
148
278
|
npm run dev
|
|
@@ -155,4 +285,3 @@ Need help? Open an issue in: [ISSUES](https://github.com/josnin/redgin/issues)
|
|
|
155
285
|
|
|
156
286
|
## Contributing
|
|
157
287
|
Want to improve and add feature? Fork the repo, add your changes and send a pull request.
|
|
158
|
-
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redgin",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "~5.3kb
|
|
3
|
+
"version": "0.1.19",
|
|
4
|
+
"description": "A lightweight (~5.3kb) library for building Web Components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"redgin",
|
|
7
7
|
"web components",
|