@spotlab/hand-matrix 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 James DiGioia
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,194 @@
1
- # Temporary Holding Version
1
+ # @spotlab/hand-matrix
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
+ A 13x13 poker hand-matrix React component. Peer-deps `react`, `react-dom`,
4
+ and [`@spotlab/range`](../range) — no Tailwind, no bundler-plugin
5
+ requirement.
6
+
7
+ ```tsx
8
+ import { RangeGrid } from "@spotlab/hand-matrix";
9
+ import "@spotlab/hand-matrix/style.css";
10
+ import { initRange } from "@spotlab/range";
11
+
12
+ await initRange(); // once, at startup
13
+
14
+ <RangeGrid range="AKs:0.5,QQ" />;
15
+ ```
16
+
17
+ ## Exports
18
+
19
+ Everything below is exported from the package root.
20
+
21
+ ### Display
22
+
23
+ - `HandMatrix` — the bare 13x13 grid primitive. Takes no range data; styling
24
+ is entirely the caller's job via `comboStyle`, keyed by combo string (e.g.
25
+ `"AKs"`). Each cell carries a `data-combo` attribute. Props:
26
+ - `comboStyle?: (combo) => CSSProperties` — per-cell style.
27
+ - `onPointerDown?`, `onPointerEnter?: CellPointerHandler` — the
28
+ `(combo, event)` handlers to use for dragging across cells, with a mouse
29
+ or a finger. `onPointerDown` releases the implicit pointer capture a
30
+ touch takes on its first cell. `onPointerEnter` fires once each time the
31
+ pointer moves onto a new cell, found by hit-testing each `pointermove`
32
+ (`elementFromPoint`), so it also fires during a touch drag, where
33
+ enter events never reach the other cells. Passing `onPointerDown` sets
34
+ `touch-action: none` and `user-select: none` on the grid, so a drag
35
+ neither scrolls the page nor selects the combo text it crosses.
36
+ - `className?: string` — merged onto the `.spotlab-hm-root` wrapper.
37
+ - `RangeGrid` — a read-only display component. Accepts the expanded
38
+ `HandFrequency[]` form or a range string directly (parsed via
39
+ `@spotlab/range`), and fills each cell to its combo's mean weight. Props:
40
+ `range: HandFrequency[] | string`, `fill?: string` (a CSS color, defaulting
41
+ to `var(--spotlab-hm-fill)`), `className?: string`.
42
+ - `cellFillGradient(stops: string[]): string` / `CELL_FILL_DIRECTION` — the
43
+ canonical left-to-right gradient helper used to render a cell's fill.
44
+ Returns `"transparent"` for no stops.
45
+
46
+ `RangeGrid`'s combo collapse (`toComboWeights`) is a wasm call, so
47
+ `initRange()` must have resolved before rendering it — see
48
+ [`@spotlab/range`](../range)'s own README for why that call is explicit
49
+ rather than automatic.
50
+
51
+ ### Editing
52
+
53
+ The editor splits a parent range across a set of actions (e.g. fold, call,
54
+ raise). Its state is an array of 169 `EditorCell`s, one per combo in
55
+ `@spotlab/range`'s `COMBOS` order. Every component is fully controlled: state
56
+ goes in as props and changes come out through `on*Change` callbacks.
57
+
58
+ - `RangeMatrixEditor` — the paintable grid. Draws each cell's action bands,
59
+ hatches dead cells (`avail` 0), marks capped cells (`avail` below 1), and
60
+ wires brush/eyedropper painting onto `HandMatrix` for mouse and touch.
61
+ Props (`RangeMatrixEditorProps`):
62
+ - `cells: EditorCell[]` and `onCellsChange(cells)`.
63
+ - `actions: EditorActionSpec[]` — index-aligned with each cell's
64
+ `weights`.
65
+ - `order: number[]` — indices into `actions`, in the order bands stack.
66
+ - `brush: number[]` and `onBrushChange(brush)` — the weights a paint
67
+ stroke applies, index-aligned with `actions`.
68
+ - `tool: PaintTool` and `onToolChange(tool)` — `"brush"` paints on press
69
+ and drag; `"eyedropper"` loads a cell's weights onto the brush and
70
+ switches back to `"brush"`.
71
+ - `className?: string`.
72
+ - `ActionWeightSliders` — one slider and numeric input per action for
73
+ editing a brush (or any weight row). Moving one action rebalances the
74
+ others so the row keeps summing to 100. Props (`ActionWeightSlidersProps`):
75
+ - `actions`, `order`, `weights: number[]` and `onWeightsChange(weights)`.
76
+ - `passive: number` — the index of the remainder action, which absorbs
77
+ whatever the others leave.
78
+ - `renderActionLabel?(action, index, isPassive)` — replaces a row's
79
+ default label.
80
+ - `renderActionExtra?(action, index)` — content after the label (e.g. a
81
+ remove button).
82
+ - `components?: { WeightInput?: ComponentType<WeightInputProps> }` — swap
83
+ the default `<input type="number">`.
84
+ - `className?: string`.
85
+ - `useRangePainting(opts)` — the brush/eyedropper state machine behind
86
+ `RangeMatrixEditor`, for building your own grid. `opts` takes the editor's
87
+ `cells`, `brush`, `tool` and their three `on*Change` callbacks. Returns
88
+ `{ onCellDown(combo), onCellEnter(combo) }`; wire them to `HandMatrix`'s
89
+ `onPointerDown`/`onPointerEnter`. A stroke ends on any window `pointerup`
90
+ or `pointercancel`.
91
+ - `seedCells(avail, actionWeights, passive): EditorCell[]` — build the
92
+ initial grid from a parent range's per-combo availability (`null` for
93
+ unconstrained) and each action's absolute per-combo weights (the
94
+ `Map<string, number>` that `toComboWeights` from `@spotlab/range`
95
+ produces).
96
+ - `cellsToComboWeights(cells, actionIndex): Map<string, number>` — collapse
97
+ one action back to absolute per-combo weights, ready to save as a range.
98
+ - `comboWeightsToCells(cells, actionWeights, passive): EditorCell[]` — the
99
+ inverse: re-derive the cells' conditional weights from absolute
100
+ per-action ranges, keeping each cell's `avail`.
101
+ - `rebalance(weights, index, value, passive): number[]` — set one action's
102
+ weight and rescale the rest so the row sums to 100. This is what
103
+ `ActionWeightSliders` uses.
104
+ - Types: `EditorCell` (`{ avail: number; weights: number[] }`, where `avail`
105
+ is the combo's cap in the parent range and `weights` are each action's
106
+ share of it, summing to 100), `EditorActionSpec`
107
+ (`{ id: string; label: string; color: string }`), `PaintTool`,
108
+ `CellPointerHandler`, `RangeMatrixEditorProps`, `ActionWeightSlidersProps`,
109
+ `WeightInputProps`.
110
+
111
+ **The editor works at combo level.** A cell is one of the 169 combos (`AKs`,
112
+ `AKo`, `QQ`), so suit-specific hands such as `AsKd` don't survive a round
113
+ trip: `seedCells` takes combo weights, and `cellsToComboWeights` returns
114
+ them. To keep suit-specific hands, set them aside before editing and add them
115
+ back to the result yourself.
116
+
117
+ ## Theming
118
+
119
+ The package ships `hand-matrix.css` (`@spotlab/hand-matrix/style.css`),
120
+ scoped entirely under a `.spotlab-hm-root` wrapper that every top-level
121
+ component renders for itself, merged with any `className` passed in. Every
122
+ custom property has a default declared on `.spotlab-hm-root`, so the
123
+ component renders correctly with zero theming.
124
+
125
+ Because the `className` you pass to a component lands on that same root, a
126
+ class that sets variables works there too (`<RangeGrid className="my-theme" />`
127
+ with `.my-theme { --spotlab-hm-border: … }`), in addition to the
128
+ `.spotlab-hm-root` selector form below.
129
+
130
+ **To override a variable, target `.spotlab-hm-root` itself** — not `:root`
131
+ and not any other ancestor. Because the package's own default is declared
132
+ directly on `.spotlab-hm-root`, a value set only on an ancestor loses to it
133
+ regardless of specificity (an element's own declared value always beats an
134
+ inherited one). This is the single most likely integration mistake:
135
+
136
+ ```css
137
+ /* This does NOT win: */
138
+ :root {
139
+ --spotlab-hm-fill: #f59e0b;
140
+ }
141
+
142
+ /* This does: */
143
+ .spotlab-hm-root {
144
+ --spotlab-hm-fill: #f59e0b;
145
+ }
146
+ ```
147
+
148
+ **The package's defaults live in a named CSS cascade layer** (`@layer
149
+ spotlab-hand-matrix`), not unlayered — this is what makes the above override
150
+ reliable. Per the CSS cascade, unlayered styles always beat every `@layer`
151
+ regardless of specificity or source order, so if the package's defaults were
152
+ unlayered, a plain override would only win when it happened to load _after_
153
+ the package's stylesheet; load it first, or wrap it in your own `@layer`,
154
+ and the override would silently lose instead. Putting the defaults in a
155
+ named layer makes any unlayered override — the common case for a plain CSS
156
+ file or style tag, which is exactly what most consumers write — win
157
+ automatically, regardless of import order.
158
+
159
+ The one case this doesn't cover for free: if **your own styles are also
160
+ inside a named layer** (for example, a Tailwind v4 app that wraps its design
161
+ tokens in `@layer base`), layer priority is decided by each layer name's
162
+ _first appearance_ in the stylesheet, not by where the override rule itself
163
+ sits. A layer named for the first time later in the cascade outranks one
164
+ named earlier, so you must import `@spotlab/hand-matrix/style.css` **before**
165
+ whatever first establishes your own layer order (e.g. before `@import
166
+ "tailwindcss"`, which emits `@layer theme, base, components, utilities;` as
167
+ its first statement) — otherwise `spotlab-hand-matrix` gets appended last and
168
+ outranks your override instead:
169
+
170
+ ```css
171
+ /* Correct: registers `spotlab-hand-matrix` before Tailwind's layers, so
172
+ `@layer base` (where your override lives) outranks it. */
173
+ @import "@spotlab/hand-matrix/style.css";
174
+ @import "tailwindcss";
175
+ ```
176
+
177
+ If you don't use cascade layers yourself (e.g. a plain CSS file, or an
178
+ Obsidian plugin with no bundler-level layer system), none of this applies —
179
+ your override is unlayered and wins unconditionally, in any import order.
180
+
181
+ Available variables: `--spotlab-hm-border`, `--spotlab-hm-border-width`,
182
+ `--spotlab-hm-cell-bg`, `--spotlab-hm-cell-fg`, `--spotlab-hm-fill`,
183
+ `--spotlab-hm-font-family`, `--spotlab-hm-font-size`,
184
+ `--spotlab-hm-font-weight`, `--spotlab-hm-radius`, `--spotlab-hm-surface-bg`,
185
+ `--spotlab-hm-muted-bg`, `--spotlab-hm-muted-fg`, `--spotlab-hm-track-bg`,
186
+ `--spotlab-hm-row-passive-bg`, `--spotlab-hm-dead-hatch`.
187
+
188
+ Action/fill colors (`RangeGrid`'s `fill` prop) are **not** theme variables —
189
+ they are data, passed as props. `--spotlab-hm-fill` is only the default used
190
+ when `fill` is omitted.
191
+
192
+ ## License
193
+
194
+ MIT — see `LICENSE`.
@@ -0,0 +1,35 @@
1
+ import { ChangeEvent, ComponentType, FC, ReactNode } from 'react';
2
+ import { EditorActionSpec } from './weights';
3
+ export type WeightInputProps = {
4
+ value: number;
5
+ "aria-label": string;
6
+ onChange: (event: ChangeEvent<HTMLInputElement>) => void;
7
+ };
8
+ export type ActionWeightSlidersProps = {
9
+ actions: EditorActionSpec[];
10
+ /** Indices into `actions`/`weights`, in the order rows should render. */
11
+ order: number[];
12
+ /** Index-aligned with `actions`. */
13
+ weights: number[];
14
+ passive: number;
15
+ onWeightsChange: (weights: number[]) => void;
16
+ /**
17
+ * Leading content for a row's header, replacing the default label — the
18
+ * slot for a control that takes the label's own position (e.g. a
19
+ * raise-size input) rather than trailing it. Defaults to the action's
20
+ * label (plus a "· remainder" suffix on the passive row).
21
+ */
22
+ renderActionLabel?: (action: EditorActionSpec, index: number, isPassive: boolean) => ReactNode;
23
+ /** Extra content rendered after an action's label — the slot for consumer-specific controls (a remove button). */
24
+ renderActionExtra?: (action: EditorActionSpec, index: number) => ReactNode;
25
+ components?: {
26
+ WeightInput?: ComponentType<WeightInputProps>;
27
+ };
28
+ className?: string;
29
+ };
30
+ /**
31
+ * One pointer-driven weight slider per action, each paired with a numeric
32
+ * input for keyboard entry. Adjusting one action rebalances the others
33
+ * proportionally so the row's weights keep summing to 100.
34
+ */
35
+ export declare const ActionWeightSliders: FC<ActionWeightSlidersProps>;
@@ -0,0 +1,26 @@
1
+ export declare const COMBO_INDEX: Map<string, number>;
2
+ /**
3
+ * One matrix cell's painted state: `avail` is the combo's cap (its weight in the
4
+ * parent range the editor is splitting), `weights` are each action's conditional
5
+ * share of that cap, summing to 100 across the node's actions.
6
+ */
7
+ export type EditorCell = {
8
+ avail: number;
9
+ weights: number[];
10
+ };
11
+ /**
12
+ * Seed the 169-cell grid from a parent range's per-combo availability and each
13
+ * action's own per-combo range, both expressed as absolute weights (a share of
14
+ * the whole deck, as `toComboWeights` from `@spotlab/range` produces). `avail`
15
+ * of `null` means the combo is unconstrained (available at 1). `passive` must be
16
+ * a valid index into `actionWeights` naming the remainder action.
17
+ */
18
+ export declare function seedCells(avail: Map<string, number> | null, actionWeights: (Map<string, number> | null)[], passive: number): EditorCell[];
19
+ /** Collapse one action's conditional weights back to absolute per-combo weights. */
20
+ export declare function cellsToComboWeights(cells: EditorCell[], actionIndex: number): Map<string, number>;
21
+ /**
22
+ * Re-derive conditional cell weights from absolute per-action ranges — the
23
+ * inverse of `cellsToComboWeights`. `passive` must be a valid index into
24
+ * `actionWeights` naming the remainder action.
25
+ */
26
+ export declare function comboWeightsToCells(cells: EditorCell[], actionWeights: (Map<string, number> | null)[], passive: number): EditorCell[];
@@ -0,0 +1,24 @@
1
+ import { FC } from 'react';
2
+ import { PaintTool } from './use-range-painting';
3
+ import { EditorCell } from './cells';
4
+ import { EditorActionSpec } from './weights';
5
+ export type RangeMatrixEditorProps = {
6
+ /** The 169-cell grid, in `@spotlab/range`'s `COMBOS` order. */
7
+ cells: EditorCell[];
8
+ /** Index-aligned with each cell's `weights` array. */
9
+ actions: EditorActionSpec[];
10
+ /** Indices into `actions`/`weights`, in the order bands should stack — a display concern the consumer owns (e.g. biggest raise first). */
11
+ order: number[];
12
+ brush: number[];
13
+ tool: PaintTool;
14
+ onCellsChange: (cells: EditorCell[]) => void;
15
+ onBrushChange: (brush: number[]) => void;
16
+ onToolChange: (tool: PaintTool) => void;
17
+ className?: string;
18
+ };
19
+ /**
20
+ * The paintable 13x13 range grid: per-cell action bands, the dead-cell hatch,
21
+ * the capped-cell cap marker, and the brush/eyedropper painting wired onto
22
+ * `HandMatrix`. Fully controlled; `useRangePainting` drives the state changes.
23
+ */
24
+ export declare const RangeMatrixEditor: FC<RangeMatrixEditorProps>;
@@ -0,0 +1,23 @@
1
+ import { EditorCell } from './cells';
2
+ export type PaintTool = "brush" | "eyedropper";
3
+ /**
4
+ * The brush/eyedropper state machine behind `RangeMatrixEditor`: drag-painting
5
+ * and eyedropping a cell's weights back onto the brush. Fully controlled — all
6
+ * state changes go out through the `on*Change` callbacks.
7
+ *
8
+ * Wire `onCellDown` to a cell's pointer-down and `onCellEnter` to the pointer
9
+ * moving onto a cell (`HandMatrix`'s `onPointerDown`/`onPointerEnter` do both,
10
+ * for mouse and touch alike). A stroke ends on any window `pointerup` or
11
+ * `pointercancel`.
12
+ */
13
+ export declare function useRangePainting(opts: {
14
+ cells: EditorCell[];
15
+ brush: number[];
16
+ tool: PaintTool;
17
+ onCellsChange: (cells: EditorCell[]) => void;
18
+ onBrushChange: (brush: number[]) => void;
19
+ onToolChange: (tool: PaintTool) => void;
20
+ }): {
21
+ onCellDown: (combo: string) => void;
22
+ onCellEnter: (combo: string) => void;
23
+ };
@@ -0,0 +1,13 @@
1
+ export declare const fixRound: (weights: number[], keep: number) => number[];
2
+ /**
3
+ * `passive` names the remainder action — the one whose weight absorbs whatever
4
+ * the others leave behind. It must be a valid index into `weights`; the caller
5
+ * decides which action that is and guarantees one exists.
6
+ */
7
+ export declare const rebalance: (weights: number[], index: number, value: number, passive: number) => number[];
8
+ export declare const sumActive: (weights: number[], passive: number) => number;
9
+ export type EditorActionSpec = {
10
+ id: string;
11
+ label: string;
12
+ color: string;
13
+ };
@@ -0,0 +1,147 @@
1
+ /*
2
+ * Defaults live in a named layer, not unlayered: unlayered CSS always beats
3
+ * every `@layer`, even one a consumer wrote specifically to override these
4
+ * variables. A named layer lets a consumer's plain (unlayered) override CSS
5
+ * win regardless of import order, and lets a consumer who uses cascade
6
+ * layers themselves (e.g. Tailwind) place their own override layer after
7
+ * this one. See the "Theming" section of README.md for how to register this
8
+ * layer's priority relative to a bundler-level layer system.
9
+ */
10
+ @layer spotlab-hand-matrix {
11
+ .spotlab-hm-root {
12
+ --spotlab-hm-border: #d4d4d8;
13
+ --spotlab-hm-border-width: 1px;
14
+ --spotlab-hm-cell-bg: transparent;
15
+ --spotlab-hm-cell-fg: #18181b;
16
+ --spotlab-hm-muted-bg: #f4f4f5;
17
+ --spotlab-hm-muted-fg: #71717a;
18
+ --spotlab-hm-fill: #10b981;
19
+ --spotlab-hm-font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
20
+ --spotlab-hm-font-size: 0.75rem;
21
+ --spotlab-hm-font-weight: 600;
22
+ --spotlab-hm-dead-hatch: repeating-linear-gradient(
23
+ 45deg,
24
+ rgba(15, 23, 42, 0.22) 0 3px,
25
+ rgba(255, 255, 255, 0.18) 3px 6px
26
+ );
27
+ --spotlab-hm-radius: 0.5rem;
28
+ --spotlab-hm-surface-bg: #ffffff;
29
+ --spotlab-hm-track-bg: #e4e4e7;
30
+ --spotlab-hm-row-passive-bg: rgba(244, 244, 245, 0.4);
31
+ }
32
+ }
33
+
34
+ .spotlab-hm-row {
35
+ display: flex;
36
+ flex-flow: row nowrap;
37
+ }
38
+
39
+ .spotlab-hm-cell {
40
+ aspect-ratio: 1 / 1;
41
+ width: calc(100% / 13);
42
+ overflow: hidden;
43
+ background-color: var(--spotlab-hm-cell-bg);
44
+ border: var(--spotlab-hm-border-width) solid var(--spotlab-hm-border);
45
+ font-family: var(--spotlab-hm-font-family);
46
+ font-size: var(--spotlab-hm-font-size);
47
+ font-weight: var(--spotlab-hm-font-weight);
48
+ color: var(--spotlab-hm-cell-fg);
49
+ }
50
+
51
+ .spotlab-hm-cell-inner {
52
+ display: flex;
53
+ height: 100%;
54
+ width: 100%;
55
+ flex-direction: column;
56
+ align-items: center;
57
+ justify-content: center;
58
+ }
59
+
60
+ .spotlab-hm-sliders {
61
+ display: flex;
62
+ flex-direction: column;
63
+ gap: 8px;
64
+ }
65
+
66
+ .spotlab-hm-slider-row {
67
+ display: flex;
68
+ flex-direction: column;
69
+ border-radius: var(--spotlab-hm-radius);
70
+ border: var(--spotlab-hm-border-width) solid var(--spotlab-hm-border);
71
+ padding: 10px 12px;
72
+ background: var(--spotlab-hm-surface-bg);
73
+ }
74
+
75
+ .spotlab-hm-slider-row[data-passive="true"] {
76
+ background: var(--spotlab-hm-row-passive-bg);
77
+ }
78
+
79
+ .spotlab-hm-slider-header {
80
+ display: flex;
81
+ align-items: center;
82
+ gap: 8px;
83
+ margin-bottom: 8px;
84
+ }
85
+
86
+ .spotlab-hm-slider-swatch {
87
+ flex: none;
88
+ height: 14px;
89
+ width: 14px;
90
+ border-radius: 3px;
91
+ border: var(--spotlab-hm-border-width) solid var(--spotlab-hm-border);
92
+ }
93
+
94
+ .spotlab-hm-slider-label {
95
+ flex: 1;
96
+ font-size: 13px;
97
+ font-weight: 600;
98
+ font-family: var(--spotlab-hm-font-family);
99
+ color: var(--spotlab-hm-cell-fg);
100
+ }
101
+
102
+ .spotlab-hm-slider-remainder {
103
+ font-size: 12px;
104
+ font-weight: 400;
105
+ color: var(--spotlab-hm-muted-fg);
106
+ }
107
+
108
+ .spotlab-hm-slider-track-row {
109
+ display: flex;
110
+ align-items: center;
111
+ gap: 10px;
112
+ }
113
+
114
+ .spotlab-hm-slider-track {
115
+ position: relative;
116
+ flex: 1;
117
+ height: 8px;
118
+ cursor: pointer;
119
+ border-radius: 5px;
120
+ border: var(--spotlab-hm-border-width) solid var(--spotlab-hm-border);
121
+ touch-action: none;
122
+ }
123
+
124
+ .spotlab-hm-slider-thumb {
125
+ position: absolute;
126
+ top: 50%;
127
+ height: 16px;
128
+ width: 16px;
129
+ transform: translate(-50%, -50%);
130
+ border-radius: 50%;
131
+ background: var(--spotlab-hm-surface-bg);
132
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
133
+ }
134
+
135
+ .spotlab-hm-weight-input {
136
+ height: 32px;
137
+ width: 58px;
138
+ flex: none;
139
+ padding: 0 6px;
140
+ text-align: center;
141
+ font-size: 13px;
142
+ font-family: var(--spotlab-hm-font-family);
143
+ color: var(--spotlab-hm-cell-fg);
144
+ background: transparent;
145
+ border-radius: calc(var(--spotlab-hm-radius) * 0.5);
146
+ border: var(--spotlab-hm-border-width) solid var(--spotlab-hm-border);
147
+ }
@@ -0,0 +1,33 @@
1
+ import { CSSProperties, FC, PointerEvent as ReactPointerEvent } from 'react';
2
+ export declare const CELL_FILL_DIRECTION = "to right";
3
+ export declare const cellFillGradient: (stops: string[]) => string;
4
+ export type CellPointerHandler = (combo: string, event: ReactPointerEvent<HTMLDivElement>) => void;
5
+ type ComboStyleProp = {
6
+ comboStyle?: (combo: string) => CSSProperties;
7
+ };
8
+ type GridPointerHandlers = {
9
+ /**
10
+ * A pointer (mouse, pen or touch) went down on `combo`'s cell. The grid
11
+ * first releases the implicit pointer capture a touch takes on the element
12
+ * it starts on, so later moves can resolve to other cells. Passing this
13
+ * also makes the grid root a drag surface (see `dragSurfaceStyle`).
14
+ */
15
+ onPointerDown?: CellPointerHandler;
16
+ /**
17
+ * The pointer moved onto `combo`'s cell. Resolved by hit-testing each
18
+ * `pointermove` (`elementFromPoint`) rather than by enter events, so it
19
+ * fires across a touch drag, where `mouseenter`/`pointerenter` never reach
20
+ * the other cells, as well as for a mouse. Fires once per change of cell,
21
+ * whether or not a button is pressed.
22
+ */
23
+ onPointerEnter?: CellPointerHandler;
24
+ };
25
+ /**
26
+ * The bare 13x13 grid. Each cell carries a `data-combo` attribute. Passing
27
+ * `onPointerDown` marks the grid as a drag surface (`dragSurfaceStyle`): a
28
+ * drag reaches the handlers instead of scrolling the page or selecting text.
29
+ */
30
+ export declare const HandMatrix: FC<{
31
+ className?: string;
32
+ } & ComboStyleProp & GridPointerHandlers>;
33
+ export {};
@@ -0,0 +1,7 @@
1
+ export { HandMatrix, cellFillGradient, CELL_FILL_DIRECTION, type CellPointerHandler, } from './hand-matrix';
2
+ export { RangeGrid } from './range-grid';
3
+ export { RangeMatrixEditor, type RangeMatrixEditorProps, } from './editor/matrix-editor';
4
+ export { ActionWeightSliders, type ActionWeightSlidersProps, type WeightInputProps, } from './editor/action-sliders';
5
+ export { useRangePainting, type PaintTool } from './editor/use-range-painting';
6
+ export { seedCells, cellsToComboWeights, comboWeightsToCells, type EditorCell, } from './editor/cells';
7
+ export { rebalance, type EditorActionSpec } from './editor/weights';
package/dist/index.js ADDED
@@ -0,0 +1,334 @@
1
+ import { useCallback as g, useEffect as y, useLayoutEffect as I, useMemo as x, useRef as v } from "react";
2
+ import { COMBOS as b, parseRange as S, toComboWeights as A } from "@spotlab/range";
3
+ import { jsx as h, jsxs as w } from "react/jsx-runtime";
4
+ var P = (t, e) => Array.from({ length: Math.ceil(t.length / e) }, (n, r) => t.slice(r * e, r * e + e)), D = "to right", N = (t) => t.length === 0 ? "transparent" : `linear-gradient(${D}, ${t.join(", ")})`, W = (t) => {
5
+ const e = t.target;
6
+ e.hasPointerCapture(t.pointerId) && e.releasePointerCapture(t.pointerId);
7
+ }, M = (t, e, n) => {
8
+ const r = t.ownerDocument.elementFromPoint(e, n)?.closest("[data-combo]");
9
+ return !r || !t.contains(r) ? null : r.getAttribute("data-combo");
10
+ }, T = ({ combo: t, onPointerDown: e, comboStyle: n }) => /* @__PURE__ */ h("div", {
11
+ className: "spotlab-hm-cell",
12
+ "data-combo": t,
13
+ style: n?.(t),
14
+ onPointerDown: e && ((r) => {
15
+ W(r), e(t, r);
16
+ }),
17
+ children: /* @__PURE__ */ h("div", {
18
+ className: "spotlab-hm-cell-inner",
19
+ children: /* @__PURE__ */ h("div", {
20
+ "data-combo": t,
21
+ children: t
22
+ })
23
+ })
24
+ }), B = ({ row: t, ...e }) => /* @__PURE__ */ h("div", {
25
+ className: "spotlab-hm-row",
26
+ children: t.map((n, r) => /* @__PURE__ */ h(T, {
27
+ combo: n,
28
+ ...e
29
+ }, r))
30
+ }), F = {
31
+ touchAction: "none",
32
+ userSelect: "none",
33
+ WebkitUserSelect: "none",
34
+ WebkitTouchCallout: "none"
35
+ }, R = ({ className: t, onPointerDown: e, onPointerEnter: n, ...r }) => {
36
+ const a = v(null), o = (i) => {
37
+ const l = M(i.currentTarget, i.clientX, i.clientY);
38
+ l !== a.current && (a.current = l, l && n?.(l, i));
39
+ };
40
+ return /* @__PURE__ */ h("div", {
41
+ className: ["spotlab-hm-root", t].filter(Boolean).join(" "),
42
+ style: e ? F : void 0,
43
+ onPointerDown: n && ((i) => {
44
+ a.current = M(i.currentTarget, i.clientX, i.clientY);
45
+ }),
46
+ onPointerMove: n && o,
47
+ onPointerLeave: n && (() => a.current = null),
48
+ children: P(b, 13).map((i, l) => /* @__PURE__ */ h(B, {
49
+ row: i,
50
+ onPointerDown: e,
51
+ ...r
52
+ }, l))
53
+ });
54
+ }, O = (t, e) => ({ background: N([`${t} 0 ${e * 100}%`, `transparent ${e * 100}% 100%`]) }), V = ({ range: t, fill: e = "var(--spotlab-hm-fill)", className: n }) => {
55
+ const r = x(() => {
56
+ const a = typeof t == "string" ? S(t) : t;
57
+ return A(a);
58
+ }, [t]);
59
+ return /* @__PURE__ */ h(R, {
60
+ className: n,
61
+ comboStyle: (a) => {
62
+ const o = r.get(a);
63
+ return o ? O(e, o) : {};
64
+ }
65
+ });
66
+ }, X = (t) => Math.max(0, Math.min(100, Math.round(t))), j = (t, e) => {
67
+ const n = t.map(Math.round);
68
+ let r = 100 - n.reduce((a, o) => a + o, 0);
69
+ if (r !== 0) {
70
+ for (let a = 0; a < n.length; a++) if (a !== e && n[a] + r >= 0) {
71
+ n[a] += r, r = 0;
72
+ break;
73
+ }
74
+ if (r !== 0) {
75
+ for (let a = 0; a < n.length; a++) if (a !== e && n[a] > 0) {
76
+ const o = Math.min(n[a], -r);
77
+ if (n[a] -= o, r += o, r === 0) break;
78
+ }
79
+ n[e] = Math.max(0, n[e] + r);
80
+ }
81
+ }
82
+ return n;
83
+ }, _ = (t, e, n, r) => {
84
+ const a = X(n), o = t.slice();
85
+ o[e] = a;
86
+ const i = 100 - a, l = [];
87
+ for (let s = 0; s < t.length; s++) s !== e && l.push(s);
88
+ const u = l.reduce((s, p) => s + t[p], 0);
89
+ if (u <= 0) {
90
+ l.forEach((p) => o[p] = 0);
91
+ const s = e === r ? l[0] : r;
92
+ s != null && (o[s] = i);
93
+ } else l.forEach((s) => o[s] = Math.round(t[s] / u * i));
94
+ return j(o, e);
95
+ }, $ = (t, e) => t.reduce((n, r, a) => a === e ? n : n + r, 0), k = new Map(b.map((t, e) => [t, e])), L = (t, e) => {
96
+ const n = t.slice();
97
+ let r = $(n, e);
98
+ if (r > 100) {
99
+ const a = 100 / r;
100
+ for (let o = 0; o < n.length; o++) o !== e && (n[o] = Math.round(n[o] * a));
101
+ r = $(n, e);
102
+ }
103
+ return n[e] = Math.max(0, 100 - r), n;
104
+ };
105
+ function Z(t, e, n) {
106
+ return b.map((r) => {
107
+ const a = t ? t.get(r) ?? 0 : 1, o = e.map(() => 0);
108
+ return a <= 0 ? (o[n] = 100, {
109
+ avail: 0,
110
+ weights: o
111
+ }) : (e.forEach((i, l) => {
112
+ const u = i?.get(r) ?? 0;
113
+ o[l] = Math.round(u / a * 100);
114
+ }), {
115
+ avail: a,
116
+ weights: L(o, n)
117
+ });
118
+ });
119
+ }
120
+ var q = 8;
121
+ function z(t, e) {
122
+ const n = /* @__PURE__ */ new Map();
123
+ return t.forEach((r, a) => {
124
+ if (r.avail <= 0) return;
125
+ const o = r.weights.reduce((l, u) => l + u, 0);
126
+ if (!o) return;
127
+ const i = Number((r.weights[e] / o * r.avail).toFixed(q));
128
+ i && n.set(b[a], i);
129
+ }), n;
130
+ }
131
+ function tt(t, e, n) {
132
+ return t.map((r, a) => {
133
+ const o = e.map(() => 0);
134
+ if (r.avail <= 0)
135
+ return o[n] = 100, {
136
+ avail: r.avail,
137
+ weights: o
138
+ };
139
+ const i = b[a];
140
+ return e.forEach((l, u) => {
141
+ const s = l?.get(i) ?? 0;
142
+ o[u] = Math.round(s / r.avail * 100);
143
+ }), {
144
+ avail: r.avail,
145
+ weights: L(o, n)
146
+ };
147
+ });
148
+ }
149
+ function G(t) {
150
+ const { cells: e, brush: n, tool: r, onCellsChange: a, onBrushChange: o, onToolChange: i } = t, l = v(null), u = g(() => {
151
+ l.current?.abort(), l.current = null;
152
+ }, []);
153
+ y(() => u, [u]);
154
+ const s = v(e);
155
+ I(() => {
156
+ s.current = e;
157
+ }, [e]);
158
+ const p = g((c) => {
159
+ const d = s.current, f = k.get(c);
160
+ if (d[f].avail <= 0) return;
161
+ const C = d.slice();
162
+ C[f] = {
163
+ avail: d[f].avail,
164
+ weights: [...n]
165
+ }, s.current = C, a(C);
166
+ }, [n, a]), m = g((c) => {
167
+ const d = k.get(c);
168
+ e[d].avail <= 0 || (o([...e[d].weights]), i("brush"));
169
+ }, [
170
+ e,
171
+ o,
172
+ i
173
+ ]);
174
+ return {
175
+ onCellDown: g((c) => {
176
+ if (r === "eyedropper") {
177
+ m(c);
178
+ return;
179
+ }
180
+ u();
181
+ const d = new AbortController();
182
+ l.current = d, window.addEventListener("pointerup", u, { signal: d.signal }), window.addEventListener("pointercancel", u, { signal: d.signal }), p(c);
183
+ }, [
184
+ r,
185
+ m,
186
+ p,
187
+ u
188
+ ]),
189
+ onCellEnter: g((c) => {
190
+ l.current && r === "brush" && p(c);
191
+ }, [r, p])
192
+ };
193
+ }
194
+ var E = (t, e, n) => {
195
+ const r = [];
196
+ let a = 0;
197
+ return n.forEach((o) => {
198
+ const i = (t.weights[o] || 0) / 100;
199
+ if (i <= 0) return;
200
+ const l = e[o].color;
201
+ r.push(`${l} ${a * 100}%`), r.push(`${l} ${(a + i) * 100}%`), a += i;
202
+ }), r.length > 0 && a < 1 - 1e-9 && r.push(`transparent ${a * 100}% 100%`), N(r);
203
+ }, et = ({ cells: t, actions: e, order: n, brush: r, tool: a, onCellsChange: o, onBrushChange: i, onToolChange: l, className: u }) => {
204
+ const { onCellDown: s, onCellEnter: p } = G({
205
+ cells: t,
206
+ brush: r,
207
+ tool: a,
208
+ onCellsChange: o,
209
+ onBrushChange: i,
210
+ onToolChange: l
211
+ });
212
+ return /* @__PURE__ */ h(R, {
213
+ className: u,
214
+ comboStyle: (m) => {
215
+ const c = t[k.get(m)];
216
+ if (c.avail <= 0) return {
217
+ background: "var(--spotlab-hm-dead-hatch)",
218
+ backgroundColor: "var(--spotlab-hm-muted-bg)",
219
+ cursor: "not-allowed",
220
+ color: "var(--spotlab-hm-muted-fg)"
221
+ };
222
+ const d = a === "eyedropper" ? "copy" : "pointer";
223
+ if (c.avail < 1) {
224
+ const f = `${c.avail * 100}%`;
225
+ return {
226
+ background: `repeating-linear-gradient(180deg, var(--spotlab-hm-muted-fg) 0 4px, transparent 4px 8px), var(--spotlab-hm-dead-hatch), ${E(c, e, n)}`,
227
+ backgroundSize: `1px 100%, ${(1 - c.avail) * 100}% 100%, ${c.avail * 100}% 100%`,
228
+ backgroundPosition: `${f} center, right center, left center`,
229
+ backgroundRepeat: "no-repeat, no-repeat, no-repeat",
230
+ cursor: d
231
+ };
232
+ }
233
+ return {
234
+ background: E(c, e, n),
235
+ cursor: d
236
+ };
237
+ },
238
+ onPointerDown: s,
239
+ onPointerEnter: p
240
+ });
241
+ }, U = (t) => /* @__PURE__ */ h("input", {
242
+ type: "number",
243
+ className: "spotlab-hm-weight-input",
244
+ ...t
245
+ }), Y = ({ value: t, color: e, passive: n, onChange: r }) => {
246
+ const a = v(null), o = v(!1), i = g((l) => {
247
+ const u = a.current;
248
+ if (!u) return;
249
+ const s = u.getBoundingClientRect();
250
+ r((l - s.left) / s.width * 100);
251
+ }, [r]);
252
+ return y(() => {
253
+ const l = (s) => {
254
+ o.current && i(s.clientX);
255
+ }, u = () => o.current = !1;
256
+ return window.addEventListener("pointermove", l), window.addEventListener("pointerup", u), () => {
257
+ window.removeEventListener("pointermove", l), window.removeEventListener("pointerup", u);
258
+ };
259
+ }, [i]), /* @__PURE__ */ h("div", {
260
+ ref: a,
261
+ role: "slider",
262
+ "aria-valuenow": t,
263
+ "aria-valuemin": 0,
264
+ "aria-valuemax": 100,
265
+ className: "spotlab-hm-slider-track",
266
+ style: { background: `linear-gradient(to right, ${e} 0 ${t}%, var(--spotlab-hm-track-bg) ${t}% 100%)` },
267
+ onPointerDown: (l) => {
268
+ o.current = !0, i(l.clientX);
269
+ },
270
+ children: /* @__PURE__ */ h("div", {
271
+ className: "spotlab-hm-slider-thumb",
272
+ style: {
273
+ left: `${t}%`,
274
+ border: `2px solid ${n ? "var(--spotlab-hm-muted-fg)" : e}`
275
+ }
276
+ })
277
+ });
278
+ }, H = (t, e) => /* @__PURE__ */ w("span", {
279
+ className: "spotlab-hm-slider-label",
280
+ children: [t.label, e && /* @__PURE__ */ h("span", {
281
+ className: "spotlab-hm-slider-remainder",
282
+ children: " · remainder"
283
+ })]
284
+ }), rt = ({ actions: t, order: e, weights: n, passive: r, onWeightsChange: a, renderActionLabel: o, renderActionExtra: i, components: l, className: u }) => {
285
+ const s = l?.WeightInput ?? U, p = (m, c) => {
286
+ a(_(n, m, c, r));
287
+ };
288
+ return /* @__PURE__ */ h("div", {
289
+ className: ["spotlab-hm-root spotlab-hm-sliders", u].filter(Boolean).join(" "),
290
+ children: e.map((m) => {
291
+ const c = t[m], d = m === r;
292
+ return /* @__PURE__ */ w("div", {
293
+ className: "spotlab-hm-slider-row",
294
+ "data-passive": d,
295
+ children: [/* @__PURE__ */ w("div", {
296
+ className: "spotlab-hm-slider-header",
297
+ children: [
298
+ /* @__PURE__ */ h("span", {
299
+ className: "spotlab-hm-slider-swatch",
300
+ style: { background: c.color }
301
+ }),
302
+ o?.(c, m, d) ?? H(c, d),
303
+ i?.(c, m)
304
+ ]
305
+ }), /* @__PURE__ */ w("div", {
306
+ className: "spotlab-hm-slider-track-row",
307
+ children: [/* @__PURE__ */ h(Y, {
308
+ value: n[m],
309
+ color: c.color,
310
+ passive: d,
311
+ onChange: (f) => p(m, f)
312
+ }), /* @__PURE__ */ h(s, {
313
+ value: n[m],
314
+ "aria-label": `${c.label} weight`,
315
+ onChange: (f) => p(m, parseInt(f.target.value || "0", 10))
316
+ })]
317
+ })]
318
+ }, c.id);
319
+ })
320
+ });
321
+ };
322
+ export {
323
+ rt as ActionWeightSliders,
324
+ D as CELL_FILL_DIRECTION,
325
+ R as HandMatrix,
326
+ V as RangeGrid,
327
+ et as RangeMatrixEditor,
328
+ N as cellFillGradient,
329
+ z as cellsToComboWeights,
330
+ tt as comboWeightsToCells,
331
+ _ as rebalance,
332
+ Z as seedCells,
333
+ G as useRangePainting
334
+ };
@@ -0,0 +1,13 @@
1
+ import { FC } from 'react';
2
+ import { HandFrequency } from '@spotlab/range';
3
+ /**
4
+ * A read-only 13x13 range grid: each cell fills to its combo's mean weight.
5
+ * Accepts the expanded hand-keyed form or a range string directly — a range
6
+ * string is parsed via `@spotlab/range`, so the caller never needs to touch
7
+ * the parser itself to render one.
8
+ */
9
+ export declare const RangeGrid: FC<{
10
+ range: HandFrequency[] | string;
11
+ fill?: string;
12
+ className?: string;
13
+ }>;
package/package.json CHANGED
@@ -1,6 +1,78 @@
1
1
  {
2
2
  "name": "@spotlab/hand-matrix",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "A 13x13 poker hand-matrix React component: the read-only grid primitive and a range-string-aware display component, themed through CSS custom properties.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/mAAdhaTTah/spotlab",
10
+ "directory": "packages/hand-matrix"
11
+ },
12
+ "homepage": "https://github.com/mAAdhaTTah/spotlab/tree/main/packages/hand-matrix#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/mAAdhaTTah/spotlab/issues"
15
+ },
16
+ "author": "James DiGioia",
17
+ "keywords": [
18
+ "poker",
19
+ "range",
20
+ "hand-matrix",
21
+ "react",
22
+ "component",
23
+ "texas-holdem"
24
+ ],
25
+ "sideEffects": [
26
+ "*.css"
27
+ ],
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ },
33
+ "./style.css": "./dist/hand-matrix.css"
34
+ },
35
+ "main": "./dist/index.js",
36
+ "types": "./dist/index.d.ts",
37
+ "files": [
38
+ "dist",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
42
+ "peerDependencies": {
43
+ "@spotlab/range": "^0.2.0",
44
+ "react": "^19.0.0",
45
+ "react-dom": "^19.0.0"
46
+ },
47
+ "devDependencies": {
48
+ "@spotlab/range": "0.2.0",
49
+ "@storybook/addon-vitest": "^10.5.5",
50
+ "@storybook/react-vite": "^10.5.5",
51
+ "@types/react": "^19.2.16",
52
+ "@types/react-dom": "^19.2.3",
53
+ "@vitejs/plugin-react": "^6.0.0",
54
+ "@vitest/browser": "^4.1.4",
55
+ "@vitest/browser-playwright": "4.1.11",
56
+ "playwright": "^1.49.1",
57
+ "react": "^19.2.7",
58
+ "react-dom": "^19.2.7",
59
+ "storybook": "^10.5.5",
60
+ "typescript": "^6.0.0",
61
+ "vite": "^8.3.1",
62
+ "vite-plugin-dts": "^5.1.2",
63
+ "vitest": "^4.1.4"
64
+ },
65
+ "publishConfig": {
66
+ "access": "public"
67
+ },
68
+ "scripts": {
69
+ "typecheck": "tsc -p tsconfig.json",
70
+ "build:vite": "vite build",
71
+ "build:css": "node scripts/copy-css.mjs",
72
+ "build": "pnpm typecheck && pnpm build:vite && pnpm build:css",
73
+ "test": "vitest run --project unit",
74
+ "storybook": "storybook dev -p 6007",
75
+ "build-storybook": "storybook build",
76
+ "test-storybook": "NODE_ENV=production vitest run --project storybook"
77
+ }
6
78
  }