0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React】ドロワーを作る

Last updated at Posted at 2025-04-05

今回はReactmaterial-uiを使ってドロワーを作成します。

インストール

以下コマンドを使って、パッケージをインストールします。

npm install @mui/material @emotion/react @emotion/styled

※上記はnpmを使用している場合で、他環境の場合は以下を参照してください。

コード

公式サイトのサンプルは以下ページです。

それをそのまま貼ってもおもしろくないので、テンプレートとして使用できる様にアレンジしたのが以下のコードです。
styletailwindcssを使用しており、xボタンのアイコンにheroiconsを使用しています。

TemplateDrawer.tsx
import React from "react";
import { Drawer } from "@mui/material";
import { XMarkIcon } from "@heroicons/react/24/outline";

/** ドロワーテンプレート */
export const TemplateDrawer = (props: {
    isOpen: boolean;
    setIsOpen: (open: boolean) => void;
    children?: JSX.Element | JSX.Element[];
    anchor?: "left" | "top" | "right" | "bottom";
}) => {
    const { isOpen, setIsOpen, children, anchor = "left" } = props;
    return (
        <Drawer
            open={isOpen}
            onClose={() => setIsOpen(false)}
            anchor={anchor}
        >
            <div className={`min-w-[300px]`}>
                <div className="ml-auto mr-3 mt-3 w-fit">
                    <XMarkIcon className="size-8 hover:bg-gray-200" onClick={() => setIsOpen(false)} />
                </div>
                {children}
            </div>
        </Drawer>
    )
}

今回作成したサンプル

以下ページにサンプルを作成しました

ドロワー内にドロワーが表示できる様に作ったので、以下 gif の様に、無限にドロワーを展開できる様になっています。

プレゼンテーション1.gif

コードは以下のところに入れてあります

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?