@unpunnyfuns/d3-blockmap 0.0.0-stage → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 unpunnyfuns
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,140 @@
1
- # Temporary Holding Version
1
+ # d3-blockmap
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Canvas progress bar for millions of blocks: one pixel column per bin, drawn from a packed bitset. Zoomable, themable with CSS variables, with an optional axis and a React component.
4
+
5
+ ![10 million blocks as a zoomable canvas bar with axis ticks](demo/example.png)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm i @unpunnyfuns/d3-blockmap
11
+ ```
12
+
13
+ Peer dependency for the React component: `react >= 19`.
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import { createBlockMap } from '@unpunnyfuns/d3-blockmap';
19
+
20
+ const map = createBlockMap(document.querySelector('#progress')!, {
21
+ totalBlocks: 10_000_000,
22
+ });
23
+
24
+ map.addBlocks(newIndices); // Uint32Array or number[]
25
+ map.addRanges([[0, 123], [256, 1024]]); // [start, end) tuples
26
+ map.getProgress(); // { completed, total }
27
+ ```
28
+
29
+ The container needs a height; the canvas fills it. Leave a little space below if you turn the axis on.
30
+
31
+ ```tsx
32
+ import { useRef, useState } from 'react';
33
+ import { BlockMap } from '@unpunnyfuns/d3-blockmap/react';
34
+ import type { BlockMapHandle } from '@unpunnyfuns/d3-blockmap/react';
35
+
36
+ export const Download = ({ totalBlocks }: { totalBlocks: number }) => {
37
+ const ref = useRef<BlockMapHandle>(null);
38
+ const [highlight, setHighlight] = useState(false);
39
+
40
+ // Call ref.current?.addRanges(...) from your data source as ranges complete.
41
+
42
+ return (
43
+ <>
44
+ <BlockMap ref={ref} totalBlocks={totalBlocks} axis highlight={highlight} style={{ height: 12 }} />
45
+ <button onClick={() => setHighlight((on) => !on)}>Highlight missing</button>
46
+ </>
47
+ );
48
+ };
49
+ ```
50
+
51
+ ## Options
52
+
53
+ | Option | Type | Default | What it does |
54
+ |---|---|---|---|
55
+ | `totalBlocks` | `number` | required | Number of blocks, up to 2^32 − 1. |
56
+ | `initialBlocks` | `ArrayLike<number>` | | Indices already complete. |
57
+ | `initialRanges` | `RangeInput` | | `[start, end)` tuples (or a flat `Uint32Array` of pairs) already complete. |
58
+ | `partial` | `"coverage" \| "gradient"` | `"coverage"` | How partly filled columns are coloured. See Theming. |
59
+ | `view` | `{ start, end }` | full range | Initial zoom window, `end` exclusive. |
60
+ | `interactive` | `boolean` | `true` | Ctrl/⌘+wheel zooms, drag pans, double-click resets; keyboard controls when focused, see Behaviour. |
61
+ | `onViewChange` | `(view) => void` | | Called when the view changes. |
62
+ | `axis` | `boolean \| { format?, tickCount? }` | `false` | Tick labels under the bar. |
63
+ | `brush` | `boolean` | `false` | A mouse or pen drag selects a block range; panning then needs Shift. See Behaviour. |
64
+ | `onBrush` | `(selection) => void` | | Called on every brush move with the live `[start, end)` or `null`. |
65
+ | `onBrushEnd` | `(selection) => void` | | Called when the gesture ends, with the snapped `[start, end)`, or `null` when it cleared the selection. A gesture that leaves the selection as it was fires nothing. |
66
+ | `onBlockClick` | `(event) => void` | | Called on click with `{ block, range, completed, total, originalEvent }`. Works even when `interactive` is `false`. |
67
+ | `label` | `(progress) => string` | | Overrides the canvas `aria-label`. See Behaviour. |
68
+
69
+ ## Methods
70
+
71
+ | Method | What it does |
72
+ |---|---|
73
+ | `setBlocks(indices)` | Replace everything with these indices. |
74
+ | `addBlocks(indices)` | Mark indices complete. Cheapest way to add single blocks. |
75
+ | `setRanges(ranges)` | Replace everything with a snapshot of ranges. |
76
+ | `addRanges(ranges)` | Mark ranges complete. Overlap is fine and counted once. |
77
+ | `removeRanges(ranges)` | Mark ranges missing again. |
78
+ | `getProgress()` | `{ completed, total }`. |
79
+ | `setView(view)` / `resetView()` / `getView()` | Zoom window control. |
80
+ | `setBrush(selection)` | Select `[start, end)` blocks, or `null` to clear. Fires no callbacks. |
81
+ | `getBrush()` | The current selection as a copy, or `null`. |
82
+ | `setHighlight(on)` | Blink incomplete columns. Steady under `prefers-reduced-motion`. |
83
+ | `getScale()` | `{ domain, range, x(block), block(x) }` to draw your own axis. |
84
+ | `destroy()` | Remove everything. |
85
+
86
+ Ranges use exclusive ends: `[0, 10]` means blocks 0 through 9. Unsorted, overlapping, and out-of-range input is normalised.
87
+
88
+ ## Behaviour
89
+
90
+ - The canvas is focusable (`tabindex="0"`) when `interactive` isn't `false`, and announces progress through `role="img"` plus an `aria-label`: `label` overrides the default `Download progress: 4,343,270 of 10,000,000 blocks (43%)` text.
91
+ - With the canvas focused: `+`/`-` (or `=`/`_`) zoom in or out around the centre of the current view, arrow keys pan by 10% of the view length, Shift+arrow by 50%, and `Home` or `0` resets to the full range.
92
+ - With `brush: true`, a mouse or pen drag selects a block range and Shift+drag pans; click an empty spot to clear. The brush is a mouse and pen gesture only, so a touch drag keeps panning and needs no modifier.
93
+ - `onBlockClick` reports the block under the pointer, or the column's range when a column covers several blocks. It does not fire for a click that lands 3 px or more from where the press began, which is how a pan or a brush drag ends, nor for a click outside the canvas. A plain click with no selection present reports the block it hit; the click that clears an existing selection does not.
94
+
95
+ ## React
96
+
97
+ Props mirror the options, including `label`, `brush`, `onBrush`, `onBrushEnd`, `onBlockClick`, plus `blocks`, `ranges`, `highlight`, `className`, `style`. `ref` is a plain prop exposing every method above except `destroy`, as `BlockMapHandle`.
98
+
99
+ - `blocks` and `ranges` apply when their identity changes, so keep the references stable.
100
+ - `partial`, `interactive`, `axis`, and `brush` recreate the instance when they change; memoise an object `axis`.
101
+ - `onViewChange`, `onBrush`, `onBrushEnd`, `onBlockClick`, and `label` can be inline, added, or removed: the latest function is called without recreating the instance. A changed `label` applies on the next progress update.
102
+ - `setBlocks` and `setRanges` on the ref hold until the `blocks` or `ranges` prop next changes.
103
+ - For high-frequency updates use the ref (`addBlocks`, `addRanges`) rather than props.
104
+
105
+ ## Theming
106
+
107
+ Set these CSS custom properties on the container or an ancestor:
108
+
109
+ | Property | Default | Used for |
110
+ |---|---|---|
111
+ | `--blockmap-missing` | `#2b2b2b` | empty columns |
112
+ | `--blockmap-complete` | `#2ecc71` | full columns |
113
+ | `--blockmap-partial` | `#e0a500` | midpoint of the `coverage` fade |
114
+ | `--blockmap-partial-low` / `-high` | `#5b3a00` / `#e0a500` | the `gradient` style |
115
+ | `--blockmap-highlight` | `#ff4d4f` | blinking incomplete columns |
116
+ | `--blockmap-brush` | `#ffffff` | brush selection |
117
+ | `--blockmap-axis-color` | `currentColor` | tick labels |
118
+
119
+ `coverage` fades missing → partial → complete by how full a column is, so range edges look crisp. `gradient` fades low → high instead, which shows density when blocks arrive scattered.
120
+
121
+ The tooltip is a `div[data-blockmap-tooltip]`, ticks are `span[data-blockmap-tick]` inside `div[data-blockmap-axis]`, and the brush overlay is an `svg[data-blockmap-brush]`; style them with CSS.
122
+
123
+ ## Notes
124
+
125
+ - Memory is 1 bit per block plus one counter per pixel column. Updates cost in proportion to what changed, not to `totalBlocks`.
126
+ - When the view is narrower than the pixel width, each block is drawn as a discrete segment.
127
+ - The axis sits just below the container at `top: 100%`; don't clip it with `overflow: hidden`.
128
+ - d3 packages are external imports, not bundled; they dedupe with your own D3.
129
+ - Editing a selection that is partly outside the view re-derives both edges from the visible span, so the off-screen edge snaps to the view bound.
130
+
131
+ ## Development
132
+
133
+ ```bash
134
+ npm run verify # lint, format check, typecheck, tests, build
135
+ npm run demo # serves the repo; open /demo/
136
+ ```
137
+
138
+ Files named `*.browser.test.ts(x)` run in headless Chromium; every other test is a Node unit test. One-time setup: `npx playwright install chromium` and `npm run prepare` (installs the commit-message hook). Shared test fakes live in `src/testing/`.
139
+
140
+ Commits follow [Conventional Commits](https://www.conventionalcommits.org); commitlint checks each message. Releases are automated: release-please opens a release PR from the commit history, and merging it tags the version, publishes the GitHub release, and stages the npm publish for approval with 2FA. There is no npm token anywhere.