-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Submenu.tsx
147 lines (129 loc) · 3.48 KB
/
Submenu.tsx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, { ReactNode, useEffect, useRef, useState } from 'react';
import cx from 'clsx';
import { InternalProps, BooleanPredicate, HandlerParamsEvent } from '../types';
import { RefTrackerProvider, useRefTrackerContext } from './RefTrackerProvider';
import { useRefTracker } from '../hooks';
import { STYLE } from '../constants';
import { cloneItems, getPredicateValue } from './utils';
export interface SubMenuProps
extends InternalProps,
Omit<React.HTMLAttributes<HTMLElement>, 'hidden'> {
/**
* Any valid node that can be rendered
*/
label: ReactNode;
/**
* Any valid node that can be rendered
*/
children: ReactNode;
/**
* Render a custom arrow
*/
arrow?: ReactNode;
/**
* Disable the `Submenu`. If a function is used, a boolean must be returned
*/
disabled?: BooleanPredicate;
/**
* Hide the `Submenu` and his children. If a function is used, a boolean must be returned
*/
hidden?: BooleanPredicate;
}
interface SubMenuState {
left?: string | number;
right?: string | number;
top?: string | number;
bottom?: string | number;
}
export const Submenu: React.FC<SubMenuProps> = ({
arrow = 'â–¶',
children,
disabled = false,
hidden = false,
label,
className,
triggerEvent,
propsFromTrigger,
style,
...rest
}) => {
const menuRefTracker = useRefTrackerContext();
const refTracker = useRefTracker();
const nodeRef = useRef<HTMLDivElement>(null);
const [position, setPosition] = useState<SubMenuState>({
left: '100%',
top: 0,
bottom: 'initial',
});
const handlerParams = {
triggerEvent: triggerEvent as HandlerParamsEvent,
props: propsFromTrigger,
};
const isDisabled = getPredicateValue(disabled, handlerParams);
const isHidden = getPredicateValue(hidden, handlerParams);
useEffect(() => {
if (nodeRef.current) {
const { innerWidth, innerHeight } = window;
const rect = nodeRef.current.getBoundingClientRect();
const style: SubMenuState = {};
if (rect.right < innerWidth) {
style.left = '100%';
style.right = undefined;
} else {
style.right = '100%';
style.left = undefined;
}
if (rect.bottom > innerHeight) {
style.bottom = 0;
style.top = 'initial';
} else {
style.bottom = 'initial';
}
setPosition(style);
}
}, []);
function handleClick(e: React.SyntheticEvent) {
e.stopPropagation();
}
function trackRef(node: HTMLElement | null) {
if (node && !isDisabled)
menuRefTracker.set(node, {
node,
isSubmenu: true,
submenuRefTracker: refTracker,
});
}
if (isHidden) return null;
const cssClasses = cx(STYLE.item, className, {
[`${STYLE.itemDisabled}`]: isDisabled,
});
const submenuStyle = {
...style,
...position,
};
return (
<RefTrackerProvider refTracker={refTracker}>
<div
{...rest}
className={cssClasses}
ref={trackRef}
tabIndex={-1}
role="menuitem"
aria-haspopup
aria-disabled={isDisabled}
>
<div className={STYLE.itemContent} onClick={handleClick}>
{label}
<span className={STYLE.submenuArrow}>{arrow}</span>
</div>
<div className={STYLE.submenu} ref={nodeRef} style={submenuStyle}>
{cloneItems(children, {
propsFromTrigger,
// injected by the parent
triggerEvent: triggerEvent!,
})}
</div>
</div>
</RefTrackerProvider>
);
};