Skip to main content
Light Dark System

Dropdown

<sc-dropdown> | ScDropdown
Since 2.0 stable

Dropdowns expose additional content that “drops down” in a panel.

Dropdowns consist of a trigger and a panel. By default, activating the trigger will expose the panel and interacting outside of the panel will close it.

Dropdowns are designed to work well with menus to provide a list of options the user can select from. However, dropdowns can also be used in lower-level applications (e.g. color picker). The API gives you complete control over showing, hiding, and positioning the panel.

Dropdown Dropdown Item 1 Dropdown Item 2 Dropdown Item 3 Checkbox Disabled Prefix Suffix Icon
<sc-dropdown>
  <sc-button slot="trigger" caret>Dropdown</sc-button>
  <sc-menu>
    <sc-menu-item>Dropdown Item 1</sc-menu-item>
    <sc-menu-item>Dropdown Item 2</sc-menu-item>
    <sc-menu-item>Dropdown Item 3</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item type="checkbox" checked>Checkbox</sc-menu-item>
    <sc-menu-item disabled>Disabled</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item>
      Prefix
      <sc-icon slot="prefix" name="gift"></sc-icon>
    </sc-menu-item>
    <sc-menu-item>
      Suffix Icon
      <sc-icon slot="suffix" name="heart"></sc-icon>
    </sc-menu-item>
  </sc-menu>
</sc-dropdown>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDivider from '@styledc-dev/styledc/dist/react/divider';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScIcon from '@styledc-dev/styledc/dist/react/icon';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const App = () => (
  <ScDropdown>
    <ScButton slot="trigger" caret>
      Dropdown
    </ScButton>
    <ScMenu>
      <ScMenuItem>Dropdown Item 1</ScMenuItem>
      <ScMenuItem>Dropdown Item 2</ScMenuItem>
      <ScMenuItem>Dropdown Item 3</ScMenuItem>
      <ScDivider />
      <ScMenuItem type="checkbox" checked>
        Checkbox
      </ScMenuItem>
      <ScMenuItem disabled>Disabled</ScMenuItem>
      <ScDivider />
      <ScMenuItem>
        Prefix
        <ScIcon slot="prefix" name="gift" />
      </ScMenuItem>
      <ScMenuItem>
        Suffix Icon
        <ScIcon slot="suffix" name="heart" />
      </ScMenuItem>
    </ScMenu>
  </ScDropdown>
);

Examples

Getting the Selected Item

When dropdowns are used with menus, you can listen for the sc-select event to determine which menu item was selected. The menu item element will be exposed in event.detail.item. You can set value props to make it easier to identify commands.

<div class="dropdown-selection">
  <sc-dropdown>
    <sc-button slot="trigger" caret>Edit</sc-button>
    <sc-menu>
      <sc-menu-item value="cut">Cut</sc-menu-item>
      <sc-menu-item value="copy">Copy</sc-menu-item>
      <sc-menu-item value="paste">Paste</sc-menu-item>
    </sc-menu>
  </sc-dropdown>
</div>

<script>
  const container = document.querySelector('.dropdown-selection');
  const dropdown = container.querySelector('sc-dropdown');

  dropdown.addEventListener('sc-select', event => {
    const selectedItem = event.detail.item;
    console.log(selectedItem.value);
  });
</script>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const App = () => {
  function handleSelect(event) {
    const selectedItem = event.detail.item;
    console.log(selectedItem.value);
  }

  return (
    <ScDropdown>
      <ScButton slot="trigger" caret>
        Edit
      </ScButton>
      <ScMenu onScSelect={handleSelect}>
        <ScMenuItem value="cut">Cut</ScMenuItem>
        <ScMenuItem value="copy">Copy</ScMenuItem>
        <ScMenuItem value="paste">Paste</ScMenuItem>
      </ScMenu>
    </ScDropdown>
  );
};

Alternatively, you can listen for the click event on individual menu items. Note that, using this approach, disabled menu items will still emit a click event.

<div class="dropdown-selection-alt">
  <sc-dropdown>
    <sc-button slot="trigger" caret>Edit</sc-button>
    <sc-menu>
      <sc-menu-item value="cut">Cut</sc-menu-item>
      <sc-menu-item value="copy">Copy</sc-menu-item>
      <sc-menu-item value="paste">Paste</sc-menu-item>
    </sc-menu>
  </sc-dropdown>
</div>

<script>
  const container = document.querySelector('.dropdown-selection-alt');
  const cut = container.querySelector('sc-menu-item[value="cut"]');
  const copy = container.querySelector('sc-menu-item[value="copy"]');
  const paste = container.querySelector('sc-menu-item[value="paste"]');

  cut.addEventListener('click', () => console.log('cut'));
  copy.addEventListener('click', () => console.log('copy'));
  paste.addEventListener('click', () => console.log('paste'));
</script>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const App = () => {
  function handleCut() {
    console.log('cut');
  }

  function handleCopy() {
    console.log('copy');
  }

  function handlePaste() {
    console.log('paste');
  }

  return (
    <ScDropdown>
      <ScButton slot="trigger" caret>
        Edit
      </ScButton>
      <ScMenu>
        <ScMenuItem onClick={handleCut}>Cut</ScMenuItem>
        <ScMenuItem onClick={handleCopy}>Copy</ScMenuItem>
        <ScMenuItem onClick={handlePaste}>Paste</ScMenuItem>
      </ScMenu>
    </ScDropdown>
  );
};

Placement

The preferred placement of the dropdown can be set with the placement attribute. Note that the actual position may vary to ensure the panel remains in the viewport.

Edit Cut Copy Paste Find Replace
<sc-dropdown placement="top-start">
  <sc-button slot="trigger" caret>Edit</sc-button>
  <sc-menu>
    <sc-menu-item>Cut</sc-menu-item>
    <sc-menu-item>Copy</sc-menu-item>
    <sc-menu-item>Paste</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item>Find</sc-menu-item>
    <sc-menu-item>Replace</sc-menu-item>
  </sc-menu>
</sc-dropdown>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDivider from '@styledc-dev/styledc/dist/react/divider';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const App = () => (
  <ScDropdown placement="top-start">
    <ScButton slot="trigger" caret>
      Edit
    </ScButton>
    <ScMenu>
      <ScMenuItem>Cut</ScMenuItem>
      <ScMenuItem>Copy</ScMenuItem>
      <ScMenuItem>Paste</ScMenuItem>
      <ScDivider />
      <ScMenuItem>Find</ScMenuItem>
      <ScMenuItem>Replace</ScMenuItem>
    </ScMenu>
  </ScDropdown>
);

Distance

The distance from the panel to the trigger can be customized using the distance attribute. This value is specified in pixels.

Edit Cut Copy Paste Find Replace
<sc-dropdown distance="30">
  <sc-button slot="trigger" caret>Edit</sc-button>
  <sc-menu>
    <sc-menu-item>Cut</sc-menu-item>
    <sc-menu-item>Copy</sc-menu-item>
    <sc-menu-item>Paste</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item>Find</sc-menu-item>
    <sc-menu-item>Replace</sc-menu-item>
  </sc-menu>
</sc-dropdown>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDivider from '@styledc-dev/styledc/dist/react/divider';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const App = () => (
  <ScDropdown distance={30}>
    <ScButton slot="trigger" caret>
      Edit
    </ScButton>
    <ScMenu>
      <ScMenuItem>Cut</ScMenuItem>
      <ScMenuItem>Copy</ScMenuItem>
      <ScMenuItem>Paste</ScMenuItem>
      <ScDivider />
      <ScMenuItem>Find</ScMenuItem>
      <ScMenuItem>Replace</ScMenuItem>
    </ScMenu>
  </ScDropdown>
);

Skidding

The offset of the panel along the trigger can be customized using the skidding attribute. This value is specified in pixels.

Edit Cut Copy Paste Find Replace
<sc-dropdown skidding="30">
  <sc-button slot="trigger" caret>Edit</sc-button>
  <sc-menu>
    <sc-menu-item>Cut</sc-menu-item>
    <sc-menu-item>Copy</sc-menu-item>
    <sc-menu-item>Paste</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item>Find</sc-menu-item>
    <sc-menu-item>Replace</sc-menu-item>
  </sc-menu>
</sc-dropdown>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDivider from '@styledc-dev/styledc/dist/react/divider';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const App = () => (
  <ScDropdown skidding={30}>
    <ScButton slot="trigger" caret>
      Edit
    </ScButton>
    <ScMenu>
      <ScMenuItem>Cut</ScMenuItem>
      <ScMenuItem>Copy</ScMenuItem>
      <ScMenuItem>Paste</ScMenuItem>
      <ScDivider />
      <ScMenuItem>Find</ScMenuItem>
      <ScMenuItem>Replace</ScMenuItem>
    </ScMenu>
  </ScDropdown>
);

To create a submenu, nest an <sc-menu slot="submenu"> element in a menu item.

Edit Undo Redo Cut Copy Paste Find Find… Find Next Find Previous Transformations Make uppercase Make lowercase Capitalize
<sc-dropdown>
  <sc-button slot="trigger" caret>Edit</sc-button>

  <sc-menu style="max-width: 200px;">
    <sc-menu-item value="undo">Undo</sc-menu-item>
    <sc-menu-item value="redo">Redo</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item value="cut">Cut</sc-menu-item>
    <sc-menu-item value="copy">Copy</sc-menu-item>
    <sc-menu-item value="paste">Paste</sc-menu-item>
    <sc-divider></sc-divider>
    <sc-menu-item>
      Find
      <sc-menu slot="submenu">
        <sc-menu-item value="find">Find…</sc-menu-item>
        <sc-menu-item value="find-previous">Find Next</sc-menu-item>
        <sc-menu-item value="find-next">Find Previous</sc-menu-item>
      </sc-menu>
    </sc-menu-item>
    <sc-menu-item>
      Transformations
      <sc-menu slot="submenu">
        <sc-menu-item value="uppercase">Make uppercase</sc-menu-item>
        <sc-menu-item value="lowercase">Make lowercase</sc-menu-item>
        <sc-menu-item value="capitalize">Capitalize</sc-menu-item>
      </sc-menu>
    </sc-menu-item>
  </sc-menu>
</sc-dropdown>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDivider from '@styledc-dev/styledc/dist/react/divider';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const css = `
  .dropdown-hoist {
    border: solid 2px var(--sc-panel-border-color);
    padding: var(--sc-spacing-medium);
    overflow: hidden;
  }
`;

const App = () => (
  <>
    <ScDropdown>
      <ScButton slot="trigger" caret>Edit</ScButton>

      <ScMenu style="max-width: 200px;">
        <ScMenuItem value="undo">Undo</ScMenuItem>
        <ScMenuItem value="redo">Redo</ScMenuItem>
        <ScDivider />
        <ScMenuItem value="cut">Cut</ScMenuItem>
        <ScMenuItem value="copy">Copy</ScMenuItem>
        <ScMenuItem value="paste">Paste</ScMenuItem>
        <ScDivider />
        <ScMenuItem>
          Find
          <ScMenu slot="submenu">
            <ScMenuItem value="find">Find…</ScMenuItem>
            <ScMenuItem value="find-previous">Find Next</ScMenuItem>
            <ScMenuItem value="find-next">Find Previous</ScMenuItem>
          </ScMenu>
        </ScMenuItem>
        <ScMenuItem>
          Transformations
          <ScMenu slot="submenu">
            <ScMenuItem value="uppercase">Make uppercase</ScMenuItem>
            <ScMenuItem value="lowercase">Make lowercase</ScMenuItem>
            <ScMenuItem value="capitalize">Capitalize</ScMenuItem>
          </ScMenu>
        </ScMenuItem>
      </ScMenu>
    </ScDropdown>
  </>
);

Hoisting

Dropdown panels will be clipped if they’re inside a container that has overflow: auto|hidden. The hoist attribute forces the panel to use a fixed positioning strategy, allowing it to break out of the container. In this case, the panel will be positioned relative to its containing block, which is usually the viewport unless an ancestor uses a transform, perspective, or filter. Refer to this page for more details.

<div class="dropdown-hoist">
  <sc-dropdown>
    <sc-button slot="trigger" caret>No Hoist</sc-button>
    <sc-menu>
      <sc-menu-item>Item 1</sc-menu-item>
      <sc-menu-item>Item 2</sc-menu-item>
      <sc-menu-item>Item 3</sc-menu-item>
    </sc-menu>
  </sc-dropdown>

  <sc-dropdown hoist>
    <sc-button slot="trigger" caret>Hoist</sc-button>
    <sc-menu>
      <sc-menu-item>Item 1</sc-menu-item>
      <sc-menu-item>Item 2</sc-menu-item>
      <sc-menu-item>Item 3</sc-menu-item>
    </sc-menu>
  </sc-dropdown>
</div>

<style>
  .dropdown-hoist {
    position: relative;
    border: solid 2px var(--sc-panel-border-color);
    padding: var(--sc-spacing-medium);
    overflow: hidden;
  }
</style>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScDivider from '@styledc-dev/styledc/dist/react/divider';
import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';

const css = `
  .dropdown-hoist {
    border: solid 2px var(--sc-panel-border-color);
    padding: var(--sc-spacing-medium);
    overflow: hidden;
  }
`;

const App = () => (
  <>
    <div className="dropdown-hoist">
      <ScDropdown>
        <ScButton slot="trigger" caret>
          No Hoist
        </ScButton>
        <ScMenu>
          <ScMenuItem>Item 1</ScMenuItem>
          <ScMenuItem>Item 2</ScMenuItem>
          <ScMenuItem>Item 3</ScMenuItem>
        </ScMenu>
      </ScDropdown>

      <ScDropdown hoist>
        <ScButton slot="trigger" caret>
          Hoist
        </ScButton>
        <ScMenu>
          <ScMenuItem>Item 1</ScMenuItem>
          <ScMenuItem>Item 2</ScMenuItem>
          <ScMenuItem>Item 3</ScMenuItem>
        </ScMenu>
      </ScDropdown>
    </div>

    <style>{css}</style>
  </>
);

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/dropdown/dropdown.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/dropdown/dropdown.js';

To import this component using a bundler:

import '@styledc-dev/styledc/dist/components/dropdown/dropdown.js';

To import this component as a React component:

import ScDropdown from '@styledc-dev/styledc/dist/react/dropdown';

Slots

Name Description
(default) The dropdown’s main content.
trigger The dropdown’s trigger, usually a <sc-button> element.

Learn more about using slots.

Properties

Name Description Reflects Type Default
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. boolean false
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. 'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'right' | 'right-start' | 'right-end' | 'left' | 'left-start' | 'left-end' 'bottom-start'
disabled Disables the dropdown so the panel will not open. boolean false
stayOpenOnSelect
stay-open-on-select
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. boolean false
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. HTMLElement | undefined -
distance The distance in pixels from which to offset the panel away from its trigger. number 0
skidding The distance in pixels from which to offset the panel along its trigger. number 0
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. boolean false
sync Syncs the popup width or height to that of the trigger element. 'width' | 'height' | 'both' | undefined undefined
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sc-show onScShow Emitted when the dropdown opens. -
sc-after-show onScAfterShow Emitted after the dropdown opens and all animations are complete. -
sc-hide onScHide Emitted when the dropdown closes. -
sc-after-hide onScAfterHide Emitted after the dropdown closes and all animations are complete. -

Learn more about events.

Methods

Name Description Arguments
show() Shows the dropdown panel. -
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. -

Learn more about methods.

Parts

Name Description
base The component’s base wrapper, an <sc-popup> element.
base__popup The popup’s exported popup part. Use this to target the tooltip’s popup container.
trigger The container that wraps the trigger.
panel The panel that gets shown when the dropdown is open.

Learn more about customizing CSS parts.

Animations

Name Description
dropdown.show The animation to use when showing the dropdown.
dropdown.hide The animation to use when hiding the dropdown.

Learn more about customizing animations.

Dependencies

This component automatically imports the following dependencies.

  • <sc-popup>