@vaadin/side-nav 24.4.0-alpha12 → 24.4.0-alpha14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/side-nav",
3
- "version": "24.4.0-alpha12",
3
+ "version": "24.4.0-alpha14",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -35,11 +35,11 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@open-wc/dedupe-mixin": "^1.3.0",
38
- "@vaadin/a11y-base": "24.4.0-alpha12",
39
- "@vaadin/component-base": "24.4.0-alpha12",
40
- "@vaadin/vaadin-lumo-styles": "24.4.0-alpha12",
41
- "@vaadin/vaadin-material-styles": "24.4.0-alpha12",
42
- "@vaadin/vaadin-themable-mixin": "24.4.0-alpha12",
38
+ "@vaadin/a11y-base": "24.4.0-alpha14",
39
+ "@vaadin/component-base": "24.4.0-alpha14",
40
+ "@vaadin/vaadin-lumo-styles": "24.4.0-alpha14",
41
+ "@vaadin/vaadin-material-styles": "24.4.0-alpha14",
42
+ "@vaadin/vaadin-themable-mixin": "24.4.0-alpha14",
43
43
  "lit": "^3.0.0"
44
44
  },
45
45
  "devDependencies": {
@@ -52,5 +52,5 @@
52
52
  "web-types.json",
53
53
  "web-types.lit.json"
54
54
  ],
55
- "gitHead": "811e8327e02a8ecdca0bb5d6528856e70d429d0c"
55
+ "gitHead": "303c07338b748bc6036a92a92cf1733c3bc351eb"
56
56
  }
@@ -94,9 +94,14 @@ declare class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixi
94
94
  expanded: boolean;
95
95
 
96
96
  /**
97
- * Whether the path of the item matches the current path.
98
- * Set when the item is appended to DOM or when navigated back
99
- * to the page that contains this item using the browser.
97
+ * Whether the item's path matches the current browser URL.
98
+ *
99
+ * A match occurs when both share the same base origin (like https://example.com),
100
+ * the same path (like /path/to/page), and the browser URL contains all
101
+ * the query parameters with the same values from the item's path.
102
+ *
103
+ * The state is updated when the item is added to the DOM or when the browser
104
+ * navigates to a new page.
100
105
  */
101
106
  readonly current: boolean;
102
107
 
@@ -115,9 +115,14 @@ class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixin(Themab
115
115
  },
116
116
 
117
117
  /**
118
- * Whether the path of the item matches the current path.
119
- * Set when the item is appended to DOM or when navigated back
120
- * to the page that contains this item using the browser.
118
+ * Whether the item's path matches the current browser URL.
119
+ *
120
+ * A match occurs when both share the same base origin (like https://example.com),
121
+ * the same path (like /path/to/page), and the browser URL contains at least
122
+ * all the query parameters with the same values from the item's path.
123
+ *
124
+ * The state is updated when the item is added to the DOM or when the browser
125
+ * navigates to a new page.
121
126
  *
122
127
  * @type {boolean}
123
128
  */
@@ -189,12 +194,14 @@ class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixin(Themab
189
194
  this.__updateCurrent();
190
195
 
191
196
  window.addEventListener('popstate', this.__boundUpdateCurrent);
197
+ window.addEventListener('side-nav-location-changed', this.__boundUpdateCurrent);
192
198
  }
193
199
 
194
200
  /** @protected */
195
201
  disconnectedCallback() {
196
202
  super.disconnectedCallback();
197
203
  window.removeEventListener('popstate', this.__boundUpdateCurrent);
204
+ window.removeEventListener('side-nav-location-changed', this.__boundUpdateCurrent);
198
205
  }
199
206
 
200
207
  /** @protected */
@@ -264,10 +271,9 @@ class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixin(Themab
264
271
  if (this.path == null) {
265
272
  return false;
266
273
  }
267
- return (
268
- matchPaths(document.location.pathname, this.path) ||
269
- this.pathAliases.some((alias) => matchPaths(document.location.pathname, alias))
270
- );
274
+
275
+ const browserPath = `${document.location.pathname}${document.location.search}`;
276
+ return matchPaths(browserPath, this.path) || this.pathAliases.some((alias) => matchPaths(browserPath, alias));
271
277
  }
272
278
  }
273
279
 
@@ -7,6 +7,7 @@ import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js';
7
7
  import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
8
8
  import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
9
9
  import { SideNavChildrenMixin, type SideNavI18n } from './vaadin-side-nav-children-mixin.js';
10
+ import type { SideNavItem } from './vaadin-side-nav-item.js';
10
11
 
11
12
  export type { SideNavI18n };
12
13
 
@@ -21,6 +22,15 @@ export interface SideNavCustomEventMap {
21
22
 
22
23
  export type SideNavEventMap = HTMLElementEventMap & SideNavCustomEventMap;
23
24
 
25
+ export type NavigateEvent = {
26
+ path: SideNavItem['path'];
27
+ target: SideNavItem['target'];
28
+ current: SideNavItem['current'];
29
+ expanded: SideNavItem['expanded'];
30
+ pathAliases: SideNavItem['pathAliases'];
31
+ originalEvent: MouseEvent;
32
+ };
33
+
24
34
  /**
25
35
  * `<vaadin-side-nav>` is a Web Component for navigation menus.
26
36
  *
@@ -83,6 +93,40 @@ declare class SideNav extends SideNavChildrenMixin(FocusMixin(ElementMixin(Thema
83
93
  */
84
94
  collapsed: boolean;
85
95
 
96
+ /**
97
+ * Callback function for router integration.
98
+ *
99
+ * When a side nav item link is clicked, this function is called and the default click action is cancelled.
100
+ * This delegates the responsibility of navigation to the function's logic.
101
+ *
102
+ * The click event action is not cancelled in the following cases:
103
+ * - The click event has a modifier (e.g. `metaKey`, `shiftKey`)
104
+ * - The click event is on an external link
105
+ * - The click event is on a link with `target="_blank"`
106
+ * - The function explicitly returns `false`
107
+ *
108
+ * The function receives an object with the properties of the clicked side-nav item:
109
+ * - `path`: The path of the navigation item.
110
+ * - `target`: The target of the navigation item.
111
+ * - `current`: A boolean indicating whether the navigation item is currently selected.
112
+ * - `expanded`: A boolean indicating whether the navigation item is expanded.
113
+ * - `pathAliases`: An array of path aliases for the navigation item.
114
+ * - `originalEvent`: The original DOM event that triggered the navigation.
115
+ *
116
+ * Also see the `location` property for updating the highlighted navigation item on route change.
117
+ */
118
+ onNavigate?: ((event: NavigateEvent) => boolean) | ((event: NavigateEvent) => void);
119
+
120
+ /**
121
+ * A change to this property triggers an update of the highlighted item in the side navigation. While it typically
122
+ * corresponds to the browser's URL, the specific value assigned to the property is irrelevant. The component has
123
+ * its own internal logic for determining which item is highlighted.
124
+ *
125
+ * The main use case for this property is when the side navigation is used with a client-side router. In this case,
126
+ * the component needs to be informed about route changes so it can update the highlighted item.
127
+ */
128
+ location: any;
129
+
86
130
  addEventListener<K extends keyof SideNavEventMap>(
87
131
  type: K,
88
132
  listener: (this: SideNav, ev: SideNavEventMap[K]) => void,
@@ -103,6 +103,48 @@ class SideNav extends SideNavChildrenMixin(FocusMixin(ElementMixin(ThemableMixin
103
103
  notify: true,
104
104
  reflectToAttribute: true,
105
105
  },
106
+
107
+ /**
108
+ * Callback function for router integration.
109
+ *
110
+ * When a side nav item link is clicked, this function is called and the default click action is cancelled.
111
+ * This delegates the responsibility of navigation to the function's logic.
112
+ *
113
+ * The click event action is not cancelled in the following cases:
114
+ * - The click event has a modifier (e.g. `metaKey`, `shiftKey`)
115
+ * - The click event is on an external link
116
+ * - The click event is on a link with `target="_blank"`
117
+ * - The function explicitly returns `false`
118
+ *
119
+ * The function receives an object with the properties of the clicked side-nav item:
120
+ * - `path`: The path of the navigation item.
121
+ * - `target`: The target of the navigation item.
122
+ * - `current`: A boolean indicating whether the navigation item is currently selected.
123
+ * - `expanded`: A boolean indicating whether the navigation item is expanded.
124
+ * - `pathAliases`: An array of path aliases for the navigation item.
125
+ * - `originalEvent`: The original DOM event that triggered the navigation.
126
+ *
127
+ * Also see the `location` property for updating the highlighted navigation item on route change.
128
+ *
129
+ * @type {function(Object): boolean | undefined}
130
+ */
131
+ onNavigate: {
132
+ attribute: false,
133
+ },
134
+
135
+ /**
136
+ * A change to this property triggers an update of the highlighted item in the side navigation. While it typically
137
+ * corresponds to the browser's URL, the specific value assigned to the property is irrelevant. The component has
138
+ * its own internal logic for determining which item is highlighted.
139
+ *
140
+ * The main use case for this property is when the side navigation is used with a client-side router. In this case,
141
+ * the component needs to be informed about route changes so it can update the highlighted item.
142
+ *
143
+ * @type {any}
144
+ */
145
+ location: {
146
+ observer: '__locationChanged',
147
+ },
106
148
  };
107
149
  }
108
150
 
@@ -114,6 +156,7 @@ class SideNav extends SideNavChildrenMixin(FocusMixin(ElementMixin(ThemableMixin
114
156
  super();
115
157
 
116
158
  this._labelId = `side-nav-label-${generateUniqueId()}`;
159
+ this.addEventListener('click', this.__onClick);
117
160
  }
118
161
 
119
162
  /**
@@ -203,10 +246,62 @@ class SideNav extends SideNavChildrenMixin(FocusMixin(ElementMixin(ThemableMixin
203
246
  }
204
247
  }
205
248
 
249
+ /** @private */
250
+ __locationChanged() {
251
+ window.dispatchEvent(new CustomEvent('side-nav-location-changed'));
252
+ }
253
+
206
254
  /** @private */
207
255
  __toggleCollapsed() {
208
256
  this.collapsed = !this.collapsed;
209
257
  }
258
+
259
+ /** @private */
260
+ __onClick(e) {
261
+ if (!this.onNavigate) {
262
+ return;
263
+ }
264
+
265
+ const hasModifier = e.metaKey || e.shiftKey;
266
+ if (hasModifier) {
267
+ // Allow default action for clicks with modifiers
268
+ return;
269
+ }
270
+
271
+ const composedPath = e.composedPath();
272
+ const item = composedPath.find((el) => el.localName && el.localName.includes('side-nav-item'));
273
+ const anchor = composedPath.find((el) => el instanceof HTMLAnchorElement);
274
+ if (!item || !item.shadowRoot.contains(anchor)) {
275
+ // Not a click on a side-nav-item anchor
276
+ return;
277
+ }
278
+
279
+ const isRelative = anchor.href && anchor.href.startsWith(location.origin);
280
+ if (!isRelative) {
281
+ // Allow default action for external links
282
+ return;
283
+ }
284
+
285
+ if (item.target === '_blank') {
286
+ // Allow default action for links with target="_blank"
287
+ return;
288
+ }
289
+
290
+ // Call the onNavigate callback
291
+ const result = this.onNavigate({
292
+ path: item.path,
293
+ target: item.target,
294
+ current: item.current,
295
+ expanded: item.expanded,
296
+ pathAliases: item.pathAliases,
297
+ originalEvent: e,
298
+ });
299
+
300
+ if (result !== false) {
301
+ // Cancel the default action if the callback didn't return false
302
+ e.preventDefault();
303
+ }
304
+ }
210
305
  }
211
306
 
212
307
  defineCustomElement(SideNav);
package/web-types.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/web-types",
3
3
  "name": "@vaadin/side-nav",
4
- "version": "24.4.0-alpha12",
4
+ "version": "24.4.0-alpha14",
5
5
  "description-markup": "markdown",
6
6
  "contributions": {
7
7
  "html": {
@@ -157,6 +157,25 @@
157
157
  ]
158
158
  }
159
159
  },
160
+ {
161
+ "name": "on-navigate",
162
+ "description": "Callback function for router integration.\n\nWhen a side nav item link is clicked, this function is called and the default click action is cancelled.\nThis delegates the responsibility of navigation to the function's logic.\n\nThe click event action is not cancelled in the following cases:\n- The click event has a modifier (e.g. `metaKey`, `shiftKey`)\n- The click event is on an external link\n- The click event is on a link with `target=\"_blank\"`\n- The function explicitly returns `false`\n\nThe function receives an object with the properties of the clicked side-nav item:\n- `path`: The path of the navigation item.\n- `target`: The target of the navigation item.\n- `current`: A boolean indicating whether the navigation item is currently selected.\n- `expanded`: A boolean indicating whether the navigation item is expanded.\n- `pathAliases`: An array of path aliases for the navigation item.\n- `originalEvent`: The original DOM event that triggered the navigation.\n\nAlso see the `location` property for updating the highlighted navigation item on route change.",
163
+ "value": {
164
+ "type": [
165
+ "function Object: boolean",
166
+ "undefined"
167
+ ]
168
+ }
169
+ },
170
+ {
171
+ "name": "location",
172
+ "description": "A change to this property triggers an update of the highlighted item in the side navigation. While it typically\ncorresponds to the browser's URL, the specific value assigned to the property is irrelevant. The component has\nits own internal logic for determining which item is highlighted.\n\nThe main use case for this property is when the side navigation is used with a client-side router. In this case,\nthe component needs to be informed about route changes so it can update the highlighted item.",
173
+ "value": {
174
+ "type": [
175
+ "any"
176
+ ]
177
+ }
178
+ },
160
179
  {
161
180
  "name": "theme",
162
181
  "description": "The theme variants to apply to the component.",
@@ -197,6 +216,25 @@
197
216
  "boolean"
198
217
  ]
199
218
  }
219
+ },
220
+ {
221
+ "name": "onNavigate",
222
+ "description": "Callback function for router integration.\n\nWhen a side nav item link is clicked, this function is called and the default click action is cancelled.\nThis delegates the responsibility of navigation to the function's logic.\n\nThe click event action is not cancelled in the following cases:\n- The click event has a modifier (e.g. `metaKey`, `shiftKey`)\n- The click event is on an external link\n- The click event is on a link with `target=\"_blank\"`\n- The function explicitly returns `false`\n\nThe function receives an object with the properties of the clicked side-nav item:\n- `path`: The path of the navigation item.\n- `target`: The target of the navigation item.\n- `current`: A boolean indicating whether the navigation item is currently selected.\n- `expanded`: A boolean indicating whether the navigation item is expanded.\n- `pathAliases`: An array of path aliases for the navigation item.\n- `originalEvent`: The original DOM event that triggered the navigation.\n\nAlso see the `location` property for updating the highlighted navigation item on route change.",
223
+ "value": {
224
+ "type": [
225
+ "function Object: boolean",
226
+ "undefined"
227
+ ]
228
+ }
229
+ },
230
+ {
231
+ "name": "location",
232
+ "description": "A change to this property triggers an update of the highlighted item in the side navigation. While it typically\ncorresponds to the browser's URL, the specific value assigned to the property is irrelevant. The component has\nits own internal logic for determining which item is highlighted.\n\nThe main use case for this property is when the side navigation is used with a client-side router. In this case,\nthe component needs to be informed about route changes so it can update the highlighted item.",
233
+ "value": {
234
+ "type": [
235
+ "any"
236
+ ]
237
+ }
200
238
  }
201
239
  ],
202
240
  "events": [
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/web-types",
3
3
  "name": "@vaadin/side-nav",
4
- "version": "24.4.0-alpha12",
4
+ "version": "24.4.0-alpha14",
5
5
  "description-markup": "markdown",
6
6
  "framework": "lit",
7
7
  "framework-config": {
@@ -89,6 +89,13 @@
89
89
  "kind": "expression"
90
90
  }
91
91
  },
92
+ {
93
+ "name": "?onNavigate",
94
+ "description": "Callback function for router integration.\n\nWhen a side nav item link is clicked, this function is called and the default click action is cancelled.\nThis delegates the responsibility of navigation to the function's logic.\n\nThe click event action is not cancelled in the following cases:\n- The click event has a modifier (e.g. `metaKey`, `shiftKey`)\n- The click event is on an external link\n- The click event is on a link with `target=\"_blank\"`\n- The function explicitly returns `false`\n\nThe function receives an object with the properties of the clicked side-nav item:\n- `path`: The path of the navigation item.\n- `target`: The target of the navigation item.\n- `current`: A boolean indicating whether the navigation item is currently selected.\n- `expanded`: A boolean indicating whether the navigation item is expanded.\n- `pathAliases`: An array of path aliases for the navigation item.\n- `originalEvent`: The original DOM event that triggered the navigation.\n\nAlso see the `location` property for updating the highlighted navigation item on route change.",
95
+ "value": {
96
+ "kind": "expression"
97
+ }
98
+ },
92
99
  {
93
100
  "name": ".i18n",
94
101
  "description": "The object used to localize this component.\n\nTo change the default localization, replace the entire\n`i18n` object with a custom one.\n\nThe object has the following structure and default values:\n```\n{\n toggle: 'Toggle child items'\n}\n```",
@@ -96,6 +103,13 @@
96
103
  "kind": "expression"
97
104
  }
98
105
  },
106
+ {
107
+ "name": ".location",
108
+ "description": "A change to this property triggers an update of the highlighted item in the side navigation. While it typically\ncorresponds to the browser's URL, the specific value assigned to the property is irrelevant. The component has\nits own internal logic for determining which item is highlighted.\n\nThe main use case for this property is when the side navigation is used with a client-side router. In this case,\nthe component needs to be informed about route changes so it can update the highlighted item.",
109
+ "value": {
110
+ "kind": "expression"
111
+ }
112
+ },
99
113
  {
100
114
  "name": "@collapsed-changed",
101
115
  "description": "Fired when the `collapsed` property changes.",