UNPKG

@dvcol/neo-svelte

Version:

Neomorphic ui library for svelte 5

46 lines (45 loc) 1.24 kB
import { getUUID } from '@dvcol/common-utils/common/string'; import { getContext, setContext } from 'svelte'; import { SvelteMap } from 'svelte/reactivity'; export class NeoPortalContext { #id; #dialogs = new SvelteMap(); #ref = $state(); #open = $derived(Array.from(this.#dialogs.values()).filter(Boolean).length); #placement = $state(); get id() { return this.#id; } get ref() { return this.#ref; } get open() { return this.#open; } get placement() { return this.#placement; } constructor(id) { this.#id = id; } updateRef(ref) { this.#ref = ref; } openDialog(id, placement) { this.#dialogs.set(id, true); this.#placement = placement; } closeDialog(id) { const dialog = this.#dialogs.get(id); if (!dialog) return; this.#dialogs.set(id, false); } } const NeoPortalContextSymbol = Symbol('NeoPortalContext'); export function getNeoPortalContext() { return getContext(NeoPortalContextSymbol); } export function setNeoPortalContext(id) { return setContext(NeoPortalContextSymbol, new NeoPortalContext(id || `neo-portal-container-${getUUID()}`)); }