Coder Social home page Coder Social logo

react-show-dialog's Introduction

React Show Dialog

A headless library to help build application flows involving modal dialogs.

Installation

npm install @s-oram/react-show-dialog --save-exact

Getting Started

Using react-show-dialog is a three step process. See react-show-dialog-examples for a working example.

1. Create a Dialog Component

The dialog component is a typical React component. It must have a onModalResult property that is called to return the result value.

import { type RequiredDialogProps } from '@s-oram/react-show-dialog'

export const ConfirmDialog = ({
  message,
  onModalResult,
}: { message: string } & RequiredDialogProps) => {
  return (
    <div>
      <p>{message}</p>
      <button onClick={() => onModalResult('OK')}>OK</button>
      <button onClick={() => onModalResult('Cancel')}>Cancel</button>
    </div>
  )
}

2. Activate the Dialog With the useShowDialog() Hook

The useShowDialog() hook returns a showDialog() function that can be called to activate your dialog.

import { useShowDialog } from '@s-oram/react-show-dialog'
import { ConfirmDialog } from './ConfirmDialog'
import { deleteProject } from './api'

export const ProjectSettings () => {

  const showDialog = useShowDialog()

  const handleResult = (modalResult: string) => {
    if (modalResult === 'OK') {
        deleteProject()
    }
  }

  return (
    <div>
      <h1>Project Settings</h1>
      <button
        onClick={() => {
          showDialog(ConfirmDialog, handleResult)
        }}
      >
        Delete project
      </button>
    </div>
  )
}

3. Add the <ShowDialogProvider> Component to Your Application

Finally we need to add the <ShowDialogProvider/> somewhere in the application component tree.

import { ShowDialogProvider } from '@s-oram/react-show-dialog'
import { ProjectSettings } from './ProjectSettings'

export default function App() {
  return (
    <ShowDialogProvider>
      <ProjectSettings />
    </ShowDialogProvider>
  )
}

Styling

Show Dialog is a headless UI library. It works equally well with any styling solution and all component libraries.

Other Projects

react-show-dialog's People

Contributors

s-oram avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.