@asd14/vblocks 5.3.0 → 5.4.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/README.md CHANGED
@@ -48,11 +48,16 @@ to write, read or fix
48
48
  - [Replace a default config](#replace-a-default-config)
49
49
  - [Style with Tailwind](#style-with-tailwind)
50
50
  - [Add VBlocks to an existing project](#add-vblocks-to-an-existing-project)
51
- - [Commands](#commands)
52
- - [`vblocks init`](#vblocks-init)
53
- - [`vblocks serve`](#vblocks-serve)
54
- - [`vblocks build`](#vblocks-build)
55
- - [`vblocks eject`](#vblocks-eject)
51
+ - [References](#references)
52
+ - [Commands](#commands)
53
+ - [`vblocks init`](#vblocks-init)
54
+ - [`vblocks serve`](#vblocks-serve)
55
+ - [`vblocks build`](#vblocks-build)
56
+ - [`vblocks eject`](#vblocks-eject)
57
+ - [Templates](#templates)
58
+ - [`plain`](#plain)
59
+ - [`xyflow`](#xyflow)
60
+ - [`xyflow-tabs`](#xyflow-tabs)
56
61
  - [License](#license)
57
62
 
58
63
  <!-- vim-markdown-toc -->
@@ -67,23 +72,18 @@ Order this README by three principles. Each settles one decision:
67
72
  - **Colocation**, the place of a fact. A caveat sits on the step or option it
68
73
  limits, in a NOTE when it is tangential or points at another section
69
74
 
70
- Each section is one Diátaxis type (https://diataxis.fr): Quick Start is a
71
- tutorial, How to is how-to guides, Vocabulary and Commands are reference.
75
+ Each section is one Diátaxis type (https://diataxis.fr):
76
+ - Quick Start -> Tutorial
77
+ - How to -> How-to guides
78
+ - Vocabulary, Commands and Templates -> Reference
72
79
  -->
73
80
 
74
81
  ## Quick Start
75
82
 
76
- <!--
77
- Diátaxis: tutorial. The fewest steps to a working bundle. It shows enough of
78
- VBlocks for the reader to decide whether VBlocks fits their project.
79
- -->
80
-
81
83
  <!--
82
84
  Using "scaffold", the field's word for what `init` does, eg. Yeoman and
83
85
  create-vite use it.
84
- -->
85
86
 
86
- <!--
87
87
  Using the `my-vblocks-` prefix, the package name, so a reader who tries several
88
88
  scaffolders spots the folder at a glance, as with `vite-project` and
89
89
  `nuxt-app`.
@@ -149,10 +149,7 @@ Several apps, eg. demos or MVP sketches, can share one `package.json` and one
149
149
  ```diff
150
150
  sketches/
151
151
  + ├─ flow-diagram/
152
- + │ ├─ src/
153
- + │ │ ├─ index.tsx
154
- + │ │ ├─ index.html
155
- + │ │ └─ index.css
152
+ + │ ├─ src/ # the files of the xyflow template
156
153
  + │ ├─ postcss.config.js
157
154
  + │ └─ tsconfig.json
158
155
  ├─ .gitignore
@@ -168,24 +165,22 @@ Several apps, eg. demos or MVP sketches, can share one `package.json` and one
168
165
  ## Vocabulary
169
166
 
170
167
  <!--
171
- Vocabulary follows Quick Start. The reader sees the terms used, then reads
172
- their definitions. How to is the first section that assumes the reader knows
173
- them.
168
+ Keep Vocabulary after Quick Start since the reader has no context to place
169
+ the definitions in.
174
170
  -->
175
171
 
176
172
  | Term | Means |
177
173
  | :----------------- | :------------------------------------------------------------ |
174
+ | **Plumbing** | The project files `init` writes: `package.json`, `.gitignore` |
178
175
  | **Project folder** | The folder with `package.json` |
179
176
  | **App folder** | The folder with `src/index.tsx` and `src/index.html` |
180
- | **Plumbing** | The project files `init` writes: `package.json`, `.gitignore` |
181
177
  | **Bundle** | The files `build` writes to `dist/` |
182
178
 
183
179
  ## How to
184
180
 
185
181
  <!--
186
- Diátaxis: how-to. Each ### names a pain as the task that relieves it. Order
187
- the guides by how early the Quick Start reader has the pain. A guide for
188
- another reader, eg. one with an existing app, goes last.
182
+ Each ### names a pain as the task that relieves it. Order the guides by how
183
+ early the Quick Start reader has the pain.
189
184
  -->
190
185
 
191
186
  ### Use environment variables
@@ -229,12 +224,12 @@ import { Component } from "@self/components"
229
224
  > The `tsconfig.json` that [`vblocks init`](#vblocks-init) writes already has
230
225
  > it. Otherwise, add:
231
226
  >
232
- > ```json
233
- > {
234
- > "compilerOptions": {
235
- > "paths": { "@self/*": ["./src/*"] }
236
- > }
237
- > }
227
+ > ```diff
228
+ > {
229
+ > "compilerOptions": {
230
+ > + "paths": { "@self/*": ["./src/*"] }
231
+ > }
232
+ > }
238
233
  > ```
239
234
 
240
235
  ### Use CSS Modules
@@ -280,7 +275,8 @@ Each config in your app folder replaces the default of the same name:
280
275
  1. **[Eject](#vblocks-eject) the default into your app folder**
281
276
 
282
277
  ```sh
283
- npx vblocks eject swc > flow-diagram/.swcrc
278
+ cd flow-diagram
279
+ npx vblocks eject swc > .swcrc
284
280
  ```
285
281
 
286
282
  2. **Update the ejected file**
@@ -303,7 +299,8 @@ VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
303
299
  app folder:
304
300
 
305
301
  > [!NOTE]
306
- > The `xyflow` template of [`vblocks init`](#vblocks-init) has this setup.
302
+ > The `xyflow` and `xyflow-tabs` templates of [`vblocks init`](#vblocks-init)
303
+ > have this setup.
307
304
 
308
305
  1. **Install Tailwind and the Tailwind plugin for PostCSS**
309
306
 
@@ -363,42 +360,49 @@ app folder:
363
360
  npm install --save-dev @asd14/vblocks # needs react and react-dom ^18 or ^19
364
361
  ```
365
362
 
366
- 2. **Ensure `index.tsx` and `index.html` are in `src/`**
363
+ 2. **Add the [client types](src/types/client.d.ts) and the
364
+ [`@self`](#stop-using--everywhere) alias to your `tsconfig.json`**
365
+
366
+ ```diff
367
+ {
368
+ "compilerOptions": {
369
+ + "types": ["@asd14/vblocks/client"],
370
+ + "paths": { "@self/*": ["./src/*"] }
371
+ }
372
+ }
373
+ ```
374
+
375
+ 3. **Ensure `index.tsx` and `index.html` are in `src/`**
367
376
 
368
377
  > [!NOTE]
369
378
  > VBlocks builds `dist/index.html` from `src/index.html` and adds the script and
370
379
  > stylesheet tags to it. Drop the tag that loads your entry, eg. Vite's
371
380
  > `<script type="module" src="/src/main.tsx"></script>`.
372
381
 
373
- 3. **Start a development server**
382
+ 4. **Start a development server**
374
383
 
375
384
  ```sh
376
385
  npx vblocks serve
377
386
  ```
378
387
 
379
- ## Commands
388
+ ## References
380
389
 
381
- <!--
382
- Diátaxis: reference. Mirror SYNOPSIS and OPTIONS of `vblocks --help`, in the
383
- same words. The SYNOPSIS puts one option per line, with its values, so it fits
384
- the width of GitHub. Below it, each option is a bullet with its default, and
385
- its description in sub-bullets.
386
- -->
390
+ ### Commands
387
391
 
388
- ### `vblocks init`
392
+ #### `vblocks init`
389
393
 
390
394
  Write a template's plumbing, app, or both into `<directory>`.
391
395
 
392
396
  ```text
393
397
  vblocks init <directory>
394
- [--template <plain|xyflow>]
398
+ [--template <plain|xyflow|xyflow-tabs>]
395
399
  [--only <app|plumbing>]
396
400
  ```
397
401
 
398
402
  - `<directory>`
399
403
  - The folder to write into. It must be missing or empty
400
404
  - `--template` (default: `plain`)
401
- - `xyflow` adds React Flow and Tailwind
405
+ - The template to write, see [Templates](#templates)
402
406
  - `--only` (default: both)
403
407
  - Write one part of the template
404
408
  - `plumbing`: `package.json`, `.gitignore`
@@ -407,7 +411,7 @@ vblocks init <directory>
407
411
  [project folder](#vocabulary), the nearest folder above `<directory>` with a
408
412
  `package.json`. Without that folder, `init` fails
409
413
 
410
- ### `vblocks serve`
414
+ #### `vblocks serve`
411
415
 
412
416
  Start the development server.
413
417
 
@@ -438,7 +442,7 @@ vblocks serve
438
442
  - Any other [`webpack serve`](https://webpack.js.org/api/cli/) option, passed
439
443
  unchanged, eg. `--host 0.0.0.0` to listen on the network
440
444
 
441
- ### `vblocks build`
445
+ #### `vblocks build`
442
446
 
443
447
  Build the app into a bundle in the `dist/` of the app folder.
444
448
 
@@ -447,8 +451,8 @@ By default, `build`:
447
451
  - Writes JS and CSS files with content-hashed names
448
452
  - Writes a `react` chunk, and a `vendors` chunk when your other dependencies
449
453
  total over 20 KB (10 KB in `development`)
450
- - Inlines `jpg`, `gif`, `png` and `svg` files up to 10 KB, writes larger ones to
451
- `dist/` with content-hashed names
454
+ - Inlines `jpg`, `gif`, `png` and `svg` files up to 10 KB
455
+ - Writes larger to `dist/` with content-hashed names
452
456
  - Writes `woff`, `woff2`, `ttf` and `eot` files to `dist/fonts/`
453
457
  - Copies `src/public/favicon.svg` and `src/public/robots.txt` of your app, or
454
458
  the defaults of VBlocks
@@ -480,7 +484,7 @@ vblocks build
480
484
  - Any other [`webpack build`](https://webpack.js.org/api/cli/) option, passed
481
485
  unchanged, eg. `--stats errors-only`
482
486
 
483
- ### `vblocks eject`
487
+ #### `vblocks eject`
484
488
 
485
489
  Print a default config to stdout.
486
490
 
@@ -491,7 +495,7 @@ vblocks eject <webpack|postcss|swc>
491
495
  - `<webpack|postcss|swc>`
492
496
  - The config to print
493
497
 
494
- Save the output in your app folder to
498
+ Run it in your app folder and save the output there, to
495
499
  [replace the default](#replace-a-default-config):
496
500
 
497
501
  ```sh
@@ -500,6 +504,55 @@ npx vblocks eject postcss > postcss.config.js
500
504
  npx vblocks eject swc > .swcrc
501
505
  ```
502
506
 
507
+ ### Templates
508
+
509
+ Every app gets `src/index.html`, `src/index.css` and `tsconfig.json`. Each
510
+ template adds its own files, and sets the `<title>` of `src/index.html`. A
511
+ template's file replaces the common one of the same path, eg. `xyflow` has its
512
+ own `src/index.css`.
513
+
514
+ #### `plain`
515
+
516
+ `src/index.tsx` renders a heading. Adds no packages.
517
+
518
+ #### `xyflow`
519
+
520
+ One diagram, and a details panel for the selected node or edge. Adds
521
+ `@xyflow/react`, `tailwindcss` and `@tailwindcss/postcss` to the `package.json`
522
+ of the project folder. Writes a `postcss.config.js` that runs Tailwind into the
523
+ app folder.
524
+
525
+ ```text
526
+ src/
527
+ ├── index.tsx # the layout: header, diagram, details panel, the selected id
528
+ ├── diagram.tsx # the nodes and edges, placed and sized in grid cells
529
+ ├── details.tsx # the panel for the selected node or edge
530
+ ├── ui/zoom-level.tsx # the zoom level, and a Reset to 100%
531
+ └── utils/grid.ts # CELL_SIZE and toPixels
532
+ ```
533
+
534
+ To show another topic, rewrite `diagram.tsx` and `details.tsx`.
535
+
536
+ #### `xyflow-tabs`
537
+
538
+ One tab per diagram, each with its own details panel. Adds the same packages and
539
+ `postcss.config.js` as `xyflow`.
540
+
541
+ ```text
542
+ src/
543
+ ├── index.tsx # the layout: header, tab bar, details panel, the selected id
544
+ ├── pages/
545
+ │ ├── build/
546
+ │ │ ├── diagram.tsx # the nodes and edges of one tab, in grid cells
547
+ │ │ └── details.tsx # the panel for its selected node or edge
548
+ │ └── serve/ # the same two files
549
+ ├── ui/zoom-level.tsx # the zoom level, and a Reset to 100%
550
+ └── utils/grid.ts # CELL_SIZE and toPixels
551
+ ```
552
+
553
+ To add a tab, add a folder in `pages/` and add it to the tab list in
554
+ `index.tsx`.
555
+
503
556
  ## License
504
557
 
505
558
  MIT
package/bin/vblocks.sh CHANGED
@@ -7,7 +7,7 @@ set -eu
7
7
  #
8
8
  # SYNOPSIS
9
9
  # vblocks init <directory>
10
- # [--template <plain|xyflow>]
10
+ # [--template <plain|xyflow|xyflow-tabs>]
11
11
  # [--only <app|plumbing>]
12
12
  # vblocks serve
13
13
  # [--mode <development|production>]
@@ -43,7 +43,7 @@ set -eu
43
43
  # serve and build pass every option not listed under OPTIONS to Webpack.
44
44
  #
45
45
  # OPTIONS
46
- # --template <name> init: plain or xyflow (default: plain)
46
+ # --template <name> init: plain, xyflow or xyflow-tabs (default: plain)
47
47
  # --only <app|plumbing> init: write only the app or only the plumbing.
48
48
  # init adds the app's dependencies to the
49
49
  # project folder: the nearest folder above
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@asd14/vblocks",
4
- "version": "5.3.0",
4
+ "version": "5.4.0",
5
5
  "description": "Zero-config React builds, as a static site or a single HTML file",
6
6
  "keywords": [
7
7
  "react",
@@ -0,0 +1,10 @@
1
+ #!/bin/sh
2
+
3
+ # Runs in the app folder, after `vblocks init` copies the app files
4
+
5
+ set -eu
6
+
7
+ # Name the page in the browser tab.
8
+ # `sed -i` differs between GNU and BSD, so write a copy and move it over
9
+ sed 's|<title>.*</title>|<title>Hello World :: VBlocks</title>|' src/index.html >src/index.html.tmp
10
+ mv -- src/index.html.tmp src/index.html
@@ -0,0 +1,74 @@
1
+ import { diagramEdges, diagramNodes } from "./diagram"
2
+
3
+ const findLabel = (nodeId: string) =>
4
+ diagramNodes.find(node => node.id === nodeId)?.data.label ?? nodeId
5
+
6
+ type LabelListProps = {
7
+ title: string
8
+ labels: string[]
9
+ }
10
+
11
+ const LabelList = ({ title, labels }: LabelListProps) => (
12
+ <div className="mt-4">
13
+ <h3 className="text-xs font-semibold tracking-wide text-stone-500 uppercase">
14
+ {title}
15
+ </h3>
16
+ <ul className="mt-1 font-mono text-sm">
17
+ {labels.length === 0 ? (
18
+ <li className="text-stone-400">none</li>
19
+ ) : (
20
+ labels.map(label => <li key={label}>{label}</li>)
21
+ )}
22
+ </ul>
23
+ </div>
24
+ )
25
+
26
+ type DetailsProps = {
27
+ selectedId: string
28
+ }
29
+
30
+ /**
31
+ * The selected step, with the steps it reads from and writes to, or the
32
+ * selected edge
33
+ */
34
+ const Details = ({ selectedId }: DetailsProps) => {
35
+ const node = diagramNodes.find(item => item.id === selectedId)
36
+
37
+ if (node) {
38
+ return (
39
+ <section>
40
+ <h2 className="font-mono text-sm font-semibold">{node.data.label}</h2>
41
+ <p className="mt-1 text-sm text-stone-600">{node.data.detail}</p>
42
+ <LabelList
43
+ title="In"
44
+ labels={diagramEdges
45
+ .filter(edge => edge.target === node.id)
46
+ .map(edge => findLabel(edge.source))}
47
+ />
48
+ <LabelList
49
+ title="Out"
50
+ labels={diagramEdges
51
+ .filter(edge => edge.source === node.id)
52
+ .map(edge => findLabel(edge.target))}
53
+ />
54
+ </section>
55
+ )
56
+ }
57
+
58
+ const edge = diagramEdges.find(item => item.id === selectedId)
59
+
60
+ return (
61
+ <section>
62
+ <h2 className="font-mono text-sm font-semibold">
63
+ {edge
64
+ ? `${findLabel(edge.source)} → ${findLabel(edge.target)}`
65
+ : selectedId}
66
+ </h2>
67
+ <p className="mt-1 text-sm text-stone-600">
68
+ {edge ? "The output of one step, read by the next" : "A new edge"}
69
+ </p>
70
+ </section>
71
+ )
72
+ }
73
+
74
+ export { Details }
@@ -0,0 +1,150 @@
1
+ import { useCallback } from "react"
2
+ import {
3
+ addEdge,
4
+ Background,
5
+ Controls,
6
+ Handle,
7
+ MarkerType,
8
+ Position,
9
+ ReactFlow,
10
+ useEdgesState,
11
+ useNodesState,
12
+ type Edge,
13
+ type Node,
14
+ type NodeProps,
15
+ type NodeTypes,
16
+ type OnConnect,
17
+ type OnSelectionChangeFunc,
18
+ } from "@xyflow/react"
19
+
20
+ import { ZoomLevel } from "./ui/zoom-level"
21
+ import { CELL_SIZE, toPixels } from "./utils/grid"
22
+
23
+ type StepNode = Node<{ label: string; detail: string }, "step">
24
+
25
+ /**
26
+ * The graph. Edit these two lists, this specimen draws the VBlocks build
27
+ */
28
+ const diagramNodes = toPixels<StepNode>([
29
+ {
30
+ id: "index.tsx",
31
+ type: "step",
32
+ position: { x: 0, y: 0 },
33
+ size: { width: 12, height: 4 },
34
+ data: { label: "src/index.tsx", detail: "The application entry point" },
35
+ },
36
+ {
37
+ id: "index.css",
38
+ type: "step",
39
+ position: { x: 14, y: 0 },
40
+ size: { width: 12, height: 4 },
41
+ data: { label: "src/index.css", detail: "The global CSS" },
42
+ },
43
+ {
44
+ id: "swc",
45
+ type: "step",
46
+ position: { x: 0, y: 7 },
47
+ size: { width: 12, height: 4 },
48
+ data: { label: "SWC", detail: "Compiles TypeScript and JSX" },
49
+ },
50
+ {
51
+ id: "postcss",
52
+ type: "step",
53
+ position: { x: 14, y: 7 },
54
+ size: { width: 12, height: 4 },
55
+ data: { label: "PostCSS", detail: "Runs Tailwind and cssnano" },
56
+ },
57
+ {
58
+ id: "index.html",
59
+ type: "step",
60
+ position: { x: 28, y: 7 },
61
+ size: { width: 12, height: 4 },
62
+ data: { label: "src/index.html", detail: "The HTML template" },
63
+ },
64
+ {
65
+ id: "webpack",
66
+ type: "step",
67
+ position: { x: 14, y: 14 },
68
+ size: { width: 12, height: 4 },
69
+ data: { label: "Webpack", detail: "Bundles the modules and the assets" },
70
+ },
71
+ {
72
+ id: "dist",
73
+ type: "step",
74
+ position: { x: 14, y: 21 },
75
+ size: { width: 12, height: 4 },
76
+ data: { label: "dist/index.html", detail: "The bundle" },
77
+ },
78
+ ])
79
+
80
+ const diagramEdges: Edge[] = [
81
+ { id: "index.tsx-swc", source: "index.tsx", target: "swc" },
82
+ { id: "index.css-postcss", source: "index.css", target: "postcss" },
83
+ { id: "swc-webpack", source: "swc", target: "webpack" },
84
+ { id: "postcss-webpack", source: "postcss", target: "webpack" },
85
+ { id: "index.html-webpack", source: "index.html", target: "webpack" },
86
+ { id: "webpack-dist", source: "webpack", target: "dist", animated: true },
87
+ ]
88
+
89
+ const Step = ({ data, selected }: NodeProps<StepNode>) => (
90
+ <div
91
+ className={`h-full w-full overflow-hidden rounded-md border bg-white px-4 py-2 shadow-sm ${selected ? "border-stone-900" : "border-stone-300"}`}>
92
+ <Handle type="target" position={Position.Top} />
93
+ <div className="font-mono text-sm font-semibold text-stone-900">
94
+ {data.label}
95
+ </div>
96
+ <div className="text-xs text-stone-500">{data.detail}</div>
97
+ <Handle type="source" position={Position.Bottom} />
98
+ </div>
99
+ )
100
+
101
+ const nodeTypes: NodeTypes = { step: Step }
102
+
103
+ type DiagramProps = {
104
+ onSelect: (entityId: string | undefined) => void
105
+ }
106
+
107
+ const Diagram = ({ onSelect }: DiagramProps) => {
108
+ const [nodes, , onNodesChange] = useNodesState(diagramNodes)
109
+ const [edges, setEdges, onEdgesChange] = useEdgesState(diagramEdges)
110
+
111
+ const onConnect: OnConnect = useCallback(
112
+ connection => {
113
+ setEdges(currentEdges => addEdge(connection, currentEdges))
114
+ },
115
+ [setEdges]
116
+ )
117
+
118
+ // Memoize it, as React Flow asks for onSelectionChange
119
+ const onSelectionChange: OnSelectionChangeFunc = useCallback(
120
+ selection => {
121
+ onSelect(selection.nodes[0]?.id ?? selection.edges[0]?.id)
122
+ },
123
+ [onSelect]
124
+ )
125
+
126
+ return (
127
+ <ReactFlow
128
+ nodes={nodes}
129
+ edges={edges}
130
+ nodeTypes={nodeTypes}
131
+ onNodesChange={onNodesChange}
132
+ onEdgesChange={onEdgesChange}
133
+ onConnect={onConnect}
134
+ onSelectionChange={onSelectionChange}
135
+ // Snap a dragged node to the dots of <Background>
136
+ snapToGrid
137
+ snapGrid={[CELL_SIZE, CELL_SIZE]}
138
+ defaultEdgeOptions={{ markerEnd: { type: MarkerType.ArrowClosed } }}
139
+ // Center the diagram at 100%, the fit-view button of <Controls> still
140
+ // fits it to the screen
141
+ fitView
142
+ fitViewOptions={{ minZoom: 1, maxZoom: 1 }}>
143
+ <Background gap={CELL_SIZE} />
144
+ <Controls />
145
+ <ZoomLevel />
146
+ </ReactFlow>
147
+ )
148
+ }
149
+
150
+ export { Diagram, diagramEdges, diagramNodes }