React で再利用可能な Modal コンポーネントを作成する

ReactReactBeginner
オンラインで実践に進む

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、イベントを通じて制御できる React の Modal コンポーネントを作成する方法を探ります。Modal コンポーネントにはクローズ ボタンがあり、Esc キーが押されたときにモーダルを閉じるためにキーボード イベント リスナーが使用されます。この実験が終了するとき、任意の React プロジェクトに簡単に統合できる再利用可能な Modal コンポーネントを作成する方法を十分に理解しているでしょう。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL react(("React")) -.-> react/FundamentalsGroup(["Fundamentals"]) react(("React")) -.-> react/AdvancedConceptsGroup(["Advanced Concepts"]) react(("React")) -.-> react/StateManagementGroup(["State Management"]) react/FundamentalsGroup -.-> react/jsx("JSX") react/FundamentalsGroup -.-> react/event_handling("Handling Events") react/AdvancedConceptsGroup -.-> react/hooks("React Hooks") react/StateManagementGroup -.-> react/use_state_reducer("Using useState and useReducer") subgraph Lab Skills react/jsx -.-> lab-38356{{"React で再利用可能な Modal コンポーネントを作成する"}} react/event_handling -.-> lab-38356{{"React で再利用可能な Modal コンポーネントを作成する"}} react/hooks -.-> lab-38356{{"React で再利用可能な Modal コンポーネントを作成する"}} react/use_state_reducer -.-> lab-38356{{"React で再利用可能な Modal コンポーネントを作成する"}} end

モーダル ダイアログ

VM には既に index.htmlscript.js が用意されています。一般的には、script.jsstyle.css にコードを追加するだけです。

このコードは、イベントを通じて制御できる Modal コンポーネントをレンダリングします。コンポーネントを使用するには、一度 Modal をインポートしてから、isVisible 属性にブール値を渡して表示します。

Modal コンポーネントには以下の機能があります。

  • keydownHandler 関数を定義し、すべてのキーボード イベントを処理し、Esc キーが押されたときに onClose を呼び出します。
  • useEffect() フックを使用して、Documentkeydown イベント リスナーを追加または削除し、各イベントで keydownHandler を呼び出します。
  • クローズ ボタンとして機能するスタイル付き <span> 要素を追加し、クリック時に onClose を呼び出します。
  • 親から渡される isVisible プロパを使用して、モーダルを表示するかどうかを判断します。
  • Modal コンポーネントをスタイル付けする CSS ファイルを含めています。
const Modal = ({ isVisible = false, title, content, footer, onClose }) => {
  const keydownHandler = ({ key }) => {
    switch (key) {
      case "Escape":
        onClose();
        break;
      default:
    }
  };

  React.useEffect(() => {
    document.addEventListener("keydown", keydownHandler);
    return () => document.removeEventListener("keydown", keydownHandler);
  });

  return !isVisible ? null : (
    <div className="modal" onClick={onClose}>
      <div className="modal-dialog" onClick={(e) => e.stopPropagation()}>
        <div className="modal-header">
          <h3 className="modal-title">{title}</h3>
          <span className="modal-close" onClick={onClose}>
            &times;
          </span>
        </div>
        <div className="modal-body">
          <div className="modal-content">{content}</div>
        </div>
        {footer && <div className="modal-footer">{footer}</div>}
      </div>
    </div>
  );
};
.modal {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(0, 0, 0, 0.25);
  animation-name: appear;
  animation-duration: 300ms;
}

.modal-dialog {
  width: 100%;
  max-width: 550px;
  background: white;
  position: relative;
  margin: 0 20px;
  max-height: calc(100vh - 40px);
  text-align: left;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow:
    0 4px 8px 0 rgba(0, 0, 0, 0.2),
    0 6px 20px 0 rgba(0, 0, 0, 0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: slide-in;
  animation-duration: 0.5s;
}

.modal-header,
.modal-footer {
  display: flex;
  align-items: center;
  padding: 1rem;
}

.modal-header {
  border-bottom: 1px solid #dbdbdb;
  justify-content: space-between;
}

.modal-footer {
  border-top: 1px solid #dbdbdb;
  justify-content: flex-end;
}

.modal-close {
  cursor: pointer;
  padding: 1rem;
  margin: -1rem -1rem -1rem auto;
}

.modal-body {
  overflow: auto;
}

.modal-content {
  padding: 1rem;
}

@keyframes appear {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slide-in {
  from {
    transform: translateY(-150px);
  }
  to {
    transform: translateY(0);
  }
}
const App = () => {
  const [isModal, setModal] = React.useState(false);
  return (
    <>
      <button onClick={() => setModal(true)}>Click Here</button>
      <Modal
        isVisible={isModal}
        title="Modal Title"
        content={<p>Add your content here</p>}
        footer={<button>Cancel</button>}
        onClose={() => setModal(false)}
      />
    </>
  );
};

ReactDOM.createRoot(document.getElementById("root")).render(<App />);

右下隅の「Go Live」をクリックして、ポート 8080 でウェブ サービスを実行してください。その後、Web 8080 タブを更新してウェブ ページをプレビューできます。

まとめ

おめでとうございます!あなたはモーダル ダイアログの実験を完了しました。あなたの技術を向上させるために、LabEx でさらに多くの実験を行って練習してください。