Debian Codegraph @

addons/web/static/lib/hoot/ui/hoot_dropdown.js

raw
1 /** @odoo-module */
2
3 import { Component, useRef, useState, xml } from "@odoo/owl";
4 import { useAutofocus, useHootKey, useWindowListener } from "../hoot_utils";
5
6 /**
7 * @typedef {{
8 * buttonClassName?: string:
9 * className?: string:
10 * slots: Record<string, any>;
11 * }} HootDropdownProps
12 */
13
14 //-----------------------------------------------------------------------------
15 // Exports
16 //-----------------------------------------------------------------------------
17
18 /** @extends {Component<HootDropdownProps, import("../hoot").Environment>} */
19 export class HootDropdown extends Component {
20 static template = xml`
21 <div class="${HootDropdown.name} relative" t-att-class="props.className" t-ref="root">
22 <button
23 t-ref="toggler"
24 class="flex rounded p-2 transition-colors"
25 t-att-class="props.buttonClassName"
26 >
27 <t t-slot="toggler" open="state.open" />
28 </button>
29 <t t-if="state.open">
30 <div
31 class="
32 hoot-dropdown absolute animate-slide-down
33 flex flex-col end-0 p-3 gap-2
34 bg-base text-base mt-1 shadow rounded z-2"
35 >
36 <button class="fixed end-2 top-2 p-1 text-rose sm:hidden" t-on-click="() => state.open = false">
37 <i class="fa fa-times w-5 h-5" />
38 </button>
39 <t t-slot="menu" open="state.open" />
40 </div>
41 </t>
42 </div>
43 `;
44 static props = {
45 buttonClassName: { type: String, optional: true },
46 className: { type: String, optional: true },
47 slots: {
48 type: Object,
49 shape: {
50 toggler: Object,
51 menu: Object,
52 },
53 },
54 };
55
56 setup() {
57 this.rootRef = useRef("root");
58 this.togglerRef = useRef("toggler");
59 this.state = useState({
60 open: false,
61 });
62
63 useAutofocus(this.rootRef);
64 useHootKey(["Escape"], this.close);
65 useWindowListener(
66 "click",
67 (ev) => {
68 const path = ev.composedPath();
69 if (!path.includes(this.rootRef.el)) {
70 this.state.open = false;
71 } else if (path.includes(this.togglerRef.el)) {
72 this.state.open = !this.state.open;
73 }
74 },
75 { capture: true }
76 );
77 }
78
79 /**
80 * @param {KeyboardEvent} ev
81 */
82 close(ev) {
83 if (this.state.open) {
84 ev.preventDefault();
85 this.state.open = false;
86 }
87 }
88 }