@canonical/ds-types 0.37.0 → 0.40.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.
@@ -1,25 +1,8 @@
1
1
  /**
2
- * Navigation item - Public API (WD405)
3
- *
4
- * A unified type for navigation items across the design system.
5
- * Used by Breadcrumbs, SiteNavigation, FileTree, MegaMenu, etc.
6
- *
7
- * Only fields consumed by the shared navigation utilities belong here.
8
- * Styling and rendering customization (CSS classes, custom renderers)
9
- * belong on the item types that components declare by extending this
10
- * one (e.g. BreadcrumbItem, MenuItem), using each framework's own idioms.
2
+ * Fields every navigation item carries, independent of its identity.
3
+ * Composed into `Item` alongside `ItemIdentity`.
11
4
  */
12
- export interface Item {
13
- /**
14
- * Unique identifier for the item when a URL is not provided.
15
- * e.g. 'section-header' for a non-navigable grouping item.
16
- */
17
- key?: string;
18
- /**
19
- * Navigation URL for the item, used for navigable links.
20
- * e.g. '/dashboard' for directing to the dashboard page.
21
- */
22
- url?: string;
5
+ interface ItemFields {
23
6
  /**
24
7
  * Display text for the item in the navigation UI.
25
8
  * e.g. 'Dashboard' as the visible label.
@@ -30,12 +13,77 @@ export interface Item {
30
13
  * e.g. true to prevent clicks on unavailable features.
31
14
  */
32
15
  disabled?: boolean;
16
+ /**
17
+ * Flag marking a node that exists to structure or decorate the list rather
18
+ * than to be chosen — a separator, a group heading, a spacer.
19
+ * e.g. true on the rule drawn between two groups of menu items.
20
+ *
21
+ * This is not a stronger `disabled`. `disabled` says "an item you may not
22
+ * choose right now"; `presentational` says "not an item", which makes
23
+ * `disabled` meaningless rather than false. Keyboard navigation skips both,
24
+ * but only `presentational` nodes are absent from the count a user perceives.
25
+ */
26
+ presentational?: boolean;
33
27
  /**
34
28
  * Array of child items for nested navigation structures.
35
29
  * e.g. Submenu items under a parent like 'Settings'.
36
30
  */
37
31
  items?: Item[];
38
32
  }
33
+ /**
34
+ * The identity of a navigation item - Public API (WD405)
35
+ *
36
+ * Every item must be addressable: the shared navigation utilities index a tree
37
+ * by item id, so an item with no identity has no place in that index. One of
38
+ * `url` or `key` is therefore required — either, or both — and the requirement
39
+ * is carried by the type instead of being checked at runtime.
40
+ *
41
+ * `url` wins when both are present, so the union discriminates on it: the
42
+ * `key`-only arm states `url?: undefined`, which is what lets a check on `url`
43
+ * prove that `key` is there.
44
+ */
45
+ type ItemIdentity = {
46
+ /**
47
+ * Unique identifier for the item, required when no URL is provided.
48
+ * Unique within the whole tree; a collision leaves only the later
49
+ * item in the index (warned in development).
50
+ * This is the answer for a non-navigable node,
51
+ * e.g. 'section-header' for a grouping item.
52
+ */
53
+ key: string;
54
+ /** Absent — an item without a URL is identified by its `key`. */
55
+ url?: undefined;
56
+ } | {
57
+ /**
58
+ * Unique identifier for the item, optional alongside a URL.
59
+ * `url` takes precedence over it when both are present.
60
+ */
61
+ key?: string;
62
+ /**
63
+ * Navigation URL for the item, used for navigable links.
64
+ * e.g. '/dashboard' for directing to the dashboard page.
65
+ * Doubles as the item's identity, taking precedence over `key`.
66
+ * Unique within the whole tree; a collision leaves only the later
67
+ * item in the index (warned in development).
68
+ */
69
+ url: string;
70
+ };
71
+ /**
72
+ * Navigation item - Public API (WD405)
73
+ *
74
+ * A unified type for navigation items across the design system.
75
+ * Used by Breadcrumbs, SiteNavigation, FileTree, MegaMenu, etc.
76
+ *
77
+ * Only fields consumed by the shared navigation utilities belong here.
78
+ * Styling and rendering customization (CSS classes, custom renderers)
79
+ * belong on the item types that components declare by composing this
80
+ * one (e.g. BreadcrumbItem, MenuItem), using each framework's own idioms.
81
+ *
82
+ * The identity requirement makes `Item` a union, so compose it with `&`
83
+ * rather than `interface … extends` (which cannot extend a union), and drop
84
+ * fields from it with `_DistributiveOmit` rather than `Omit`.
85
+ */
86
+ export type Item = ItemFields & ItemIdentity;
39
87
  /**
40
88
  * Annotated navigation item - Private API (WD405)
41
89
  *
@@ -46,9 +94,21 @@ export interface Item {
46
94
  * (extra fields beyond the base WD405 `Item`, e.g. `icon`/`slot`) survives
47
95
  * annotation with its fields intact and fully typed.
48
96
  *
97
+ * Distributes over union item types: `_Item<A | B>` annotates each member
98
+ * separately (`_Item<A> | _Item<B>`-shaped), so a discriminated union of entry
99
+ * kinds (e.g. a menu item vs a separator) keeps its discriminant and
100
+ * member-specific fields after annotation. A plain `Omit<A | B, "items">`
101
+ * would collapse the union to its common keys. Children remain the **whole**
102
+ * union: a tree of `A | B` entries has `_Item<A | B>[]` children on every
103
+ * member, whichever member the parent is.
104
+ *
105
+ * Instantiate `_Item` with the tree's FULL entry union: `_Item<OneMember>`
106
+ * re-types `items` with just that member, statically hiding the other entry
107
+ * kinds a heterogeneous tree's children can hold at runtime.
108
+ *
49
109
  * @template T - The item type being annotated (defaults to the base `Item`)
50
110
  */
51
- export type _Item<T extends Item = Item> = Omit<T, "items"> & {
111
+ export type _Item<T extends Item = Item> = _DistributiveOmit<T, "items"> & {
52
112
  /**
53
113
  * URL (or key) of the parent item in the navigation hierarchy.
54
114
  * e.g. '/parent' for a child item under '/parent', or null for the root.
@@ -65,10 +125,34 @@ export type _Item<T extends Item = Item> = Omit<T, "items"> & {
65
125
  *
66
126
  * `T`'s own `items` is omitted before intersecting so this annotated form
67
127
  * (`_Item<T>[]`) is the one surfaced when iterating children — otherwise the
68
- * intersection would expose `T`'s base element type.
128
+ * intersection would expose `T`'s base element type. `T` here is the whole
129
+ * (possibly union) entry type, so children stay heterogeneous regardless of
130
+ * which member the parent is.
69
131
  */
70
132
  items?: _Item<T>[];
71
133
  };
134
+ /**
135
+ * A distributing `Omit` - Private API (WD405)
136
+ *
137
+ * An `Omit` that distributes over union types: `_DistributiveOmit<A | B, K>`
138
+ * is `Omit<A, K> | Omit<B, K>`, where the plain `Omit<A | B, K>` would first
139
+ * collapse the union to its common keys. `Item` is itself a union — its
140
+ * identity requirement is one — so dropping a field from it (a flat trail with
141
+ * no `items`, say) needs this rather than `Omit`, which would quietly make
142
+ * both `key` and `url` optional again. Intersecting the annotation fields
143
+ * with the distributed union then distributes too, keeping `_Item<A | B>`
144
+ * member-shaped. The conditional is only on the `Omit` half — the annotation
145
+ * fields above stay a concrete object type, so generic code can access
146
+ * `parentUrl`/`depth`/`items` without resolving the conditional.
147
+ *
148
+ * Exported, but private in the same sense as `_Item` and `_Index`: it is for
149
+ * code that DEFINES an item type, not for code that passes items around. An
150
+ * application composing its own item adds fields with `&` and needs nothing
151
+ * from here; it is the packages declaring `BreadcrumbItem`, `TabItem` and the
152
+ * rest that must drop a field from a union, and they live behind this same
153
+ * `_` convention.
154
+ */
155
+ export type _DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
72
156
  /**
73
157
  * Index for quick lookup of items by URL or key - Private API (WD405)
74
158
  *
@@ -79,4 +163,5 @@ export type _Item<T extends Item = Item> = Omit<T, "items"> & {
79
163
  export type _Index<T extends Item = Item> = {
80
164
  [url: string]: _Item<T>;
81
165
  };
166
+ export {};
82
167
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/navigation/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,IAAI;IACnB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;CAChB;AAED;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG;IAC5D;;;OAGG;IACH,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAEzB;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;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,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,IAAI,CAAC,SAAS,OAAO,GACvE,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GACV,KAAK,CAAC;AAEV;;;;;;GAMG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,IAAI;IAC1C,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;CACzB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canonical/ds-types",
3
3
  "description": "Types and related type utilities for Pragma.",
4
- "version": "0.37.0",
4
+ "version": "0.40.0",
5
5
  "type": "module",
6
6
  "main": "dist/esm/index.js",
7
7
  "module": "dist/esm/index.js",
@@ -45,11 +45,11 @@
45
45
  "test": "echo 'Type-only package — no runtime tests'"
46
46
  },
47
47
  "devDependencies": {
48
- "@biomejs/biome": "2.4.9",
49
- "@canonical/biome-config": "^0.37.0",
50
- "@canonical/typescript-config": "^0.37.0",
51
- "@canonical/webarchitect": "^0.37.0",
48
+ "@biomejs/biome": "2.5.13",
49
+ "@canonical/biome-config": "^0.40.0",
50
+ "@canonical/typescript-config": "^0.40.0",
51
+ "@canonical/webarchitect": "^0.40.0",
52
52
  "typescript": "^5.9.3"
53
53
  },
54
- "gitHead": "ac8383bdb4be1bcd6fa6c4960a1e3d78c0279688"
54
+ "gitHead": "9136b71518260c5f33e7619da087198c931e4c13"
55
55
  }