All Projects β†’ toomuchdesign β†’ next-use-contextual-routing

toomuchdesign / next-use-contextual-routing

Licence: MIT License
Generate contextual routing / modal routes paths for Next.js

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to next-use-contextual-routing

Awesome Nextjs
πŸ“” πŸ“š A curated list of awesome resources : books, videos, articles about using Next.js (A minimalistic framework for universal server-rendered React applications)
Stars: ✭ 6,812 (+8863.16%)
Mutual labels:  nextjs, routing
Swr
React Hooks for data fetching
Stars: ✭ 20,348 (+26673.68%)
Mutual labels:  hook, nextjs
Nextjs Dynamic Routes
[Deprecated] Super simple way to create dynamic routes with Next.js
Stars: ✭ 145 (+90.79%)
Mutual labels:  nextjs, routing
React Cool Portal
😎 πŸ’ React hook for Portals, which renders modals, dropdowns, tooltips etc. to <body> or else.
Stars: ✭ 458 (+502.63%)
Mutual labels:  hook, modal
Next Routes
Universal dynamic routes for Next.js
Stars: ✭ 2,354 (+2997.37%)
Mutual labels:  nextjs, routing
Next Sanity
Sanity.io toolkit for Next.js
Stars: ✭ 115 (+51.32%)
Mutual labels:  hook, nextjs
prophet
η”¨δΊŽζž„ε»Ί react δΈ­εŽε°ε‰η«―εΊ”η”¨ζ‘†ζžΆ
Stars: ✭ 12 (-84.21%)
Mutual labels:  hook
wefootwear-store
next js footwear store e-commerce πŸš€πŸš€πŸš€
Stars: ✭ 17 (-77.63%)
Mutual labels:  nextjs
bootstrap-modal-wrapper
Bootstrap modal factory that supports dynamic modal creations and nested stacked modal features.
Stars: ✭ 18 (-76.32%)
Mutual labels:  modal
next-with-ts
Next.js for boiler plate to easy development.
Stars: ✭ 22 (-71.05%)
Mutual labels:  nextjs
hookr
PHP action and filter hook system
Stars: ✭ 39 (-48.68%)
Mutual labels:  hook
EthBuildersNYC-website
The EthBuilders Website. Part Experimentation. Part Magical Unicorn. All Fun. Learn to build a Full Stack DApp with lots of cutting edge features, together.
Stars: ✭ 18 (-76.32%)
Mutual labels:  nextjs
folio
Interactive Portfolio with Next, GSAP, Tailwind, and React
Stars: ✭ 20 (-73.68%)
Mutual labels:  nextjs
pulse
A Platzi Flavored Markdown desktop editor
Stars: ✭ 22 (-71.05%)
Mutual labels:  nextjs
heroku-nextjs-build
Heroku build adapter for Next.js (an npm module)
Stars: ✭ 12 (-84.21%)
Mutual labels:  nextjs
react-wordpress
Example of a React+Next.js+WordPress application.
Stars: ✭ 36 (-52.63%)
Mutual labels:  nextjs
nextjs-typescript-express-boilerplate
A next.js boilerplate app with Typescript and an Express API server
Stars: ✭ 30 (-60.53%)
Mutual labels:  nextjs
mirror-next
A Next.js-powered frontend for your Mirror publication
Stars: ✭ 98 (+28.95%)
Mutual labels:  nextjs
quirrel-next
Moved to main Quirrel repo.
Stars: ✭ 19 (-75%)
Mutual labels:  nextjs
next-page-tester
DOM integration testing for Next.js
Stars: ✭ 566 (+644.74%)
Mutual labels:  nextjs

Next use contextual routing

Build status Test coverage report Npm version Bundle size

React hook that generates round trip hrefs to enable contextual routing for Next.js. Take a look to the demo πŸ‘ˆ.

Contextual routing in action

With Next links

import React from 'react';
import Link from 'next/link';
import { useContextualRouting } from 'next-use-contextual-routing';

function MyPage() {
  const { makeContextualHref, returnHref } = useContextualRouting();

  return (
    <ul>
      <li>
        <Link
          href={makeContextualHref({ id: 33 })}
          as="/route-to-visit-contextually"
          shallow
        >
          Start contextual routing
        </Link>
      </li>
      <li>
        <Link href={returnHref} shallow>
          Back to where contextual routing was started
        </Link>
      </li>
    </ul>
  );
}

With Next router

import React from 'react';
import { useRouter } from 'next/router';
import { useContextualRouting } from 'next-use-contextual-routing';

function MyPage() {
  const router = useRouter();
  const { makeContextualHref, returnHref } = useContextualRouting();

  const openModal = () =>
    router.push(
      makeContextualHref({ id: 33 }),
      '/route-to-visit-contextually',
      {
        shallow: true,
      }
    );

  const closeModal = () =>
    router.push(returnHref, undefined, { shallow: true });

  return (
    <ul>
      <li>
        <button onClick={openModal}>Open contextual modal</button>
      </li>
      <li>
        <button onClick={closeModal}>Close contextual modal</button>
      </li>
    </ul>
  );
}

Why?

Contextual routing presents 2 challenges:

  • Persist a href string able to keep the initial page state consistent throughout the whole contextual navigation
  • Persist the url to return to when contextual routing is terminated

Next use contextual routing abstracts these responsibilities as a React hook.

It also works in statically exported apps and supports navigation with browsers' back button.

API

const { makeContextualHref, returnHref } = useContextualRouting();

makeContextualHref

const makeContextualHref: (extraQueryParams: { [key: string]: any }) => string;

The function returns the path to provide as href to start or keep alive contextual navigation. The generated path describes the state of the page to keep alive while contextual navigation is active.

It optionally accepts an object describing extra parameters to append to contextual navigation href.

returnHref

const returnHref: string;

The path to return to to close contextual navigation.

Notes

Naming

Contextual routing pattern is also known as:

Scroll behaviour on refresh

When using Contextual routing, default browsers scroll behaviour on page refresh might produce undesired results. If the case, setting history.scrollRestoration to manual could improve user experience.

history.scrollRestoration = 'manual';
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].