1- import { inject , InjectionKey , provide , Ref , unref } from 'vue' ;
1+ import { inject , InjectionKey , provide , ref , Ref , unref } from 'vue' ;
2+ import isBrowser from './isBrowser' ;
23
34type E = Element | Ref < Element | undefined | null > ;
45export const OutsideSymbol : InjectionKey < [ E , E ] > = Symbol ( ) ;
56
67export function useClickOutside ( ) {
7- const tupleOutsideElement = inject ( OutsideSymbol , [ document . body , document . body ] ) ;
8+ const tupleBody = isBrowser ? ( [ document . body , document . body ] as [ E , E ] ) : [ ref ( null ) , ref ( null ) ] ;
9+ const tupleOutsideElement = inject ( OutsideSymbol , tupleBody ) ;
810 return {
911 clickOutside ( e : MouseEvent ) {
1012 const target = unref ( tupleOutsideElement [ 0 ] ) || document . body ;
@@ -30,6 +32,7 @@ export function useClickOutside() {
3032}
3133
3234export function useOutside ( target : Element | Ref < Element | undefined | null > ) {
33- const tupleOutsideElement = inject ( OutsideSymbol , [ document . body , document . body ] ) ;
35+ const tupleBody = isBrowser ? ( [ document . body , document . body ] as [ E , E ] ) : [ ref ( null ) , ref ( null ) ] ;
36+ const tupleOutsideElement = inject ( OutsideSymbol , tupleBody ) ;
3437 provide ( OutsideSymbol , [ tupleOutsideElement [ 1 ] , target ] ) ;
3538}
0 commit comments