@tanstack/svelte-table 9.0.0-alpha.2 → 9.0.0-alpha.27

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.
Files changed (44) hide show
  1. package/README.md +117 -0
  2. package/dist/AppCell.svelte +13 -0
  3. package/dist/AppCell.svelte.d.ts +9 -0
  4. package/dist/AppHeader.svelte +13 -0
  5. package/dist/AppHeader.svelte.d.ts +9 -0
  6. package/dist/AppTable.svelte +11 -0
  7. package/dist/AppTable.svelte.d.ts +7 -0
  8. package/dist/FlexRender.svelte +103 -0
  9. package/dist/FlexRender.svelte.d.ts +51 -0
  10. package/dist/Subscribe.svelte +45 -0
  11. package/dist/Subscribe.svelte.d.ts +44 -0
  12. package/dist/context-keys.d.ts +3 -0
  13. package/dist/context-keys.js +3 -0
  14. package/dist/createTable.svelte.d.ts +32 -0
  15. package/dist/createTable.svelte.js +82 -0
  16. package/dist/createTableHook.svelte.d.ts +236 -0
  17. package/dist/createTableHook.svelte.js +170 -0
  18. package/dist/createTableState.svelte.d.ts +2 -0
  19. package/dist/createTableState.svelte.js +12 -0
  20. package/dist/index.d.ts +8 -3
  21. package/dist/index.js +6 -3
  22. package/dist/merge-objects.d.ts +8 -0
  23. package/dist/merge-objects.js +29 -0
  24. package/dist/render-component.d.ts +64 -9
  25. package/dist/render-component.js +60 -4
  26. package/package.json +17 -9
  27. package/src/AppCell.svelte +13 -0
  28. package/src/AppHeader.svelte +13 -0
  29. package/src/AppTable.svelte +11 -0
  30. package/src/FlexRender.svelte +103 -0
  31. package/src/Subscribe.svelte +45 -0
  32. package/src/context-keys.ts +3 -0
  33. package/src/createTable.svelte.ts +165 -0
  34. package/src/createTableHook.svelte.ts +636 -0
  35. package/src/createTableState.svelte.ts +15 -0
  36. package/src/index.ts +15 -3
  37. package/src/merge-objects.ts +43 -0
  38. package/src/render-component.ts +74 -10
  39. package/dist/flex-render.svelte +0 -17
  40. package/dist/flex-render.svelte.d.ts +0 -28
  41. package/dist/table.svelte.d.ts +0 -36
  42. package/dist/table.svelte.js +0 -87
  43. package/src/flex-render.svelte +0 -35
  44. package/src/table.svelte.ts +0 -121
package/README.md ADDED
@@ -0,0 +1,117 @@
1
+ <div align="center">
2
+ <img src="./media/header_table.png" alt="TanStack Table">
3
+ </div>
4
+
5
+ <br />
6
+
7
+ <div align="center">
8
+ <a href="https://npmjs.com/package/@tanstack/react-table" target="\_parent">
9
+ <img alt="npm downloads" src="https://img.shields.io/npm/dm/@tanstack/react-table.svg" />
10
+ </a>
11
+ <a href="https://github.com/tanstack/table" target="\_parent">
12
+ <img alt="github stars" src="https://img.shields.io/github/stars/tanstack/react-table.svg?style=social&label=Star" />
13
+ </a>
14
+ <a href="https://bundlephobia.com/result?p=@tanstack/react-table@latest" target="\_parent">
15
+ <img alt="bundle size" src="https://badgen.net/bundlephobia/minzip/@tanstack/react-table@latest" />
16
+ </a>
17
+ </div>
18
+
19
+ <div align="center">
20
+ <a href="#badge">
21
+ <img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
22
+ </a>
23
+ <a href="https://bestofjs.org/projects/tanstack-table"><img alt="Best of JS" src="https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=TanStack%2Ftable%26since=daily" /></a>
24
+ <a href="https://twitter.com/tan_stack"><img src="https://img.shields.io/twitter/follow/tan_stack.svg?style=social" alt="Follow @TanStack"/></a>
25
+ </div>
26
+
27
+ ### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)
28
+
29
+ </div>
30
+
31
+ # TanStack Table
32
+
33
+ > [!NOTE]
34
+ > You may know TanStack Table by the adapter names:
35
+ >
36
+ > - [Angular Table](https://tanstack.com/table/alpha/docs/framework/angular/angular-table)
37
+ > - [Lit Table](https://tanstack.com/table/alpha/docs/framework/lit/lit-table)
38
+ > - [React Table](https://tanstack.com/table/alpha/docs/framework/react/react-table)
39
+ > - [Solid Table](https://tanstack.com/table/alpha/docs/framework/solid/solid-table)
40
+ > - [Svelte Table](https://tanstack.com/table/alpha/docs/framework/svelte/svelte-table)
41
+ > - [Vue Table](https://tanstack.com/table/alpha/docs/framework/vue/vue-table)
42
+
43
+ A headless table library for building powerful datagrids with full control over markup, styles, and behavior.
44
+
45
+ - Framework‑agnostic core with bindings for React, Vue & Solid
46
+ - 100% customizable — bring your own UI, components, and styles
47
+ - Sorting, filtering, grouping, aggregation & row selection
48
+ - Lightweight, virtualizable & server‑side friendly
49
+
50
+ ### <a href="https://tanstack.com/table">Read the Docs →</a>
51
+
52
+ ## Get Involved
53
+
54
+ - We welcome issues and pull requests!
55
+ - Participate in [GitHub discussions](https://github.com/TanStack/table/discussions)
56
+ - Chat with the community on [Discord](https://discord.com/invite/WrRKjPJ)
57
+ - See [CONTRIBUTING.md](./CONTRIBUTING.md) for setup instructions
58
+
59
+ ## Partners
60
+
61
+ <table align="center">
62
+ <tr>
63
+ <td>
64
+ <a href="https://www.coderabbit.ai/?via=tanstack&dub_id=aCcEEdAOqqutX6OS">
65
+ <picture>
66
+ <source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/coderabbit-dark-CMcuvjEy.svg" height="40" />
67
+ <source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/coderabbit-light-DVMJ2jHi.svg" height="40" />
68
+ <img src="https://tanstack.com/assets/coderabbit-light-DVMJ2jHi.svg" height="40" alt="CodeRabbit" />
69
+ </picture>
70
+ </a>
71
+ </td>
72
+ <td padding="20">
73
+ <a href="https://www.cloudflare.com?utm_source=tanstack">
74
+ <picture>
75
+ <source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/cloudflare-white-DQDB7UaL.svg" height="60" />
76
+ <source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/cloudflare-black-CPufaW0B.svg" height="60" />
77
+ <img src="https://tanstack.com/assets/cloudflare-black-CPufaW0B.svg" height="60" alt="Cloudflare" />
78
+ </picture>
79
+ </a>
80
+ </td>
81
+ <td>
82
+ <a href="https://ag-grid.com/react-data-grid/?utm_source=reacttable&utm_campaign=githubreacttable" style="display: flex; align-items: center; border: none;">
83
+ <picture>
84
+ <source media="(prefers-color-scheme: dark)" srcset="./media/ag-grid-dark.svg" height="40" />
85
+ <source media="(prefers-color-scheme: light)" srcset="./media/ag-grid-light.svg" height="40" />
86
+ <img src="https://raw.githubusercontent.com/tannerlinsley/files/master/partners/ag-grid.svg" height="60" alt="AG Grid" />
87
+ </picture>
88
+ </a>
89
+ </td>
90
+ </tr>
91
+ </table>
92
+
93
+ <div align="center">
94
+ <img src="./media/partner_logo.svg" alt="Table & you?" height="65">
95
+ <p>
96
+ We're looking for TanStack Table Partners to join our mission! Partner with us to push the boundaries of TanStack Table and build amazing things together.
97
+ </p>
98
+ <a href="mailto:partners@tanstack.com?subject=TanStack Table Partnership"><b>LET'S CHAT</b></a>
99
+ </div>
100
+
101
+ ## Explore the TanStack Ecosystem
102
+
103
+ - <a href="https://github.com/tanstack/config"><b>TanStack Config</b></a> – Tooling for JS/TS packages
104
+ - <a href="https://github.com/tanstack/db"><b>TanStack DB</b></a> – Reactive sync client store
105
+ - <a href="https://github.com/tanstack/devtools"><b>TanStack DevTools</b></a> – Unified devtools panel
106
+ - <a href="https://github.com/tanstack/form"><b>TanStack Form</b></a> – Type‑safe form state
107
+ - <a href="https://github.com/tanstack/pacer"><b>TanStack Pacer</b></a> – Debouncing, throttling, batching <br/>
108
+ - <a href="https://github.com/tanstack/query"><b>TanStack Query</b></a> – Async state & caching
109
+ - <a href="https://github.com/tanstack/ranger"><b>TanStack Ranger</b></a> – Range & slider primitives
110
+ - <a href="https://github.com/tanstack/router"><b>TanStack Router</b></a> – Type‑safe routing, caching & URL state
111
+ - <a href="https://github.com/tanstack/router"><b>TanStack Start</b></a> – Full‑stack SSR & streaming
112
+ - <a href="https://github.com/tanstack/store"><b>TanStack Store</b></a> – Reactive data store
113
+ - <a href="https://github.com/tanstack/virtual"><b>TanStack Virtual</b></a> – Virtualized rendering
114
+
115
+ … and more at <a href="https://tanstack.com"><b>TanStack.com »</b></a>
116
+
117
+ <!-- USE THE FORCE LUKE -->
@@ -0,0 +1,13 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte'
3
+
4
+ interface Props {
5
+ cell: any
6
+ cellComponents: any
7
+ children: Snippet<[any]>
8
+ }
9
+
10
+ let { children, cell, cellComponents }: Props = $props()
11
+ </script>
12
+
13
+ {@render children?.(Object.assign(cell, cellComponents))}
@@ -0,0 +1,9 @@
1
+ import type { Snippet } from 'svelte';
2
+ interface Props {
3
+ cell: any;
4
+ cellComponents: any;
5
+ children: Snippet<[any]>;
6
+ }
7
+ declare const AppCell: import("svelte").Component<Props, {}, "">;
8
+ type AppCell = ReturnType<typeof AppCell>;
9
+ export default AppCell;
@@ -0,0 +1,13 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte'
3
+
4
+ interface Props {
5
+ header: any
6
+ headerComponents: any
7
+ children: Snippet<[any]>
8
+ }
9
+
10
+ let { children, header, headerComponents }: Props = $props()
11
+ </script>
12
+
13
+ {@render children?.(Object.assign(header, headerComponents))}
@@ -0,0 +1,9 @@
1
+ import type { Snippet } from 'svelte';
2
+ interface Props {
3
+ header: any;
4
+ headerComponents: any;
5
+ children: Snippet<[any]>;
6
+ }
7
+ declare const AppHeader: import("svelte").Component<Props, {}, "">;
8
+ type AppHeader = ReturnType<typeof AppHeader>;
9
+ export default AppHeader;
@@ -0,0 +1,11 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte'
3
+
4
+ interface Props {
5
+ children: Snippet
6
+ }
7
+
8
+ let { children }: Props = $props()
9
+ </script>
10
+
11
+ {@render children?.()}
@@ -0,0 +1,7 @@
1
+ import type { Snippet } from 'svelte';
2
+ interface Props {
3
+ children: Snippet;
4
+ }
5
+ declare const AppTable: import("svelte").Component<Props, {}, "">;
6
+ type AppTable = ReturnType<typeof AppTable>;
7
+ export default AppTable;
@@ -0,0 +1,103 @@
1
+ <script
2
+ lang="ts"
3
+ generics="TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData"
4
+ >
5
+ import { isFunction } from '@tanstack/table-core'
6
+ import {
7
+ RenderComponentConfig,
8
+ RenderSnippetConfig,
9
+ } from './render-component'
10
+ import type {
11
+ Cell,
12
+ CellContext,
13
+ CellData,
14
+ ColumnDefTemplate,
15
+ Header,
16
+ HeaderContext,
17
+ RowData,
18
+ TableFeatures,
19
+ } from '@tanstack/table-core'
20
+
21
+ type Props =
22
+ | {
23
+ /** The cell or header field of the current cell's column definition. */
24
+ content?: ColumnDefTemplate<
25
+ | HeaderContext<TFeatures, TData, TValue>
26
+ | CellContext<TFeatures, TData, TValue>
27
+ >
28
+ /** The result of the `getContext()` function of the header or cell */
29
+ context:
30
+ | HeaderContext<TFeatures, TData, TValue>
31
+ | CellContext<TFeatures, TData, TValue>
32
+ cell?: never
33
+ header?: never
34
+ footer?: never
35
+ }
36
+ | {
37
+ cell: Cell<TFeatures, TData, TValue>
38
+ content?: never
39
+ context?: never
40
+ header?: never
41
+ footer?: never
42
+ }
43
+ | {
44
+ header: Header<TFeatures, TData, TValue>
45
+ content?: never
46
+ context?: never
47
+ cell?: never
48
+ footer?: never
49
+ }
50
+ | {
51
+ footer: Header<TFeatures, TData, TValue>
52
+ content?: never
53
+ context?: never
54
+ cell?: never
55
+ header?: never
56
+ }
57
+
58
+ let props: Props = $props()
59
+
60
+ // Resolve content and context from either the new cell/header/footer props
61
+ // or the legacy content/context props.
62
+ const resolved = $derived.by(() => {
63
+ if ('cell' in props && props.cell) {
64
+ return {
65
+ content: props.cell.column.columnDef.cell,
66
+ context: props.cell.getContext(),
67
+ }
68
+ }
69
+ if ('header' in props && props.header) {
70
+ return {
71
+ content: props.header.column.columnDef.header,
72
+ context: props.header.getContext(),
73
+ }
74
+ }
75
+ if ('footer' in props && props.footer) {
76
+ return {
77
+ content: props.footer.column.columnDef.footer,
78
+ context: props.footer.getContext(),
79
+ }
80
+ }
81
+ return {
82
+ content: props.content,
83
+ context: props.context,
84
+ }
85
+ })
86
+
87
+ // Compute the render result reactively
88
+ const result = $derived(
89
+ isFunction(resolved.content)
90
+ ? resolved.content(resolved.context as any)
91
+ : undefined,
92
+ )
93
+ </script>
94
+
95
+ {#if typeof resolved.content === 'string'}
96
+ {resolved.content}
97
+ {:else if result instanceof RenderComponentConfig}
98
+ <result.component {...result.props} />
99
+ {:else if result instanceof RenderSnippetConfig}
100
+ {@render result.snippet(result.params)}
101
+ {:else if result !== undefined}
102
+ {result}
103
+ {/if}
@@ -0,0 +1,51 @@
1
+ import type { Cell, CellContext, CellData, ColumnDefTemplate, Header, HeaderContext, RowData, TableFeatures } from '@tanstack/table-core';
2
+ declare function $$render<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData>(): {
3
+ props: {
4
+ /** The cell or header field of the current cell's column definition. */
5
+ content?: ColumnDefTemplate<HeaderContext<TFeatures, TData, TValue> | CellContext<TFeatures, TData, TValue>>;
6
+ /** The result of the `getContext()` function of the header or cell */
7
+ context: HeaderContext<TFeatures, TData, TValue> | CellContext<TFeatures, TData, TValue>;
8
+ cell?: never;
9
+ header?: never;
10
+ footer?: never;
11
+ } | {
12
+ cell: Cell<TFeatures, TData, TValue>;
13
+ content?: never;
14
+ context?: never;
15
+ header?: never;
16
+ footer?: never;
17
+ } | {
18
+ header: Header<TFeatures, TData, TValue>;
19
+ content?: never;
20
+ context?: never;
21
+ cell?: never;
22
+ footer?: never;
23
+ } | {
24
+ footer: Header<TFeatures, TData, TValue>;
25
+ content?: never;
26
+ context?: never;
27
+ cell?: never;
28
+ header?: never;
29
+ };
30
+ exports: {};
31
+ bindings: "";
32
+ slots: {};
33
+ events: {};
34
+ };
35
+ declare class __sveltets_Render<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData> {
36
+ props(): ReturnType<typeof $$render<TFeatures, TData, TValue>>['props'];
37
+ events(): ReturnType<typeof $$render<TFeatures, TData, TValue>>['events'];
38
+ slots(): ReturnType<typeof $$render<TFeatures, TData, TValue>>['slots'];
39
+ bindings(): "";
40
+ exports(): {};
41
+ }
42
+ interface $$IsomorphicComponent {
43
+ new <TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<TFeatures, TData, TValue>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<TFeatures, TData, TValue>['props']>, ReturnType<__sveltets_Render<TFeatures, TData, TValue>['events']>, ReturnType<__sveltets_Render<TFeatures, TData, TValue>['slots']>> & {
44
+ $$bindings?: ReturnType<__sveltets_Render<TFeatures, TData, TValue>['bindings']>;
45
+ } & ReturnType<__sveltets_Render<TFeatures, TData, TValue>['exports']>;
46
+ <TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData>(internal: unknown, props: ReturnType<__sveltets_Render<TFeatures, TData, TValue>['props']> & {}): ReturnType<__sveltets_Render<TFeatures, TData, TValue>['exports']>;
47
+ z_$$bindings?: ReturnType<__sveltets_Render<any, any, any>['bindings']>;
48
+ }
49
+ declare const FlexRender: $$IsomorphicComponent;
50
+ type FlexRender<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData> = InstanceType<typeof FlexRender<TFeatures, TData, TValue>>;
51
+ export default FlexRender;
@@ -0,0 +1,45 @@
1
+ <script
2
+ lang="ts"
3
+ generics="TFeatures extends TableFeatures, TData extends RowData, TSelected = unknown"
4
+ >
5
+ import type {
6
+ NoInfer,
7
+ RowData,
8
+ TableFeatures,
9
+ TableState,
10
+ } from '@tanstack/table-core'
11
+ import type { Snippet } from 'svelte'
12
+ import type { SvelteTable } from './createTable.svelte'
13
+
14
+ type Props<
15
+ TFeatures extends TableFeatures,
16
+ TData extends RowData,
17
+ TSelected,
18
+ > = {
19
+ /**
20
+ * A function that selects a portion of the table state to subscribe to.
21
+ * @param state - The full table state
22
+ * @returns The selected portion of state
23
+ */
24
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSelected
25
+ /**
26
+ * A Svelte snippet that receives the selected state and renders content.
27
+ * The snippet will be called with the selected state whenever it changes.
28
+ */
29
+ children: Snippet<[state: Readonly<TSelected>]>
30
+ /**
31
+ * The table instance to subscribe to
32
+ */
33
+ table: SvelteTable<TFeatures, TData, unknown>
34
+ }
35
+
36
+ let { table, selector, children }: Props<TFeatures, TData, TSelected> =
37
+ $props()
38
+ </script>
39
+
40
+ <!--
41
+ Access table.store.state in the template (a reactive context).
42
+ The constructReactivityFeature interceptor makes this trigger
43
+ the $state-based notifier, so Svelte tracks it automatically.
44
+ -->
45
+ {@render children(selector(table.store.state as TableState<TFeatures>))}
@@ -0,0 +1,44 @@
1
+ import type { NoInfer, RowData, TableFeatures, TableState } from '@tanstack/table-core';
2
+ import type { Snippet } from 'svelte';
3
+ import type { SvelteTable } from './createTable.svelte';
4
+ type Props<TFeatures extends TableFeatures, TData extends RowData, TSelected> = {
5
+ /**
6
+ * A function that selects a portion of the table state to subscribe to.
7
+ * @param state - The full table state
8
+ * @returns The selected portion of state
9
+ */
10
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSelected;
11
+ /**
12
+ * A Svelte snippet that receives the selected state and renders content.
13
+ * The snippet will be called with the selected state whenever it changes.
14
+ */
15
+ children: Snippet<[state: Readonly<TSelected>]>;
16
+ /**
17
+ * The table instance to subscribe to
18
+ */
19
+ table: SvelteTable<TFeatures, TData, unknown>;
20
+ };
21
+ declare function $$render<TFeatures extends TableFeatures, TData extends RowData, TSelected = unknown>(): {
22
+ props: Props<TFeatures, TData, TSelected>;
23
+ exports: {};
24
+ bindings: "";
25
+ slots: {};
26
+ events: {};
27
+ };
28
+ declare class __sveltets_Render<TFeatures extends TableFeatures, TData extends RowData, TSelected = unknown> {
29
+ props(): ReturnType<typeof $$render<TFeatures, TData, TSelected>>['props'];
30
+ events(): ReturnType<typeof $$render<TFeatures, TData, TSelected>>['events'];
31
+ slots(): ReturnType<typeof $$render<TFeatures, TData, TSelected>>['slots'];
32
+ bindings(): "";
33
+ exports(): {};
34
+ }
35
+ interface $$IsomorphicComponent {
36
+ new <TFeatures extends TableFeatures, TData extends RowData, TSelected = unknown>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['props']>, ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['events']>, ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['slots']>> & {
37
+ $$bindings?: ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['bindings']>;
38
+ } & ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['exports']>;
39
+ <TFeatures extends TableFeatures, TData extends RowData, TSelected = unknown>(internal: unknown, props: ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['props']> & {}): ReturnType<__sveltets_Render<TFeatures, TData, TSelected>['exports']>;
40
+ z_$$bindings?: ReturnType<__sveltets_Render<any, any, any>['bindings']>;
41
+ }
42
+ declare const Subscribe: $$IsomorphicComponent;
43
+ type Subscribe<TFeatures extends TableFeatures, TData extends RowData, TSelected = unknown> = InstanceType<typeof Subscribe<TFeatures, TData, TSelected>>;
44
+ export default Subscribe;
@@ -0,0 +1,3 @@
1
+ export declare const tableContextKey = "__tanstack_table_context_key";
2
+ export declare const cellContextKey = "__tanstack_cell_context_key";
3
+ export declare const headerContextKey = "__tanstack_header_context_key";
@@ -0,0 +1,3 @@
1
+ export const tableContextKey = '__tanstack_table_context_key';
2
+ export const cellContextKey = '__tanstack_cell_context_key';
3
+ export const headerContextKey = '__tanstack_header_context_key';
@@ -0,0 +1,32 @@
1
+ import type { NoInfer, RowData, Table, TableFeatures, TableOptions, TableState } from '@tanstack/table-core';
2
+ import type { Snippet } from 'svelte';
3
+ export type SvelteTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}> = Table<TFeatures, TData> & {
4
+ /**
5
+ * A Svelte component that allows you to subscribe to the table state.
6
+ *
7
+ * This is useful for opting into state subscriptions for specific parts of the table state.
8
+ *
9
+ * @example
10
+ * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
11
+ * {(state) => (
12
+ * <tr>
13
+ * // render the row
14
+ * </tr>
15
+ * )}
16
+ * </table.Subscribe>
17
+ */
18
+ Subscribe: <TSelected>(props: {
19
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSelected;
20
+ children: ((state: Readonly<TSelected>) => Snippet) | Snippet;
21
+ }) => any;
22
+ /**
23
+ * The selected state of the table. This state may not match the structure of `table.store.state` because it is selected by the `selector` function that you pass as the 2nd argument to `createTable`.
24
+ *
25
+ * @example
26
+ * const table = createTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state
27
+ *
28
+ * console.log(table.state.globalFilter)
29
+ */
30
+ readonly state: Readonly<TSelected>;
31
+ };
32
+ export declare function createTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptions<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): SvelteTable<TFeatures, TData, TSelected>;
@@ -0,0 +1,82 @@
1
+ import { constructReactivityFeature, constructTable, } from '@tanstack/table-core';
2
+ import { useStore } from '@tanstack/svelte-store';
3
+ import { untrack } from 'svelte';
4
+ import { mergeObjects } from './merge-objects';
5
+ export function createTable(tableOptions, selector = () => ({})) {
6
+ // 1. Create $state-based notifier for reactivity feature
7
+ let notifierValue = $state(0);
8
+ // 2. Construct reactivity feature (same pattern as solid/vue/angular)
9
+ const svelteReactivityFeature = constructReactivityFeature({
10
+ stateNotifier: () => notifierValue,
11
+ optionsNotifier: () => notifierValue,
12
+ });
13
+ // 3. Merge reactivity feature into options using mergeObjects (preserves getters)
14
+ const mergedOptions = mergeObjects(tableOptions, {
15
+ _features: mergeObjects(tableOptions._features, {
16
+ svelteReactivityFeature,
17
+ }),
18
+ });
19
+ // 4. Set up resolved options with mergeOptions handler
20
+ const resolvedOptions = mergeObjects({
21
+ mergeOptions: (defaultOptions, newOptions) => {
22
+ return mergeObjects(defaultOptions, newOptions);
23
+ },
24
+ }, mergedOptions);
25
+ // 5. Construct table
26
+ const table = constructTable(resolvedOptions);
27
+ // 6. Subscribe to all state and options via useStore
28
+ const allState = useStore(table.store, (state) => state);
29
+ const allOptions = useStore(table.optionsStore, (options) => options);
30
+ // 7. Sync store changes -> notifier.
31
+ // Use $effect.pre so this runs before DOM updates (like Solid's createComputed).
32
+ // Use untrack for the write so the effect only depends on allState/allOptions,
33
+ // not on notifierValue itself (which would cause an infinite loop).
34
+ $effect.pre(() => {
35
+ allState.current;
36
+ allOptions.current;
37
+ untrack(() => {
38
+ notifierValue++;
39
+ });
40
+ });
41
+ // 8. Sync options reactively. When controlled state changes (e.g., $state
42
+ // inside createTableState), the effect re-runs and calls setOptions.
43
+ // Use $effect.pre so the table sees updated options BEFORE the DOM renders,
44
+ // ensuring getRowModel() returns current data (not stale, one-frame-behind data).
45
+ // The reactive reads (state getters, data getter) happen OUTSIDE untrack
46
+ // so they become dependencies. The setOptions call is INSIDE untrack to
47
+ // prevent tracking notifierValue (read via store.state's stateNotifier
48
+ // interceptor inside setOptions), which would cause an infinite loop.
49
+ $effect.pre(() => {
50
+ // Read reactive getters to create $effect dependencies on external state
51
+ const state = mergedOptions.state;
52
+ if (state) {
53
+ for (const key in state) {
54
+ void state[key];
55
+ }
56
+ }
57
+ void mergedOptions.data;
58
+ untrack(() => {
59
+ table.setOptions((prev) => {
60
+ return mergeObjects(prev, mergedOptions);
61
+ });
62
+ });
63
+ });
64
+ // 9. Subscribe component
65
+ table.Subscribe = function Subscribe(props) {
66
+ const selected = useStore(table.store, props.selector);
67
+ if (typeof props.children === 'function') {
68
+ return props.children(selected.current);
69
+ }
70
+ return props.children;
71
+ };
72
+ // 10. State selector
73
+ const stateStore = useStore(table.store, selector);
74
+ Object.defineProperty(table, 'state', {
75
+ get() {
76
+ return stateStore.current;
77
+ },
78
+ configurable: true,
79
+ enumerable: true,
80
+ });
81
+ return table;
82
+ }