@sheetwrite/svelte 0.1.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 Sheetwrite contributors
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 ADDED
@@ -0,0 +1,50 @@
1
+ # @sheetwrite/svelte
2
+
3
+ Svelte 5 components for Sheetwrite.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add @sheetwrite/svelte
9
+ ```
10
+
11
+ Svelte 5 is a peer dependency. Core and WASM arrive transitively.
12
+
13
+ ## Data-first component
14
+
15
+ ```svelte
16
+ <script lang="ts">
17
+ import { Sheetwrite } from "@sheetwrite/svelte";
18
+ import "@sheetwrite/svelte/styles.css";
19
+ </script>
20
+
21
+ <Sheetwrite
22
+ {columns}
23
+ defaultRows={products}
24
+ height="500px"
25
+ onGridChange={(event) => console.log(event.source, event.transaction.patches)}
26
+ />
27
+ ```
28
+
29
+ `defaultRows` seeds an uncontrolled grid and is never mutated. Changing its identity intentionally creates a new generation. Use `height` or `fill`; `fill` requires an already-sized ancestor.
30
+
31
+ `onGridChange` carries the complete transaction. Persist `event.transaction.patches`, not only `event.changes`; use `SyncCoordinator` for acknowledgements, retry, and remote ordering.
32
+
33
+ WASM initializes on client mount. Use `fallback` while loading, `onInitializationError` for failures, and `wasmSource` for an explicit source.
34
+
35
+ ## Advanced component
36
+
37
+ ```svelte
38
+ <SheetwriteGrid bind:grid {workbook} {data} fill />
39
+ ```
40
+
41
+ The bindable `grid` is published before `onReady({ grid, generation, reason })` and clears during replacement or unmount. Reset-bound inputs replace the generation; presentation props update live.
42
+
43
+ Grid events cover changes, viewport, selection, editing, search, and active-sheet changes.
44
+
45
+ For vanilla/preload control, use `initSheetwrite()` and `createGrid()` from `@sheetwrite/core`. See [installation](https://sheetwrite.vercel.app/docs/start/installation/), [Svelte integration](https://sheetwrite.vercel.app/docs/frameworks/svelte/), and [collaboration](https://sheetwrite.vercel.app/docs/guides/collaboration/).
46
+
47
+ XLSX is not installed by this adapter. If the toolbar configuration enables
48
+ XLSX export, install `@sheetwrite/xlsx` and import
49
+ `@sheetwrite/xlsx/register` before the action (or lazily inside its handler).
50
+ CSV/TSV require no optional package.
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@sheetwrite/svelte",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Svelte adapter for Sheetwrite.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/chh-ay/Sheetwrite.git",
10
+ "directory": "packages/svelte"
11
+ },
12
+ "homepage": "https://chh-ay.github.io/Sheetwrite/",
13
+ "bugs": {
14
+ "url": "https://github.com/chh-ay/Sheetwrite/issues"
15
+ },
16
+ "keywords": [
17
+ "spreadsheet",
18
+ "data-grid",
19
+ "svelte",
20
+ "canvas"
21
+ ],
22
+ "svelte": "./src/index.ts",
23
+ "exports": {
24
+ ".": {
25
+ "svelte": "./src/index.ts",
26
+ "types": "./src/index.ts",
27
+ "default": "./src/index.ts"
28
+ },
29
+ "./styles.css": "./styles.css"
30
+ },
31
+ "files": [
32
+ "src",
33
+ "styles.css"
34
+ ],
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "provenance": true
38
+ },
39
+ "scripts": {
40
+ "build": "bun run typecheck",
41
+ "typecheck": "svelte-check --tsconfig ./tsconfig.json"
42
+ },
43
+ "peerDependencies": {
44
+ "svelte": ">=5"
45
+ },
46
+ "dependencies": {
47
+ "@sheetwrite/core": "0.1.0"
48
+ },
49
+ "devDependencies": {
50
+ "svelte-check": "4.7.3",
51
+ "typescript": "^6"
52
+ }
53
+ }
@@ -0,0 +1,192 @@
1
+ <script lang="ts">
2
+ import { initSheetwrite, isSheetwriteReady, type GridOptions } from "@sheetwrite/core";
3
+ import {
4
+ createGridController,
5
+ getGridResetReason,
6
+ gridSizeStyle,
7
+ } from "@sheetwrite/core/adapter";
8
+ import type { GridController, GridReadyReason } from "@sheetwrite/core/adapter";
9
+ import { untrack } from "svelte";
10
+ import type { SheetwriteGridProps as Props } from "./props.js";
11
+
12
+
13
+ let {
14
+ workbook,
15
+ data,
16
+ datasource,
17
+ datasourceStorage,
18
+ renderer = "canvas",
19
+ workerUrl,
20
+ theme,
21
+ readOnly,
22
+ protectionResolver,
23
+ mutationPolicy,
24
+ renderers,
25
+ overscan,
26
+ minColumns,
27
+ config,
28
+ wasmSource,
29
+ height,
30
+ fill,
31
+ fallback,
32
+ onGridChange,
33
+ onSelectionChange,
34
+ onViewportChange,
35
+ onEditBegin,
36
+ onEditCommit,
37
+ onSearch,
38
+ onActiveSheetChange,
39
+ onReady,
40
+ onInitializationError,
41
+ grid = $bindable(),
42
+ ...hostAttributes
43
+ }: Props = $props();
44
+
45
+ let host: HTMLDivElement;
46
+ let controller: GridController | undefined = $state();
47
+ let loading = $state(!isSheetwriteReady());
48
+ let generation = 0;
49
+
50
+ const handlers = {
51
+ onGridChange: (event: Parameters<NonNullable<typeof onGridChange>>[0]) => onGridChange?.(event),
52
+ onSelectionChange: (event: Parameters<NonNullable<typeof onSelectionChange>>[0]) =>
53
+ onSelectionChange?.(event),
54
+ onViewportChange: (event: Parameters<NonNullable<typeof onViewportChange>>[0]) =>
55
+ onViewportChange?.(event),
56
+ onEditBegin: (event: Parameters<NonNullable<typeof onEditBegin>>[0]) => onEditBegin?.(event),
57
+ onEditCommit: (event: Parameters<NonNullable<typeof onEditCommit>>[0]) => onEditCommit?.(event),
58
+ onSearch: (event: Parameters<NonNullable<typeof onSearch>>[0]) => onSearch?.(event),
59
+ onActiveSheetChange: (event: Parameters<NonNullable<typeof onActiveSheetChange>>[0]) =>
60
+ onActiveSheetChange?.(event),
61
+ };
62
+
63
+ const UNSET_WASM_SOURCE = Symbol("unset-wasm-source");
64
+ let previousOptions: GridOptions | null = null;
65
+ let lastRequestedOptions: GridOptions | null = null;
66
+ let previousWasmSource: Props["wasmSource"] | typeof UNSET_WASM_SOURCE = UNSET_WASM_SOURCE;
67
+ let initializationToken = 0;
68
+ let disposed = false;
69
+
70
+ function teardownGrid(): void {
71
+ const active = untrack(() => controller);
72
+ grid = undefined;
73
+ controller = undefined;
74
+ active?.destroy();
75
+ }
76
+
77
+ function publishReadyGrid(): void {
78
+ if (disposed || untrack(() => controller) || !lastRequestedOptions) return;
79
+ const options = lastRequestedOptions;
80
+ const reason: GridReadyReason =
81
+ generation === 0
82
+ ? "initial"
83
+ : (previousOptions && getGridResetReason(previousOptions, options)) ?? "input-reset";
84
+ const active = untrack(() => createGridController(host, options, handlers));
85
+ controller = active;
86
+ grid = active.grid;
87
+ previousOptions = options;
88
+ generation += 1;
89
+ loading = false;
90
+ untrack(() => onReady?.({ grid: active.grid, generation, reason }));
91
+ }
92
+
93
+ $effect(() => {
94
+ const resetInputs = {
95
+ workbook,
96
+ data,
97
+ datasource,
98
+ datasourceStorage,
99
+ renderer,
100
+ workerUrl,
101
+ protectionResolver,
102
+ mutationPolicy,
103
+ renderers,
104
+ wasmSource,
105
+ };
106
+ const currentOptions = (): GridOptions =>
107
+ untrack(() => ({
108
+ workbook: resetInputs.workbook,
109
+ data: resetInputs.data,
110
+ datasource: resetInputs.datasource,
111
+ datasourceStorage: resetInputs.datasourceStorage,
112
+ renderer: resetInputs.renderer,
113
+ workerUrl: resetInputs.workerUrl,
114
+ protectionResolver: resetInputs.protectionResolver,
115
+ mutationPolicy: resetInputs.mutationPolicy,
116
+ renderers: resetInputs.renderers,
117
+ theme,
118
+ readOnly,
119
+ overscan,
120
+ minColumns,
121
+ config,
122
+ }));
123
+ const requestedOptions = currentOptions();
124
+ const activeController = untrack(() => controller);
125
+ const wasmChanged =
126
+ previousWasmSource === UNSET_WASM_SOURCE || previousWasmSource !== resetInputs.wasmSource;
127
+ const resetReason =
128
+ lastRequestedOptions && getGridResetReason(lastRequestedOptions, requestedOptions);
129
+ const sourceOnlyChange = wasmChanged && resetReason === null && activeController !== undefined;
130
+ const needsNewGeneration =
131
+ lastRequestedOptions === null ||
132
+ resetReason !== null ||
133
+ (wasmChanged && (!isSheetwriteReady() || activeController === undefined));
134
+
135
+ previousWasmSource = resetInputs.wasmSource;
136
+ lastRequestedOptions = requestedOptions;
137
+ const token = ++initializationToken;
138
+ if (sourceOnlyChange) {
139
+ void initSheetwrite(resetInputs.wasmSource).catch((error: unknown) => {
140
+ if (!disposed && token === initializationToken) {
141
+ untrack(() => onInitializationError?.(error));
142
+ }
143
+ });
144
+ return;
145
+ }
146
+ if (!needsNewGeneration) return;
147
+
148
+ teardownGrid();
149
+ loading = !isSheetwriteReady();
150
+
151
+ void (async () => {
152
+ const alreadyReady = isSheetwriteReady();
153
+ try {
154
+ const initialization = initSheetwrite(resetInputs.wasmSource);
155
+ if (alreadyReady) publishReadyGrid();
156
+ await initialization;
157
+ if (!alreadyReady && !disposed && isSheetwriteReady()) publishReadyGrid();
158
+ } catch (error) {
159
+ if (disposed || token !== initializationToken) return;
160
+ loading = true;
161
+ untrack(() => onInitializationError?.(error));
162
+ }
163
+ })();
164
+ });
165
+
166
+ $effect(() => {
167
+ return () => {
168
+ disposed = true;
169
+ initializationToken += 1;
170
+ teardownGrid();
171
+ };
172
+ });
173
+
174
+ $effect(() => controller?.setReadOnly(readOnly ?? false));
175
+ $effect(() => controller?.setConfig(config));
176
+ $effect(() => controller?.setTheme(theme));
177
+ $effect(() => controller?.setOverscan(overscan));
178
+ $effect(() => controller?.setMinColumns(minColumns));
179
+
180
+ let sizing = $derived(gridSizeStyle({ height, fill }));
181
+ </script>
182
+
183
+ <div
184
+ {...hostAttributes}
185
+ bind:this={host}
186
+ class={["sheetwrite", hostAttributes.class]}
187
+ style:width={sizing.width}
188
+ style:height={sizing.height}
189
+ style:min-height={sizing.minHeight}
190
+ >
191
+ {#if loading && fallback}{@render fallback()}{/if}
192
+ </div>
@@ -0,0 +1,8 @@
1
+ import type { Grid } from "@sheetwrite/core";
2
+ import type { Component } from "svelte";
3
+ import type { SheetwriteGridProps } from "./props.js";
4
+
5
+ export type { SheetwriteGridProps } from "./props.js";
6
+ /** Advanced framework component for workbook data or datasource input. */
7
+ declare const SheetwriteGrid: Component<SheetwriteGridProps, {}, "grid">;
8
+ export default SheetwriteGrid;
@@ -0,0 +1,22 @@
1
+ <script lang="ts" generics="Row extends Record<string, CellScalar>">
2
+ import type { CellScalar } from "@sheetwrite/core";
3
+ import { createSimpleGridInput } from "@sheetwrite/core/adapter";
4
+ import GridComponent from "./Grid.svelte";
5
+ import type { SheetwriteProps } from "./props.js";
6
+
7
+ type Props = SheetwriteProps<Row>;
8
+
9
+ let { columns, defaultRows, sheetName, grid = $bindable(), ...props }: Props = $props();
10
+ let input = $derived(createSimpleGridInput({ columns, defaultRows, sheetName }));
11
+
12
+ $effect(() => {
13
+ if (
14
+ (props.height === undefined && !props.fill) ||
15
+ (props.height !== undefined && props.fill)
16
+ ) {
17
+ throw new Error("Sheetwrite: provide exactly one of height or fill");
18
+ }
19
+ });
20
+ </script>
21
+
22
+ <GridComponent {...props} {...input} bind:grid />
@@ -0,0 +1,8 @@
1
+ import type { CellScalar } from "@sheetwrite/core";
2
+ import type { Component } from "svelte";
3
+ import type { SheetwriteProps } from "./props.js";
4
+
5
+ export type { SheetwriteProps } from "./props.js";
6
+ /** Owns a sheet derived from `columns` and `defaultRows`. Bind `grid` for imperative access; it clears on reset or unmount. */
7
+ declare const Sheetwrite: Component<SheetwriteProps<Record<string, CellScalar>>, {}, "grid">;
8
+ export default Sheetwrite;
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type { CellScalar, Grid } from "@sheetwrite/core";
2
+ export type { GridReadyEvent, SimpleColumn } from "@sheetwrite/core/adapter";
3
+ export type { SheetwriteGridProps } from "./Grid.svelte";
4
+ export { default as SheetwriteGrid } from "./Grid.svelte";
5
+ export type { SheetwriteProps } from "./Sheetwrite.svelte";
6
+ /** Convenience component for local object rows. Bind `grid` to access the live `Grid`. */
7
+ export { default as Sheetwrite } from "./Sheetwrite.svelte";
package/src/props.ts ADDED
@@ -0,0 +1,66 @@
1
+ import type { CellScalar, Grid, GridOptions } from "@sheetwrite/core";
2
+ import type {
3
+ GridAdapterEventHandlers,
4
+ GridSizeProps,
5
+ SheetwriteInitializationProps,
6
+ SimpleColumn,
7
+ } from "@sheetwrite/core/adapter";
8
+ import type { Snippet } from "svelte";
9
+ import type { HTMLAttributes } from "svelte/elements";
10
+
11
+ /** Advanced framework adapter props for workbook data or datasource ownership. */
12
+ export interface SheetwriteGridProps
13
+ extends Omit<HTMLAttributes<HTMLDivElement>, keyof GridAdapterEventHandlers | "children">,
14
+ GridAdapterEventHandlers,
15
+ SheetwriteInitializationProps {
16
+ /** Live workbook schema adopted by the Grid. */
17
+ workbook: GridOptions["workbook"];
18
+ /** Eager values for the active sheet. */
19
+ data?: GridOptions["data"];
20
+ /** Lazy visible-row provider. */
21
+ datasource?: GridOptions["datasource"];
22
+ /** Datasource storage policy. */
23
+ datasourceStorage?: GridOptions["datasourceStorage"];
24
+ /** Canvas or worker paint backend. */
25
+ renderer?: GridOptions["renderer"];
26
+ /** Browser-fetchable worker module URL. */
27
+ workerUrl?: GridOptions["workerUrl"];
28
+ /** Live resolved-theme overrides. */
29
+ theme?: GridOptions["theme"];
30
+ /** Disables mutations, not navigation. */
31
+ readOnly?: GridOptions["readOnly"];
32
+ /** Client protected-range check. */
33
+ protectionResolver?: GridOptions["protectionResolver"];
34
+ /** Atomic or partial denial policy. */
35
+ mutationPolicy?: GridOptions["mutationPolicy"];
36
+ /** Named custom cell renderers. */
37
+ renderers?: GridOptions["renderers"];
38
+ /** Extra rows painted around the viewport. */
39
+ overscan?: GridOptions["overscan"];
40
+ /** Minimum column count with padding. */
41
+ minColumns?: GridOptions["minColumns"];
42
+ /** Built-in UI control configuration. */
43
+ config?: GridOptions["config"];
44
+ /** Host height as pixels or a CSS length. */
45
+ height?: number | string;
46
+ /** Fills the parent's available size. */
47
+ fill?: true;
48
+ /** Content shown until initialization succeeds. */
49
+ fallback?: Snippet;
50
+ /** Bindable live Grid, cleared on reset or unmount. */
51
+ grid?: Grid;
52
+ }
53
+
54
+ /** Simple framework adapter props for columns and default row objects. */
55
+ export type SheetwriteProps<Row extends Record<string, CellScalar>> = Omit<
56
+ SheetwriteGridProps,
57
+ "workbook" | "data" | "datasource" | "height" | "fill"
58
+ > &
59
+ GridSizeProps & {
60
+ /** Ordered schema for the owned sheet. */
61
+ columns: readonly SimpleColumn<Row>[];
62
+ /** Initial rows; missing column keys become `null`. */
63
+ defaultRows: readonly Row[];
64
+ /** Sheet name; defaults to `Sheet 1`. */
65
+ sheetName?: string;
66
+ };
package/styles.css ADDED
@@ -0,0 +1 @@
1
+ @import "@sheetwrite/core/styles.css";