Debian Codegraph @

node/@patternfly/react-core/src/components/OverflowMenu/OverflowMenu.tsx

raw
1 import { Component } from 'react';
2 import styles from '@patternfly/react-styles/css/components/OverflowMenu/overflow-menu';
3 import { css } from '@patternfly/react-styles';
4 import { OverflowMenuContext } from './OverflowMenuContext';
5 import { debounce } from '../../helpers/util';
6 import { globalWidthBreakpoints, globalHeightBreakpoints } from '../../helpers/constants';
7 import { getResizeObserver } from '../../helpers/resizeObserver';
8 import { PickOptional } from '../../helpers/typeUtils';
9
10 export interface OverflowMenuProps extends React.HTMLProps<HTMLDivElement> {
11 /** Any elements that can be rendered in the menu */
12 children?: any;
13 /** Additional classes added to the OverflowMenu. */
14 className?: string;
15 /** Indicates breakpoint at which to switch between expanded and collapsed states. The "sm" breakpoint does not apply to vertical overflow menus. */
16 breakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl';
17 /** A container reference to base the specified breakpoint on instead of the viewport width. */
18 breakpointReference?: HTMLElement | (() => HTMLElement) | React.RefObject<any>;
19 /** Indicates the overflow menu orientation is vertical and should respond to height changes instead of width. */
20 isVertical?: boolean;
21 }
22
23 export interface OverflowMenuState extends React.HTMLProps<HTMLDivElement> {
24 isBelowBreakpoint: boolean;
25 breakpointRef: HTMLElement;
26 }
27
28 class OverflowMenu extends Component<OverflowMenuProps, OverflowMenuState> {
29 static displayName = 'OverflowMenu';
30
31 static defaultProps: PickOptional<OverflowMenuProps> = {
32 isVertical: false
33 };
34
35 constructor(props: OverflowMenuProps) {
36 super(props);
37 this.state = {
38 isBelowBreakpoint: false,
39 breakpointRef: undefined
40 };
41 }
42
43 observer: any = () => {};
44
45 getBreakpointRef() {
46 const { breakpointReference } = this.props;
47
48 if ((breakpointReference as React.RefObject<any>).current) {
49 return (breakpointReference as React.RefObject<any>).current;
50 } else if (typeof breakpointReference === 'function') {
51 return breakpointReference();
52 }
53 }
54
55 componentDidMount() {
56 const reference = this.props.breakpointReference ? this.getBreakpointRef() : undefined;
57
58 this.setState({ breakpointRef: reference });
59 this.observer = getResizeObserver(reference, this.handleResizeWithDelay);
60 this.handleResize();
61 }
62
63 componentDidUpdate(prevProps: Readonly<OverflowMenuProps>, prevState: Readonly<OverflowMenuState>): void {
64 const reference = this.props.breakpointReference ? this.getBreakpointRef() : undefined;
65
66 if (prevState.breakpointRef !== reference) {
67 // To remove any previous observer/event listener from componentDidMount before adding a new one
68 this.observer();
69 this.setState({ breakpointRef: reference });
70 this.observer = getResizeObserver(reference, this.handleResizeWithDelay);
71 this.handleResize();
72 }
73 }
74
75 componentWillUnmount() {
76 this.observer();
77 }
78
79 handleResize = () => {
80 const { isVertical } = this.props;
81 if (isVertical) {
82 this.handleResizeHeight();
83 } else {
84 this.handleResizeWidth();
85 }
86 };
87
88 handleResizeWidth = () => {
89 const breakpointWidth = globalWidthBreakpoints[this.props.breakpoint];
90 if (!breakpointWidth) {
91 // eslint-disable-next-line no-console
92 console.error('OverflowMenu will not be visible without a valid breakpoint.');
93 return;
94 }
95
96 const relativeWidth = this.state.breakpointRef ? this.state.breakpointRef.clientWidth : window.innerWidth;
97 const isBelowBreakpoint = relativeWidth < breakpointWidth;
98 if (this.state.isBelowBreakpoint !== isBelowBreakpoint) {
99 this.setState({ isBelowBreakpoint });
100 }
101 };
102
103 handleResizeHeight = () => {
104 const breakpointHeight = globalHeightBreakpoints[this.props.breakpoint];
105 if (breakpointHeight === 0) {
106 // eslint-disable-next-line no-console
107 console.warn('The "sm" breakpoint does not apply to vertical overflow menus.');
108 return;
109 }
110
111 if (!breakpointHeight) {
112 // eslint-disable-next-line no-console
113 console.error('OverflowMenu will not be visible without a valid breakpoint.');
114 return;
115 }
116
117 const relativeHeight = this.state.breakpointRef ? this.state.breakpointRef.clientHeight : window.innerHeight;
118 const isBelowBreakpoint = relativeHeight < breakpointHeight;
119 if (this.state.isBelowBreakpoint !== isBelowBreakpoint) {
120 this.setState({ isBelowBreakpoint });
121 }
122 };
123
124 handleResizeWithDelay = debounce(this.handleResize, 250);
125
126 render() {
127 // eslint-disable-next-line @typescript-eslint/no-unused-vars
128 const { className, breakpoint, children, breakpointReference, isVertical, ...props } = this.props;
129
130 return (
131 <div {...props} className={css(styles.overflowMenu, isVertical && styles.modifiers.vertical, className)}>
132 <OverflowMenuContext.Provider value={{ isBelowBreakpoint: this.state.isBelowBreakpoint }}>
133 {children}
134 </OverflowMenuContext.Provider>
135 </div>
136 );
137 }
138 }
139
140 OverflowMenu.contextType = OverflowMenuContext;
141
142 export { OverflowMenu };