datatable-tree 0.0.0-stage → 0.1.1

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 (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +251 -2
  3. package/dist/DataTable.d.ts +583 -0
  4. package/dist/DataTable.js +620 -0
  5. package/dist/Draggable.d.ts +35 -0
  6. package/dist/Draggable.js +91 -0
  7. package/dist/ListBox.d.ts +132 -0
  8. package/dist/ListBox.js +243 -0
  9. package/dist/ListVirtualizer.d.ts +59 -0
  10. package/dist/ListVirtualizer.js +180 -0
  11. package/dist/TreeList.d.ts +196 -0
  12. package/dist/TreeList.js +300 -0
  13. package/dist/config.d.ts +55 -0
  14. package/dist/config.js +33 -0
  15. package/dist/datatable.css +161 -0
  16. package/dist/hooks.d.ts +27 -0
  17. package/dist/hooks.js +115 -0
  18. package/dist/index.d.ts +12 -0
  19. package/dist/index.js +15 -0
  20. package/dist/interfaces.d.ts +269 -0
  21. package/dist/interfaces.js +3 -0
  22. package/dist/mobx-adapter.d.ts +12 -0
  23. package/dist/mobx-adapter.js +21 -0
  24. package/dist/reactivity.d.ts +112 -0
  25. package/dist/reactivity.js +155 -0
  26. package/dist/styles.d.ts +64 -0
  27. package/dist/styles.js +86 -0
  28. package/dist/theme.d.ts +160 -0
  29. package/dist/theme.js +239 -0
  30. package/dist/util.d.ts +28 -0
  31. package/dist/util.js +45 -0
  32. package/package.json +143 -4
  33. package/src/DataTable.tsx +1332 -0
  34. package/src/Draggable.tsx +133 -0
  35. package/src/ListBox.tsx +423 -0
  36. package/src/ListVirtualizer.ts +234 -0
  37. package/src/TreeList.tsx +590 -0
  38. package/src/config.tsx +96 -0
  39. package/src/css.d.ts +1 -0
  40. package/src/datatable.css +161 -0
  41. package/src/hooks.ts +138 -0
  42. package/src/index.ts +15 -0
  43. package/src/interfaces.ts +309 -0
  44. package/src/mobx-adapter.ts +24 -0
  45. package/src/reactivity.ts +222 -0
  46. package/src/styles.tsx +102 -0
  47. package/src/theme.ts +346 -0
  48. package/src/util.ts +60 -0
package/dist/styles.js ADDED
@@ -0,0 +1,86 @@
1
+ ;
2
+ /** Combines a Styles object with an optional default class and default CSSProperties
3
+ * to produce a pair { style, className }. A user-defined class name in `s` replaces
4
+ * the default class. Each individual style in `s` can replace individual styles in
5
+ * `defaultStyle`, but non-conflicting styles are merged.
6
+ *
7
+ * This function can also be used to simply convert { style, className, ... } to
8
+ * { style, className }, removing additional properties. Components that accept
9
+ * Styles can use this method to easily set their class/style, e.g.
10
+ *
11
+ * export function MyComponent(p: Styles & { children: React.ReactNode; }) {
12
+ * const styles = useStyles();
13
+ * return (<div {...classAndStyle(p, styles.mycomp)}>{p.children}</div>);
14
+ * }
15
+ *
16
+ * Using the Styles type, it is straightforward to allow components to change styles in
17
+ * grandchildren elements, e.g. the `onTable` and `onTableBody` props in this example:
18
+ *
19
+ * export function WrappedTable(p: Styles & {
20
+ * children: React.ReactNode;
21
+ * onTable?: Styles;
22
+ * onTableBody?: Styles;
23
+ * }) {
24
+ * const styles = useStyles();
25
+ * return (
26
+ * <div {...classAndStyle(p, styles.container)}>
27
+ * <Table {...classAndStyle(p.onTable, styles.table)}>
28
+ * <TableBody {...classAndStyle(p.onTableBody)}>
29
+ * {p.children}
30
+ * </TableBody>
31
+ * </Table>
32
+ * </div>
33
+ * );
34
+ * }
35
+ *
36
+ * In this example, TableBody is a MUI component that accepts className and style
37
+ * (but not `class`). Given a child component that accepts `Styles`, you can
38
+ * write <Child {...p.onChild}/> rather than <Child {...classAndStyle(p.onChild)}/>. */
39
+ export function classAndStyle(s, defaultClass, defaultStyle) {
40
+ var _a, _b, _c;
41
+ let result;
42
+ if (s) {
43
+ let className;
44
+ if (s.useDefaultClass && defaultClass) {
45
+ className = `${defaultClass} ${(_a = s.className) !== null && _a !== void 0 ? _a : s.class}`;
46
+ }
47
+ else {
48
+ className = (_c = (_b = s.className) !== null && _b !== void 0 ? _b : s.class) !== null && _c !== void 0 ? _c : defaultClass;
49
+ }
50
+ result = {
51
+ style: combineStyle(s.style, defaultStyle),
52
+ className,
53
+ };
54
+ if (s.role) {
55
+ result.role = s.role;
56
+ }
57
+ return result;
58
+ }
59
+ else if (defaultClass !== null && defaultClass !== void 0 ? defaultClass : defaultStyle) {
60
+ return {
61
+ style: defaultStyle,
62
+ className: defaultClass,
63
+ };
64
+ }
65
+ }
66
+ /** Same as `classAndStyle` except that other properties from `s` are also copied to the output. */
67
+ export function classAndStyleEtc(s, defaultClass, defaultStyle) {
68
+ var _a, _b, _c;
69
+ let className;
70
+ if (s.useDefaultClass && defaultClass) {
71
+ className = `${defaultClass} ${(_a = s.className) !== null && _a !== void 0 ? _a : s.class}`;
72
+ }
73
+ else {
74
+ className = (_c = (_b = s.className) !== null && _b !== void 0 ? _b : s.class) !== null && _c !== void 0 ? _c : defaultClass;
75
+ }
76
+ return Object.assign(Object.assign({}, s), { style: combineStyle(s.style, defaultStyle), className });
77
+ }
78
+ /** Combines two `CSSProperties|undefined` values, with properties in the first
79
+ * overriding properties in the second. */
80
+ export function combineStyle(style, defaultStyle) {
81
+ if (style) {
82
+ return defaultStyle && style
83
+ ? Object.assign(Object.assign({}, defaultStyle), style) : style !== null && style !== void 0 ? style : defaultStyle;
84
+ }
85
+ return defaultStyle;
86
+ }
@@ -0,0 +1,160 @@
1
+ import { CSSProperties } from 'react';
2
+ import { nully } from './util.js';
3
+ /**
4
+ * Theming
5
+ * -------
6
+ *
7
+ * The stylesheet `datatable.css` contains all structural styling under stable, prefixed
8
+ * class names (`.dtbl-*`). Every color/spacing decision in it reads a CSS custom property
9
+ * (`--dtbl-*`) whose fallback value is the built-in default theme (`lightTheme`).
10
+ * A theme is a typed bundle of those custom properties, split into two halves that can be
11
+ * mixed and matched freely with an object spread:
12
+ *
13
+ * - `TableColors`: color scheme (backgrounds, borders, text and selection colors)
14
+ * - `TableMetrics`: everything else (cell/item padding, tree indent, resize cursor)
15
+ * - `TableTheme` is simply `TableColors & TableMetrics`, so any combination such as
16
+ * `{ ...darkColors, ...compactMetrics }` is a complete theme.
17
+ *
18
+ * Applying a theme sets the variables on the component's outer element (never on `:root`),
19
+ * so tables with different themes can coexist on one page.
20
+ *
21
+ * Ways to apply a theme:
22
+ * - App-wide/subtree default: `<TableConfig theme={darkTheme}>...</TableConfig>`
23
+ * - Whole page, including portals and `*Core` components: `applyTheme(darkTheme)`
24
+ * - Per table: `<DataTable theme={myTheme} .../>` (overrides the `TableConfig` theme)
25
+ * - Stylesheet-based: pass a string; it is added as a class to the outer element, and your
26
+ * stylesheet defines the `--dtbl-*` variables under that class, e.g.
27
+ *
28
+ * .compact-blue { --dtbl-cell-padding: 2px 4px; --dtbl-selected-bg: #cde; }
29
+ *
30
+ * When using `DataTableCore`/`TreeListCore`/`ListCore` (which render no outer element),
31
+ * apply the theme to your own container: `<table {...themeProps(myTheme)}>`.
32
+ */
33
+ export interface TableColors {
34
+ /** Sets the CSS `color-scheme` property on the container (not a --dtbl-* variable).
35
+ * `'light dark'` makes `light-dark(...)` color values (as used by the `auto*` presets)
36
+ * follow the browser's light/dark preference; `'light'`/`'dark'` also render matching
37
+ * scrollbars inside the scrollable wrapper. Note: `applyTheme` targets `<html>` by
38
+ * default, where `color-scheme: dark` affects the whole page's form controls and
39
+ * default canvas — theme a subtree or table instead if that's unwanted. */
40
+ colorScheme?: string;
41
+ /** Background of data rows and list items (--dtbl-row-bg). */
42
+ rowBackground?: string;
43
+ /** Background of the sticky header cells (--dtbl-header-bg; default: inherit, i.e. the
44
+ * row background, which the header row must repaint to cover scrolled data rows). */
45
+ headerBackground?: string;
46
+ /** Text color of rows and items (--dtbl-text; default: inherit from the page). */
47
+ textColor?: string;
48
+ /** Bottom border of all cells, as a full CSS border value (--dtbl-row-border). */
49
+ rowBorder?: string;
50
+ /** Background of selected rows/items (--dtbl-selected-bg). */
51
+ selectedBackground?: string;
52
+ /** Background of hovered selected rows (--dtbl-hover-selected-bg). */
53
+ hoverSelectedBackground?: string;
54
+ /** Background of hovered unselected rows (--dtbl-hover-unselected-bg). */
55
+ hoverUnselectedBackground?: string;
56
+ /** Background of rows whose (grand)parent row is selected, when the
57
+ * `highlightChildrenOfSelectedParents` item prop is enabled (--dtbl-parent-selected-bg). */
58
+ parentSelectedBackground?: string;
59
+ /** CSS `cursor` shown over the column-resize drag handle (--dtbl-resize-cursor).
60
+ * Can be a `url(...) x y, fallback` value; defaults to `defaultResizeCursor`. */
61
+ resizeCursor?: string;
62
+ }
63
+ export interface TableMetrics {
64
+ /** Padding of table cells, unless the `noPadding` item prop is set (--dtbl-cell-padding). */
65
+ cellPadding?: string;
66
+ /** Padding of list/tree items outside tables (--dtbl-item-padding). */
67
+ itemPadding?: string;
68
+ /** Indent per level of tree nesting (--dtbl-indent). A number means pixels. This is
69
+ * ignored by tables/trees that have an explicit `indentPerLevel` prop. */
70
+ treeIndent?: number | string;
71
+ /** Tint drawn over every other table row — the 1st, 3rd, 5th... (--dtbl-stripe-bg;
72
+ * default: transparent, i.e. no striping). Although this is a color, it lives in
73
+ * `TableMetrics` because a translucent
74
+ * value (see the `stripedRows` preset) reads correctly over any color scheme, making
75
+ * striping an independent layout choice. Selection and hover tints replace the stripe
76
+ * on their row. Note: with `virtualize`, off-screen rows collapse into placeholders, so
77
+ * stripe parity is relative to the rendered rows and can shift while scrolling. */
78
+ stripeBackground?: string;
79
+ }
80
+ /** A complete visual style for tables/lists: a color scheme plus metrics. The two halves
81
+ * are independent, so `{ ...darkColors, ...compactMetrics }` is a valid theme. */
82
+ export interface TableTheme extends TableColors, TableMetrics {
83
+ /** Extra class name(s) for the outer element, e.g. to add rules beyond the variables. */
84
+ className?: string;
85
+ }
86
+ /** The double-headed arrow drawn over a column-resize handle: a 40x30 PNG inlined as a data
87
+ * URI, so that the package ships no separate image asset and needs no bundler support for
88
+ * one. Exported for callers that want the image on their own resize affordances. */
89
+ export declare const resizeCursorImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAeCAYAAABe3VzdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGJoAABiaAZsyYXAAAAEbSURBVFhH7ZSLDsIgDAA3jf//wUZnb4GlEsbaTmUmuwSpPLobMMahI9M0zfUoSHUllPKU9kdqp+pHErwR6kJ7lr/Mvx0RkXuqFylNd8EtTsG9nIJ7aV40ta8qI3fU3Cljmjks4xiTn8Xdl8cSh1ZQJ7TA2CzqxS3olctEJV2CUblMRNJ8Bmty0paiNrV50rZM1rl1H7FJUCf4FIXIkr8U3Nzib8gBOcmd/q6yKcjb8Cafhpx5pVqYthhqK2kVr83Tcjq37iM2f8VMYkIJ7a2Shi1oAQuua4bENUkrXjlwCUJUMiIHbkHwSkblICQIPNDyUOu4NcKCv+IU3Ev4bESQs5iid+Qjkq6dF3UvDiPIalFKDrHFa/zBFg/DC/as2Q7ADo1PAAAAAElFTkSuQmCC";
90
+ /** The `resizeCursor` of every preset: `resizeCursorImage` with its hotspot on the arrow's
91
+ * pivot, degrading to the browser's own `col-resize` where images are unsupported. Kept in
92
+ * sync with the `--dtbl-resize-cursor` fallback in datatable.css. */
93
+ export declare const defaultResizeCursor = "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAeCAYAAABe3VzdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGJoAABiaAZsyYXAAAAEbSURBVFhH7ZSLDsIgDAA3jf//wUZnb4GlEsbaTmUmuwSpPLobMMahI9M0zfUoSHUllPKU9kdqp+pHErwR6kJ7lr/Mvx0RkXuqFylNd8EtTsG9nIJ7aV40ta8qI3fU3Cljmjks4xiTn8Xdl8cSh1ZQJ7TA2CzqxS3olctEJV2CUblMRNJ8Bmty0paiNrV50rZM1rl1H7FJUCf4FIXIkr8U3Nzib8gBOcmd/q6yKcjb8Cafhpx5pVqYthhqK2kVr83Tcjq37iM2f8VMYkIJ7a2Shi1oAQuua4bENUkrXjlwCUJUMiIHbkHwSkblICQIPNDyUOu4NcKCv+IU3Ev4bESQs5iid+Qjkq6dF3UvDiPIalFKDrHFa/zBFg/DC/as2Q7ADo1PAAAAAElFTkSuQmCC) 30 15, col-resize";
94
+ /** The dark scheme: near-black rows with green selection. */
95
+ export declare const darkColors: TableColors;
96
+ /** The light counterpart of `darkColors` (green-tinted selection on white). These values
97
+ * equal the `var()` fallbacks in datatable.css except that the fallback text color is
98
+ * `inherit`, so this is (nearly) the appearance of an un-themed table. */
99
+ export declare const lightColors: TableColors;
100
+ /** A cooler light scheme: gray header band, gray borders, blue selection. */
101
+ export declare const lightGrayColors: TableColors;
102
+ /** A softer dark scheme: gray rows with blue selection. */
103
+ export declare const darkGrayColors: TableColors;
104
+ /** A warm light scheme: cream rows, amber selection. */
105
+ export declare const lightAmberColors: TableColors;
106
+ /** A warm dark scheme: deep-brown rows, amber selection. */
107
+ export declare const darkAmberColors: TableColors;
108
+ /** `lightColors` or `darkColors`, following the browser's light/dark preference. */
109
+ export declare const autoColors: TableColors;
110
+ /** `lightGrayColors` or `darkGrayColors`, following the browser's light/dark preference. */
111
+ export declare const autoGrayColors: TableColors;
112
+ /** `lightAmberColors` or `darkAmberColors`, following the browser's light/dark preference. */
113
+ export declare const autoAmberColors: TableColors;
114
+ /** The default spacing, equal to the `var()` fallbacks in datatable.css. */
115
+ export declare const defaultMetrics: TableMetrics;
116
+ /** Denser spacing, for information-heavy screens. */
117
+ export declare const compactMetrics: TableMetrics;
118
+ /** Add-on: gives every other table row (starting with the first) a faint tint. Translucent,
119
+ * so it works over any color
120
+ * scheme — combine it with any theme, e.g. `{ ...darkTheme, ...stripedRows }`. */
121
+ export declare const stripedRows: TableMetrics;
122
+ /** The default theme: `lightColors` with default spacing. */
123
+ export declare const lightTheme: TableTheme;
124
+ /** Dark theme (near-black rows, green selection). */
125
+ export declare const darkTheme: TableTheme;
126
+ /** Cooler light theme (gray header band, blue selection). */
127
+ export declare const lightGrayTheme: TableTheme;
128
+ /** Softer dark theme (gray rows, blue selection). */
129
+ export declare const darkGrayTheme: TableTheme;
130
+ /** Warm light theme (cream rows, amber selection). */
131
+ export declare const lightAmberTheme: TableTheme;
132
+ /** Warm dark theme (deep-brown rows, amber selection). */
133
+ export declare const darkAmberTheme: TableTheme;
134
+ /** `lightTheme` or `darkTheme`, following the browser's light/dark preference. */
135
+ export declare const autoTheme: TableTheme;
136
+ /** `lightGrayTheme` or `darkGrayTheme`, following the browser's light/dark preference. */
137
+ export declare const autoGrayTheme: TableTheme;
138
+ /** `lightAmberTheme` or `darkAmberTheme`, following the browser's light/dark preference. */
139
+ export declare const autoAmberTheme: TableTheme;
140
+ /** Converts a theme to CSS custom properties, for the `style` of a container element. */
141
+ export declare function themeToCssVars(theme: TableTheme | string | nully): CSSProperties | undefined;
142
+ /** Applies a theme to a DOM element ― by default `<html>`, theming every table on the page
143
+ * (including tables inside portals and ones using the `*Core` components, which render no
144
+ * outer element of their own to carry the variables). Variables the theme doesn't specify
145
+ * are removed from the element, so the theme replaces any previously-applied one.
146
+ *
147
+ * This basically does `Object.assign(document.documentElement.style, themeToCssVars(theme))`
148
+ * except that `Object.assign` doesn't work on that `style`, so `setProperty` is used instead. */
149
+ export declare function applyTheme(theme: TableTheme | string | nully, element?: HTMLElement): void;
150
+ /** Converts a theme to `{ className, style }` for spreading onto a container element. */
151
+ export declare function themeProps(theme: TableTheme | string | nully): {
152
+ className: string | undefined;
153
+ style: CSSProperties | undefined;
154
+ };
155
+ /** Resolves the effective theme (the `theme` prop of a component if given, otherwise the
156
+ * `TableConfig` theme) into a class name and style to merge into a container element. */
157
+ export declare function useThemeProps(themeProp: TableTheme | string | nully): {
158
+ className?: string;
159
+ style?: CSSProperties;
160
+ };
package/dist/theme.js ADDED
@@ -0,0 +1,239 @@
1
+ import { useContext } from 'react';
2
+ import { TableConfigContext } from './config.js';
3
+ import { cx } from './util.js';
4
+ /** The double-headed arrow drawn over a column-resize handle: a 40x30 PNG inlined as a data
5
+ * URI, so that the package ships no separate image asset and needs no bundler support for
6
+ * one. Exported for callers that want the image on their own resize affordances. */
7
+ export const resizeCursorImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAeCAYAAABe3VzdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGJoAABiaAZsyYXAAAAEbSURBVFhH7ZSLDsIgDAA3jf//wUZnb4GlEsbaTmUmuwSpPLobMMahI9M0zfUoSHUllPKU9kdqp+pHErwR6kJ7lr/Mvx0RkXuqFylNd8EtTsG9nIJ7aV40ta8qI3fU3Cljmjks4xiTn8Xdl8cSh1ZQJ7TA2CzqxS3olctEJV2CUblMRNJ8Bmty0paiNrV50rZM1rl1H7FJUCf4FIXIkr8U3Nzib8gBOcmd/q6yKcjb8Cafhpx5pVqYthhqK2kVr83Tcjq37iM2f8VMYkIJ7a2Shi1oAQuua4bENUkrXjlwCUJUMiIHbkHwSkblICQIPNDyUOu4NcKCv+IU3Ev4bESQs5iid+Qjkq6dF3UvDiPIalFKDrHFa/zBFg/DC/as2Q7ADo1PAAAAAElFTkSuQmCC';
8
+ /** The `resizeCursor` of every preset: `resizeCursorImage` with its hotspot on the arrow's
9
+ * pivot, degrading to the browser's own `col-resize` where images are unsupported. Kept in
10
+ * sync with the `--dtbl-resize-cursor` fallback in datatable.css. */
11
+ export const defaultResizeCursor = `url(${resizeCursorImage}) 30 15, col-resize`;
12
+ /** The dark scheme: near-black rows with green selection. */
13
+ export const darkColors = {
14
+ colorScheme: 'dark',
15
+ rowBackground: '#121212',
16
+ headerBackground: 'inherit',
17
+ textColor: 'rgba(255, 255, 255, 0.87)',
18
+ rowBorder: '1px solid #2d2d2d',
19
+ selectedBackground: 'rgba(11, 58, 46, 0.85)',
20
+ hoverSelectedBackground: 'rgba(11, 58, 46, 1)',
21
+ hoverUnselectedBackground: 'rgba(11, 58, 46, 0.2)',
22
+ parentSelectedBackground: 'rgba(11, 58, 46, 0.2)',
23
+ resizeCursor: defaultResizeCursor,
24
+ };
25
+ /** The light counterpart of `darkColors` (green-tinted selection on white). These values
26
+ * equal the `var()` fallbacks in datatable.css except that the fallback text color is
27
+ * `inherit`, so this is (nearly) the appearance of an un-themed table. */
28
+ export const lightColors = {
29
+ colorScheme: 'light',
30
+ rowBackground: '#ffffff',
31
+ headerBackground: 'inherit',
32
+ textColor: '#1d2321',
33
+ rowBorder: '1px solid #d9e0dd',
34
+ selectedBackground: 'rgba(11, 58, 46, 0.22)',
35
+ hoverSelectedBackground: 'rgba(11, 58, 46, 0.32)',
36
+ hoverUnselectedBackground: 'rgba(11, 58, 46, 0.08)',
37
+ parentSelectedBackground: 'rgba(11, 58, 46, 0.08)',
38
+ resizeCursor: defaultResizeCursor,
39
+ };
40
+ /** A cooler light scheme: gray header band, gray borders, blue selection. */
41
+ export const lightGrayColors = {
42
+ colorScheme: 'light',
43
+ rowBackground: '#fbfcfd',
44
+ headerBackground: '#e9edf1',
45
+ textColor: '#1c1c1c',
46
+ rowBorder: '1px solid #c9d1d9',
47
+ selectedBackground: '#cee2f5',
48
+ hoverSelectedBackground: '#bdd7f0',
49
+ hoverUnselectedBackground: 'rgba(30, 60, 90, 0.06)',
50
+ parentSelectedBackground: 'rgba(30, 60, 90, 0.06)',
51
+ resizeCursor: defaultResizeCursor,
52
+ };
53
+ /** A softer dark scheme: gray rows with blue selection. */
54
+ export const darkGrayColors = {
55
+ colorScheme: 'dark',
56
+ rowBackground: '#1c1c1c',
57
+ headerBackground: 'inherit',
58
+ textColor: 'rgba(255, 255, 255, 0.87)',
59
+ rowBorder: '1px solid #3a3a3a',
60
+ selectedBackground: '#2a4a68',
61
+ hoverSelectedBackground: '#345a7e',
62
+ hoverUnselectedBackground: 'rgba(255, 255, 255, 0.08)',
63
+ parentSelectedBackground: 'rgba(255, 255, 255, 0.08)',
64
+ resizeCursor: defaultResizeCursor,
65
+ };
66
+ /** A warm light scheme: cream rows, amber selection. */
67
+ export const lightAmberColors = {
68
+ colorScheme: 'light',
69
+ rowBackground: '#fff8ec',
70
+ headerBackground: 'inherit',
71
+ textColor: '#4a3210',
72
+ rowBorder: '1px solid #e8d9bd',
73
+ selectedBackground: '#ffd9a0',
74
+ hoverSelectedBackground: '#f5c87e',
75
+ hoverUnselectedBackground: 'rgba(200, 140, 0, 0.10)',
76
+ parentSelectedBackground: 'rgba(200, 140, 0, 0.10)',
77
+ resizeCursor: defaultResizeCursor,
78
+ };
79
+ /** A warm dark scheme: deep-brown rows, amber selection. */
80
+ export const darkAmberColors = {
81
+ colorScheme: 'dark',
82
+ rowBackground: '#211709',
83
+ headerBackground: 'inherit',
84
+ textColor: 'rgba(255, 243, 224, 0.9)',
85
+ rowBorder: '1px solid #4a3a1e',
86
+ selectedBackground: 'rgba(196, 128, 28, 0.5)',
87
+ hoverSelectedBackground: 'rgba(196, 128, 28, 0.65)',
88
+ hoverUnselectedBackground: 'rgba(255, 190, 92, 0.12)',
89
+ parentSelectedBackground: 'rgba(255, 190, 92, 0.12)',
90
+ resizeCursor: defaultResizeCursor,
91
+ };
92
+ /* The `auto*` schemes below follow the browser's light/dark preference, with no JavaScript
93
+ * listener: every color is a CSS `light-dark()` pair (supported by all major browsers since
94
+ * mid-2024), and `colorScheme: 'light dark'` is what tells the browser to pick a side based
95
+ * on the user's preference. Each pairs the light/dark presets of the same name. */
96
+ /** `lightColors` or `darkColors`, following the browser's light/dark preference. */
97
+ export const autoColors = {
98
+ colorScheme: 'light dark',
99
+ rowBackground: 'light-dark(#ffffff, #121212)',
100
+ headerBackground: 'inherit',
101
+ textColor: 'light-dark(#1d2321, rgba(255, 255, 255, 0.87))',
102
+ rowBorder: '1px solid light-dark(#d9e0dd, #2d2d2d)',
103
+ selectedBackground: 'light-dark(rgba(11, 58, 46, 0.22), rgba(11, 58, 46, 0.85))',
104
+ hoverSelectedBackground: 'light-dark(rgba(11, 58, 46, 0.32), rgba(11, 58, 46, 1))',
105
+ hoverUnselectedBackground: 'light-dark(rgba(11, 58, 46, 0.08), rgba(11, 58, 46, 0.2))',
106
+ parentSelectedBackground: 'light-dark(rgba(11, 58, 46, 0.08), rgba(11, 58, 46, 0.2))',
107
+ resizeCursor: defaultResizeCursor,
108
+ };
109
+ /** `lightGrayColors` or `darkGrayColors`, following the browser's light/dark preference. */
110
+ export const autoGrayColors = {
111
+ colorScheme: 'light dark',
112
+ rowBackground: 'light-dark(#fbfcfd, #1c1c1c)',
113
+ headerBackground: 'light-dark(#e9edf1, #1c1c1c)',
114
+ textColor: 'light-dark(#1c1c1c, rgba(255, 255, 255, 0.87))',
115
+ rowBorder: '1px solid light-dark(#c9d1d9, #3a3a3a)',
116
+ selectedBackground: 'light-dark(#cee2f5, #2a4a68)',
117
+ hoverSelectedBackground: 'light-dark(#bdd7f0, #345a7e)',
118
+ hoverUnselectedBackground: 'light-dark(rgba(30, 60, 90, 0.06), rgba(255, 255, 255, 0.08))',
119
+ parentSelectedBackground: 'light-dark(rgba(30, 60, 90, 0.06), rgba(255, 255, 255, 0.08))',
120
+ resizeCursor: defaultResizeCursor,
121
+ };
122
+ /** `lightAmberColors` or `darkAmberColors`, following the browser's light/dark preference. */
123
+ export const autoAmberColors = {
124
+ colorScheme: 'light dark',
125
+ rowBackground: 'light-dark(#fff8ec, #211709)',
126
+ headerBackground: 'inherit',
127
+ textColor: 'light-dark(#4a3210, rgba(255, 243, 224, 0.9))',
128
+ rowBorder: '1px solid light-dark(#e8d9bd, #4a3a1e)',
129
+ selectedBackground: 'light-dark(#ffd9a0, rgba(196, 128, 28, 0.5))',
130
+ hoverSelectedBackground: 'light-dark(#f5c87e, rgba(196, 128, 28, 0.65))',
131
+ hoverUnselectedBackground: 'light-dark(rgba(200, 140, 0, 0.10), rgba(255, 190, 92, 0.12))',
132
+ parentSelectedBackground: 'light-dark(rgba(200, 140, 0, 0.10), rgba(255, 190, 92, 0.12))',
133
+ resizeCursor: defaultResizeCursor,
134
+ };
135
+ /** The default spacing, equal to the `var()` fallbacks in datatable.css. */
136
+ export const defaultMetrics = {
137
+ cellPadding: '6px 6px',
138
+ itemPadding: '6px 10px',
139
+ treeIndent: 20,
140
+ };
141
+ /** Denser spacing, for information-heavy screens. */
142
+ export const compactMetrics = {
143
+ cellPadding: '2px 6px',
144
+ itemPadding: '2px 8px',
145
+ treeIndent: 14,
146
+ };
147
+ /** Add-on: gives every other table row (starting with the first) a faint tint. Translucent,
148
+ * so it works over any color
149
+ * scheme — combine it with any theme, e.g. `{ ...darkTheme, ...stripedRows }`. */
150
+ export const stripedRows = {
151
+ stripeBackground: 'rgba(128, 128, 128, 0.15)',
152
+ };
153
+ /** The default theme: `lightColors` with default spacing. */
154
+ export const lightTheme = Object.assign(Object.assign({}, lightColors), defaultMetrics);
155
+ /** Dark theme (near-black rows, green selection). */
156
+ export const darkTheme = Object.assign(Object.assign({}, darkColors), defaultMetrics);
157
+ /** Cooler light theme (gray header band, blue selection). */
158
+ export const lightGrayTheme = Object.assign(Object.assign({}, lightGrayColors), defaultMetrics);
159
+ /** Softer dark theme (gray rows, blue selection). */
160
+ export const darkGrayTheme = Object.assign(Object.assign({}, darkGrayColors), defaultMetrics);
161
+ /** Warm light theme (cream rows, amber selection). */
162
+ export const lightAmberTheme = Object.assign(Object.assign({}, lightAmberColors), defaultMetrics);
163
+ /** Warm dark theme (deep-brown rows, amber selection). */
164
+ export const darkAmberTheme = Object.assign(Object.assign({}, darkAmberColors), defaultMetrics);
165
+ /** `lightTheme` or `darkTheme`, following the browser's light/dark preference. */
166
+ export const autoTheme = Object.assign(Object.assign({}, autoColors), defaultMetrics);
167
+ /** `lightGrayTheme` or `darkGrayTheme`, following the browser's light/dark preference. */
168
+ export const autoGrayTheme = Object.assign(Object.assign({}, autoGrayColors), defaultMetrics);
169
+ /** `lightAmberTheme` or `darkAmberTheme`, following the browser's light/dark preference. */
170
+ export const autoAmberTheme = Object.assign(Object.assign({}, autoAmberColors), defaultMetrics);
171
+ const themeVarNames = {
172
+ rowBackground: '--dtbl-row-bg',
173
+ headerBackground: '--dtbl-header-bg',
174
+ textColor: '--dtbl-text',
175
+ rowBorder: '--dtbl-row-border',
176
+ cellPadding: '--dtbl-cell-padding',
177
+ itemPadding: '--dtbl-item-padding',
178
+ treeIndent: '--dtbl-indent',
179
+ stripeBackground: '--dtbl-stripe-bg',
180
+ selectedBackground: '--dtbl-selected-bg',
181
+ hoverSelectedBackground: '--dtbl-hover-selected-bg',
182
+ hoverUnselectedBackground: '--dtbl-hover-unselected-bg',
183
+ parentSelectedBackground: '--dtbl-parent-selected-bg',
184
+ resizeCursor: '--dtbl-resize-cursor',
185
+ };
186
+ /** Converts a theme to CSS custom properties, for the `style` of a container element. */
187
+ export function themeToCssVars(theme) {
188
+ if (theme == null || typeof theme === 'string') {
189
+ return undefined;
190
+ }
191
+ let style = {};
192
+ if (theme.colorScheme != null) {
193
+ style.colorScheme = theme.colorScheme;
194
+ }
195
+ for (let key of Object.keys(themeVarNames)) {
196
+ let value = theme[key];
197
+ if (value != null) {
198
+ style[themeVarNames[key]] = typeof value === 'number' ? `${value}px` : value;
199
+ }
200
+ }
201
+ return style;
202
+ }
203
+ /** Applies a theme to a DOM element ― by default `<html>`, theming every table on the page
204
+ * (including tables inside portals and ones using the `*Core` components, which render no
205
+ * outer element of their own to carry the variables). Variables the theme doesn't specify
206
+ * are removed from the element, so the theme replaces any previously-applied one.
207
+ *
208
+ * This basically does `Object.assign(document.documentElement.style, themeToCssVars(theme))`
209
+ * except that `Object.assign` doesn't work on that `style`, so `setProperty` is used instead. */
210
+ export function applyTheme(theme, element) {
211
+ const vars = themeToCssVars(theme);
212
+ if (vars) {
213
+ const style = (element !== null && element !== void 0 ? element : document.documentElement).style;
214
+ for (let name of [...Object.values(themeVarNames), 'color-scheme']) {
215
+ const value = name === 'color-scheme' ? vars.colorScheme : vars[name];
216
+ if (value != null) {
217
+ style.setProperty(name, String(value));
218
+ }
219
+ else {
220
+ style.removeProperty(name);
221
+ }
222
+ }
223
+ }
224
+ }
225
+ /** Converts a theme to `{ className, style }` for spreading onto a container element. */
226
+ export function themeProps(theme) {
227
+ return {
228
+ className: typeof theme === 'string' ? theme : theme === null || theme === void 0 ? void 0 : theme.className,
229
+ style: themeToCssVars(theme),
230
+ };
231
+ }
232
+ /** Resolves the effective theme (the `theme` prop of a component if given, otherwise the
233
+ * `TableConfig` theme) into a class name and style to merge into a container element. */
234
+ export function useThemeProps(themeProp) {
235
+ const config = useContext(TableConfigContext);
236
+ const theme = themeProp !== null && themeProp !== void 0 ? themeProp : config.theme;
237
+ const { className, style } = themeProps(theme);
238
+ return { className: cx(className) || undefined, style };
239
+ }
package/dist/util.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { IMapSource, ISet } from './interfaces.js';
2
+ /** Shorthand for `null | undefined`, i.e. the states a value can have when it's absent. */
3
+ export type nully = null | undefined;
4
+ /** Makes the properties `K` of `T` optional, e.g. `Optionalize<Props, 'color'|'size'>`. */
5
+ export type Optionalize<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
6
+ /** Creates a new object whose prototype is the given object, so that properties of the
7
+ * original object are visible on the new object, but new properties can be added without
8
+ * modifying the original. */
9
+ export declare function newObjectWithPrototype<T extends object>(prototype: T): T;
10
+ /** Converts each key-value pair of a map to an element of an output array, like Array.map. */
11
+ export declare function mapMap<K, V, R>(map: IMapSource<K, V>, mapper: (k: K, v: V, index: number) => R): R[];
12
+ /** Adds `key` to the set if it is absent, or removes it if it is present. */
13
+ export declare function toggleSetMembership<T>(set: ISet<T>, key: T): void;
14
+ /** True on macOS/iOS, where ⌘ plays the role that Ctrl plays elsewhere (the OS treats
15
+ * Ctrl+click as a right-click, so Ctrl can't be used as a mouse modifier there). */
16
+ export declare const isMacLike: boolean;
17
+ /** Whether the platform's multi-select mouse modifier is held: ⌘ on Mac (where Ctrl+click
18
+ * means right-click), Ctrl elsewhere. Use for mouse events; keyboard shortcuts should
19
+ * usually accept either key since the OS reserves different combos on each platform. */
20
+ export declare function isCtrlOrCmd(e: Readonly<{
21
+ ctrlKey?: boolean;
22
+ metaKey?: boolean;
23
+ }>): boolean;
24
+ /** Returns a copy of `obj` without the specified keys (replaces lodash `omit`). */
25
+ export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
26
+ /** Combines class names, ignoring null/undefined/false/'' (replaces the `classnames`
27
+ * package for the simple case of string arguments). */
28
+ export declare function cx(...classes: Array<string | false | nully>): string;
package/dist/util.js ADDED
@@ -0,0 +1,45 @@
1
+ /** Creates a new object whose prototype is the given object, so that properties of the
2
+ * original object are visible on the new object, but new properties can be added without
3
+ * modifying the original. */
4
+ export function newObjectWithPrototype(prototype) {
5
+ return Object.create(prototype);
6
+ }
7
+ /** Converts each key-value pair of a map to an element of an output array, like Array.map. */
8
+ export function mapMap(map, mapper) {
9
+ let result = [];
10
+ map.forEach(function (v, k) {
11
+ this.result.push(mapper(k, v, this.index++));
12
+ }, { result, index: 0 });
13
+ return result;
14
+ }
15
+ /** Adds `key` to the set if it is absent, or removes it if it is present. */
16
+ export function toggleSetMembership(set, key) {
17
+ if (set.has(key)) {
18
+ set.delete(key);
19
+ }
20
+ else {
21
+ set.add(key);
22
+ }
23
+ }
24
+ /** True on macOS/iOS, where ⌘ plays the role that Ctrl plays elsewhere (the OS treats
25
+ * Ctrl+click as a right-click, so Ctrl can't be used as a mouse modifier there). */
26
+ export const isMacLike = typeof navigator !== 'undefined' && /Mac|iP(hone|ad|od)/.test(navigator.userAgent);
27
+ /** Whether the platform's multi-select mouse modifier is held: ⌘ on Mac (where Ctrl+click
28
+ * means right-click), Ctrl elsewhere. Use for mouse events; keyboard shortcuts should
29
+ * usually accept either key since the OS reserves different combos on each platform. */
30
+ export function isCtrlOrCmd(e) {
31
+ return isMacLike ? !!e.metaKey : !!e.ctrlKey;
32
+ }
33
+ /** Returns a copy of `obj` without the specified keys (replaces lodash `omit`). */
34
+ export function omit(obj, ...keys) {
35
+ let result = Object.assign({}, obj);
36
+ for (let key of keys) {
37
+ delete result[key];
38
+ }
39
+ return result;
40
+ }
41
+ /** Combines class names, ignoring null/undefined/false/'' (replaces the `classnames`
42
+ * package for the simple case of string arguments). */
43
+ export function cx(...classes) {
44
+ return classes.filter(c => c).join(' ');
45
+ }