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

gatsby-plugin-modal-routing

v1.2.0

Published

Gatsby plugin to enable routing of modal pages

Downloads

3,611

Readme

gatsby-plugin-modal-routing

Adds support for viewing gatsby pages within modals at their gatsby defined routes.

Examples

  • https://gatsby-starter-with-gatsby-plugin-modal-routing.netlify.com/
  • https://gatsbygram-with-gatsby-plugin-modal-routing.netlify.com/

Install

npm install --save gatsby-plugin-modal-routing

Motivation

The problem: how to handle modals (stateful routes) with gatsby's page based routing?

We want a modal to open within the context of whatever page it was linked to, change the browser's URL, and render server-side allowing permalink navigation.

Current Gatsby V2 examples which use PageRenderer flicker when re-rendering content underneath the modal. This plugin aims to handle modal routing edge cases and provide a consistent rendering experience with a flexible API.

How to use

Note that this plugin is currently in alpha, and this API is subject to change

Add the plugin to your gatsby-config.js:

// gatsby-config.js

module.exports = {
  plugins: [
    `gatsby-plugin-modal-routing`
  ]
];

Plugin Options

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-modal-routing`,
      options: {
        // A selector to set react-modal's app root to, default is `#___gatsby`
        // See http://reactcommunity.org/react-modal/accessibility/#app-element
        appElement: '#___gatsby',

        // Object of props that will be passed to the react-modal container
        // See http://reactcommunity.org/react-modal/#usage
        modalProps: { },
      }
    }
  ]
];

Rendering page content in modals

Any gatsby page may be rendered in a modal if it is routed to appropriately (see next section below for creating a modal link).

The ModalRoutingContext React.Context component can be used to conditionally render content if the page is rendered in a modal.

The Context consumer is passes an object with modal and closeTo properties to it's child render function

  • modal (boolean) - indicates if the page content will be rendered in a modal. Use this to conditionally render modal content like a close button.
  • closeTo (string) - if the page content is rendering in a modal, denotes the pathname of the page where the modal was opened, otherwise null.

Example:

// pages/modal-example.js

import React from 'react'
import { Link } from 'gatsby'
import { ModalRoutingContext } from 'gatsby-plugin-modal-routing'

const ModalExamplePage = () => (
  <ModalRoutingContext.Consumer>
    {({ modal, closeTo }) => (
      <div>
        {modal ? (
          <Link to={closeTo}>
            Close
          </Link>
        ) : (
          <header>
            <h1>
              Website Title
            </h1>
          </header>
        )}

        <h2>Modal Page</h2>

        <Link to="/">Go back to the homepage</Link>
      </div>
    )}
  </ModalRoutingContext.Consumer>
)

export default ModalExamplePage

Opening a page in a modal

Pages can be opened in a modal context by passing the { modal: true } flag to Link state.

Example:

// src/components/some-component.js

import { Link } from 'gatsby'

...

<Link
  to="/login/"
  state={{
    modal: true
  }}
>
  Login
</Link>

gatsby-plugin-modal-routing also provides a Link component as a convenience to encapsulate this flag for you.

This is equivalent to the example above:

// src/components/some-component.js

import { Link } from 'gatsby-plugin-modal-routing'

...

<Link
  to="/login/"
  asModal
>
  Login
</Link>

Scroll State

When the site opens a modal, gatsby's default scroll update is prevented, so that the underlying page remains scrolled at the same position.

When routing to a non-modal page from a modal, gatsby's default scroll update is allowed, causing the page to scroll to the top. This is the case even if the non-modal page is the same as the underlying page.

To prevent this, pass the { noScroll: true } flag to Link state.

// src/components/modal-content.js

import { Link } from 'gatsby'

...

<Link
  to="/"
  state={{
    noScroll: true
  }}
>
  Close Modal
</Link>

As a convenience, this plugin's Link component will detect if the to pathname matches the content rendered under the modal and set the noScroll flag for you.

// src/components/modal-content.js

import { Link } from 'gatsby-plugin-modal-routing'

...

<Link
  to="/"
>
  Close Modal
</Link>

To prevent scrolling on the underlying page after navigation to a modal is complete, see this thread for different strategies.