Skip to content

Commit

Permalink
cleanup; config opts
Browse files Browse the repository at this point in the history
  • Loading branch information
mateothegreat committed Nov 3, 2024
1 parent 2e04b72 commit 7d82b68
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 44 deletions.
51 changes: 22 additions & 29 deletions src/lib/default-overlay.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import { IsFocusWithin } from 'runed';
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
import type { ModalInstance } from './modal-instance';
Expand All @@ -14,51 +13,45 @@
externalClickEvent: (e: PointerEvent) => void;
}
onMount(() => {
instance.element = ref;
// Focus the modal content div when the component is mounted
modalContentRef?.focus();
});
onDestroy(() => {
dispatch('close');
document.removeEventListener('click', handleClick, true);
});
let ref: HTMLElement = $state();
let modalContentRef: HTMLDivElement;
const dispatch = createEventDispatcher<Record<keyof $$Events, any>>();
const handleClick = (e: MouseEvent) => {
// const target = e.target as Node;
// if (ref && !ref.contains(target)) {
// if (instance.manager.isOnTop(instance.config.id)) {
// instance.manager.close(instance.config.id);
// }
// }
if (instance.config.closeOnExternalClick) {
const target = e.target as Node;
// Check if the click target is outside the modal content
if (modalContentRef && !modalContentRef.contains(target)) {
if (instance.manager.isOnTop(instance.config.id)) {
instance.manager.close(instance.config.id);
}
}
}
};
const focusWithin = new IsFocusWithin(() => ref);
const handleKeyDown = (e: KeyboardEvent) => {
console.log(e.key);
if (e.key === 'Escape') {
const handleKeyDown = async (e: KeyboardEvent) => {
if (e.key === 'Escape' && instance.config.closeOnEscape) {
await instance.config.escFn?.();
instance.manager.close(instance.config.id);
}
};
document.addEventListener('click', handleClick, true);
onMount(() => {
instance.element = ref;
modalContentRef?.focus();
document.addEventListener('mousedown', handleClick);
});
$effect(() => {
console.log(focusWithin.current);
onDestroy(() => {
dispatch('close');
// Remove click event listener on cleanup
document.removeEventListener('mousedown', handleClick);
});
</script>

<div bind:this={ref} class="modal-overlay absolute bottom-0 left-0 right-0 top-0 flex h-full w-full items-center justify-center bg-black/50 backdrop-blur-sm transition-all duration-500 {instance.config.classes}">
<div bind:this={modalContentRef} role="modal" tabindex="0" onkeydown={handleKeyDown} onclick={handleClick} class="modal-content">
{focusWithin.current}
<div bind:this={modalContentRef} role="dialog" tabindex="0" on:keydown={handleKeyDown} class="modal-content">
<instance.config.component {instance} {...rest} />
</div>
</div>
5 changes: 3 additions & 2 deletions src/lib/modal-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ export class ModalConfig<T = any> {
public classes?: string;
public component: any;
public closeIcon?: boolean;
public closeOnEscape?: boolean;
public closeOnExternalClick?: boolean;
public closeOnEscape?: boolean = true;
public closeOnExternalClick?: boolean = true;
public data?: T;
public escFn?: () => Promise<void>;

public constructor(obj: ModalConfig<T>) {
Object.assign(this, obj);
Expand Down
10 changes: 1 addition & 9 deletions src/lib/modal-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mount, unmount } from 'svelte';
import DefaultOverlay from './default-overlay.svelte';
import type { ModalConfig } from './modal-config';
import { ModalInstance } from './modal-instance';
import { visible } from './store';

export class ModalManager {
public modals: { [name: string]: ModalInstance<any> } = {};

Expand Down Expand Up @@ -31,14 +31,6 @@ export class ModalManager {
delete this.modals[id];
}

public show(id: string): void {
visible.set(true);
}

public hide(id: string): void {
visible.set(false);
}

public getIndex(id: string): number {
return this.modals[id].index;
}
Expand Down
3 changes: 0 additions & 3 deletions src/lib/store.ts

This file was deleted.

14 changes: 13 additions & 1 deletion test/app/src/app.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@
</script>

<button on:click={() => modalManager.open({ id: 'modal-one', component: ModalOne }, { name: 'one' })}>Open Modal One</button>
<button on:click={() => modalManager.open({ id: 'modal-two', component: ModalTwo }, { name: 'two' })}>Open Modal Two</button>
<button
on:click={() =>
modalManager.open({
id: 'modal-two',
component: ModalTwo,
escFn: async () => {
// simulate some async work
console.log('esc pressed, waiting 1 second before closing');
await new Promise((resolve) => setTimeout(resolve, 1000));
}
})}>
Open Modal Two (delayed esc key closing)
</button>

0 comments on commit 7d82b68

Please sign in to comment.