First working prototype
This commit is contained in:
29
src/lib/router.svelte.js
Normal file
29
src/lib/router.svelte.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Three screens, so no router dependency — just enough hash handling that the
|
||||
* Android back button behaves inside an installed PWA.
|
||||
*
|
||||
* `route === null` means "wherever the day state says", which is the default the
|
||||
* worker should land on.
|
||||
*/
|
||||
|
||||
const ROUTES = ['resume', 'today', 'settings'];
|
||||
|
||||
function fromHash() {
|
||||
const h = location.hash.replace(/^#\/?/, '');
|
||||
return ROUTES.includes(h) ? h : null;
|
||||
}
|
||||
|
||||
export const router = $state({ route: fromHash() });
|
||||
|
||||
window.addEventListener('hashchange', () => {
|
||||
router.route = fromHash();
|
||||
});
|
||||
|
||||
export function go(route) {
|
||||
location.hash = route ? `#/${route}` : '';
|
||||
}
|
||||
|
||||
export function back() {
|
||||
if (history.length > 1) history.back();
|
||||
else go(null);
|
||||
}
|
||||
Reference in New Issue
Block a user