Loading...

How to create a generic Dialog component in TailwindCSS that takes in title, description, etc. as props?

question tailwindcss front-end
Ram Patra Published on February 23, 2024

If you want to create a separate, generic Dialog component where you can pass in the title and description as props, you can do so by defining a reusable Dialog component. You can even pass HTML content in the description of the Dialog component, you can do so by utilizing React’s dangerouslySetInnerHTML attribute. Here’s how you can achieve this:

  1. Create a Generic Dialog Component:

    // Dialog.js
    import React from 'react';
    import { Dialog } from '@headlessui/react';
    
    function CustomDialog({ title, description, isOpen, onClose }) {
      return (
        <Dialog open={isOpen} onClose={onClose}>
          <Dialog.Overlay className="fixed inset-0 bg-black opacity-30" />
          <div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-white p-4">
            <Dialog.Title>{title}</Dialog.Title>
            <Dialog.Description>
              <div dangerouslySetInnerHTML= />
            </Dialog.Description>
            <button onClick={onClose}>Close</button>
          </div>
        </Dialog>
      );
    }
    
    export default CustomDialog;
    
  2. Use the Generic Dialog Component:

    // Example.js
    import React, { Fragment, useState } from 'react';
    import CustomDialog from './Dialog';
    
    function Example() {
      const [isOpen, setIsOpen] = useState(false);
    
      return (
        <Fragment>
          <button onClick={() => setIsOpen(true)}>Open Dialog</button>
          <CustomDialog
            isOpen={isOpen}
            onClose={() => setIsOpen(false)}
            title="Dialog Title"
            description="<p>This is the <strong>content</strong> of the dialog.</p>"
          />
        </Fragment>
      );
    }
    
    export default Example;
    

Inside the CustomDialog component, we use dangerouslySetInnerHTML to render the HTML content safely. However, please be cautious when using dangerouslySetInnerHTML as it can expose your application to cross-site scripting (XSS) attacks if not used carefully. Make sure that you trust the source of the HTML content or sanitize it properly before rendering.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on February 23, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question front-end tailwindcss February 17, 2024 How to center an element using Tailwind CSS?

In Tailwind CSS, you can center an element using flexbox utilities directly in your HTML markup. Here’s how you can center an element horizontally and vertically:

question tailwind front-end September 19, 2024 How to create a tooltip with just Tailwind CSS (no libraries)?

This blog post shows you how you can create a simple tooltip with some text in it that shows up on hover. This solution only utilises the classes available in TailwindCSS. It doesn’t depend on any 3rd party libraries.

question react tailwindcss September 19, 2024 A simple React Tooltip component that uses TailwindCSS only (no libraries)

Here’s a React component that allows you to attach the tooltip to any element by passing it as a child. The component will render the tooltip and an arrow pointing downward.