rc-dropdown UI Component

4.2.1 · active · verified Sun Apr 19

rc-dropdown is a fundamental, unstyled UI component for React applications, providing the core logic and accessibility features for dropdown menus, tooltips, and contextual overlays. It is currently at stable version 4.2.1 and is part of the extensive react-component library ecosystem, known for building highly customizable and performant UI primitives. While it offers essential dropdown functionalities like visibility control, placement, and event handling, it intentionally leaves styling and complex menu rendering to the consumer or higher-level UI libraries like Ant Design, which heavily utilize rc-dropdown internally. The project maintains an active development pace with regular updates, including refactors and minor feature enhancements, but major version bumps can introduce significant internal changes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates a basic controlled dropdown component with a simple HTML overlay and a button trigger, showcasing `rc-dropdown`'s core usage including visibility management, placement, and basic animation setup.

import React, { useState } from 'react';
import Dropdown from 'rc-dropdown';
import type { DropdownProps } from 'rc-dropdown';
import 'rc-dropdown/assets/index.css'; // Recommended for basic styling structure

const menuOverlay = (
  <div style={{
    padding: '8px',
    background: '#fff',
    border: '1px solid #ddd',
    borderRadius: '4px',
    boxShadow: '0 2px 8px rgba(0,0,0,0.15)'
  }}>
    <p>Dropdown Item 1</p>
    <p>Dropdown Item 2</p>
    <p>Dropdown Item 3</p>
  </div>
);

const ControlledDropdown: React.FC = () => {
  const [visible, setVisible] = useState(false);

  const handleVisibleChange: DropdownProps['onVisibleChange'] = (newVisible) => {
    console.log('Dropdown visibility changed:', newVisible);
    setVisible(newVisible);
  };

  return (
    <div style={{ margin: '50px', display: 'inline-block' }}>
      <Dropdown
        overlay={menuOverlay}
        animation="slide-up"
        placement="bottomLeft"
        onVisibleChange={handleVisibleChange}
        visible={visible}
        trigger={['click']} // Also supports 'hover', 'contextMenu'
        overlayClassName="my-custom-dropdown-overlay"
      >
        <button type="button" style={{ padding: '10px 20px', cursor: 'pointer' }}>
          {visible ? 'Hide Dropdown' : 'Show Dropdown'}
        </button>
      </Dropdown>
    </div>
  );
};

export default ControlledDropdown;

view raw JSON →