1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"use client";
type Closer = { id: string; close: () => void };
const stack: Closer[] = [];
const listeners = new Set<() => void>();
export function pushOverlay(closer: Closer) {
stack.push(closer);
listeners.forEach((l) => l());
}
export function removeOverlay(id: string) {
const i = stack.findIndex((c) => c.id === id);
if (i !== -1) stack.splice(i, 1);
listeners.forEach((l) => l());
}
export function handleBack(): boolean {
const top = stack[stack.length - 1];
if (!top) return false;
top.close();
return true;
}
export function subscribeOverlays(fn: () => void): () => void {
listeners.add(fn);
return () => listeners.delete(fn);
}