@amazon/vinyl-tsx 1.1.0 → 1.1.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/{docs/TSX.md → README.md} +44 -89
- package/package.json +6 -6
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @amazon/vinyl-tsx
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
[](https://amazonmusic.github.io/vinyl)
|
|
4
|
+
[](https://www.npmjs.com/package/@amazon/vinyl-tsx)
|
|
5
|
+
[](https://bundlejs.com/?q=@amazon/vinyl-tsx)
|
|
6
|
+
|
|
7
|
+
A lightweight, reactive JSX runtime for creating dynamic DOM elements with
|
|
8
|
+
TypeScript support. Provides a declarative way to build user interfaces with
|
|
9
|
+
reactive data binding via [`@amazon/vinyl-observable`](../vinyl-observable).
|
|
6
10
|
|
|
7
11
|
## Features
|
|
8
12
|
|
|
9
|
-
- **Reactive
|
|
10
|
-
automatically
|
|
11
|
-
- **Hook
|
|
12
|
-
|
|
13
|
-
- **Lifecycle
|
|
14
|
-
elements
|
|
15
|
-
- **TypeScript
|
|
16
|
-
extraction
|
|
17
|
-
- **Zero
|
|
18
|
-
dependencies
|
|
19
|
-
- **Performance
|
|
20
|
-
selective rendering
|
|
21
|
-
|
|
22
|
-
##
|
|
23
|
-
|
|
24
|
-
```
|
|
13
|
+
- **Reactive properties** — bind observable values to DOM properties that update
|
|
14
|
+
automatically.
|
|
15
|
+
- **Hook extensions** — enhanced DOM manipulation through custom hooks (`style`,
|
|
16
|
+
`visible`, `classList`).
|
|
17
|
+
- **Lifecycle management** — automatic cleanup and connection tracking for DOM
|
|
18
|
+
elements.
|
|
19
|
+
- **TypeScript support** — full type safety with utility types for property
|
|
20
|
+
extraction.
|
|
21
|
+
- **Zero dependencies** — lightweight runtime with no external framework
|
|
22
|
+
dependencies.
|
|
23
|
+
- **Performance optimized** — efficient updates with passive event handlers and
|
|
24
|
+
selective rendering.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```shell
|
|
25
29
|
npm install @amazon/vinyl-tsx @amazon/vinyl-util @amazon/vinyl-observable
|
|
26
30
|
```
|
|
27
31
|
|
|
@@ -31,9 +35,8 @@ npm install @amazon/vinyl-tsx @amazon/vinyl-util @amazon/vinyl-observable
|
|
|
31
35
|
|
|
32
36
|
Configure your TypeScript compiler to use the vinyl-tsx JSX factory:
|
|
33
37
|
|
|
34
|
-
// tsconfig.json
|
|
35
|
-
|
|
36
38
|
```json
|
|
39
|
+
// tsconfig.json
|
|
37
40
|
{
|
|
38
41
|
"compilerOptions": {
|
|
39
42
|
"jsx": "preserve",
|
|
@@ -65,8 +68,8 @@ const container = (
|
|
|
65
68
|
|
|
66
69
|
### Reactive Properties
|
|
67
70
|
|
|
68
|
-
Use vinyl-observable to create reactive properties that automatically
|
|
69
|
-
DOM:
|
|
71
|
+
Use `@amazon/vinyl-observable` to create reactive properties that automatically
|
|
72
|
+
update the DOM:
|
|
70
73
|
|
|
71
74
|
```tsx
|
|
72
75
|
import { data } from '@amazon/vinyl-observable'
|
|
@@ -83,16 +86,12 @@ const counter = (
|
|
|
83
86
|
</div>
|
|
84
87
|
)
|
|
85
88
|
|
|
86
|
-
// Updates automatically when observable changes
|
|
87
89
|
count.value = 5 // DOM updates to show "Count: 5"
|
|
88
90
|
isVisible.value = false // Button becomes hidden
|
|
89
91
|
```
|
|
90
92
|
|
|
91
93
|
## Hook Extensions
|
|
92
94
|
|
|
93
|
-
Amazon Vinyl TSX provides several built-in hook extensions for enhanced DOM
|
|
94
|
-
manipulation:
|
|
95
|
-
|
|
96
95
|
### Style Hook
|
|
97
96
|
|
|
98
97
|
Apply CSS styles with support for both static and observable values:
|
|
@@ -116,7 +115,6 @@ const styledDiv = (
|
|
|
116
115
|
</div>
|
|
117
116
|
)
|
|
118
117
|
|
|
119
|
-
// Updates automatically
|
|
120
118
|
color.value = 'green' // Text color changes to green
|
|
121
119
|
```
|
|
122
120
|
|
|
@@ -159,8 +157,6 @@ const element = (
|
|
|
159
157
|
<div
|
|
160
158
|
onConnect={(el) => {
|
|
161
159
|
console.log('Element connected:', el)
|
|
162
|
-
|
|
163
|
-
// Return cleanup function (optional)
|
|
164
160
|
return () => {
|
|
165
161
|
console.log('Element disconnected:', el)
|
|
166
162
|
}
|
|
@@ -173,8 +169,7 @@ const element = (
|
|
|
173
169
|
|
|
174
170
|
### Event Handlers
|
|
175
171
|
|
|
176
|
-
|
|
177
|
-
scroll events:
|
|
172
|
+
Touch and scroll events use passive listeners automatically:
|
|
178
173
|
|
|
179
174
|
```tsx
|
|
180
175
|
const element = (
|
|
@@ -200,25 +195,18 @@ const list = (
|
|
|
200
195
|
<Fragment>
|
|
201
196
|
<li>Item 1</li>
|
|
202
197
|
<li>Item 2</li>
|
|
203
|
-
<li>Item 3</li>
|
|
204
198
|
</Fragment>
|
|
205
199
|
)
|
|
206
200
|
|
|
207
|
-
// Or with JSX fragment syntax
|
|
208
201
|
const list2 = (
|
|
209
202
|
<>
|
|
210
203
|
<li>Item 1</li>
|
|
211
204
|
<li>Item 2</li>
|
|
212
|
-
<li>Item 3</li>
|
|
213
205
|
</>
|
|
214
206
|
)
|
|
215
207
|
```
|
|
216
208
|
|
|
217
|
-
##
|
|
218
|
-
|
|
219
|
-
### Custom Components
|
|
220
|
-
|
|
221
|
-
Create reusable components as functions:
|
|
209
|
+
## Custom Components
|
|
222
210
|
|
|
223
211
|
```tsx
|
|
224
212
|
interface ButtonProps {
|
|
@@ -235,30 +223,10 @@ function Button({ text, onClick, variant = 'primary' }: ButtonProps) {
|
|
|
235
223
|
)
|
|
236
224
|
}
|
|
237
225
|
|
|
238
|
-
// Usage
|
|
239
226
|
const myButton = <Button text="Click me" onClick={() => alert('Clicked!')} />
|
|
240
227
|
```
|
|
241
228
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
Text content automatically becomes reactive when using observables:
|
|
245
|
-
|
|
246
|
-
```tsx
|
|
247
|
-
const message = data('Hello')
|
|
248
|
-
const count = data(0)
|
|
249
|
-
|
|
250
|
-
const dynamicContent = (
|
|
251
|
-
<div>
|
|
252
|
-
<p>{message}</p>
|
|
253
|
-
<p>Count: {count}</p>
|
|
254
|
-
</div>
|
|
255
|
-
)
|
|
256
|
-
|
|
257
|
-
message.value = 'Hello World!' // Text updates automatically
|
|
258
|
-
count.value = 42 // Count updates automatically
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
### Conditional Rendering
|
|
229
|
+
## Conditional Rendering
|
|
262
230
|
|
|
263
231
|
Unlike React, vinyl-tsx does not re-render — JSX expressions are evaluated once
|
|
264
232
|
at creation time. Use the `visible` hook to reactively show or hide content:
|
|
@@ -281,36 +249,23 @@ const userCard = (
|
|
|
281
249
|
|
|
282
250
|
## Best Practices
|
|
283
251
|
|
|
284
|
-
1. **Initialize
|
|
285
|
-
|
|
252
|
+
1. **Initialize the connected observer** once in your application to enable
|
|
253
|
+
lifecycle management:
|
|
286
254
|
|
|
287
|
-
```tsx
|
|
288
|
-
import { initializeConnectedObserver } from '@amazon/vinyl-tsx'
|
|
289
|
-
|
|
290
|
-
// Call once at application startup
|
|
291
|
-
const cleanup = initializeConnectedObserver()
|
|
255
|
+
```tsx
|
|
256
|
+
import { initializeConnectedObserver } from '@amazon/vinyl-tsx'
|
|
292
257
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
```
|
|
258
|
+
const cleanup = initializeConnectedObserver()
|
|
259
|
+
```
|
|
296
260
|
|
|
297
|
-
|
|
261
|
+
2. **Use observables for dynamic content** — prefer observable values for
|
|
298
262
|
properties that change over time.
|
|
263
|
+
3. **Leverage hook extensions** — `style`, `visible`, and `classList` cover most
|
|
264
|
+
common DOM manipulations.
|
|
265
|
+
4. **Type safety** — let TypeScript check your props and children.
|
|
266
|
+
5. **Memory management** — observable subscriptions are cleaned up automatically
|
|
267
|
+
when elements are disconnected.
|
|
299
268
|
|
|
300
|
-
|
|
301
|
-
`classList` for common DOM manipulations.
|
|
302
|
-
|
|
303
|
-
3. **Type Safety**: Take advantage of TypeScript's type checking for props and
|
|
304
|
-
children.
|
|
305
|
-
|
|
306
|
-
4. **Memory Management**: The library automatically handles cleanup for
|
|
307
|
-
observable subscriptions when elements are disconnected.
|
|
308
|
-
|
|
309
|
-
## Performance Considerations
|
|
269
|
+
## License
|
|
310
270
|
|
|
311
|
-
-
|
|
312
|
-
changed
|
|
313
|
-
- **Passive Events**: Touch and scroll events use passive listeners for better
|
|
314
|
-
performance
|
|
315
|
-
- **Efficient Cleanup**: Automatic subscription cleanup prevents memory leaks
|
|
316
|
-
- **Minimal Bundle Size**: Zero runtime dependencies keep bundle size small
|
|
271
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amazon/vinyl-tsx",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "TSX declarative markup for creating UI",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -69,15 +69,15 @@
|
|
|
69
69
|
"watch:release": "tsx buildSrc/build.ts --watch --release"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@amazon/vinyl-build-utils": "^1.1.
|
|
73
|
-
"@amazon/vinyl-jasmine-wrapper": "^1.1.
|
|
72
|
+
"@amazon/vinyl-build-utils": "^1.1.1",
|
|
73
|
+
"@amazon/vinyl-jasmine-wrapper": "^1.1.1"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@amazon/vinyl-observable": "^1.1.
|
|
77
|
-
"@amazon/vinyl-util": "^1.1.
|
|
76
|
+
"@amazon/vinyl-observable": "^1.1.1",
|
|
77
|
+
"@amazon/vinyl-util": "^1.1.1"
|
|
78
78
|
},
|
|
79
79
|
"browserslist": [
|
|
80
80
|
"defaults, chrome 52, firefox 54, edge 15, safari 8"
|
|
81
81
|
],
|
|
82
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "e5a3429d802564132b16808a5b8a8e15a023a548"
|
|
83
83
|
}
|