-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathOverflowMenu.tsx
More file actions
136 lines (113 loc) · 4.73 KB
/
OverflowMenu.tsx
File metadata and controls
136 lines (113 loc) · 4.73 KB
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
import { Component } from 'react';
import styles from '@patternfly/react-styles/css/components/OverflowMenu/overflow-menu';
import { css } from '@patternfly/react-styles';
import { OverflowMenuContext } from './OverflowMenuContext';
import { debounce } from '../../helpers/util';
import { globalWidthBreakpoints, globalHeightBreakpoints } from '../../helpers/constants';
import { getResizeObserver } from '../../helpers/resizeObserver';
import { PickOptional } from 'src/helpers';
export interface OverflowMenuProps extends React.HTMLProps<HTMLDivElement> {
/** Any elements that can be rendered in the menu */
children?: any;
/** Additional classes added to the OverflowMenu. */
className?: string;
/** Indicates breakpoint at which to switch between expanded and collapsed states */
breakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl';
/** A container reference to base the specified breakpoint on instead of the viewport width. */
breakpointReference?: HTMLElement | (() => HTMLElement) | React.RefObject<any>;
/** Indicates the overflow menu orientation is vertical and should respond to height changes instead of width. */
isVertical?: boolean;
}
export interface OverflowMenuState extends React.HTMLProps<HTMLDivElement> {
isBelowBreakpoint: boolean;
breakpointRef: HTMLElement;
}
class OverflowMenu extends Component<OverflowMenuProps, OverflowMenuState> {
static displayName = 'OverflowMenu';
static defaultProps: PickOptional<OverflowMenuProps> = {
isVertical: false
};
constructor(props: OverflowMenuProps) {
super(props);
this.state = {
isBelowBreakpoint: false,
breakpointRef: undefined
};
}
observer: any = () => {};
getBreakpointRef() {
const { breakpointReference } = this.props;
if ((breakpointReference as React.RefObject<any>).current) {
return (breakpointReference as React.RefObject<any>).current;
} else if (typeof breakpointReference === 'function') {
return breakpointReference();
}
}
componentDidMount() {
const reference = this.props.breakpointReference ? this.getBreakpointRef() : undefined;
this.setState({ breakpointRef: reference });
this.observer = getResizeObserver(reference, this.handleResizeWithDelay);
this.handleResize();
}
componentDidUpdate(prevProps: Readonly<OverflowMenuProps>, prevState: Readonly<OverflowMenuState>): void {
const reference = this.props.breakpointReference ? this.getBreakpointRef() : undefined;
if (prevState.breakpointRef !== reference) {
// To remove any previous observer/event listener from componentDidMount before adding a new one
this.observer();
this.setState({ breakpointRef: reference });
this.observer = getResizeObserver(reference, this.handleResizeWithDelay);
this.handleResize();
}
}
componentWillUnmount() {
this.observer();
}
handleResize = () => {
const { isVertical } = this.props;
if (isVertical) {
this.handleResizeHeight();
} else {
this.handleResizeWidth();
}
};
handleResizeWidth = () => {
const breakpointWidth = globalWidthBreakpoints[this.props.breakpoint];
if (!breakpointWidth) {
// eslint-disable-next-line no-console
console.error('OverflowMenu will not be visible without a valid breakpoint.');
return;
}
const relativeWidth = this.state.breakpointRef ? this.state.breakpointRef.clientWidth : window.innerWidth;
const isBelowBreakpoint = relativeWidth < breakpointWidth;
if (this.state.isBelowBreakpoint !== isBelowBreakpoint) {
this.setState({ isBelowBreakpoint });
}
};
handleResizeHeight = () => {
const breakpointHeight = globalHeightBreakpoints[this.props.breakpoint];
if (!breakpointHeight) {
// eslint-disable-next-line no-console
console.error('OverflowMenu will not be visible without a valid breakpoint.');
return;
}
const relativeHeight = this.state.breakpointRef ? this.state.breakpointRef.clientHeight : window.innerHeight;
const isBelowBreakpoint = relativeHeight < breakpointHeight;
if (this.state.isBelowBreakpoint !== isBelowBreakpoint) {
this.setState({ isBelowBreakpoint });
}
};
handleResizeWithDelay = debounce(this.handleResize, 250);
render() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { className, breakpoint, children, breakpointReference, isVertical, ...props } = this.props;
return (
<div {...props} className={css(styles.overflowMenu, isVertical && styles.modifiers.vertical, className)}>
<OverflowMenuContext.Provider value={{ isBelowBreakpoint: this.state.isBelowBreakpoint }}>
{children}
</OverflowMenuContext.Provider>
</div>
);
}
}
OverflowMenu.contextType = OverflowMenuContext;
export { OverflowMenu };