Skip to content

Commit 045688d

Browse files
committed
fix: ssr support
1 parent e1e48c0 commit 045688d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/utils/outside.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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

34
type E = Element | Ref<Element | undefined | null>;
45
export const OutsideSymbol: InjectionKey<[E, E]> = Symbol();
56

67
export 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

3234
export 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

Comments
 (0)