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