@openremote/or-mwc-components 1.6.0 → 1.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,7 @@ import listStyle from "@material/list/dist/mdc.list.css";
16
16
  // @ts-ignore
17
17
  import menuSurfaceStyle from "@material/menu-surface/dist/mdc.menu-surface.css";
18
18
  import {getItemTemplate, getListTemplate, ListItem, ListType, MDCListActionEvent} from "./or-mwc-list";
19
+ import { ref } from 'lit/directives/ref.js';
19
20
  // @ts-ignore
20
21
  const menuStyle = require("@material/menu/dist/mdc.menu.css");
21
22
 
@@ -51,20 +52,52 @@ declare global {
51
52
  }
52
53
  }
53
54
 
54
- export function getContentWithMenuTemplate(content: TemplateResult, menuItems: (ListItem | ListItem[] | null)[], selectedValues: string[] | string | undefined, valueChangedCallback: (values: string[] | string) => void, closedCallback?: () => void, multiSelect = false, translateValues = true, midHeight = false, fullWidth = false): TemplateResult {
55
+ export function positionMenuAtElement<T extends OrMwcMenu>(
56
+ menu: T,
57
+ hostElement?: HTMLElement
58
+ ): T {
59
+ if (!hostElement) {
60
+ hostElement = document.body;
61
+ }
62
+ const rect = hostElement.getBoundingClientRect();
63
+
64
+ // Applying a style that is calculated from the runtime coordinates of
65
+ // the host element.
66
+ Object.assign(menu.style, {
67
+ position: 'fixed',
68
+ top: `${rect.bottom}px`,
69
+ left: `${rect.left}px`,
70
+ zIndex: '1000',
71
+ display: 'block'
72
+ });
73
+
74
+ return menu;
75
+ }
76
+
77
+ export function getContentWithMenuTemplate(content: TemplateResult, menuItems: (ListItem | ListItem[] | null)[], selectedValues: string[] | string | undefined, valueChangedCallback: (values: string[] | string) => void, closedCallback?: () => void, multiSelect = false, translateValues = true, midHeight = false, fullWidth = false, menuId = "menu", fixedToHost = false): TemplateResult {
78
+ let menuRef: OrMwcMenu | null = null; // Reference to the menu
55
79
 
56
80
  const openMenu = (evt: Event) => {
57
81
  if (!menuItems) {
58
82
  return;
59
83
  }
60
84
 
85
+ if (fixedToHost && menuRef) {
86
+ const hostElement = evt.currentTarget as HTMLElement;
87
+
88
+ // Using run time coordinates to assign a fixed position to the menu
89
+ positionMenuAtElement(
90
+ menuRef,
91
+ hostElement
92
+ );
93
+ }
61
94
  ((evt.currentTarget as Element).parentElement!.lastElementChild as OrMwcMenu).open();
62
95
  };
63
96
 
64
97
  return html`
65
98
  <span>
66
99
  <span @click="${openMenu}">${content}</span>
67
- ${menuItems ? html`<or-mwc-menu ?multiselect="${multiSelect}" @or-mwc-menu-closed="${() => {if (closedCallback) { closedCallback(); }} }" @or-mwc-menu-changed="${(evt: OrMwcMenuChangedEvent) => {if (valueChangedCallback) { valueChangedCallback(evt.detail); }} }" .translateValues="${translateValues}" .values="${selectedValues}" .menuItems="${menuItems}" .midHeight="${midHeight}" .fullWidth="${fullWidth}" id="menu"></or-mwc-menu>` : ``}
100
+ ${menuItems ? html`<or-mwc-menu ?multiselect="${multiSelect}" @or-mwc-menu-closed="${() => {if (closedCallback) { closedCallback(); }} }" @or-mwc-menu-changed="${(evt: OrMwcMenuChangedEvent) => {if (valueChangedCallback) { valueChangedCallback(evt.detail); }} }" .translateValues="${translateValues}" .values="${selectedValues}" .menuItems="${menuItems}" .midHeight="${midHeight}" .fullWidth="${fullWidth}" id="${menuId}" ${ref(el => (menuRef = el as OrMwcMenu))}></or-mwc-menu>` : ``}
68
101
  </span>
69
102
  `;
70
103
  }