39 lines
862 B
TypeScript
39 lines
862 B
TypeScript
import { Popup } from '~/utils/popup'
|
|
|
|
export function usePopup() {
|
|
const popup = useState<Popup<any> | undefined>('popup', () => undefined)
|
|
|
|
function get<P>(): Popup<P> | undefined {
|
|
return popup.value as Popup<P> | undefined
|
|
}
|
|
|
|
function require<P>(): Popup<P> {
|
|
if (popup.value) {
|
|
return popup.value as Popup<P>
|
|
}
|
|
throw new Error('Expected to be in open popup state.')
|
|
}
|
|
|
|
function open<P>(next: Popup<P>) {
|
|
popup.value = next
|
|
setBodyScrolling(false)
|
|
}
|
|
|
|
function callback(payload?: unknown) {
|
|
popup.value?.config?.callback?.(payload)
|
|
}
|
|
|
|
function close() {
|
|
popup.value = undefined
|
|
setBodyScrolling(true)
|
|
}
|
|
|
|
return { get, require, open, close, callback }
|
|
}
|
|
|
|
function setBodyScrolling(enabled: boolean) {
|
|
if (import.meta.client) {
|
|
document.body.style.overflow = enabled ? '' : 'hidden'
|
|
}
|
|
}
|