Browser History API state manager
npm install @cfware/history-stateBrowser History API state manager.
``js
import historyState, * as historyFunctions from '@cfware/history-state';
historyState.addEventListener('update', () => {
/ An internal navigation has occurred with history API. /
});
historyState.addEventListener('refuse', () => {
/ An internal navigation has been refused due to dirty state. /
if (window.confirm('Do you really want to leave this page without saving?')) {
/ honor the back/forward/link click requested by the user. /
historyFunctions.bypassDirty();
}
});
`
This should be used instead of history.state which will contain additional fields@cfware/history-state
that are internal to .
Use historyState.pushState or historyState.replaceState to modify. historyState.state
should be treated as if it is frozen.
Default null
This must be used in place of history.pushState. This function causes an update orrefuse event to be dispatched depending on historyFunctions.isDirty().
This must be used in place of history.replaceState. This replaces the current history
entry.
Unlike historyState.pushState this function does not cause update or refuse to bedirty
dispatched. This function succeeds regardless of status.
#### update
This is dispatched when the current location is changed, including upon window onload.
#### refuse
This is dispatched when a location change is refused due to historyState.dirty being
true. This will happen when the user hits back/forward without leaving the SPA or when
an internal link is clicked.
Calling this function after a refuse event will allow navigation that was blocked
by the dirty status.
This can be set true or false to indicate if the current page has unsaved changes.
Default false
Retrieve the current dirty status.
This attaches a click event listener to element which intercepts normal clicks
on any element visible from element. During startup this is run for documenthistoryFunctions.linkInterceptor
so in most cases you will not need to run manually. The`
exception is closed shadow roots, for example:js
const shadowRoot = this.attachShadow({mode: 'closed'});
this.shadowRoot.innerHTML = 'link';
Any link click that is intercepted results in a call to historyState.pushState.
The listenerOptions argument is passed to element.addEventListener as the second
argument.
This component listens for beforeunload. If historyState.isDirty() is true the unload
will be canceled.
By default this module will intercept clicks on links. Links to pages withindocument.baseURI will be treated as part of the SPA. This is disabled per link bytarget
adding the , download or no-history-state attributes.
The default click listener can be disabled by calling historyFunctions.setDefaultInterceptOptions(false)false
before window.onload occurs. Values other than will be used as the options argument to
the default interceptor.
The link interceptor will not take any action if event.preventDefault() has already
been run by another listener.
This event should be ignored, monitor the update event of historyState` instead.
[npm-image]: https://img.shields.io/npm/v/@cfware/history-state.svg
[npm-url]: https://npmjs.org/package/@cfware/history-state