-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
usePopoverSurface.ts
95 lines (86 loc) · 3.38 KB
/
usePopoverSurface.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as React from 'react';
import { getIntrinsicElementProps, useMergedRefs, slot } from '@fluentui/react-utilities';
import { useModalAttributes } from '@fluentui/react-tabster';
import { usePopoverContext_unstable } from '../../popoverContext';
import type { PopoverSurfaceProps, PopoverSurfaceState } from './PopoverSurface.types';
/**
* Create the state required to render PopoverSurface.
*
* The returned state can be modified with hooks such as usePopoverSurfaceStyles_unstable,
* before being passed to renderPopoverSurface_unstable.
*
* @param props - props from this instance of PopoverSurface
* @param ref - reference to root HTMLDivElement of PopoverSurface
*/
export const usePopoverSurface_unstable = (
props: PopoverSurfaceProps,
ref: React.Ref<HTMLDivElement>,
): PopoverSurfaceState => {
const contentRef = usePopoverContext_unstable(context => context.contentRef);
const openOnHover = usePopoverContext_unstable(context => context.openOnHover);
const setOpen = usePopoverContext_unstable(context => context.setOpen);
const mountNode = usePopoverContext_unstable(context => context.mountNode);
const arrowRef = usePopoverContext_unstable(context => context.arrowRef);
const size = usePopoverContext_unstable(context => context.size);
const withArrow = usePopoverContext_unstable(context => context.withArrow);
const appearance = usePopoverContext_unstable(context => context.appearance);
const trapFocus = usePopoverContext_unstable(context => context.trapFocus);
const inertTrapFocus = usePopoverContext_unstable(context => context.inertTrapFocus);
const inline = usePopoverContext_unstable(context => context.inline);
const { modalAttributes } = useModalAttributes({
trapFocus,
legacyTrapFocus: !inertTrapFocus,
alwaysFocusable: !trapFocus,
});
const state: PopoverSurfaceState = {
inline,
appearance,
withArrow,
size,
arrowRef,
mountNode,
components: {
root: 'div',
},
root: slot.always(
getIntrinsicElementProps('div', {
// FIXME:
// `contentRef` is wrongly assigned to be `HTMLElement` instead of `HTMLDivElement`
// but since it would be a breaking change to fix it, we are casting ref to it's proper type
ref: useMergedRefs(ref, contentRef) as React.Ref<HTMLDivElement>,
role: trapFocus ? 'dialog' : 'group',
'aria-modal': trapFocus ? true : undefined,
...modalAttributes,
...props,
}),
{ elementType: 'div' },
),
};
const {
onMouseEnter: onMouseEnterOriginal,
onMouseLeave: onMouseLeaveOriginal,
onKeyDown: onKeyDownOriginal,
} = state.root;
state.root.onMouseEnter = (e: React.MouseEvent<HTMLDivElement>) => {
if (openOnHover) {
setOpen(e, true);
}
onMouseEnterOriginal?.(e);
};
state.root.onMouseLeave = (e: React.MouseEvent<HTMLDivElement>) => {
if (openOnHover) {
setOpen(e, false);
}
onMouseLeaveOriginal?.(e);
};
state.root.onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
// only close if the event happened inside the current popover
// If using a stack of inline popovers, the user should call `stopPropagation` to avoid dismissing the entire stack
if (e.key === 'Escape' && contentRef.current?.contains(e.target as HTMLDivElement)) {
e.preventDefault();
setOpen(e, false);
}
onKeyDownOriginal?.(e);
};
return state;
};