@canonical/ds-types 0.37.0 → 0.38.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,73 @@ 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
+ * This is the answer for a non-navigable node,
49
+ * e.g. 'section-header' for a grouping item.
50
+ */
51
+ key: string;
52
+ /** Absent — an item without a URL is identified by its `key`. */
53
+ url?: undefined;
54
+ } | {
55
+ /**
56
+ * Unique identifier for the item, optional alongside a URL.
57
+ * `url` takes precedence over it when both are present.
58
+ */
59
+ key?: string;
60
+ /**
61
+ * Navigation URL for the item, used for navigable links.
62
+ * e.g. '/dashboard' for directing to the dashboard page.
63
+ * Doubles as the item's identity, taking precedence over `key`.
64
+ */
65
+ url: string;
66
+ };
67
+ /**
68
+ * Navigation item - Public API (WD405)
69
+ *
70
+ * A unified type for navigation items across the design system.
71
+ * Used by Breadcrumbs, SiteNavigation, FileTree, MegaMenu, etc.
72
+ *
73
+ * Only fields consumed by the shared navigation utilities belong here.
74
+ * Styling and rendering customization (CSS classes, custom renderers)
75
+ * belong on the item types that components declare by composing this
76
+ * one (e.g. BreadcrumbItem, MenuItem), using each framework's own idioms.
77
+ *
78
+ * The identity requirement makes `Item` a union, so compose it with `&`
79
+ * rather than `interface … extends` (which cannot extend a union), and drop
80
+ * fields from it with `_DistributiveOmit` rather than `Omit`.
81
+ */
82
+ export type Item = ItemFields & ItemIdentity;
39
83
  /**
40
84
  * Annotated navigation item - Private API (WD405)
41
85
  *
@@ -46,9 +90,21 @@ export interface Item {
46
90
  * (extra fields beyond the base WD405 `Item`, e.g. `icon`/`slot`) survives
47
91
  * annotation with its fields intact and fully typed.
48
92
  *
93
+ * Distributes over union item types: `_Item<A | B>` annotates each member
94
+ * separately (`_Item<A> | _Item<B>`-shaped), so a discriminated union of entry
95
+ * kinds (e.g. a menu item vs a separator) keeps its discriminant and
96
+ * member-specific fields after annotation. A plain `Omit<A | B, "items">`
97
+ * would collapse the union to its common keys. Children remain the **whole**
98
+ * union: a tree of `A | B` entries has `_Item<A | B>[]` children on every
99
+ * member, whichever member the parent is.
100
+ *
101
+ * Instantiate `_Item` with the tree's FULL entry union: `_Item<OneMember>`
102
+ * re-types `items` with just that member, statically hiding the other entry
103
+ * kinds a heterogeneous tree's children can hold at runtime.
104
+ *
49
105
  * @template T - The item type being annotated (defaults to the base `Item`)
50
106
  */
51
- export type _Item<T extends Item = Item> = Omit<T, "items"> & {
107
+ export type _Item<T extends Item = Item> = _DistributiveOmit<T, "items"> & {
52
108
  /**
53
109
  * URL (or key) of the parent item in the navigation hierarchy.
54
110
  * e.g. '/parent' for a child item under '/parent', or null for the root.
@@ -65,10 +121,34 @@ export type _Item<T extends Item = Item> = Omit<T, "items"> & {
65
121
  *
66
122
  * `T`'s own `items` is omitted before intersecting so this annotated form
67
123
  * (`_Item<T>[]`) is the one surfaced when iterating children — otherwise the
68
- * intersection would expose `T`'s base element type.
124
+ * intersection would expose `T`'s base element type. `T` here is the whole
125
+ * (possibly union) entry type, so children stay heterogeneous regardless of
126
+ * which member the parent is.
69
127
  */
70
128
  items?: _Item<T>[];
71
129
  };
130
+ /**
131
+ * A distributing `Omit` - Private API (WD405)
132
+ *
133
+ * An `Omit` that distributes over union types: `_DistributiveOmit<A | B, K>`
134
+ * is `Omit<A, K> | Omit<B, K>`, where the plain `Omit<A | B, K>` would first
135
+ * collapse the union to its common keys. `Item` is itself a union — its
136
+ * identity requirement is one — so dropping a field from it (a flat trail with
137
+ * no `items`, say) needs this rather than `Omit`, which would quietly make
138
+ * both `key` and `url` optional again. Intersecting the annotation fields
139
+ * with the distributed union then distributes too, keeping `_Item<A | B>`
140
+ * member-shaped. The conditional is only on the `Omit` half — the annotation
141
+ * fields above stay a concrete object type, so generic code can access
142
+ * `parentUrl`/`depth`/`items` without resolving the conditional.
143
+ *
144
+ * Exported, but private in the same sense as `_Item` and `_Index`: it is for
145
+ * code that DEFINES an item type, not for code that passes items around. An
146
+ * application composing its own item adds fields with `&` and needs nothing
147
+ * from here; it is the packages declaring `BreadcrumbItem`, `TabItem` and the
148
+ * rest that must drop a field from a union, and they live behind this same
149
+ * `_` convention.
150
+ */
151
+ export type _DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
72
152
  /**
73
153
  * Index for quick lookup of items by URL or key - Private API (WD405)
74
154
  *
@@ -79,4 +159,5 @@ export type _Item<T extends Item = Item> = Omit<T, "items"> & {
79
159
  export type _Index<T extends Item = Item> = {
80
160
  [url: string]: _Item<T>;
81
161
  };
162
+ export {};
82
163
  //# 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;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;CACpB,CAAC;AAEF;;;;;;GAM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,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.38.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.38.0",
50
+ "@canonical/typescript-config": "^0.38.0",
51
+ "@canonical/webarchitect": "^0.38.0",
52
52
  "typescript": "^5.9.3"
53
53
  },
54
- "gitHead": "ac8383bdb4be1bcd6fa6c4960a1e3d78c0279688"
54
+ "gitHead": "4d10a68a9b299af6fe3d1d7d053e54ca128944ad"
55
55
  }