npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@asphalt-react/modal

v2.0.0-rc.9

Published

Modal

Downloads

200

Readme

Modal

npm

Modal provides a dialog interface to confirm user actions or to communicate an important message and get a response while maintaining the context of the current view. Modal interrupts a user's workflow by design. Modal blocks the user's access to the on-page content while it's visible. When visible, modal also disables the scroll on the body element and enables it when closed.

Modal is a controlled component and does not manage visibility state on it's own. Modal appears towards the top of the viewport by default but you can place it in center as well.

Usage

While effective when used correctly, use Modals sparingly to limit disruption to the user.

import { Modal } from "@asphalt-react/modal"

function App() {
  const [open, setOpen] = React.useState(false)

  return (
    <Modal
      open={open}
      onClose={() => {
        setOpen(false)
      }}
    >
      <span>Are you sure</span>
    </Modal>
  )
}

Helper Components

Modal exports few additional components to render content. These components help create confirmation dialogs easily.

  1. Title - Use it to render the Modal title.
  2. Description - Use it to render the Modal description.
  3. Footer - Use it to render the Modal footer.
import { Modal, Title, Description, Footer } from "@asphalt-react/modal"

function App() {
  const [open, setOpen] = React.useState(false)

  return (
    <Modal
      open={open}
      onClose={() => {
        setOpen(false)
      }}
    >
      <Title>Confirm</Title>
      <Title>Are you sure</Title>
      <Footer>
        <Button>Yes</Button>
        <Button
          onClick={() => {
            setOpen(false)
          }}
        >
          No
        </Button>
      </Footer>
    </Modal>
  )
}

When to use

  • An immediate response is required from the user:

    Use a modal to request information that is preventing the system from continuing a user-initiated process.

  • Confirm a user decision:

    Use a modal to confirm user decisions. Clearly describe the action being confirmed and explain any potential consequences that it may cause. Both the title and the primary button should reflect the action that will occur. If the action is destructive or irreversible then use a danger modal.

When not to use

  • Modals prevent access to the main page:

    Don’t use if additional information outside the modal needs to be consulted. While a modal is opened a user cannot interact with the main page and is restricted to only the information inside the modal for making decisions. Modal tasks should be easy to complete with the limited information presented inside the modal itself. If a user needs access to additional information then consider using a full page instead.

  • Avoid nesting modals:

    One modal should never trigger another modal. When a modal's task is dependent on another modal then perform the first task outside of a modal.

  • Avoid full page modals:

    Avoid using modals to display complex forms or large amounts of information. If a modal needs more space than the component allows then the content should be displayed on a page of its own and not inside a modal. A modal is not an alternative to page.

Accessibility

Adds aria-modal attribute to let assistive technologies handle it as a dialog role. By default the first focusable element receives focus when modal opens. Modal passes aria-* attributes to its topmost element.

For better screen reader support, add an id to the description node and apply it to aria-describedby.

  • Use Tab to move focus to the next focusable element inside the modal.
  • Use Esc to close the Modal.

Data Attributes

Modal accepts data-* attributes and forwards them to its top level element.

Props

children

React Node for modal content.

| type | required | default | | ---- | -------- | ------- | | node | false | null |

open

Controls the visibility of the modal.

| type | required | default | | ---- | -------- | ------- | | bool | false | false |

bezel

Adds padding inside modal.

| type | required | default | | ---- | -------- | ------- | | bool | false | true |

closeOnEsc

Closes modal on ESC keypress.

| type | required | default | | ---- | -------- | ------- | | bool | false | true |

center

Places the modal in the center of the viewport.

| type | required | default | | ---- | -------- | ------- | | bool | false | false |

onClose

Function to call when modal closes.

| type | required | default | | ---- | -------- | ------- | | func | false | null |

bodyScrollLock

Prevents the content behind the Modal from scrolling when the Modal is visible.

| type | required | default | | ---- | -------- | ------- | | bool | false | true |

dismiss

Add a cross button to the top right of the modal.

| type | required | default | | ---- | -------- | ------- | | bool | false | false |

Title

Title renders a heading for the Modal's content.

Props

children

Title of the modal.

| type | required | default | | ---- | -------- | ------- | | node | false | null |

separate

Adds a margin to separate it from its sibling elements.

| type | required | default | | ---- | -------- | ------- | | bool | false | true |

Description

Description renders the content for the Dialog.

Props

children

Node for content

| type | required | default | | ---- | -------- | ------- | | node | false | null |

separate

Adds a margin to separate it from its sibling elements.

| type | required | default | | ---- | -------- | ------- | | bool | false | true |

Footer

Use Footer to add interactive elements in Modal to accept user actions.

Props

children

Node for content.

| type | required | default | | ---- | -------- | ------- | | node | false | null |

alignCenter

Places the content at center.

| type | required | default | | ---- | -------- | ------- | | bool | false | false |

alignEnd

Places the content at the end.

| type | required | default | | ---- | -------- | ------- | | bool | false | false |

ModalLegacy

Modal uses the "dialog" element which is well supported on all major browsers. This is still a fairly new API which the browsers vendors added recently. If you have users that use older browsers which lack support for the "dialog" element, use the ModalLegacy component to wrap the Modal component.

Usage

import { Modal, ModalLegacy } from "@asphalt-react/modal"

export const App = () => {
  const modalRef = React.useRef(null)

  return (
    <ModalLegacy modalRef={modalRef}>
      <Modal open={true} ref={ModalRef}>Lorem ipsum</Modal>
    </ModalLegacy>
  )
}

Props

children

Modal to render in older browsers.

| type | required | default | | ---- | -------- | ------- | | node | false | N/A |

modalRef

React ref of the Modal.

| type | required | default | | ----- | -------- | ------- | | union | false | N/A |