@gtkx/components 1.0.0-rc.3 → 1.0.0-rc.4

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 (60) hide show
  1. package/README.md +4 -5
  2. package/dist/adw/combo-row.d.ts +1 -0
  3. package/dist/adw/combo-row.d.ts.map +1 -1
  4. package/dist/adw/combo-row.js.map +1 -1
  5. package/dist/adw/types.d.ts +2 -1
  6. package/dist/adw/types.d.ts.map +1 -1
  7. package/dist/adw/types.js.map +1 -1
  8. package/dist/column-view.d.ts.map +1 -1
  9. package/dist/column-view.js +2 -2
  10. package/dist/column-view.js.map +1 -1
  11. package/dist/drop-down.d.ts +1 -0
  12. package/dist/drop-down.d.ts.map +1 -1
  13. package/dist/drop-down.js.map +1 -1
  14. package/dist/grid-view.js +1 -1
  15. package/dist/grid-view.js.map +1 -1
  16. package/dist/index.d.ts +1 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +0 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/internal/cells.d.ts +3 -3
  21. package/dist/internal/cells.d.ts.map +1 -1
  22. package/dist/internal/cells.js.map +1 -1
  23. package/dist/internal/collection-index.d.ts +5 -5
  24. package/dist/internal/collection-index.d.ts.map +1 -1
  25. package/dist/internal/collection-index.js +3 -3
  26. package/dist/internal/collection-index.js.map +1 -1
  27. package/dist/internal/collection-model.d.ts.map +1 -1
  28. package/dist/internal/collection-model.js +2 -5
  29. package/dist/internal/collection-model.js.map +1 -1
  30. package/dist/internal/collection.d.ts +2 -2
  31. package/dist/internal/collection.d.ts.map +1 -1
  32. package/dist/internal/collection.js.map +1 -1
  33. package/dist/internal/drop-down.d.ts.map +1 -1
  34. package/dist/internal/drop-down.js.map +1 -1
  35. package/dist/internal/use-collection.d.ts +4 -4
  36. package/dist/internal/use-collection.d.ts.map +1 -1
  37. package/dist/internal/use-collection.js +2 -2
  38. package/dist/internal/use-collection.js.map +1 -1
  39. package/dist/types.d.ts +64 -40
  40. package/dist/types.d.ts.map +1 -1
  41. package/dist/types.js.map +1 -1
  42. package/package.json +7 -7
  43. package/src/adw/combo-row.tsx +1 -0
  44. package/src/adw/types.ts +2 -1
  45. package/src/column-view.tsx +11 -11
  46. package/src/drop-down.tsx +1 -0
  47. package/src/grid-view.tsx +1 -1
  48. package/src/index.ts +7 -11
  49. package/src/internal/cells.tsx +12 -12
  50. package/src/internal/collection-index.ts +16 -16
  51. package/src/internal/collection-model.ts +2 -5
  52. package/src/internal/collection.ts +2 -2
  53. package/src/internal/drop-down.tsx +4 -4
  54. package/src/internal/use-collection.tsx +6 -6
  55. package/src/types.ts +70 -52
  56. package/dist/size-group.d.ts +0 -12
  57. package/dist/size-group.d.ts.map +0 -1
  58. package/dist/size-group.js +0 -41
  59. package/dist/size-group.js.map +0 -1
  60. package/src/size-group.tsx +0 -77
package/src/types.ts CHANGED
@@ -6,22 +6,19 @@ import type {
6
6
  GtkGridViewProps,
7
7
  GtkListViewProps,
8
8
  } from "@gtkx/jsx/gtk";
9
- import type { ComponentPropsWithRef, ElementType, ReactNode, Ref } from "react";
10
-
11
- /** Props of a container's Child component: the widget to render and its own props. */
12
- type ChildProps<C extends ElementType> = {
13
- component: C;
14
- } & ComponentPropsWithRef<C>;
9
+ import type { ReactNode } from "react";
15
10
 
16
11
  /**
17
12
  * A single item in a collection model, identified by a stable id and holding an
18
13
  * arbitrary value. Nested items form a tree.
19
14
  */
20
- type Item<T = unknown> = {
15
+ type ListItem<T = unknown> = {
21
16
  /** Stable identifier used to track the item across updates and selection. */
22
17
  id: string;
18
+ /** Payload handed to the cell renderer as `ListItemRenderArgs.item`. */
23
19
  value: T;
24
- children?: Item<T>[] | undefined;
20
+ /** Child items nested under this one, which turn a plain item list into a tree. */
21
+ children?: ListItem<T>[] | undefined;
25
22
  /** Hides the tree expander arrow even when the item has children. */
26
23
  hideExpander?: boolean | undefined;
27
24
  /** Adds indentation matching the item's depth in the tree. */
@@ -31,17 +28,20 @@ type Item<T = unknown> = {
31
28
  };
32
29
 
33
30
  /** A group of items rendered under a shared section header. */
34
- type Section<S = unknown, T = unknown> = {
31
+ type ListSection<S = unknown, T = unknown> = {
35
32
  /** Stable identifier used to track the section across updates. */
36
33
  id: string;
34
+ /** Payload handed to the section header renderer as `ListSectionRenderArgs.section`. */
37
35
  value: S;
38
36
  /** Items belonging to this section. */
39
- data: Item<T>[];
37
+ data: ListItem<T>[];
40
38
  };
41
39
 
42
- /** Arguments passed to an {@link ItemRenderer} when rendering one cell. */
43
- type RenderItemArgs<T> = {
40
+ /** Arguments passed to a {@link ListItemRenderer} when rendering one cell. */
41
+ type ListItemRenderArgs<T> = {
42
+ /** Value of the `ListItem` being rendered. */
44
43
  item: T;
44
+ /** Position of the item in the flattened item list, which excludes section headers and collapsed tree rows. */
45
45
  index: number;
46
46
  /** Depth of the item within a tree, starting at zero for top-level items. */
47
47
  depth?: number | undefined;
@@ -49,57 +49,79 @@ type RenderItemArgs<T> = {
49
49
  isExpanded?: boolean | undefined;
50
50
  };
51
51
 
52
- /** Arguments passed to a {@link HeaderRenderer} when rendering one section header. */
53
- type RenderHeaderArgs<S> = {
52
+ /** Arguments passed to a {@link ListSectionRenderer} when rendering one section header. */
53
+ type ListSectionRenderArgs<S> = {
54
+ /** Value of the `ListSection` whose header is being rendered. */
54
55
  section: S;
55
56
  };
56
57
 
57
58
  /** Renders the contents of one cell of a collection view. */
58
- type ItemRenderer<T> = (args: RenderItemArgs<T>) => ReactNode;
59
+ type ListItemRenderer<T> = (args: ListItemRenderArgs<T>) => ReactNode;
59
60
  /** Renders the contents of one section header of a collection view. */
60
- type HeaderRenderer<S> = (args: RenderHeaderArgs<S>) => ReactNode;
61
+ type ListSectionRenderer<S> = (args: ListSectionRenderArgs<S>) => ReactNode;
61
62
 
63
+ /** Size a collection view requests for each cell before its contents render, keeping scroll estimates steady. */
62
64
  type ItemSizeProps = {
65
+ /** Height in pixels every cell asks for until its contents render; unset lets each cell size itself. */
63
66
  estimatedItemHeight?: number | undefined;
67
+ /** Width in pixels every cell asks for until its contents render; unset lets each cell size itself. */
64
68
  estimatedItemWidth?: number | undefined;
65
69
  };
66
70
 
71
+ /** Controlled selection shared by the multi-item collection views. */
67
72
  type SelectionProps = {
73
+ /** Ids of the items to keep selected; omitting it leaves the view's own selection alone. */
68
74
  selectedIds?: string[] | null | undefined;
75
+ /** Called with the ids of every selected item whenever the selection changes, and once on mount. */
69
76
  onSelectionChanged?: ((ids: string[]) => void) | null | undefined;
77
+ /** How much the user may select, one item at a time unless `MULTIPLE` or `NONE` is given. */
70
78
  selectionMode?: Gtk.SelectionMode | null | undefined;
71
79
  };
72
80
 
81
+ /** Controlled expansion for the views that turn nested `ListItem.children` into a tree. */
73
82
  type ExpansionProps = {
83
+ /** Ids of the items to keep expanded; omitting it leaves the rows' own expanded state alone. */
74
84
  expandedIds?: string[] | null | undefined;
85
+ /** Called with the ids of every expanded row whenever expansion changes. */
75
86
  onExpandedChange?: ((ids: string[]) => void) | null | undefined;
76
87
  };
77
88
 
89
+ /** The data a collection view renders, either as a plain item list or grouped into sections. */
78
90
  type SourceProps<T, S> = {
79
- items?: Item<T>[] | undefined;
80
- sections?: Section<S, T>[] | undefined;
91
+ /** Items to render, nesting through `ListItem.children` for a tree; ignored once `sections` is given. */
92
+ items?: ListItem<T>[] | undefined;
93
+ /** Items grouped under section headers, rendered in place of `items`. */
94
+ sections?: ListSection<S, T>[] | undefined;
81
95
  };
82
96
 
83
97
  /** One column of a {@link ColumnView}, pairing Gtk.ColumnViewColumn props with a cell renderer. */
84
- type Column<T = unknown> = Omit<GtkColumnViewColumnProps, "factory" | "sorter" | "id" | "title"> & {
98
+ type ColumnViewColumn<T = unknown> = Omit<GtkColumnViewColumnProps, "factory" | "sorter" | "id" | "title"> & {
85
99
  /** Stable identifier, also used to address the column through sorting props. */
86
100
  id: string;
101
+ /** Text shown in the column header. */
87
102
  title: string;
88
- renderCell: ItemRenderer<T>;
89
- /** Whether clicking the column header sorts by it. */
90
- sortable?: boolean | undefined;
103
+ /** Renders the contents of this column's cell for one item. */
104
+ renderCell: ListItemRenderer<T>;
105
+ /** Makes the column header clickable, reporting the choice through `onSortChanged`. */
106
+ isSortable?: boolean | undefined;
107
+ /** Menu element popped up as the column header's context menu, on a right-click. */
91
108
  headerMenu?: ReactNode;
92
109
  };
93
110
 
111
+ /** The declarative collection props {@link ColumnView} adds on top of Gtk.ColumnView's own. */
94
112
  type ColumnViewOwnProps<T, S> = SelectionProps &
95
113
  ExpansionProps &
96
114
  SourceProps<T, S> &
97
115
  Omit<ItemSizeProps, "estimatedItemWidth"> & {
98
- columns: Column<T>[];
99
- renderHeader?: HeaderRenderer<S> | null | undefined;
116
+ /** Columns to render, in order; each carries its own cell renderer. */
117
+ columns: ColumnViewColumn<T>[];
118
+ /** Renders the header shown above each section. */
119
+ renderHeader?: ListSectionRenderer<S> | null | undefined;
100
120
  /** Id of the column the view is sorted by, making sorting controlled. */
101
121
  sortColumn?: string | null | undefined;
122
+ /** Direction `sortColumn` is sorted in, defaulting to ascending. */
102
123
  sortOrder?: Gtk.SortType | null | undefined;
124
+ /** Called when the user sorts from a header, with the primary column's id, or null, and its order. */
103
125
  onSortChanged?: ((column: string | null, order: Gtk.SortType) => void) | null | undefined;
104
126
  };
105
127
 
@@ -115,17 +137,21 @@ type ColumnViewProps<T = unknown, S = unknown> = Omit<
115
137
  > &
116
138
  ColumnViewOwnProps<T, S>;
117
139
 
140
+ /** The declarative collection props {@link DropDown} and `ComboRow` add on top of their widget's own. */
118
141
  type DropDownOwnProps<T, S> = SourceProps<T, S> & {
119
142
  /** Id of the currently selected item, making the selection controlled. */
120
143
  selectedId?: string | null | undefined;
144
+ /** Called with the id of the item that became selected. */
121
145
  onSelectionChanged?: ((id: string) => void) | null | undefined;
122
- renderItem?: ItemRenderer<T> | null | undefined;
146
+ /** Renders the collapsed display, and the popup rows too unless `renderListItem` is given. */
147
+ renderItem?: ListItemRenderer<T> | null | undefined;
123
148
  /** Renderer for items in the open popup list, falling back to renderItem when omitted. */
124
- renderListItem?: ItemRenderer<T> | null | undefined;
149
+ renderListItem?: ListItemRenderer<T> | null | undefined;
125
150
  /** Renderer for section headers in the popup list. */
126
- renderHeader?: HeaderRenderer<S> | null | undefined;
151
+ renderHeader?: ListSectionRenderer<S> | null | undefined;
127
152
  };
128
153
 
154
+ /** A drop-down-shaped widget's props with its model and factories swapped for the declarative collection props. */
129
155
  type DropDownWidgetProps<Widget, T, S> = Omit<
130
156
  Widget,
131
157
  "model" | "factory" | "listFactory" | "headerFactory" | keyof DropDownOwnProps<T, S>
@@ -139,10 +165,13 @@ DropDownOwnProps<T, S>;
139
165
  */
140
166
  type DropDownProps<T = unknown, S = unknown> = DropDownWidgetProps<GtkDropDownProps, T, S>;
141
167
 
168
+ /** The declarative collection props {@link GridView} adds on top of Gtk.GridView's own. */
142
169
  type GridViewOwnProps<T> = ItemSizeProps &
143
170
  SelectionProps & {
144
- items?: Item<T>[] | undefined;
145
- renderItem: ItemRenderer<T>;
171
+ /** Items to render as cells; a grid is always flat, so `ListItem.children` is ignored. */
172
+ items?: ListItem<T>[] | undefined;
173
+ /** Renders the contents of one cell. */
174
+ renderItem: ListItemRenderer<T>;
146
175
  };
147
176
 
148
177
  /**
@@ -153,12 +182,15 @@ type GridViewOwnProps<T> = ItemSizeProps &
153
182
  type GridViewProps<T = unknown> = Omit<GtkGridViewProps, "model" | "factory" | keyof GridViewOwnProps<T>> &
154
183
  GridViewOwnProps<T>;
155
184
 
185
+ /** The declarative collection props {@link ListView} adds on top of Gtk.ListView's own. */
156
186
  type ListViewOwnProps<T, S> = ItemSizeProps &
157
187
  SelectionProps &
158
188
  ExpansionProps &
159
189
  SourceProps<T, S> & {
160
- renderItem: ItemRenderer<T>;
161
- renderHeader?: HeaderRenderer<S> | null | undefined;
190
+ /** Renders the contents of one row. */
191
+ renderItem: ListItemRenderer<T>;
192
+ /** Renders the header shown above each section. */
193
+ renderHeader?: ListSectionRenderer<S> | null | undefined;
162
194
  };
163
195
 
164
196
  /**
@@ -173,34 +205,20 @@ type ListViewProps<T = unknown, S = unknown> = Omit<
173
205
  > &
174
206
  ListViewOwnProps<T, S>;
175
207
 
176
- /** Props for {@link SizeGroup}. */
177
- type SizeGroupProps = {
178
- /** How the group equalizes sizes: horizontal, vertical, or both. */
179
- mode?: Gtk.SizeGroupMode | null | undefined;
180
- ref?: Ref<Gtk.SizeGroup | null>;
181
- children?: ReactNode;
182
- };
183
-
184
- /** Adds a single widget, rendered by the given component, to the enclosing {@link SizeGroup}. */
185
- type SizeGroupChildProps<C extends ElementType> = ChildProps<C>;
186
-
187
208
  export {
188
209
  type SelectionProps,
189
210
  type ExpansionProps,
190
- type ChildProps,
191
211
  type DropDownOwnProps,
192
212
  type DropDownWidgetProps,
193
- type Item,
194
- type Section,
195
- type RenderItemArgs,
196
- type RenderHeaderArgs,
197
- type ItemRenderer,
198
- type HeaderRenderer,
199
- type Column,
213
+ type ListItem,
214
+ type ListSection,
215
+ type ListItemRenderArgs,
216
+ type ListSectionRenderArgs,
217
+ type ListItemRenderer,
218
+ type ListSectionRenderer,
219
+ type ColumnViewColumn,
200
220
  type ColumnViewProps,
201
221
  type DropDownProps,
202
222
  type GridViewProps,
203
223
  type ListViewProps,
204
- type SizeGroupProps,
205
- type SizeGroupChildProps,
206
224
  };
@@ -1,12 +0,0 @@
1
- import type { ElementType, ReactNode } from "react";
2
- import type { SizeGroupChildProps, SizeGroupProps } from "./types.js";
3
- type SizeGroupComponent = ((props: SizeGroupProps) => ReactNode) & {
4
- Child: <C extends ElementType>(props: SizeGroupChildProps<C>) => ReactNode;
5
- };
6
- /**
7
- * Creates a Gtk.SizeGroup that keeps widgets joined through `SizeGroup.Child`
8
- * at a common size in the given mode, without contributing a widget of its own.
9
- */
10
- declare const SizeGroup: SizeGroupComponent;
11
- export { SizeGroup };
12
- //# sourceMappingURL=size-group.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"size-group.d.ts","sourceRoot":"","sources":["../src/size-group.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAoB,MAAM,OAAO,CAAC;AAItE,OAAO,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAStE,KAAK,kBAAkB,GAAG,CAAC,CAAC,KAAK,EAAE,cAAc,KAAK,SAAS,CAAC,GAAG;IAC/D,KAAK,EAAE,CAAC,CAAC,SAAS,WAAW,EAAE,KAAK,EAAE,mBAAmB,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC;CAC9E,CAAC;AAIF;;;GAGG;AACH,QAAA,MAAM,SAAS,EAAE,kBAA4E,CAAC;AAoD9F,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -1,41 +0,0 @@
1
- import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { GtkSizeGroup } from "@gtkx/jsx/gtk";
3
- import { useMergedRef } from "@gtkx/react/internal";
4
- import { createContext, useCallback, useContext, useState } from "react";
5
- const SizeGroupContext = createContext(null);
6
- const SizeGroupChild = SizeGroupChildImpl;
7
- /**
8
- * Creates a Gtk.SizeGroup that keeps widgets joined through `SizeGroup.Child`
9
- * at a common size in the given mode, without contributing a widget of its own.
10
- */
11
- const SizeGroup = Object.assign(SizeGroupRoot, { Child: SizeGroupChild });
12
- const useRegister = () => {
13
- const register = useContext(SizeGroupContext);
14
- if (register === null) {
15
- throw new Error("<SizeGroup.Child> must be a child of <SizeGroup>");
16
- }
17
- return register;
18
- };
19
- function SizeGroupChildImpl(props) {
20
- const { component: Component, ref, ...rest } = props;
21
- const refCallback = useMergedRef(ref, useRegister());
22
- return _jsx(Component, { ...rest, ref: refCallback });
23
- }
24
- const addWidget = (widgets, widget) => widgets.includes(widget) ? widgets : [...widgets, widget];
25
- const removeWidget = (widgets, widget) => widgets.filter((entry) => entry !== widget);
26
- function SizeGroupRoot(props) {
27
- const { mode, ref, children } = props;
28
- const [widgets, setWidgets] = useState([]);
29
- const register = useCallback((widget) => {
30
- if (widget === null) {
31
- return;
32
- }
33
- setWidgets((previous) => addWidget(previous, widget));
34
- return () => {
35
- setWidgets((previous) => removeWidget(previous, widget));
36
- };
37
- }, [setWidgets]);
38
- return (_jsxs(_Fragment, { children: [_jsx(GtkSizeGroup, { ref: ref, mode: mode, widgets: widgets }), _jsx(SizeGroupContext.Provider, { value: register, children: children })] }));
39
- }
40
- export { SizeGroup };
41
- //# sourceMappingURL=size-group.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"size-group.js","sourceRoot":"","sources":["../src/size-group.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAczE,MAAM,gBAAgB,GAAG,aAAa,CAAkB,IAAI,CAAC,CAAC;AAC9D,MAAM,cAAc,GAAG,kBAAyF,CAAC;AACjH;;;GAGG;AACH,MAAM,SAAS,GAAuB,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC,CAAC;AAE9F,MAAM,WAAW,GAAG,GAAa,EAAE;IAC/B,MAAM,QAAQ,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAE9C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACxE,CAAC;IAED,OAAO,QAAQ,CAAC;AACpB,CAAC,CAAC;AAEF,SAAS,kBAAkB,CAAC,KAAiC;IACzD,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK,CAAC;IACrD,MAAM,WAAW,GAAG,YAAY,CAAoB,GAAG,EAAE,WAAW,EAAE,CAAC,CAAC;IAExE,OAAO,KAAC,SAAS,OAAK,IAAI,EAAE,GAAG,EAAE,WAAW,GAAI,CAAC;AACrD,CAAC;AAED,MAAM,SAAS,GAAG,CAAC,OAAqB,EAAE,MAAkB,EAAgB,EAAE,CAC1E,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,CAAC,CAAC;AAE9D,MAAM,YAAY,GAAG,CAAC,OAAqB,EAAE,MAAkB,EAAgB,EAAE,CAC7E,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC;AAEhD,SAAS,aAAa,CAAC,KAAqB;IACxC,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;IACtC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAe,EAAE,CAAC,CAAC;IAEzD,MAAM,QAAQ,GAAG,WAAW,CACxB,CAAC,MAAM,EAAE,EAAE;QACP,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;YAClB,OAAO;QACX,CAAC;QAED,UAAU,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;QAEtD,OAAO,GAAG,EAAE;YACR,UAAU,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;QAC7D,CAAC,CAAC;IACN,CAAC,EACD,CAAC,UAAU,CAAC,CACf,CAAC;IAEF,OAAO,CACH,8BACI,KAAC,YAAY,IAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAI,EACxD,KAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,QAAQ,YAAG,QAAQ,GAA6B,IACnF,CACN,CAAC;AACN,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC","sourcesContent":["import type * as Gtk from \"@gtkx/gi/gtk\";\nimport type { ElementType, ReactNode, Ref, RefCallback } from \"react\";\nimport { GtkSizeGroup } from \"@gtkx/jsx/gtk\";\nimport { useMergedRef } from \"@gtkx/react/internal\";\nimport { createContext, useCallback, useContext, useState } from \"react\";\nimport type { SizeGroupChildProps, SizeGroupProps } from \"./types.js\";\n\ntype Register = RefCallback<Gtk.Widget | null>;\n\ntype SizeGroupChildRuntimeProps = {\n component: ElementType;\n ref?: Ref<Gtk.Widget | null> | undefined;\n} & Record<string, unknown>;\n\ntype SizeGroupComponent = ((props: SizeGroupProps) => ReactNode) & {\n Child: <C extends ElementType>(props: SizeGroupChildProps<C>) => ReactNode;\n};\n\nconst SizeGroupContext = createContext<Register | null>(null);\nconst SizeGroupChild = SizeGroupChildImpl as <C extends ElementType>(props: SizeGroupChildProps<C>) => ReactNode;\n/**\n * Creates a Gtk.SizeGroup that keeps widgets joined through `SizeGroup.Child`\n * at a common size in the given mode, without contributing a widget of its own.\n */\nconst SizeGroup: SizeGroupComponent = Object.assign(SizeGroupRoot, { Child: SizeGroupChild });\n\nconst useRegister = (): Register => {\n const register = useContext(SizeGroupContext);\n\n if (register === null) {\n throw new Error(\"<SizeGroup.Child> must be a child of <SizeGroup>\");\n }\n\n return register;\n};\n\nfunction SizeGroupChildImpl(props: SizeGroupChildRuntimeProps): ReactNode {\n const { component: Component, ref, ...rest } = props;\n const refCallback = useMergedRef<Gtk.Widget | null>(ref, useRegister());\n\n return <Component {...rest} ref={refCallback} />;\n}\n\nconst addWidget = (widgets: Gtk.Widget[], widget: Gtk.Widget): Gtk.Widget[] =>\n widgets.includes(widget) ? widgets : [...widgets, widget];\n\nconst removeWidget = (widgets: Gtk.Widget[], widget: Gtk.Widget): Gtk.Widget[] =>\n widgets.filter((entry) => entry !== widget);\n\nfunction SizeGroupRoot(props: SizeGroupProps): ReactNode {\n const { mode, ref, children } = props;\n const [widgets, setWidgets] = useState<Gtk.Widget[]>([]);\n\n const register = useCallback<Register>(\n (widget) => {\n if (widget === null) {\n return;\n }\n\n setWidgets((previous) => addWidget(previous, widget));\n\n return () => {\n setWidgets((previous) => removeWidget(previous, widget));\n };\n },\n [setWidgets],\n );\n\n return (\n <>\n <GtkSizeGroup ref={ref} mode={mode} widgets={widgets} />\n <SizeGroupContext.Provider value={register}>{children}</SizeGroupContext.Provider>\n </>\n );\n}\n\nexport { SizeGroup };\n"]}
@@ -1,77 +0,0 @@
1
- import type * as Gtk from "@gtkx/gi/gtk";
2
- import type { ElementType, ReactNode, Ref, RefCallback } from "react";
3
- import { GtkSizeGroup } from "@gtkx/jsx/gtk";
4
- import { useMergedRef } from "@gtkx/react/internal";
5
- import { createContext, useCallback, useContext, useState } from "react";
6
- import type { SizeGroupChildProps, SizeGroupProps } from "./types.js";
7
-
8
- type Register = RefCallback<Gtk.Widget | null>;
9
-
10
- type SizeGroupChildRuntimeProps = {
11
- component: ElementType;
12
- ref?: Ref<Gtk.Widget | null> | undefined;
13
- } & Record<string, unknown>;
14
-
15
- type SizeGroupComponent = ((props: SizeGroupProps) => ReactNode) & {
16
- Child: <C extends ElementType>(props: SizeGroupChildProps<C>) => ReactNode;
17
- };
18
-
19
- const SizeGroupContext = createContext<Register | null>(null);
20
- const SizeGroupChild = SizeGroupChildImpl as <C extends ElementType>(props: SizeGroupChildProps<C>) => ReactNode;
21
- /**
22
- * Creates a Gtk.SizeGroup that keeps widgets joined through `SizeGroup.Child`
23
- * at a common size in the given mode, without contributing a widget of its own.
24
- */
25
- const SizeGroup: SizeGroupComponent = Object.assign(SizeGroupRoot, { Child: SizeGroupChild });
26
-
27
- const useRegister = (): Register => {
28
- const register = useContext(SizeGroupContext);
29
-
30
- if (register === null) {
31
- throw new Error("<SizeGroup.Child> must be a child of <SizeGroup>");
32
- }
33
-
34
- return register;
35
- };
36
-
37
- function SizeGroupChildImpl(props: SizeGroupChildRuntimeProps): ReactNode {
38
- const { component: Component, ref, ...rest } = props;
39
- const refCallback = useMergedRef<Gtk.Widget | null>(ref, useRegister());
40
-
41
- return <Component {...rest} ref={refCallback} />;
42
- }
43
-
44
- const addWidget = (widgets: Gtk.Widget[], widget: Gtk.Widget): Gtk.Widget[] =>
45
- widgets.includes(widget) ? widgets : [...widgets, widget];
46
-
47
- const removeWidget = (widgets: Gtk.Widget[], widget: Gtk.Widget): Gtk.Widget[] =>
48
- widgets.filter((entry) => entry !== widget);
49
-
50
- function SizeGroupRoot(props: SizeGroupProps): ReactNode {
51
- const { mode, ref, children } = props;
52
- const [widgets, setWidgets] = useState<Gtk.Widget[]>([]);
53
-
54
- const register = useCallback<Register>(
55
- (widget) => {
56
- if (widget === null) {
57
- return;
58
- }
59
-
60
- setWidgets((previous) => addWidget(previous, widget));
61
-
62
- return () => {
63
- setWidgets((previous) => removeWidget(previous, widget));
64
- };
65
- },
66
- [setWidgets],
67
- );
68
-
69
- return (
70
- <>
71
- <GtkSizeGroup ref={ref} mode={mode} widgets={widgets} />
72
- <SizeGroupContext.Provider value={register}>{children}</SizeGroupContext.Provider>
73
- </>
74
- );
75
- }
76
-
77
- export { SizeGroup };