質問するログイン新規登録
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

初心者

初心者は、プログラミングやITに不慣れな方が、基礎的な知識やスキルを身につける際に直面する疑問や課題に関する投稿に使用されます。入門書や学習サイトで学び始めた方、初めての開発環境構築でつまずいた方などに向けた質問が多く見られます。

Q&A

解決済

1回答

105閲覧

React ボタンを押すと開閉するサイドバーの作成で、SVGの色が変えられない

grgr

総合スコア1

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

初心者

初心者は、プログラミングやITに不慣れな方が、基礎的な知識やスキルを身につける際に直面する疑問や課題に関する投稿に使用されます。入門書や学習サイトで学び始めた方、初めての開発環境構築でつまずいた方などに向けた質問が多く見られます。

0グッド

0クリップ

投稿2025/07/25 05:58

0

0

実現したいこと

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 />

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

index.cssの影響でした。
index.cssを修正して、実現できました。

投稿2025/07/25 07:26

grgr

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問