/*---------------------------------------------------------------------------------------------
 * 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, Size } from "@itwin/core-react";
import { SquareButton } from "./SquareButton";
import { SvgMore } from "@itwin/itwinui-icons-react";
/** @alpha */
export class MenuButton extends React.PureComponent {
    constructor(props) {
        super(props);
        this._menu = null;
        this._open = () => {
            this.setState({ expanded: true }, () => {
                // istanbul ignore else
                if (this._menu)
                    this._menu.focus();
            });
        };
        this._handleClick = (event) => {
            if (this.state.expanded) {
                event.stopPropagation();
                this.setState({ expanded: false });
            }
            else {
                this._open();
            }
        };
        // istanbul ignore next
        this._handleClose = () => {
            this.setState({ expanded: false });
        };
        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) {
        // istanbul ignore else
        if (div) {
            const rect = div.getBoundingClientRect();
            const size = new Size(rect.width, rect.height);
            // istanbul ignore else
            if (this.props.onSizeKnown)
                this.props.onSizeKnown(size);
        }
    }
}
//# sourceMappingURL=MenuButton.js.map