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?

MUI Menu の中身を左右にスクロールして全体を表示する

Posted at

横幅の小さいデバイスでは、メニューの中身の幅が大きいと画面をはみ出してしまいます。
そこで、中身を左右にスクロールすることで全体を表示できるようにします。

1.gif

実装

ポイントは Menu コンポーネントのプロパティの以下の2点です。

  • slotProps / paper / style / overflowX: auto
  • MenuListProps / style / width: max-content
import './App.css';

import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';

const App = () => {
  return (
    <div className="content">
      <Menu
        open
        sx={{
          width: '10em',
        }}
        slotProps={{
          paper: {
            style: {
              overflowX: 'auto',
            },
          },
        }}
        MenuListProps={{
          style: {
            width: 'max-content',
          },
        }}
      >
        <MenuItem>VERY LONG MENU ITEM 1</MenuItem>
        <MenuItem>VERY LONG MENU ITEM 2</MenuItem>
        <MenuItem>VERY LONG MENU ITEM 3</MenuItem>
      </Menu>
    </div>
  );
};

export default App;

Menu コンポーネントの内部実装は、Popover コンポーネントの中に MenuList コンポーネントが配置されたものです。
Popover コンポーネントの中の Paper に overflow 属性を設定し、Paper の子である MenuList の幅をその内容の MenuItem の幅に合わせることで実現します。

環境

  • MUI v6
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?