Lädt...


🔧 Let's create an animated Drawer using React and Tailwind CSS


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Drawer component was popularized by Material UI. It's a sliding panel, often used for navigation and showing additional content. It slides in from the side of the screen, overlaying the main content. This design pattern is convenient for menus, filters, or any elements you want to put in focus without cluttering the main interface.

This is the design we are going to create using React and Tailwind CSS

Drawer design

The Drawer slide in from the left side of the screen based on button click.

Drawer animation

Drawer placement

Drawer is a modal component by its nature. So we have to render it outside HTML content flow and on top of it, same as we do with dialogs.

React offers us Portals as a technical solution for this problem. It allows developers to render component children into a DOM node that exists outside the hierarchy of the parent component.

Now we are going to create a component to append portals to document.body, ensuring that they are rendered on top of main content.

import type {FC, ReactNode} from 'react';
import {useId, useState, useEffect} from 'react';
import {createPortal} from 'react-dom';

export type Props = {
    children: ReactNode;
};

export const Portal: FC<Props> = ({children}) => {
    // assign unique id to easily find portal in DOM
    const id = useId();
    const [containerAttached, setContainerAttached] = useState(false);
    useEffect(() => {
        // check if conatiner attached to the DOM
        if (!containerAttached) {
            const element = document.createElement('div');
            element.id = id;
            document.body.appendChild(element);
            setContainerAttached(true);
        }
        // don't forget to remove container when portal unmounts
        return () => {
            containerAttached && document.getElementById(id)!.remove();
        };
    }, [id, containerAttached]);

    return containerAttached 
        && createPortal(children, document.getElementById(id)!, id);
};

Drawer component API

In terms of features, we should be able to control Drawer's open/close state. And we will also provide properties to set the look and feel of it.

Below is the complete API we will implement for the component.

type Props = {
    // Control Drawer visibility
    isOpen?: boolean;
    // Provide an external class name
    className?: string;
    // Callback to trigger when modal needs to be closed
    onDismiss?: () => void;
};

export const Drawer: FC<Props> = ({
    isOpen,
    children,
    className,
    onDismiss,
}) => {
    // provide user a way to close Drawer
    const handleBackdropClick = useCallback(() => {
        onDismiss?.();
    }, [onDismiss]);
    return (
        <Portal>
            {isOpen && (
                <Fragment>
                    {/* Backdrop */}
                    <div onClick={handleBackdropClick} />
                    {/* Drawer content */}
                    <div>{children}</div>
                </Fragment>
            )}
        </Portal>
    );
};

Animate Drawer

In order to match the provided design, we need to render and animate between these two visual states:

Drawer state Start Animation End
Enter Not visible to the user. Behind the left border. Slides right from behind the left corner Fully visible
Exit Fully visible Slides left behind the left corner Not visible to the user. Behind the left border.

In order to implement this, we have to add enter and exit animations. While the former is easy to implement using just CSS, the latter requires us to use javascript to manipulate the DOM. Tailwind's Headless UI library has a convenient Transition component.

const Drawer: FC<Props> = ({ isOpen, children, className }) => {
    return (
        // `show` controls visibility. 
        // `appear` plays animation when rendered first time
        <Transition show={isOpen} appear={true}>
            <TransitionChild>
                <div
                    className={classNames(
                        // Tailwind classes to stick Drawer to the left side
                        'fixed bottom-0 left-0 h-dvh',
                        // Configure transition between states.
                        'transition duration-300 ease-in',
                        // `data-[closed]:` selector applies appended
                        // class name when Drawer is in the closed state.
                        // `-translate-x-full` moves the element 100% width left
                        'data-[closed]:-translate-x-full',
                        // Helpful for component reusability
                        className,
                    )}
                >
                    {children}
                </div>
            </TransitionChild>
        </Transition>
    );
};

Styling backdrop

Backdrop is an element rendered between main content and modal element. We have to provide following Tailwind CSS classes to it:

fixed: Sets position: fixed;
inset-0: CSS shortcut for top, right, left, bottom: 0;
bg-gray-300/30: Sets background color and opacity;
backdrop-blur-sm: Blurs main content.

Full demo

...

🔧 Let's create an animated Drawer using React and Tailwind CSS


📈 79.13 Punkte
🔧 Programmierung

🔧 How to create a bottom drawer with Tailwind CSS and Alpinejs


📈 47.08 Punkte
🔧 Programmierung

🔧 How to create a card with an animated background using Tailwind CSS


📈 43.31 Punkte
🔧 Programmierung

🔧 Creating an animated text gradient with Tailwind CSS (and vanilla CSS)


📈 41.63 Punkte
🔧 Programmierung

🔧 How to create an animated navigation on scroll with Tailwind CSS and JavaScript


📈 40.55 Punkte
🔧 Programmierung

🔧 How to create animated image cards with Tailwind CSS and Astrojs


📈 40.55 Punkte
🔧 Programmierung

🔧 How to create a card with animated background with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated envelope with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated profile card with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated input field with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated toggle with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated background gradient with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated expanding search bar with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated avatar stack widget with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create a JavaScript-Free animated accordion FAQ section with only Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create animated blog cards with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 How to create an animated loader with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 Learn how to create a cool animated envelope with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 Learn how to create an animated toggle with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 Learn how to create an animated background gradient with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 Learn how to create an animated expanding search bar with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 Learn how to create an animated avatar stack widget with Tailwind CSS


📈 39.24 Punkte
🔧 Programmierung

🔧 Configurare Tailwind CSS: Guida all'Inizializzazione | Setting Up Tailwind CSS: Initialization Guide


📈 36.53 Punkte
🔧 Programmierung

🔧 How to upgrade an Astro JS project from Tailwind CSS v3 to Tailwind CSS v4 ( Alpha )


📈 36.53 Punkte
🔧 Programmierung

🔧 Create reusable button Components with React,Typescript , Tailwind and Tailwind-variants


📈 35.45 Punkte
🔧 Programmierung

🔧 🔥 Create a Responsive Portfolio Website Using React JS & Tailwind CSS


📈 34.57 Punkte
🔧 Programmierung

🔧 🔥 Create a Responsive Law Firm Website Using React JS & Tailwind CSS


📈 34.57 Punkte
🔧 Programmierung

🔧 Navbar Drawer using html css and javascript https://www.instagram.com/webstreet_code/


📈 33.97 Punkte
🔧 Programmierung

🔧 Gradient animated text with Tailwind CSS


📈 33 Punkte
🔧 Programmierung

🔧 Creating a Modern Animated Badge Component with Tailwind CSS


📈 33 Punkte
🔧 Programmierung

🔧 Make an Animated Menu like Stripe with React, Tailwind, and AI


📈 33 Punkte
🔧 Programmierung

🔧 Choosing the Right CSS Approach: Tailwind CSS vs Bootstrap vs Vanilla CSS


📈 32.9 Punkte
🔧 Programmierung

🔧 Tailwind CSS vs. Traditional CSS in a React app: Pros, Cons, and Best Use Cases


📈 32.9 Punkte
🔧 Programmierung

🔧 How to Create an Animated Navbar with HTML & CSS (Using Transform & Transitions)


📈 32.36 Punkte
🔧 Programmierung

🔧 How to Create Scroll Animations with React, Tailwind CSS, and Framer Motion


📈 31.81 Punkte
🔧 Programmierung

matomo