Skip to content

Commit bc7d7f1

Browse files
no-history feature
1 parent bba03e4 commit bc7d7f1

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vaadin-component-factory/vcf-anchor-nav",
3-
"version": "23.3.0",
3+
"version": "23.4.0",
44
"description": "Component with tabs used as anchor navigation and content sections. Automates the linking of tabs and sections.",
55
"main": "vcf-anchor-nav.js",
66
"author": "Vaadin Ltd",

src/vcf-anchor-nav.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { html, PolymerElement } from '@polymer/polymer/polymer-element';
22
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin';
33
import { ElementMixin } from '@vaadin/component-base/src/element-mixin';
4-
import { smoothScrollPolyfill, stickyPolyfill } from '../lib/common-js-modules.esm';
4+
import { smoothScrollPolyfill, stickyPolyfill } from './common-js-modules.esm';
55
import { ResizeObserver } from '@juggle/resize-observer';
66
import '@vaadin/tabs/vaadin-tabs';
77
import '@vaadin/tabs/vaadin-tab';
@@ -148,7 +148,7 @@ export class AnchorNavElement extends ElementMixin(ThemableMixin(PolymerElement)
148148
}
149149

150150
static get version() {
151-
return '23.3.0';
151+
return '23.4.0';
152152
}
153153

154154
static get properties() {
@@ -206,6 +206,7 @@ export class AnchorNavElement extends ElementMixin(ThemableMixin(PolymerElement)
206206
noHistory: {
207207
type: Boolean,
208208
value: false,
209+
reflectToAttribute: true,
209210
notify: true
210211
}
211212
};
@@ -270,7 +271,6 @@ export class AnchorNavElement extends ElementMixin(ThemableMixin(PolymerElement)
270271
this.sections.forEach(section => section.removeAttribute('tabindex'));
271272
};
272273
this.addEventListener('no-history-changed', event => {
273-
// TODO: multiple <vcf-anchor-navs>, one with no-history, one without.
274274
this._toggleNoHistory(this.noHistory);
275275
});
276276
}
@@ -284,7 +284,7 @@ export class AnchorNavElement extends ElementMixin(ThemableMixin(PolymerElement)
284284
}
285285

286286
_disableNoHistory() {
287-
if (!history.elementsWithNoHistory || !history.oldPushState) {
287+
if (!history.elementsWithNoHistory) {
288288
return;
289289
}
290290
const indexOfCurrent = history.elementsWithNoHistory.indexOf(this);
@@ -293,19 +293,30 @@ export class AnchorNavElement extends ElementMixin(ThemableMixin(PolymerElement)
293293
}
294294
history.elementsWithNoHistory.splice(indexOfCurrent, 1);
295295
if (history.elementsWithNoHistory.length === 0) {
296-
history.pushState = history.oldPushState;
297-
history.oldPushState = null;
296+
this._clearOldPushState();
298297
}
299298
}
300299

301300
_enableNoHistory() {
302301
if (!history.elementsWithNoHistory || history.elementsWithNoHistory.length === 0) {
303302
history.elementsWithNoHistory = [this];
304303
history.oldPushState = history.pushState;
305-
history.pushState = function pushState() {
306-
history.replaceState('', '');
304+
history.currentPathname = window.location.pathname;
305+
history.pushState = function pushState(state, unused, url) {
306+
let incomingPathname = null;
307+
if (typeof url === 'string' || url instanceof String) {
308+
incomingPathname = url;
309+
} else if (typeof url === 'object' && !Array.isArray(url) && url !== null) {
310+
incomingPathname = url.pathname;
311+
}
312+
if (incomingPathname != history.currentPathname) {
313+
history.currentPathname = incomingPathname;
314+
history.oldPushState(state, unused, url);
315+
} else {
316+
history.replaceState('', '');
317+
}
307318
};
308-
} else {
319+
} else if (!history.elementsWithNoHistory.includes(this)) {
309320
history.elementsWithNoHistory.push(this);
310321
}
311322
}
@@ -323,6 +334,11 @@ export class AnchorNavElement extends ElementMixin(ThemableMixin(PolymerElement)
323334
super.disconnectedCallback();
324335
}
325336

337+
connectedCallback() {
338+
super.connectedCallback();
339+
this._toggleNoHistory(this.noHistory);
340+
}
341+
326342
_onNavFocus(e) {
327343
if (AnchorNavElement.isSame(e.target)) {
328344
this.sections.forEach(section => section.setAttribute('tabindex', '0'));

0 commit comments

Comments
 (0)