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

react-custom-accessible-modal

v0.1.8

Published

A React responsive modal designed for accessibility

Downloads

3

Readme

React plugin : react-custom-accessible-modal

A React responsive modal component that handles accessibility requirements :

  • use of Escape key to close the modal
  • traps focus inside the modal
  • hides background content
  • focus on whatever field you want when the modal is closed (optional)

You can also choose to apply your own custom style for the button, the icon and message

Features

React > v.18.2.0
Node.js > v.16.17.1

Install

Once you have set up your React app :

npm install react-custom-accessible-modal

Use

  • Import the plugin in your React app :
  import { Modal } from "react-custom-accessible-modal";
  • Set the state and variables in the component where the plugin is imported (e.g.):
  import { useState } from "react";

  const [showModal, setShowModal] = useState(false);

  const myIcon = <svgPath>;
  const myMessage = "Profile created successfully !";
  const myButtonText = "OK";
  const myAriaLabel = "OK, close modal";
  const closeModal = {() => setShowModal(false)};
  const myButtonStyle = { backgroundColor: "#0891b2" };
  const myIconStyle = { borderRadius: "30px" };
  const myMessageStyle = { fontSize: "45px" };
  • Pass an event to open the modal in your component :
const openModal = (e) => {
    e.preventDefault();
    setShowModal(true);
  };
  • Return the modal plugin in your JSX with conditional rendering :
{showModal && (
<Modal icon={myIcon} message={myMessage} buttonText={myButtonText} ariaLabel={myAriaLabel} closeModal={closeModal} buttonStyle={myButtonStyle} iconStyle={myIconStyle} messageStyle={myMessageStyle}
/>)}

Optional feature

Set focus on a specific element when the modal is closed :

  • Set the useRef hook to target the element that will receive the focus in your component (e.g.) :
  import { useRef } from "react";

  const inputRef = useRef(null);

  <input ref={inputRef} />
  • Replace the closeModal variable we have set up above by this one :
  const closeModal = () => {
    setShowModal(false);
    inputRef.current.focus();
  };

Props

| Parameter | Type | Description | | :------------- | :--------- | :--------------------------------------------------------------------------------------------------------- | | icon | object | icon must be in SVG format | | message | string | message confirming that the action was successfully performed | | buttonText | string | text displayed inside the button | | ariaLabel | string | label aiming to help users of assistive technologies, especially if buttonText value is not quite explicit | | closeModal | function | fired when the user clicks on the button | | buttonStyle | string | change the button styling by passing the properties directly to the prop | | iconStyle | string | change the icon styling by passing the properties directly to the prop | | messageStyle | string | change the message styling by passing the properties directly to the prop |

Demo

Here are 2 examples of how to use the plugin to give feedback to the user after a form submission.

  • With focus : when the user gets back to the page, the focus is placed on a specific element
import React, { useState, useRef } from "react";
import { Modal } from "react-custom-accessible-modal";

export default function MyComponent() {
  //  Define state and variables
  const [showModal, setShowModal] = useState(false);

  const myIcon = <svgPath>;
  const myMessage = "Profile created successfully !";
  const myButtonText = "OK";
  const myAriaLabel = "OK, close modal";
  const myButtonStyle = { backgroundColor: "#0891b2" };
  const myIconStyle = { borderRadius: "30px" };
  const myMessageStyle = { fontSize: "45px" };
  const openModal = (e) => {
    e.preventDefault();
    setShowModal(true);
  };

  // Put focus on a specific element when modal is closed
  const inputRef = useRef(null);
  const closeModal = () => {
    setShowModal(false);
    inputRef.current.focus();
  };

  return (
    //Pass the onClick or onSubmit event that opens the modal
    <form onSubmit={openModal}>
      <input />
      <input ref={inputRef} />
      <button type="submit">Save</button>
      {/* The modal is conditionally rendered */}
      {showModal && (
        <Modal
          icon={myIcon}
          message={myMessage}
          buttonText={myButtonText}
          ariaLabel={myAriaLabel}
          closeModal={closeModal}
          buttonStyle={myButtonStyle}
          iconStyle={myIconStyle}
          messageStyle={myMessageStyle}
        />
      )}
    </form>
  );
};
  • Without focus
import React, { useState } from "react";
import { Modal } from "react-custom-accessible-modal";

export default function MyComponent() {
  // Define state and variables
  const [showModal, setShowModal] = useState(false);

  const myIcon = <svgPath>;;
  const myMessage = "Profile created successfully !";
  const myButtonText = "OK";
  const myAriaLabel = "OK, close modal";
  const closeModal = {() => setShowModal(false)};
  const myButtonStyle = { backgroundColor: "#0891b2" };
  const myIconStyle = { borderRadius: "30px" };
  const myMessageStyle = { fontSize: "45px" };

  const openModal = (e) => {
    e.preventDefault();
    setShowModal(true);
  };

  return (
    //Pass the onClick or onSubmit event that opens the modal
    <form onSubmit={openModal}>
      <button type="submit">Save</button>

      {/* The modal is conditionally rendered */}
      {showModal && (
        <Modal
          icon={myIcon}
          message={myMessage}
          buttonText={myButtonText}
          ariaLabel={myAriaLabel}
          closeModal={closeModal}
          buttonStyle={myButtonStyle}
          iconStyle={myIconStyle}
          messageStyle={myMessageStyle}
        />
      )}
    </form>
  );
};