Skip to main content
Light Dark System

Dropdown

<et2-dropdown> | Et2Dropdown

et2-dropdown is a panel that hangs off a trigger and floats over the page. It is the plumbing behind menus and overflow buttons: it positions the panel, flips it when it would leave the viewport, closes it on Escape or on a click outside, and puts focus back on the trigger afterwards.

It holds two things: whatever you put in the trigger slot, and the panel content in the default slot. What goes in the panel is up to you - a menu, a small form, a bit of help text.

If all you need is “pick one of these values”, use a select instead. A dropdown has no value and submits nothing.

Examples

A trigger and a panel

Clicking the trigger opens the panel; clicking anywhere else closes it.

Options Show completed Show private
<et2-dropdown>
    <et2-button slot="trigger" caret>Options</et2-button>
    <et2-vbox>
        <et2-checkbox>Show completed</et2-checkbox>
        <et2-checkbox>Show private</et2-checkbox>
    </et2-vbox>
</et2-dropdown>

A menu

The common case. An sl-menu of sl-menu-items gives the familiar menu look, and the sl-select event tells you which item was chosen. The dropdown closes itself on a selection unless you set stayOpenOnSelect.

Actions Edit Copy Delete

chosen: nothing

<et2-dropdown id="dropdown-menu-example">
    <et2-button slot="trigger" caret>Actions</et2-button>
    <sl-menu>
        <sl-menu-item value="edit">Edit</sl-menu-item>
        <sl-menu-item value="copy">Copy</sl-menu-item>
        <sl-menu-item value="delete">Delete</sl-menu-item>
    </sl-menu>
</et2-dropdown>
<p>chosen: <span id="dropdown-menu-output">nothing</span></p>
<script>
    const out = document.getElementById("dropdown-menu-output");
    document.getElementById("dropdown-menu-example").addEventListener("sl-select", (e) =>
    {
        out.textContent = e.detail.item.value;
    });
</script>

Where the panel appears

placement is a preference, not a promise - the panel is moved if it would not fit. distance and skidding nudge it away from the trigger and along it.

To the right Above
<et2-dropdown placement="right-start" distance="10">
    <et2-button slot="trigger">To the right</et2-button>
    <et2-description value="placement=right-start"></et2-description>
</et2-dropdown>
<et2-dropdown placement="top" distance="10">
    <et2-button slot="trigger">Above</et2-button>
    <et2-description value="placement=top"></et2-description>
</et2-dropdown>

Opening on hover

toggleOnHover opens the panel when the pointer enters the dropdown and closes it when it leaves. Only use it for panels the user reads rather than uses - moving the mouse towards a control inside can leave the dropdown on the way and close it.

Hover over me
<et2-dropdown toggleOnHover>
    <et2-button slot="trigger">Hover over me</et2-button>
    <et2-description value="No click needed."></et2-description>
</et2-dropdown>

Escaping a scrolling container

A dropdown inside an element with overflow: auto is clipped by it, because the panel is a child of that element like everything else. hoist switches the panel to fixed positioning so it can hang outside.

Open both dropdowns below. The first one’s panel is cut off at the bottom edge of its box; the second one’s is not.

Clipped
Hoisted
<div style="height: 7em; overflow: auto; border: 1px solid var(--sl-color-neutral-300); padding: var(--sl-spacing-small)">
    <et2-dropdown>
        <et2-button slot="trigger">Clipped</et2-button>
        <et2-vbox>
            <et2-description value="First line"></et2-description>
            <et2-description value="Second line"></et2-description>
            <et2-description value="Third line - cut off"></et2-description>
        </et2-vbox>
    </et2-dropdown>
</div>
<div style="height: 7em; overflow: auto; border: 1px solid var(--sl-color-neutral-300); padding: var(--sl-spacing-small)">
    <et2-dropdown hoist>
        <et2-button slot="trigger">Hoisted</et2-button>
        <et2-vbox>
            <et2-description value="First line"></et2-description>
            <et2-description value="Second line"></et2-description>
            <et2-description value="Third line - still visible"></et2-description>
        </et2-vbox>
    </et2-dropdown>
</div>

Opening it from javascript

open reflects the state, and show() / hide() change it. sl-show and sl-hide fire on the dropdown either way.

Trigger

open: false

<et2-dropdown id="dropdown-api-example">
    <et2-button slot="trigger">Trigger</et2-button>
    <et2-description value="Panel"></et2-description>
</et2-dropdown>
<et2-button id="dropdown-api-button" label="Toggle from outside"></et2-button>
<p>open: <span id="dropdown-api-output">false</span></p>
<script>
    const dropdown = document.getElementById("dropdown-api-example");
    const out = document.getElementById("dropdown-api-output");
    const show = () => {out.textContent = dropdown.open;};

    dropdown.addEventListener("sl-show", show);
    dropdown.addEventListener("sl-hide", show);
    document.getElementById("dropdown-api-button").addEventListener("click", () =>
    {
        customElements.whenDefined("et2-dropdown")
            .then(() => dropdown.updateComplete)
            .then(() => dropdown.open ? dropdown.hide() : dropdown.show());
    });
</script>

Properties

Name Description Reflects Type Default
toggleOnHover Open the panel on mouse hover, instead of requiring a click on the trigger boolean false

Learn more about attributes and properties.

Inherited properties (29)

Et2Widget

Name Description
accesskey Accesskey provides a hint for generating a keyboard shortcut for the current element. The attribute value must consist of a single printable character.
actions Set Actions on the widget Each action is defined as an object: move: { type: “drop”, acceptedTypes: “mail”, icon: “move”, caption: “Move to” onExecute: javascript:mail_move” } This will turn the widget into a drop target for “mail” drag types. When “mail” drag types are dropped, the global function mail_move(egwAction action, egwActionObject sender) will be called. The ID of the dragged “mail” will be in sender.id, some information about the sender will be in sender.context. The etemplate2 widget involved can typically be found in action.parent.data.widget, so your handler can operate in the widget context easily. The location varies depending on your action though. It might be action.parent.parent.data.widget To customise how the actions are handled for a particular widget, override _link_actions(). It handles the more widget-specific parts.
align Used by Et2Box to determine alignment. Allowed values are left, right
class CSS Class. This class is applied to the outside, on the web component itself. Due to how WebComponents work, this might not change anything inside the component.
data Set the dataset from a CSV
deferredProperties Any attribute that refers to row content cannot be resolved immediately, but some like booleans cannot stay a string because it’s a boolean attribute. We store them for later, and parse when they’re fully in their row. If you are creating a widget that can go in a nextmatch row, and it has boolean attributes that can change for each row, add those attributes into deferredProperties
disabled Defines whether this widget is visibly disabled. The widget is still visible, but clearly cannot be interacted with. Widgets disabled in the template will not return a value to the application code, even if re-enabled via javascript before submitting. To allow a disabled widget to be re-enabled and return a value, disable via javascript in the app’s et2_ready() instead of an attribute in the template file.
dom_id Get the actual DOM ID, which has been prefixed to make sure it’s unique.
full Take every column of a layout, the same as span="all". Boolean rather than a bare marker so it can be driven from content the way the rest of the template can - full="@is_wide" resolves through the array manager like any other boolean attribute. That does mean it needs a value: write full="true", since an empty full="" resolves to false and removes the attribute again. Reflects for the same reason grow does.
grow How much of a layout’s leftover space this widget should take. Only means anything inside a container that has a layout (see Et2LayoutController): a bare grow or grow="1" takes an equal share of whatever the non-growing children left over, grow="2" takes twice what a grow="1" sibling does. It has to reflect. The layout stylesheet matches the [grow] attribute - CSS cannot see a property - and transformAttributes() only calls setAttribute() for an attribute the element already carries or a property declared as reflecting. Widgets built from a .xet are created with document.createElement() and handed their attributes as an object, so they carry none at that point: without this declaration grow="1" lands as an inert JS property and nothing in the layout ever matches it.
hidden The widget is not visible. As far as the user is concerned, the widget does not exist. Widgets hidden with an attribute in the template may not be created in the DOM, and will not return a value. Widgets can be hidden after creation, and they may return a value if hidden this way.
id Get the ID of the widget
label The label of the widget This is usually displayed in some way. It’s also important for accessability. This is defined in the parent somewhere, and re-defining it causes labels to disappear
noLang Disable any translations for the widget
parentId Parent is different than what is specified in the template / hierarchy. Widget ID of another node to insert this node into instead of the normal location
span How many of a layout’s columns this widget should take. span="all" is every column wherever the widget landed, span="end" / span="*" is from its own column to the end of its line. Only means anything inside a container that has a layout, and only for the column layouts - stack gives every child the full width anyway. The same name is also read straight off the XML node by the legacy <grid> widget, for the unrelated job of counting table cells to merge; that path never looks at the widget, so declaring it here does not disturb it. Reflecting it does mean a legacy grid’s span="all" cells now carry the attribute too, which matches nothing outside a layout. Reflects for the same reason grow does.
statustext Tooltip which is shown for this element on hover
styles WebComponent *
options Get property-values as object
supportedWidgetClasses et2_widget compatability

SlDropdown⁠ (Shoelace)

Name Description
containingElement The dropdown will close when the user interacts outside of this element (e.g. clicking). Useful for composing other components that use a dropdown internally.
distance The distance in pixels from which to offset the panel away from its trigger.
hoist Enable this option to prevent the panel from being clipped when the component is placed inside a container with overflow: auto|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios.
open Indicates whether or not the dropdown is open. You can toggle this attribute to show and hide the dropdown, or you can use the show() and hide() methods and this attribute will reflect the dropdown’s open state.
placement The preferred placement of the dropdown panel. Note that the actual placement may vary as needed to keep the panel inside of the viewport.
skidding The distance in pixels from which to offset the panel along its trigger.
stayOpenOnSelect By default, the dropdown is closed when an item is selected. This attribute will keep it open instead. Useful for dropdowns that allow for multiple interactions.
sync Syncs the popup width or height to that of the trigger element.

LitElement

Name Description
updateComplete A read-only promise that resolves when the component has finished updating.

Methods

Name Description Arguments
_mouseOutEvent() Close the dropdown once the mouse leaves it event:

Learn more about methods.

Inherited methods (24)

Et2Widget

Name Description
checkCreateNamespace() Checks whether a namespace exists for this element in the content array. If yes, an own perspective of the content array is created. If not, the parent content manager is used. Constructor attributes are passed in case a child needs to make decisions
clone() Creates a copy of this widget.
createElementFromNode() Create a et2_widget from an XML node. First the type and attributes are read from the node. Then the readonly & modifications arrays are checked for changes specific to the loaded data. Then the appropriate constructor is called. After the constructor returns, the widget has a chance to further initialize itself from the XML node when the widget’s loadFromXML() method is called with the node.
getArrayMgr() Returns the array manager object for the given part
getArrayMgrs() Returns an associative array containing the top-most array managers.
getChildren() Get child widgets Use .children to get web component children
getInstanceManager() Returns the instance manager
getPath() Returns the path into the data array. By default, array manager takes care of this, but some extensions need to override this
getRoot() Returns the base widget Usually this is the same as getInstanceManager().widgetContainer
loadFromXML() Loads the widget tree from an XML node
loadingFinished() Needed for legacy compatability.
parseXMLAttrs() The parseXMLAttrs function takes an XML DOM attributes object and adds the given attributes to the _target associative array. This function also parses the legacyOptions. N.B. This is only used for legacy widgets. WebComponents use transformAttributes() and do their own handling of attributes.
set_label() NOT the setter, since we cannot add to the DOM before connectedCallback() TODO: This is not best practice. Should just set property, DOM modification should be done in render https://lit-element.polymer-project.org/guide/templates#design-a-performant-template
setArrayMgr() Sets the array manager for the given part
setArrayMgrs() Sets all array manager objects - this function can be used to set the root array managers of the container object.
setInstanceManager() Set the instance manager Normally this is not needed as it’s set on the top-level container, and we just return that reference
_handleClick() Click handler calling custom handler set via onclick attribute to this.onclick
destroy() et2_widget compatability
set_class() Set the widget class
set_disabled() Wrapper on this.disabled because legacy had it.
set_statustext() supports legacy set_statustext

SlDropdown⁠ (Shoelace)

Name Description
hide() Hides the dropdown panel
reposition() Instructs the dropdown menu to reposition. Useful when the position or size of the trigger changes when the menu is activated.
show() Shows the dropdown panel.