UNPKG

bits-ui

Version:

The headless components for Svelte.

41 lines (40 loc) 1.19 kB
import { box } from "svelte-toolbelt"; import { FocusScope } from "./focus-scope.svelte.js"; export class FocusScopeManager { static instance; #scopeStack = box([]); #focusHistory = new WeakMap(); static getInstance() { if (!this.instance) { this.instance = new FocusScopeManager(); } return this.instance; } register(scope) { const current = this.getActive(); if (current && current !== scope) { current.pause(); } this.#scopeStack.current = this.#scopeStack.current.filter((s) => s !== scope); this.#scopeStack.current.unshift(scope); } unregister(scope) { this.#scopeStack.current = this.#scopeStack.current.filter((s) => s !== scope); const next = this.getActive(); if (next) { next.resume(); } } getActive() { return this.#scopeStack.current[0]; } setFocusMemory(scope, element) { this.#focusHistory.set(scope, element); } getFocusMemory(scope) { return this.#focusHistory.get(scope); } isActiveScope(scope) { return this.getActive() === scope; } }