/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module AccuDraw
 */
import "./MenuButton.scss";
import * as React from "react";
import { ContextMenu, Icon } from "@itwin/core-react";
import { SquareButton } from "./SquareButton.js";
import { SvgMore } from "@itwin/itwinui-icons-react";
/** @public */
export class MenuButton extends React.PureComponent {
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    _menu = null;
    constructor(props) {
        super(props);
        this.state = {
            expanded: false,
        };
    }
    render() {
        const { point, className, style, onSizeKnown, ...buttonProps } = this.props;
        const divStyle = {
            top: point.y,
            left: point.x,
        };
        return (React.createElement("div", { className: "uifw-menu-button", style: divStyle, ref: (e) => this.setDivRef(e) },
            React.createElement(SquareButton, { ...buttonProps, className: className, style: style, onClick: this._handleClick },
                React.createElement(Icon, { iconSpec: React.createElement(SvgMore, null) })),
            React.createElement(ContextMenu, { ref: (el) => {
                    this._menu = el;
                }, selectedIndex: 0, onSelect: this._handleClose, onOutsideClick: this._handleClose, onEsc: this._handleClose, opened: this.state.expanded, autoflip: false }, this.props.children)));
    }
    setDivRef(div) {
        if (!div)
            return;
        const rect = div.getBoundingClientRect();
        this.props.onSizeKnown?.({ width: rect.width, height: rect.height });
    }
    _open = () => {
        this.setState({ expanded: true }, () => {
            if (this._menu)
                this._menu.focus();
        });
    };
    _handleClick = (event) => {
        if (this.state.expanded) {
            event.stopPropagation();
            this.setState({ expanded: false });
        }
        else {
            this._open();
        }
    };
    _handleClose = () => {
        this.setState({ expanded: false });
    };
}
//# sourceMappingURL=MenuButton.js.map