🎉 Init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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'
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user