React Modern Drawer
React Modern Drawer is a lightweight and performant component library for creating highly customizable slide-out drawers in React applications. Currently at version 1.4.0, it offers a simple API to integrate responsive drawers for navigation, settings, or auxiliary content. The project maintains an active development pace, with recent updates focused on type improvements and build process fixes. Key differentiators include its small bundle size, excellent performance across various browsers, and ease of use, making it a strong candidate for projects prioritizing efficiency and developer experience without relying on larger UI frameworks. It provides fine-grained control over drawer direction, size, and overlay behavior.
Common errors
-
Module not found: Can't resolve 'react-modern-drawer/dist/index.css' in '...'
cause The CSS file for the drawer component was not imported or the path is incorrect.fixAdd `import 'react-modern-drawer/dist/index.css';` to your main application file (e.g., `App.tsx` or `index.tsx`). -
TypeError: (0 , react_modern_drawer__WEBPACK_IMPORTED_MODULE_2__.Drawer) is not a function
cause Attempting to import the `Drawer` component as a named export when it is a default export.fixChange the import statement from `import { Drawer } from 'react-modern-drawer';` to `import Drawer from 'react-modern-drawer';`. -
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause React hooks (like `useState` or `useCallback`) are being used outside of a functional React component, or there's a mismatch in React versions (peer dependency issue).fixEnsure all hook calls are within the body of a functional React component and that your installed React version meets the `>=16.0.0` peer dependency requirement.
Warnings
- gotcha Failing to import the component's CSS styles will result in an unstyled or non-functional drawer. The styles are crucial for layout and animation.
- gotcha The `Drawer` component is a default export, meaning `import Drawer from 'react-modern-drawer';` is the correct syntax. Using named import syntax like `{ Drawer }` will cause import errors.
- gotcha While the library ships with TypeScript types, specific type improvements in versions like 1.3.1 might introduce minor breaking changes to type signatures if you are using strict TypeScript settings, requiring small adjustments in your type declarations.
Install
-
npm install react-modern-drawer -
yarn add react-modern-drawer -
pnpm add react-modern-drawer
Imports
- Drawer
import { Drawer } from 'react-modern-drawer';import Drawer from 'react-modern-drawer';
- DrawerProps
import type { DrawerProps } from 'react-modern-drawer'; - CSS Styles
import 'react-modern-drawer/src/index.css';
import 'react-modern-drawer/dist/index.css';
Quickstart
import React, { useState, useCallback } from 'react';
import Drawer from 'react-modern-drawer';
import 'react-modern-drawer/dist/index.css';
const MyDrawerComponent: React.FC = () => {
const [isOpen, setIsOpen] = useState<boolean>(false);
const toggleDrawer = useCallback(() => {
setIsOpen((prevState) => !prevState);
}, []);
return (
<div>
<button onClick={toggleDrawer} style={{ padding: '10px 20px', cursor: 'pointer' }}>
{isOpen ? 'Close' : 'Open'} Drawer
</button>
<Drawer
open={isOpen}
onClose={toggleDrawer}
direction='right'
size={320}
className='custom-drawer-style'
overlayClassName='custom-overlay-style'
>
<div style={{ padding: '20px', backgroundColor: '#f0f0f0', height: '100%' }}>
<h2>Drawer Content</h2>
<p>This is a modern drawer example with custom styling.</p>
<button onClick={toggleDrawer} style={{ marginTop: '15px' }}>
Close from inside
</button>
</div>
</Drawer>
</div>
);
};
export default MyDrawerComponent;