/** @odoo-module */ import { Component, useRef, useState, xml } from "@odoo/owl"; import { useAutofocus, useWindowListener } from "../hoot_utils"; /** * @typedef {{ * buttonClassName?: string: * className?: string: * slots: Record; * }} HootDropdownProps */ //----------------------------------------------------------------------------- // Exports //----------------------------------------------------------------------------- /** @extends {Component} */ export class HootDropdown extends Component { static template = xml`
`; static props = { buttonClassName: { type: String, optional: true }, className: { type: String, optional: true }, slots: { type: Object, shape: { toggler: Object, menu: Object, }, }, }; setup() { this.rootRef = useRef("root"); this.togglerRef = useRef("toggler"); this.state = useState({ open: false, }); useAutofocus(this.rootRef); useWindowListener("keydown", (ev) => { if (this.state.open && ev.key === "Escape") { ev.preventDefault(); this.state.open = false; } }); useWindowListener( "click", (ev) => { const path = ev.composedPath(); if (!path.includes(this.rootRef.el)) { this.state.open = false; } else if (path.includes(this.togglerRef.el)) { this.state.open = !this.state.open; } }, { capture: true } ); } }