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

modales

v1.3.0

Published

Router and modals advanced system

Downloads

16

Readme

npm version Build Status Maintainability Test Coverage

Modales provides a high level interface to manage modals and Route modals inside your React application. Modales is not a/(part of a) CSS library so modals content will look just as you like them to look; Modales will just display a modal in the center of the screen with the content you provide which also takes into account content that is bigger that the view and will provide scroll bars to it.

Modales uses react-router to successfully manage route modales, it also provides extra tools to navigate.

Install

npm install modales

yarn add modales

Getting staterd

You will need a Modales instance to connect, this will connect with the ModalesProvider.

// modales.js

import Modales from 'modales'

const configuration = { blurEnabled: true, routeModalsEnabled: true }
const modales = new Modales(configuration)

export default modales

Typescript:

// modales.ts

import Modales, { ModalesConfiguration } from 'modales'

const configuration: ModalesConfiguration = { blurEnabled: true, routeModalsEnabled: true }
const modales: Modales = new Modales(configuration)

export default modales

Then we need to use the ModalesProvider component to wrap your app and enable the modals display. You should also wrap the app with the react-router Router

// App.js
import React from 'react'
import { BrowserRouter, Route } from 'react-router-dom'
import { ModalesProvider } from 'modales'

// Modales instance
import modales from './modales'

export default class App extends React.Component {
  render() {
    return (
      <div className="app">
        <BrowserRouter>
          <ModalesProvider modales={modales}>
            <Route exact path="/" component={SomeComponent}></Route>
          </ModalesProvider>
        </BrowserRouter>
      </div>
    )
  }
}

Navigate to a modal route

Use react-router as you normally do just set the state of any route to contain the property modal as true.

<Link to={{ pathname: '/modal', state: { modal: true } }}>Visit modal route</Link>

Groups

If you want to give the user a feeling like navigating inside the route modal, something like IMDB when you click on the picture and navigate between them and when you go back no matter how many pictures you navigate it will take you to the movie page.

For this we have modalGroups so when you navigate to a new route in the same modal group it will just change the content of the modal with the new route.

Additionally use replace to make the effect of going back we talked early.

<Link replace to={{ pathname: '/modal', state: { modal: true, modalGroup: 'pictures' } }}>
  Visit modal route
</Link>

Background

You can choose between 3 types of backgrounds transparent | translucent | blurred, just pass the background prop into the state.

<Link replace to={{ pathname: '/modal', state: { modal: true, background: 'blurred' } }}>
  Visit modal route
</Link>

Custom modals

Use the Modales instance to launch modals with a particular content. Optionally you can also provided the kind of background and a onOutsideClick callback and a onScape one, to handle if you really want the modal to be closed (Probably launch another modal asking "are you sure?" to the user).

//anyfile.js
import modales from './modales'

// Return true if you actually want the modal to be closed
function onOutsideClick(event) {
  if (somethig) {
    return true
  }
}

// Return true if you actually want the modal to be closed
function onScape(event) {
  if (somethig) {
    return true
  }
}

modales.launchModal(<SomeModalConetent props />, 'transparent', onOutsideClick, onScape)

Typescript:

//anyfile.ts
import modales, { ModalBackground } from './modales'

// Return true if you actally want the modal to be closed
function onOutsideClick(event: MouseEvent): boolean {
  if (somethig) {
    return true
  }
}

// Return true if you actally want the modal to be closed
function onScape(event: KeyboardEvent): boolean {
  if (somethig) {
    return true
  }
}

const background: ModalBackground = 'transparent'

modales.launchModal(<SomeModalConetent props />, background, onOutsideClick, onScape)

Router Ref

Modales comes with a useful reference to the router so you can call route actions from your modales instance.

//anyfile.js
import modales from './modales'

modales.router.push('/path', { modal: true })
modales.router.goBack()

Manually Pop a custom modal

If you really need to pop the top modal without depending onOutsideClick and onScape events you can use the modal instance to do so.

//anyfile.js
import modales from './modales'

modales.launchModal(<SomeModalConetent props />)

setTimeOut(() => {
  modales.popModal()
}, 2000)

WARNING: do not use this for a route modal, route modals depend on the navigation history so you can only dismiss a route modal by navigating back or to another non modal route.

Contributions

PRs are welcome

Lisence

MIT