| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx } from "react/jsx-runtime"; |
| 3 |
import { Component } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/OverflowMenu/overflow-menu.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { OverflowMenuContext } from './OverflowMenuContext'; |
| 7 |
import { debounce } from '../../helpers/util'; |
| 8 |
import { globalWidthBreakpoints, globalHeightBreakpoints } from '../../helpers/constants'; |
| 9 |
import { getResizeObserver } from '../../helpers/resizeObserver'; |
| 10 |
class OverflowMenu extends Component { |
| 11 |
constructor(props) { |
| 12 |
super(props); |
| 13 |
this.observer = () => { }; |
| 14 |
this.handleResize = () => { |
| 15 |
const { isVertical } = this.props; |
| 16 |
if (isVertical) { |
| 17 |
this.handleResizeHeight(); |
| 18 |
} |
| 19 |
else { |
| 20 |
this.handleResizeWidth(); |
| 21 |
} |
| 22 |
}; |
| 23 |
this.handleResizeWidth = () => { |
| 24 |
const breakpointWidth = globalWidthBreakpoints[this.props.breakpoint]; |
| 25 |
if (!breakpointWidth) { |
| 26 |
// eslint-disable-next-line no-console |
| 27 |
console.error('OverflowMenu will not be visible without a valid breakpoint.'); |
| 28 |
return; |
| 29 |
} |
| 30 |
const relativeWidth = this.state.breakpointRef ? this.state.breakpointRef.clientWidth : window.innerWidth; |
| 31 |
const isBelowBreakpoint = relativeWidth < breakpointWidth; |
| 32 |
if (this.state.isBelowBreakpoint !== isBelowBreakpoint) { |
| 33 |
this.setState({ isBelowBreakpoint }); |
| 34 |
} |
| 35 |
}; |
| 36 |
this.handleResizeHeight = () => { |
| 37 |
const breakpointHeight = globalHeightBreakpoints[this.props.breakpoint]; |
| 38 |
if (breakpointHeight === 0) { |
| 39 |
// eslint-disable-next-line no-console |
| 40 |
console.warn('The "sm" breakpoint does not apply to vertical overflow menus.'); |
| 41 |
return; |
| 42 |
} |
| 43 |
if (!breakpointHeight) { |
| 44 |
// eslint-disable-next-line no-console |
| 45 |
console.error('OverflowMenu will not be visible without a valid breakpoint.'); |
| 46 |
return; |
| 47 |
} |
| 48 |
const relativeHeight = this.state.breakpointRef ? this.state.breakpointRef.clientHeight : window.innerHeight; |
| 49 |
const isBelowBreakpoint = relativeHeight < breakpointHeight; |
| 50 |
if (this.state.isBelowBreakpoint !== isBelowBreakpoint) { |
| 51 |
this.setState({ isBelowBreakpoint }); |
| 52 |
} |
| 53 |
}; |
| 54 |
this.handleResizeWithDelay = debounce(this.handleResize, 250); |
| 55 |
this.state = { |
| 56 |
isBelowBreakpoint: false, |
| 57 |
breakpointRef: undefined |
| 58 |
}; |
| 59 |
} |
| 60 |
getBreakpointRef() { |
| 61 |
const { breakpointReference } = this.props; |
| 62 |
if (breakpointReference.current) { |
| 63 |
return breakpointReference.current; |
| 64 |
} |
| 65 |
else if (typeof breakpointReference === 'function') { |
| 66 |
return breakpointReference(); |
| 67 |
} |
| 68 |
} |
| 69 |
componentDidMount() { |
| 70 |
const reference = this.props.breakpointReference ? this.getBreakpointRef() : undefined; |
| 71 |
this.setState({ breakpointRef: reference }); |
| 72 |
this.observer = getResizeObserver(reference, this.handleResizeWithDelay); |
| 73 |
this.handleResize(); |
| 74 |
} |
| 75 |
componentDidUpdate(prevProps, prevState) { |
| 76 |
const reference = this.props.breakpointReference ? this.getBreakpointRef() : undefined; |
| 77 |
if (prevState.breakpointRef !== reference) { |
| 78 |
// To remove any previous observer/event listener from componentDidMount before adding a new one |
| 79 |
this.observer(); |
| 80 |
this.setState({ breakpointRef: reference }); |
| 81 |
this.observer = getResizeObserver(reference, this.handleResizeWithDelay); |
| 82 |
this.handleResize(); |
| 83 |
} |
| 84 |
} |
| 85 |
componentWillUnmount() { |
| 86 |
this.observer(); |
| 87 |
} |
| 88 |
render() { |
| 89 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 90 |
const _a = this.props, { className, breakpoint, children, breakpointReference, isVertical } = _a, props = __rest(_a, ["className", "breakpoint", "children", "breakpointReference", "isVertical"]); |
| 91 |
return (_jsx("div", Object.assign({}, props, { className: css(styles.overflowMenu, isVertical && styles.modifiers.vertical, className), children: _jsx(OverflowMenuContext.Provider, { value: { isBelowBreakpoint: this.state.isBelowBreakpoint }, children: children }) }))); |
| 92 |
} |
| 93 |
} |
| 94 |
OverflowMenu.displayName = 'OverflowMenu'; |
| 95 |
OverflowMenu.defaultProps = { |
| 96 |
isVertical: false |
| 97 |
}; |
| 98 |
OverflowMenu.contextType = OverflowMenuContext; |
| 99 |
export { OverflowMenu }; |
| 100 |
//# sourceMappingURL=OverflowMenu.js.map |