実現したいこと
ReactでWebアプリ(勉強/個人用)を作成しています。
ボタンを押したらサイドバーが開閉するものを作成したいです。
(ChatGPTのサイドバーのようなもの)
発生している問題・分からないこと
サイドバー開閉用のボタンの色が変更出きません。
IconButtonを作成して、Iconを指定するようにしています。
アドバイスいただけないでしょうか。
該当のソースコード
JavaScript
1import { useState } from "react"; 2 3// icon 4import { LuListTodo } from "react-icons/lu"; 5import { MdKeyboardDoubleArrowLeft } from "react-icons/md"; 6import { MdKeyboardDoubleArrowRight } from "react-icons/md"; 7import IconButton from "./components/ui/IconButton" 8 9export default function AppLayout({ children }) { 10 const [isVisible, setIsVisible] = useState(true); 11 12 const onClickSwitchVisible = () => { 13 setIsVisible(!isVisible); 14 }; 15 16 return ( 17 <div className="flex h-screen"> 18 {/* aside: 開閉可能 */} 19 {isVisible && ( 20 <aside className="w-64 border-r border-gray-300 hidden md:block lg:w-64 md:w-48 sm:w-40" style={{ padding: '1rem', position: 'relative' }}> 21 <div className="flex flex-col items-center justify-center relative"> 22 {/* 閉じるボタン */} 23 <div className="self-end mb-2"> 24 <IconButton 25 icon={() => <MdKeyboardDoubleArrowLeft className="text-2xl text-black" />} 26 onClick={onClickSwitchVisible} 27 className="p-2 bg-white text-black rounded-full" 28 /> 29 </div> 30 <GrowNav /> 31 </div> 32 33 {/* page nav : page毎のnavigation */} 34 </aside> 35 )} 36 {/* asideが閉じているとき、開くボタンを表示 */} 37 {!isVisible && ( 38 <div className="w-15 hidden md:flex items-start justify-center border-r border-gray-300" style={{ padding: '1rem 0' }}> 39 <IconButton 40 icon={() => <MdKeyboardDoubleArrowRight className="text-2xl" style={{ color: 'black' }} />} 41 onClick={onClickSwitchVisible} 42 className="flex items-center justify-center p-2 rounded-full aspect-square w-10 h-10 bg-white text-black" 43 /> 44 </div> 45 )} 46 <main className="flex-1 p-4 overflow-auto"> 47 {children} 48 </main> 49 </div> 50 ) 51} 52 53// 共通 54function GrowNav() { 55 return ( 56 <div className="border-b border-gray-300" style={{ paddingBottom: '1rem', marginBottom: '1rem' }}> 57 <h1 className="text-lg font-semibold" style={{ marginBottom: '2rem' }}> grow nav </h1> 58 <nav className="flex gap-2"> 59 <IconButton to="/todo" icon={() => <LuListTodo className="text-2xl" />} /> 60 <IconButton to="/todo" icon={() => <LuListTodo className="text-2xl" />} /> 61 </nav> 62 </div> 63 ) 64}
JaveScript
1import { Link } from "react-router-dom"; 2 3export default function IconButton({ 4 icon: Icon, label = "", to = "", onClick = null, className = "", type = "button" 5}) { 6 const content = ( 7 <> 8 {Icon && <Icon />} 9 {label && <span>{label}</span>} 10 </> 11 ); 12 13 if (to) { 14 return ( 15 <Link to={to} className={`flex items-center justify-center ${className}`}> 16 {content} 17 </Link> 18 ); 19 } 20 21 return ( 22 <button 23 onClick={onClick} 24 type={type} 25 className={`flex items-center justify-center p-2 rounded-full aspect-square w-10 h-10 ${className}`} 26 > 27 {content} 28 </button> 29 ); 30}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
下記試しました。
① icon={() => }の設定
jsx
1 <IconButton 2 icon={() => <MdKeyboardDoubleArrowLeft className="text-2xl text-black" />} 3 onClick={onClickSwitchVisible} 4 className="p-2 bg-white text-black rounded-full" 5 />
② icon={() => <icon style ={{color: 'black' }}
jsx
1 <IconButton 2 icon={() => <MdKeyboardDoubleArrowLeft className="text-2xl" style={{ color: 'black' }} />} 3 onClick={onClickSwitchVisible} 4 className="p-2 bg-white rounded-full" 5 />
③ className="... text-black bg-white"
jsx
1 <IconButton 2 icon={MdKeyboardDoubleArrowLeft} 3 onClick={onClickSwitchVisible} 4 className="p-2 bg-white text-black rounded-full" 5 />
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。