2.6.0 • Published 7 years ago

fyndiq-component-modal v2.6.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

fyndiq-component-modal npm

Preview

A Modal component for Fyndiq

Installation

The package can be installed through NPM:

npm i -S fyndiq-component-modal

It exports two main components: a Modal component which is fully customizable, as well as a confirm utility to quickly create beautiful confirm dialogs.

Modal

Usage

The easiest way to use the Modal component is to open it using ModalButton component:

import React from 'react'
import { ModalButton } from 'fyndiq-component-modal'

// Normal usage
<ModalButton button="Open Modal">
  Modal content
</ModalButton>

This setup will take care of opening the modal when pressing on the button, as well as closing the modal when clicking outside or pressing escape.

To customize the modal itself, you can pass it as a children:

import React from 'react'
import Modal, { ModalButton } from 'fyndiq-component-modal'

// Advanced styling
<ModalButton>
  <Modal overlayClassName="overlay" wrapperClassName="wrapper">
    Content
  </Modal>
</ModalButton>

// Access the onClose method from the children by using a function
<ModalButton>
  <Modal>
    {({ onClose }) => (
      <div>
        Content
        <button onClick={onClose}>Close popup</button>
      </div>
    )}
  </Modal>
</ModalButton>

For advanced use, you can use directly the Modal component and control its open prop, as well as binding the onClose callback prop.

API

The component Modal has the following customizable props:

NameTypeDescriptionDefault value
openBooleanShows or hide the modalfalse
 children React Node or FunctionContent of the modal. If a function is passed, a onClose prop will be passed to it, to allow for programatical closing null
 overlayClassName String ClassName of the background overlay 
 wrapperClassName String ClassName of the content wrapper 
 closeClassName String ClassName of the close button 
 forced Boolean Force the modal to be shown. Clicking outside or pressing ESC won't close it, instead you will have to use the children prop in function mode to programatically close it false
 onClose FunctionHandler method called when the modal is closed () => {}

The component ModalButton has the following customizable props

NameTypeDescriptionDefault value
buttonButton or StringContent of the button that will open the modal. If a string is passed, it will be converted to a Button ComponentOpen Modal
 children React ElementContent of the modal. If the element is not a Modal Component, it will be wrapped into one. Pass a Modal component as children to allow customisation of the Modal. null

Confirm

Usage

The easiest way to create a confirm flow is to use the confirm creation utility, as well as the Confirm React Component to customize its appearance.

import React from 'react'
import { Confirm, ConfirmWrapper, confirm } from 'fyndiq-component-modal'

// First you need to render the <ConfirmWrapper /> somewhere on your app.
// It is recommended that you put it somewhere in the root of your app:
const MyApp = () => (
  <React.Fragment>
    <ConfirmWrapper />
    <Layout /> {/* Rest of your app*/}
  </React.Fragment>
)

// This example shows how to display a warning confirm dialog,
// that pops up when the user clicks on a Delete button.
import { Warning } from 'fyndiq-icons'
import Button from 'fyndiq-component-button'

<Button onClick={confirmDelete}>
  ⚠️ Delete something
</Button>

// Here is where the magic happens ✨
async confirmDelete() => {
  const isValidated = await confirm(
    <Confirm
      type="warning"
      icon={<Warning />}
      title="Do you really want to delete that thing?"
      message="If you delete it, there is no way back"
      validateButton="Delete"
    />
  )

  if (isValidated) {
    // do something
  }
}

API

The component Confirm has the following customizable props

NameTypeDescriptionDefault value
typeStringColor scheme of the dialog. One of info, warning, successinfo
titleNodeTitle of the notificationAre you sure?
messageNodeAdditional message for the popup''
iconIconIcon shown on top of the confirm dialognull
validateButtonString or ButtonValidate button (or text)OK
cancelButtonString or ButtonCancel button (or text)Cancel
openBooleanShows or hide the confirm dialog. Used internally by the confirm utilitytrue
onValidateFunctionHandler called when the user clicked on validate. Used internally by the confirm utility function() => {}
onCancelFunctionHandler called when the user clicked on cancel. Used internally by the confirm utility function() => {}
2.6.0

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.1

7 years ago

2.4.0

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago