Skip to content

About

Super easy own react modals

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

React modals

reliability Security Rating Maintainability Rating Vulnerabilities Bugs Lines of Code size semantic version

Table of contents

Getting started

The package is distributed using npm, the node package manager.

npm i --save @lomray/react-modals

How to use

Use this to open application-owned modal content from hooks or refs. It supplies lifecycle events and a root renderer, not styling, focus management, keyboard handling, or an accessible dialog implementation. Supply those in your Modal component. There is no ModalProvider export; ModalRoot is a named export.

Mount one ModalRoot per application and open modals after it has mounted. Roots listen to shared event channels, so several roots are not independent scopes. The small shell below demonstrates the wiring only, not a production dialog.

import React, { type FC, type PropsWithChildren } from 'react';
import { ModalRoot, useModal, type IModalToggle } from '@lomray/react-modals';

const ModalShell: FC<PropsWithChildren<IModalToggle>> = ({ children, isVisible }) =>
  isVisible ? <section>{children}</section> : null;

interface MessageProps extends IModalToggle {
  text: string;
}

const Message: FC<MessageProps> = ({ text, closeModal }) => (
  <div>
    <p>{text}</p>
    <button onClick={closeModal}>Close</button>
  </div>
);

const OpenButton = () => {
  const [open, hide] = useModal(Message);
  return (
    <>
      <button onClick={() => open({ text: 'Hello' })}>Open</button>
      <button onClick={() => hide()}>Hide</button>
    </>
  );
};

export const App = () => (
  <>
    <OpenButton />
    <ModalRoot Modal={ModalShell} />
  </>
);

ModalRoot unsubscribes when it unmounts. A modal opened by a hook does not automatically close when that hook's owner unmounts; call hide() when your application's lifecycle requires it. For access outside the owning component, pass a createModalRef<MessageProps>() as hookRef in the second argument to useModal; its methods are assigned by an effect, so do not call it before mount.

useModalMobx also passes the current MobX parent context ID to modal content. It does not add a provider or style the dialog.

Release 2.0.1 declares peers on React, react-router-dom, @lomray/client-helpers and @lomray/react-mobx-manager. Install compatible versions even for the basic hook; the package root re-exports the MobX hook. The docs test records its pinned fixture separately from these broad peer ranges. It checks opening and closing in a simulated DOM, not accessibility or an SSR integration.

Demo

Explore demo app to more understand.

Bugs and feature requests

Bug or a feature request, please open a new issue.

About

Super easy own react modals

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages