CxJS

Dropdown

import { Dropdown } from 'cx/widgets'; Copied

The Dropdown widget is commonly used in lookup fields, date fields, and menus, but it can also be used independently. It positions itself relative to a trigger element and automatically adjusts placement based on available screen space.

<div>
  <Button
    onClick={(e, { store }) => {
      store.toggle(m.showDropdown)
    }}
  >
    Toggle Dropdown
  </Button>
  <Dropdown
    visible={m.showDropdown}
    arrow
    offset={4}
    placementOrder="down-right up-right"
    class="p-2"
  >
    <Menu>
      <MenuItem>Option 1</MenuItem>
      <MenuItem>Option 2</MenuItem>
      <MenuItem>Option 3</MenuItem>
    </Menu>
  </Dropdown>
</div>

By default, the dropdown positions itself relative to its previous sibling element. Use arrow to show a pointer and offset to control the distance from the trigger.

Use onResolveRelatedElement to position the dropdown relative to a different element than the default previous sibling.

<div className="p-5 border-2 border-dashed border-gray-300 rounded relative">
  <span className="absolute top-1 right-2 text-xs text-gray-400">
    Parent Container
  </span>
  <Button
    onClick={(e, { store }) => {
      store.toggle(m.showDropdown)
    }}
  >
    Toggle Parent Dropdown
  </Button>
  <Dropdown
    visible={m.showDropdown}
    arrow
    offset={10}
    placement="down"
    onResolveRelatedElement={(el) => el.parentElement!}
    style="padding: 20px; max-width: 300px"
  >
    Dropdown positioned relative to the parent container.
  </Dropdown>
</div>
Parent Container

Search Suggestions

Combine Dropdown with TextField using trackFocus and focused bindings to create search suggestion interfaces. Set matchWidth to make the dropdown match the input width.

<div>
  <TextField
    value={m.query}
    focused={m.showSuggestions}
    trackFocus
    icon="search"
    placeholder="Search..."
    inputAttrs={{ autoComplete: "off" }}
  />
  <Dropdown
    visible={m.showSuggestions}
    offset={1}
    placementOrder="down-right up-right"
    style="padding: 20px;"
    matchWidth
  >
    Display search results here.
  </Dropdown>
</div>

Positioning

Positioning is controlled by relatedElement and placement properties. The default relatedElement is the element rendered prior to the dropdown. Use onResolveRelatedElement callback to specify a different anchor element. Placement direction is determined by available screen space and controlled by placement and placementOrder props.

Configuration

PropertyTypeDescription
visibleBooleanPropControls dropdown visibility.
placementstringDefines where the dropdown will be placed: up, down, left, right, or corners like down-right.
placementOrderstringAvailable placement options. The dropdown picks placement to maximize visibility. Default: "up down right left".
offsetnumberDistance from the trigger element. Default: 0.
arrowbooleanShows an arrow pointing to the trigger element.
matchWidthbooleanMatch the dropdown width to the trigger element.
matchMaxWidthbooleanMatch the dropdown max-width to the trigger element.
constrainbooleanConstrain the dropdown within the viewport.
closeOnScrollDistancenumberAuto-close if page scrolls this distance. Default: 50.
relatedElementElementDOM element used as anchor for positioning.
onResolveRelatedElementfunctionCallback to resolve the related element: (beaconEl) => Element.
autoFocusbooleanFocus first focusable element when opened.
focusablebooleanMake the dropdown focusable.
dismissOnFocusOutbooleanClose when focus leaves the dropdown.