1
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?

【Chakra V3】Drawerを表示している間のアイコンボタンスタイルを変更する

Last updated at Posted at 2026-01-11

はじめに

Chakra v3でDrawer(サイドバー)を呼び出すアイコンボタンを実装した際に、クリックした後のスタイルを変更する方法を調べるのに時間がかかったため備忘として残します。

問題

サイドバーを表示している間、アイコンボタンの背景色がグレーになってしまうため、サイドバー表示中のスタイルを指定する方法を探しました。
レコーディング 2026-01-11 230927.gif

該当のソースコードは以下です。

修正前
  return (
    <Drawer.Root
      open={open}
      onOpenChange={(e) => setOpen(e.open)}
      placement={"start"}
    >
      <Drawer.Trigger asChild>
        <IconButton
          aria-label="sidebar open icon"
          variant="ghost"
        >
          <RxHamburgerMenu color="white" />
        </IconButton>
      </Drawer.Trigger>
      <Portal>
        <Drawer.Backdrop />
        <Drawer.Positioner>
          <Drawer.Content w="150px" maxW="150px">
            <Drawer.Body>
              <Stack>
                <DrawerActionTrigger asChild>
                  <Button variant="ghost" onClick={() => handleClick("/home")}>
                    TOP
                  </Button>
                </DrawerActionTrigger>

                <DrawerActionTrigger asChild>
                  <Button variant="ghost" onClick={() => handleClick("/users")}>
                    ユーザ一一覧
                  </Button>
                </DrawerActionTrigger>
                <DrawerActionTrigger asChild>
                  <Button
                    variant="ghost"
                    onClick={() => handleClick("/setting")}
                  >
                    設定
                  </Button>
                </DrawerActionTrigger>
              </Stack>
            </Drawer.Body>
          </Drawer.Content>
        </Drawer.Positioner>
      </Portal>
    </Drawer.Root>
  );

解決方法

Drawer.Trigger配下のIconButtonに_expanded={{ bg: "none" }}を設定すればOKです。

修正後
  return (
    <Drawer.Root
      open={open}
      onOpenChange={(e) => setOpen(e.open)}
      placement={"start"}
    >
      <Drawer.Trigger asChild>
        <IconButton
          aria-label="sidebar open icon"
          variant="ghost"
+          _expanded={{ bg: "none" }}
        >
          <RxHamburgerMenu color="white" />
        </IconButton>
      </Drawer.Trigger>
      <Portal>
        <Drawer.Backdrop />
        <Drawer.Positioner>
          <Drawer.Content w="150px" maxW="150px">
            <Drawer.Body>
              <Stack>
                <DrawerActionTrigger asChild>
                  <Button variant="ghost" onClick={() => handleClick("/home")}>
                    TOP
                  </Button>
                </DrawerActionTrigger>

                <DrawerActionTrigger asChild>
                  <Button variant="ghost" onClick={() => handleClick("/users")}>
                    ユーザ一一覧
                  </Button>
                </DrawerActionTrigger>
                <DrawerActionTrigger asChild>
                  <Button
                    variant="ghost"
                    onClick={() => handleClick("/setting")}
                  >
                    設定
                  </Button>
                </DrawerActionTrigger>
              </Stack>
            </Drawer.Body>
          </Drawer.Content>
        </Drawer.Positioner>
      </Portal>
    </Drawer.Root>
  );

サイドバーを表示している間にボタン背景色がグレーにならないようになりました。
レコーディング 2026-01-11 233420.gif

_expandedは何か?

HTMLに出力された<IconButton />コンポーネントを確認すると、aria-expanded="true"という属性が設定されています。

<button aria-expanded="true" ... >...<button>

image.png

Chakraでは様々なスタイルを設定する方法を提供していますが、その中の一つにARIA Attributeという指定方法があります。
これはaria-xxx=trueという属性を持つ要素に対して、_xxx:{{color:"red"}}のようにスタイルを設定できる機能です。

サイドバーを表示するとaria-expanded="true"という属性が自動で付与されるので、その際に適用されるスタイルとして_expanded={{ bg: "none" }}を指定しました。

おわりに

今までずっとtailwindcssばかり使っていましたが、chakraも一度覚えれば生産性がかなり高まりそうです。

参考

JISOUのメンバー募集中!

プログラミングコーチングJISOUでは、新たなメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
興味のある方は、ぜひホームページをのぞいてみてください!
▼▼▼
https://projisou.jp

1
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
1
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?