• Stars
    star
    119
  • Rank 297,870 (Top 6 %)
  • Language
    TypeScript
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

No more horizontal scroll when using 100vw 🎉. No more issues with the 100vh in mobile browsers 🤯.

next-real-viewport

No more horizontal scroll when using 100vw 🎉. No more issues with the 100vh in mobile browsers 🤯. You know the bug. The one that drives you crazy.

viewport

  1. You need to set some element's width to be full screen.
  2. You use width: 100vw
  3. Then you get your desktop computer, and your mouse introduces a scrollbar. Huh.
  4. Now your site has a tiny horizontal scroll 🤮

Or maybe

  1. You need to set some element's height to be full screen.
  2. You use height: 100vh
  3. Then you get your iphone and Safari's UI is all over. Wot?

This package simply calculates the real width of the viewport and sets some css variables to the document root, so you can enjoy a life without horizontal scroll, nor Safari issues.

  • ✅ Use via css variables (--vw and --vh) or via the useRealViewport hook
  • ✅ Listen to screen resizing
  • ✅ No flash on load (both SSR and SSG)

Install

npm install next-real-viewport

or

yarn add next-real-viewport

Set Up

RealViewportProvider (context)

To start using next-real-viewport, simply use the exported provider anywhere you want. The recommended place to use it is in a custom _app.

// pages/_app.{js,tsx}

import { RealViewportProvider } from "next-real-viewport";

function MyApp({ Component, pageProps }) {
  return (
    <RealViewportProvider>
      <Component {...pageProps} />
    </RealViewportProvider>
  );
}

export default MyApp;

Use

In CSS

You can use the css variables anywhere:

.fullWidth {
  width: calc(var(--vw) * 100);
}

.fullHeight {
  height: calc(var(--vh) * 100);
}

useRealViewport

Maybe you don't want to use the css variables (i don't know why anyone might not want to, they're awesome). But here's how to get the absolute values:

import { useRealViewport } from "next-real-viewport";

const Demo = ({ children }) => {
  const { vw, vh } = useRealViewport();

  return (
    <div
      style={{
        width: vw * 100,
      }}
    >
      {children}
    </div>
  );
};

Some Components

next-real-viewport comes with two layout components:

  • <ViewportWidthBox />
  • <ViewportHeightBox />
import { ViewportWidthBox, ViewportHeightBox } from "next-real-viewport";

const Demo = ({ children }) => {
  return (
    <>
      <ViewportWidthBox center>
        My full screen content here. A table, maybe.
      </ViewportWidthBox>
      <ViewportHeightBox>
        My full height content. A mobile menu, maybe.
      </ViewportHeightBox>
    </>
  );
};

Discussion

The Layout Shift

Inspired by next-themes, RealVwProvider automatically injects a script into next/head to update the html element with the css variable values before the rest of your page loads. This means the page will not have layout shift under any circumstances.

The React Context

Or, "Why do we use React Context for this?"

React Context is not used only for the hook, useRealViewport. No, we mainly use it because we need a listener for the resize event, and we don't want more than one.

Could the listener be set inside the <script />? Hm, maybe... But I haven't explored the downsides of having that (mainly, having more render-blocking JS).

PRs are welcome!

More Repositories

1

scrollytelling

A library for creating Scrollytelling animations, powered by React & GSAP.
TypeScript
1,117
star
2

basement-grotesque

The basement grotesque typeface.
TypeScript
301
star
3

commerce-toolkit

Ship better storefronts 🛍
TypeScript
279
star
4

basement-laboratory

We combine intensive technology with design expertise.
JavaScript
230
star
5

next-typescript

We make cool shit that performs, and we start with this template.
TypeScript
170
star
6

next-shopify-ts

A boilerplate for using Next.js with Shopify, TypeScript, and react-query 🔥
TypeScript
33
star
7

next-shopify

A context, a hook, and an API route handler, to manage a Shopify Storefront in your Next.js app.
TypeScript
30
star
8

blender-bezier-exporter

Python
23
star
9

gists

to store snippets of code (because you can't create gists as an org 🤷)
JavaScript
21
star
10

workshop-nextjs-pizza

TypeScript
21
star
11

bsmnt-links

A free URL shortener to deploy with your own domain.
TypeScript
10
star
12

creative-dev-challenge

A challenge for devs aiming to join basement as Creative Developer
TypeScript
10
star
13

next-contentful-graphql

Setup for using next with Contentful's Graphql API and have code generation for the schema and the queries and stuff... with great typescript support.
CSS
8
star
14

create-bsmnt-app

TypeScript
7
star
15

app-dir-example

App dir example made with our next-typescript starter.
CSS
7
star
16

react-shopify-drop

A react library to interact with Shopify's Storefront API
TypeScript
6
star
17

challenge

First step in order to become a basementer, lol.
3
star
18

b-hydra

CSS
3
star
19

scripts

Python
3
star
20

creative-toolkit

TypeScript
3
star
21

lbac-website

TypeScript
2
star
22

livepeer-org

JavaScript
1
star
23

next-shopify-drop

TypeScript
1
star