Max Schmitt 18b51308ff
test(svelte): migrate svelte-navigator to custom minimal router (#30225)
Repro: `cd tests/components/ct-svelte && rm -rf node_modules
package-lock.json && npm i && npx playwright test --project=chromium`

Follow-up based on
https://github.com/microsoft/playwright/pull/28624#issuecomment-1858608101.

Svelte has no router by default, only SvelteKit - so lets remove the
package which is not maintained anymore and not recommended.
2024-04-04 16:01:08 +02:00

35 lines
835 B
Svelte

<script>
import { onMount, onDestroy } from 'svelte';
import LoginPage from './pages/LoginPage.svelte';
import DashboardPage from './pages/DashboardPage.svelte';
let path = '';
function updatePath() {
path = window.location.pathname;
}
onMount(() => {
updatePath();
window.addEventListener('popstate', updatePath);
});
onDestroy(() => {
window.removeEventListener('popstate', updatePath);
});
/**
* @param newPath {string}
*/
function navigate(newPath) {
history.pushState({}, '', newPath);
updatePath();
}
</script>
<header>
<a on:click={(e) => { e.preventDefault(); navigate('/'); }} href='/login'>Login</a>
<a on:click={(e) => { e.preventDefault(); navigate('/dashboard'); }} href='/dashboard'>Dashboard</a>
</header>
{#if path === '/'}
<LoginPage />
{:else if path === '/dashboard'}
<DashboardPage />
{/if}