feat(overlays): add managed overlay package
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { Toaster, createToastManager } from "@workspace/ui/components/toast"
|
||||
|
||||
import { useOverlayScope } from "./scope-context"
|
||||
import { globalOverlayScope, type OverlayScope } from "./scope"
|
||||
import type {
|
||||
ToastOptions,
|
||||
ToastPromiseOptions,
|
||||
ToastUpdateOptions,
|
||||
} from "./types"
|
||||
|
||||
const toastChannel = Symbol("overlay.toast")
|
||||
|
||||
function getToastManager(scope: OverlayScope) {
|
||||
return scope.getChannel(toastChannel, createToastManager)
|
||||
}
|
||||
|
||||
export function openToast<Data extends object = Record<string, never>>(
|
||||
options: ToastOptions<Data>
|
||||
) {
|
||||
const { scope = globalOverlayScope, ...toastOptions } = options
|
||||
return getToastManager(scope).add(toastOptions)
|
||||
}
|
||||
|
||||
export function updateToast<Data extends object = Record<string, never>>(
|
||||
id: string,
|
||||
options: ToastUpdateOptions<Data>
|
||||
) {
|
||||
const { scope = globalOverlayScope, ...toastOptions } = options
|
||||
getToastManager(scope).update(id, toastOptions)
|
||||
}
|
||||
|
||||
export function dismissToast({
|
||||
id,
|
||||
scope = globalOverlayScope,
|
||||
}: {
|
||||
id?: string
|
||||
scope?: OverlayScope
|
||||
} = {}) {
|
||||
getToastManager(scope).close(id)
|
||||
}
|
||||
|
||||
export function toastPromise<
|
||||
Value,
|
||||
Data extends object = Record<string, never>,
|
||||
>(promise: Promise<Value>, options: ToastPromiseOptions<Value, Data>) {
|
||||
const { scope = globalOverlayScope, ...toastOptions } = options
|
||||
return getToastManager(scope).promise(promise, toastOptions)
|
||||
}
|
||||
|
||||
export interface ToastHostProps {
|
||||
scope?: OverlayScope
|
||||
}
|
||||
|
||||
export function ToastHost({ scope }: ToastHostProps) {
|
||||
const contextualScope = useOverlayScope()
|
||||
const resolvedScope = scope ?? contextualScope
|
||||
return <Toaster toastManager={getToastManager(resolvedScope)} />
|
||||
}
|
||||
|
||||
export type {
|
||||
ToastOptions,
|
||||
ToastPromiseOptions,
|
||||
ToastUpdateOptions,
|
||||
} from "./types"
|
||||
Reference in New Issue
Block a user