Open Some React Component with TailwindCSS Modals
Published: 2023-02-17 19:18:23

If you want to open a TailwindCSS Modal with React Component inside, you may need to create a new Component with the modal class. Here Is simple example of a `Modal` Component. 

```ts
interface ModalProps {
  isOpen: boolean;
  onClose: () => void;
  children: React.ReactElement;
}

const MyModal: React.FC<ModalProps> = ({ isOpen, onClose, children }) => {
  return (
    <>
      {isOpen && (
        <div className="fixed z-10 inset-0 overflow-y-auto p-4 ">
          <div className="flex items-center justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
            <div
              className="fixed inset-0 transition-opacity"
              aria-hidden="true"
            >
              <div className="absolute inset-0 bg-gray-500 opacity-75"></div>
            </div>

            <span
              className="hidden sm:inline-block sm:align-middle sm:h-screen"
              aria-hidden="true"
            >
              &#8203;
            </span>

            <div
              className="inline-block align-bottom bg-gray-900 p-4 rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full"
              role="dialog"
              aria-modal="true"
              aria-labelledby="modal-headline"
            >
              <div className="absolute top-0 right-0 pt-4 pr-4">
                <button
                  onClick={onClose}
                  className="text-gray-600 hover:text-gray-400 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
                >
                  <span className="sr-only">Close</span>
                  <svg
                    className="h-6 w-6"
                    xmlns="http://www.w3.org/2000/svg"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                    aria-hidden="true"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M6 18L18 6M6 6l12 12"
                    />
                  </svg>
                </button>
              </div>

              {children}
            </div>
          </div>
        </div>
      )}
    </>
  );
};

export default MyModal;
```

and if you are planning to use this component to open some React Component as it's child you can simply do this by 

```ts
import React, { useState } from "react";
import MyForm from "../Forms/MyForm";
import MyModal from "../Modals/MyModal";


function Welcome() {
  const [isOpen, setIsOpen] = useState(false);
  const onClose = () => {
    setIsOpen(false);
  };

  const onOpen = () => {
    setIsOpen(true);
  };

  return (
    <div className="mt-9">
     
     
      <button
        className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
        onClick={onOpen}
      >
        Open Modal
      </button>
      <MyModal isOpen={isOpen} onClose={onClose}>
        {<MyForm/>}
      </MyModal>
    </div>
  );
}

export default Welcome;
```