Dropdown

The dropdown component is a UI component built with React that allows you to show a list of items when clicking on a trigger element (ie. a button) that you can use to build dropdown menus, lists, and more.

The default styles are built with the utility classes from Tailwind CSS and you can use the custom props from React to customize the behaviour and positioning of the dropdowns.

To start using the component make sure that you have imported it from Pol-ui:

import { Dropdown } from "pol-ui";

Default Dropdown

Dropdown

import { DropdownItem, Dropdown } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
    <Dropdown label="Dropdown">
      <DropdownItem
        label="Undo"
        onClick={() => console.log("Undo")}
        shortcut="Ctrl+Z"
      />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut" />
      <Dropdown label="Copy as">
        <DropdownItem label="Text" />
        <DropdownItem label="Video" />
        <Dropdown label="Image" nestingIcon="🎞️">
          <DropdownItem label=".png" />
          <DropdownItem label=".jpg" />
          <DropdownItem label=".svg" />
          <DropdownItem label=".gif" />
        </Dropdown>
        <DropdownItem label="Audio" />
      </Dropdown>
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>
    </Dropdown>
  );
};
export default DropdownComponent;

Props

Prop nameTypeDefaultDescription
labelstring-The label of the dropdown
nestedbooleanfalseIf the dropdown is nested
childrenReact.ReactNode-The content of the dropdown
iconReact.FC<React.ComponentProps<'svg'>>-The icon of the dropdown
nestingIconReact.ReactNode-The icon of the nested dropdown
themeDropdownTheme-The theme of the dropdown
disabledbooleanfalseIf the dropdown is disabled
colorColors'primary'The color of the dropdown
sizeMainSizes'md'The size of the dropdown
roundedRoundedSizes'md'The rounded of the dropdown
triggerReact.JSX.Element-The trigger of the dropdown
Prop nameTypeDefaultDescription
labelstring-The label of the item
disabledbooleanfalseIf the item is disabled
customThemeDropdownTheme-The custom theme of the item
shortcutDropdownTheme-The shortcut of the item

Examples

Simple Dropdown

To create a simple dropdown you can use the Dropdown component and pass the items prop with an array of objects with the label and value properties.

Simple dropdown

import { DropdownItem, Dropdown } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
    <Dropdown label="Dropdown">
      <DropdownItem label="Undo" onClick={() => console.log("Undo")} />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut text " shortcut="⌘Z" />
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>
    </Dropdown>
  );
};
export default DropdownComponent;

All colors

All colors

import { DropdownItem, Dropdown, theme } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
    <div className="flex gap-3 flex-wrap">
    {Object.keys(theme.dropdown.root.color).map(c => (
      <Dropdown label={c} color={c as keyof typeof theme.dropdown.root.color} key={c}>
         <DropdownItem
        label="Undo"
        onClick={() => console.log("Undo")}
        shortcut="Ctrl+Z"
      />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut" />
      <Dropdown label="Copy as">
        <DropdownItem label="Text" />
        <DropdownItem label="Video" />
        <Dropdown label="Image" nestingIcon="🎞️">
          <DropdownItem label=".png" />
          <DropdownItem label=".jpg" />
          <DropdownItem label=".svg" />
          <DropdownItem label=".gif" />
        </Dropdown>
        <DropdownItem label="Audio" />
      </Dropdown>
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>
      </Dropdown>
    ))}
  </div>
  );
};
export default DropdownComponent;

All roundness

All roundness

import { DropdownItem, Dropdown,theme } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
    <div className="flex gap-3 flex-wrap items-center">
    {Object.keys(theme.dropdown.size).map(c => (
      <Dropdown label={c} size={c as keyof typeof theme.dropdown.size} key={c}>
      <DropdownItem
        label="Undo"
        onClick={() => console.log("Undo")}
        shortcut="Ctrl+Z"
      />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut" />
      <Dropdown label="Copy as">
        <DropdownItem label="Text" />
        <DropdownItem label="Video" />
        <Dropdown label="Image" nestingIcon="🎞️">
          <DropdownItem label=".png" />
          <DropdownItem label=".jpg" />
          <DropdownItem label=".svg" />
          <DropdownItem label=".gif" />
        </Dropdown>
        <DropdownItem label="Audio" />
      </Dropdown>
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>      </Dropdown>
    ))}
  </div>
  );
};
export default DropdownComponent;

All sizes

All sizes

import { DropdownItem, Dropdown, theme } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
     <div className="flex gap-3 flex-wrap items-center">
    {Object.keys(theme.dropdown.size).map(c => (
      <Dropdown label={c} size={c as keyof typeof theme.dropdown.size} key={c}>
         <DropdownItem
        label="Undo"
        onClick={() => console.log("Undo")}
        shortcut="Ctrl+Z"
      />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut" />
      <Dropdown label="Copy as">
        <DropdownItem label="Text" />
        <DropdownItem label="Video" />
        <Dropdown label="Image" nestingIcon="🎞️">
          <DropdownItem label=".png" />
          <DropdownItem label=".jpg" />
          <DropdownItem label=".svg" />
          <DropdownItem label=".gif" />
        </Dropdown>
        <DropdownItem label="Audio" />
      </Dropdown>
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>
      </Dropdown>
    ))}
  </div>
  );
};
export default DropdownComponent;

Disabled

Disabled

import { DropdownItem, Dropdown } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
    <Dropdown disabled label={`I'm disabled 🥲`}>
      <DropdownItem
        label="Undo"
        onClick={() => console.log("Undo")}
        shortcut="Ctrl+Z"
      />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut" />
      <Dropdown label="Copy as">
        <DropdownItem label="Text" />
        <DropdownItem label="Video" />
        <Dropdown label="Image" nestingIcon="🎞️">
          <DropdownItem label=".png" />
          <DropdownItem label=".jpg" />
          <DropdownItem label=".svg" />
          <DropdownItem label=".gif" />
        </Dropdown>
        <DropdownItem label="Audio" />
      </Dropdown>
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>
    </Dropdown>
  );
};
export default DropdownComponent;

Custom trigger

Custom trigger

import { DropdownItem, Dropdown } from "pol-ui";
import React from "react";
 
const DropdownComponent = () => {
  return (
    <Dropdown
      label="Dropdown with custom trigger"
      trigger={
        <div className="flex items-center gap-2 rounded-full border p-3 hover:bg-secondary-200 transition-all">
          <span>Name</span>
          <Avatar
            size="sm"
            img="https://polgubau.com/_next/image?url=%2Fimages%2Fme.png&w=256&q=75"
            status="online"
          />
        </div>
      }
    >
      <DropdownItem
        label="Undo"
        onClick={() => console.log("Undo")}
        shortcut="Ctrl+Z"
      />
      <DropdownItem label="Redo" disabled />
      <DropdownItem label="Cut" />
      <Dropdown label="Copy as">
        <DropdownItem label="Text" />
        <DropdownItem label="Video" />
        <Dropdown label="Image" nestingIcon="🎞️">
          <DropdownItem label=".png" />
          <DropdownItem label=".jpg" />
          <DropdownItem label=".svg" />
          <DropdownItem label=".gif" />
        </Dropdown>
        <DropdownItem label="Audio" />
      </Dropdown>
      <Dropdown label="Share">
        <DropdownItem label="Mail" />
        <DropdownItem label="Instagram" />
      </Dropdown>
    </Dropdown>
  );
};
export default DropdownComponent;

Theme

To learn more about how to customize the appearance of components, please see the Theme docs.

interface DropdownTheme {
  root: {
    icon: string;
    disabled: string;
    base: string;
    color: ColorsType;
  };
  rounded: RoundedSizesTypes;
  size: MainSizesType;
  item: {
    base: string;
    color: ColorsType;
  };
  floating: {
    base: string;
  };
}

Since February 16, 2024

Proudly Open Source
  • Npm

  • Github

  • Creator

  • Made with love and Pol-ui by Pol Gubau Amores