Sadrazam – Hovermenu Showcase

Dropdown menus with click and hover triggers, positioned automatically

Overview

The Hovermenu component creates dynamically-positioned dropdown menus attached to a trigger element. It supports click (default) and mouseenter triggers, optional titles, optional backdrop (backdrop), and open/close lifecycle callbacks. Access it via the global: Sadrazam.Hovermenu

// Configuration options (from Hovermenu.help()) selector // Target element (Element or CSS selector). Required. trigger // Event type: 'click' (default) or 'mouseenter' backdrop // Show backdrop overlay. Default: false title // Menu title HTML. Default: null content // Function returning HTML string or Element. Required. openFunc // Callback before opening. Optional. closeFunc // Callback before closing. Optional.

1. Basic Click Dropdown

Default trigger is click. Click the button to open, click again to close. The content function returns an HTML string for the menu body.

Click Trigger
trigger: 'click'
new Sadrazam.Hovermenu({ selector: '#basic-click', trigger: 'click', content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-pencil-simple"></i> Edit</li> <li><i class="ph-light ph-copy"></i> Duplicate</li> <li><i class="ph-light ph-archive"></i> Archive</li> </ul>` });

2. Hover Trigger

With trigger: 'mouseenter', the menu opens on hover and stays open while the cursor remains over the trigger or menu (hover protection). Clicking the trigger also closes it.

Hover Trigger
trigger: 'mouseenter'
new Sadrazam.Hovermenu({ selector: '#hover-trigger', trigger: 'mouseenter', content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-eye"></i> Preview</li> <li><i class="ph-light ph-download-simple"></i> Download</li> <li><i class="ph-light ph-share-network"></i> Share</li> </ul>` });

3. Menu With Title

Set the title option to render a header above the content area. The title is inserted as raw HTML into the menu container.

With Title
title: '<div class="hovermenu-title">...</div>'
new Sadrazam.Hovermenu({ selector: '#with-title', title: '<div class="hovermenu-title">Sort Options</div>', content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-clock"></i> Newest First</li> <li><i class="ph-light ph-clock-counter-clockwise"></i> Oldest First</li> <li><i class="ph-light ph-text-aa"></i> Alphabetical</li> <li><i class="ph-light ph-star"></i> Most Popular</li> </ul>` });

4. Backdrop (Backdrop Overlay)

Setting backdrop: true renders a semi-transparent overlay behind the menu. Clicking the overlay closes the menu. Uses the Backdrop module internally.

With Backdrop
backdrop: true
new Sadrazam.Hovermenu({ selector: '#with-backdrop', backdrop: true, content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-gear"></i> Settings</li> <li><i class="ph-light ph-question"></i> Help & Support</li> <hr class="menu-divider"> <li class="danger"><i class="ph-light ph-sign-out"></i> Sign Out</li> </ul>` });

5. Open / Close Callbacks

Use openFunc and closeFunc to run logic before the menu opens or closes. Check the browser console for log output.

Callbacks
openFunc / closeFunc
new Sadrazam.Hovermenu({ selector: '#with-callbacks', openFunc: () => console.log('Menu opened!'), closeFunc: () => console.log('Menu closed!'), content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-info"></i> Open your console</li> <li><i class="ph-light ph-bug"></i> Check log output</li> </ul>` });

6. Selector as DOM Element

The selector option accepts both a CSS selector string and a direct Element reference. This is useful when you already have a DOM reference.

DOM Element
selector: document.querySelector(...)
const el = document.querySelector('#element-ref'); new Sadrazam.Hovermenu({ selector: el, content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-check"></i> Passed as Element</li> <li><i class="ph-light ph-check"></i> Not a string selector</li> </ul>` });

7. Content From DOM Element

The content function can return a DOM Element instead of a string. Child nodes are moved into the menu while open and restored when closed. The callback receives the trigger element and the Hovermenu instance as arguments.

DOM Content
content: () => element
// Hidden source element in the HTML // <div id="dom-content-source" style="display:none">...</div> const source = document.querySelector('#dom-content-source'); new Sadrazam.Hovermenu({ selector: '#dom-content', content: (triggerEl, instance) => source });

8. Different Trigger Elements

Any element can be a trigger. Below are examples with an avatar button, a badge, and an icon-only button. The menu auto-positions below the trigger (or above if there is not enough space below).

Avatar
User profile menu
Badge
Notification dropdown
Icon Button
Compact kebab menu
// Avatar trigger new Sadrazam.Hovermenu({ selector: '#avatar-trigger', title: '<div class="hovermenu-title">Account</div>', content: () => `...profile menu HTML...` }); // Badge trigger new Sadrazam.Hovermenu({ selector: '#badge-trigger', title: '<div class="hovermenu-title">Notifications</div>', content: () => `...notification list HTML...` }); // Icon button trigger new Sadrazam.Hovermenu({ selector: '#icon-trigger', content: () => `...kebab menu HTML...` });

9. Programmatic Close & Destroy

Use Hovermenu.remove(element) to programmatically close a hovermenu. Use Hovermenu.destroy(element) or instance.destroy() for full teardown: closes the menu, removes the trigger listener, and clears the instance reference. Pass any element inside the trigger or the trigger itself. It finds the nearest [data-hovermenu-id] ancestor. Use Hovermenu.getInstance(element) to retrieve the instance from any child element.

Auto-Close
Hovermenu.remove(el)
new Sadrazam.Hovermenu({ selector: '#auto-close', openFunc: () => { setTimeout(() => { const el = document.querySelector('#auto-close'); Sadrazam.Hovermenu.remove(el); }, 2000); }, content: () => `<p style="padding:4px">This will close in 2 seconds...</p>` });

10. Combined: Title + Backdrop + Hover + Callbacks

All configuration options used together. Hover to open, with a title bar, backdrop overlay, and lifecycle callbacks logging to the console.

All Options
All config options active
new Sadrazam.Hovermenu({ selector: '#combined', trigger: 'mouseenter', backdrop: true, title: '<div class="hovermenu-title">Quick Actions</div>', openFunc: () => console.log('Combined: opened'), closeFunc: () => console.log('Combined: closed'), content: () => ` <ul class="menu-list"> <li><i class="ph-light ph-plus"></i> New Item</li> <li><i class="ph-light ph-upload-simple"></i> Import</li> <li><i class="ph-light ph-export"></i> Export</li> <hr class="menu-divider"> <li class="danger"><i class="ph-light ph-trash"></i> Delete All</li> </ul>` });

CSS Structure Reference

The component generates the following DOM structure. Styles are defined in hovermenu.scss under the .hovermenu namespace.

<!-- Generated DOM structure --> <div class="hovermenu" id="hovermenu-{timestamp}"> <div class="hovermenu-arrow-up"></div> <!-- or hovermenu-arrow-down --> <div>...title HTML...</div> <!-- only if title option is set --> <div class="hovermenu-content"> ...content... <!-- from content() function --> </div> </div> /* Key CSS classes */ .hovermenu /* Main container: absolute, min-width 200px, bordered */ .hovermenu-arrow-up /* Arrow pointing up (menu below trigger) */ .hovermenu-arrow-down /* Arrow pointing down (menu above trigger) */ .hovermenu-title /* Title bar with background */ .hovermenu-content /* Content area with padding */