3 ms·
Sorry in a bit of rush as it's Sunday here, I will try to write a blog post with more detailed example later tonight. For now, I will share this snippet: do
by laktek 10y ago
Sorry in a bit of rush as it's Sunday here, I will try to write a blog post with more detailed example later tonight.
For now, I will share this snippet:
document.addEventListener('state-change', (ev) => {
const { key, action } = ev.detail;
const state = { key, action };
window.history.pushState(state, null, `/${action}/${key}`);
console.log(`triggered state change. action: ${action} key: ${key}`);
return handleState(state);
});
And then in your app code (for example, when a user clicks a button), trigger a state change:
const event = new CustomEvent('state-change', {
detail : {
state: state,
key: key
}
});
document.dispatchEvent(event);
- Siilwyn 10y agoHey just wanted to say thanks! Never heard of the `CustomEvent` API I [used it](https://github.com/AyriaPublic/desktop/commit/3e3b0ad76e7624717e4576873f1732d70e310b30#diff-15ee858ae5d5aa9b3e2846cfd1c4c466R17 https://github.com/AyriaPublic/desktop/commit/3e3b0ad76e7624...) in a small work in progress Electron app. ^^