Skip to content

Commit

Permalink
shallow routing
Browse files Browse the repository at this point in the history
  • Loading branch information
TGlide committed Feb 23, 2024
1 parent de43d95 commit dbdcc40
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion dialog-shallow-routing/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ declare global {
// interface Locals {}
// interface PageData {}
// interface Platform {}
interface PageState {
dialogOpen?: string;
}
}
}

export {};
export { };
13 changes: 11 additions & 2 deletions dialog-shallow-routing/src/lib/Dialog/Content.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<script lang="ts">
import { melt } from '@melt-ui/svelte';
import { createSync, melt } from '@melt-ui/svelte';
import { dialogRegistry, type DialogName } from '.';
import { page } from '$app/stores';
export let name: DialogName;
const {
elements: { portalled, title, content, description, close, overlay }
elements: { portalled, title, content, description, close, overlay },
states: { open }
} = dialogRegistry.get(name);
const sync = createSync({ open });
$: sync.open($page.state.dialogOpen === name, ($open) => {
if ($page.state.dialogOpen !== name) {
dialogRegistry.shallow(name, $open);
}
});
</script>

<div use:melt={$portalled}>
Expand Down
14 changes: 13 additions & 1 deletion dialog-shallow-routing/src/lib/Dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createDialog, type CreateDialogProps, type Dialog as DialogType } from '@melt-ui/svelte';
import Trigger from './Trigger.svelte';
import Content from './Content.svelte';
import { pushState } from '$app/navigation';

export const DIALOG_NAMES = ['settings', 'login', 'delete'] as const;

Expand All @@ -22,9 +23,20 @@ function createDialogRegistry() {
registry.set(name, dialog);
}

// Shallow routing
function shallow(name: DialogName, open: boolean) {
if (open) {

pushState('', {
dialogOpen: open ? name : 'none'
});
}
}

return {
get,
set
set,
shallow
};
}

Expand Down

0 comments on commit dbdcc40

Please sign in to comment.