この記事は 2025 年 7 月 18 日に投稿しました。

目次
リンク
1. はじめに
こんにちは、iOS のエディタアプリPWEditorとその後継PWEditor2の開発者の二俣です。
今回はReactのMaterial UIでPopoverを縦方向中央、横方向左に表示する方法についてです。
2. ReactのMaterial UIでPopoverを縦方向中央、横方向左に表示する
ReactのMaterail UIでPopoverを縦方向中央、横方向左に表示するには、anchorOrigniプロパティにvertical: "center", horizontal: "left"を指定します。
実装例
※create-react-appで作成したTypeScriptのプロジェクトです。
index.tsx
import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; const root = ReactDOM.createRoot( document.getElementById("root") as HTMLElement ); root.render( <React.StrictMode> <App /> </React.StrictMode> );
App.tsx
import * as React from "react"; import Box from "@mui/material/Box"; import Popover from "@mui/material/Popover"; import Typography from "@mui/material/Typography"; import Button from "@mui/material/Button"; export default function BasicPopover() { const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>( null ); const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; const open = Boolean(anchorEl); const id = open ? "simple-popover" : undefined; return ( <Box sx={{ margin: 4 }}> <Button aria-describedby={id} variant="contained" onClick={handleClick}> Open Popover </Button> <Popover id={id} open={open} anchorEl={anchorEl} onClose={handleClose} anchorOrigin={{ vertical: "center", horizontal: "left", }} > <Typography sx={{ p: 2 }}>The content of the Popover.</Typography> </Popover> </Box> ); }
実行結果
オープン前

オープン後

リファレンス
3. おわりに
前回
と関連の内容です。
リンク
紹介している一部の記事のコードはGitlabで公開しています。
興味のある方は覗いてみてください。
私が勤務しているニューラルでは、主に組み込み系ソフトの開発を行っております。
弊社製品のハイブリッド OS Bi-OSは高い技術力を評価されており、特に制御系や通信系を得意としています。
私自身はiOS モバイルアプリやウィンドウズアプリを得意としております。
ソフトウェア開発に関して相談などございましたら、お気軽にご連絡ください。
また一緒に働きたい技術者の方も随時募集中です。
興味がありましたらご連絡ください。
EMAIL : info-nr@newral.co.jp / m-futamata@newral.co.jp
TEL : 042-523-3663
FAX : 042-540-1688

