Code Monkey home page Code Monkey logo

react-div-100vh's Introduction

Div100vh React component

CircleCI npm version

This is a workaround for iOS Safari and other mobile browsers.

The problem

At the top of the page, mobile browsers cover bottom of 100vh page with "browser chrome" (that's the name for browser navigation/context buttons, don't confuse with the browser from Google), effectively cropping it. If you have something important at the bottom of your splash screen, chances are it will not be visible/available until user scrolls.

More on this issue here.

The solution

iOS screenshots

<div style={{height: '100vh'}}> <Div100vh>
Page cropped by bottom Safari chrome Page cropped by bottom Safari chrome

The demo

Browse https://react-div-100vh.netlify.com on your phone!

API

  • Install it: npm install --save react-div-100vh or yarn add react-div-100vh
  • Import the component and wrap your stuff with <Div100vh> as you would with a normal <div style={{height: '100vh'}}>, but this time mobile browsers should display the whole page on load:

The default behavior

import Div100vh from 'react-div-100vh'

const MyFullscreenComponent = () => (
  <Div100vh>
    <marquee>Your stuff goes here</marquee>
  </Div100vh>
)

Using rvh units

If you want to set min-height (or any other property) instead, you can use made up rvh ("real viewport height") units in values of an object passed to style prop. Div100vh will find any style declarations with this unit and calculate the value as a percentage of window.innerHeight:

  <Div100vh style={{minHeight: '50rvh'}}>
    <marquee>This is inside a div that takes at least 50% of viewport height.</marquee>
  </Div100vh>

If you don't specify style prop, it works as if you specified {height: '100rvh'}; <Div100vh> is equivalent to <Div100vh style={{height: '100rvh'}}>.

If you do pass anything to the style prop, no implicit style is applied. You can do something like:

<Div100vh
  style={{maxHeight: '70rvh', color: 'blue'}}
  onClick={() => console.log('hi')}
>
  <p>my content here</p>
</Div100vh>

The rest of the props are passed unchanged to the underlying div that Div100vh renders.

Rendering non-<div> elements

You can also pass an as prop to render other elements than <div>s - for example, <main>, <section>, <footer>, et cetera.

<Div100vh as="main">
  <p>Some main content</p>
</Div100vh>

โ— Keep in mind that this works best with block-level elements, as inline-level elements don't respond to the CSS height property.

Additional considerations

Please note that most likely you will want to set body {margin: 0} css, unless you use some css reset that does it for you.

Testing

This component is tested with Jest and BrowserStack Logo

react-div-100vh's People

Contributors

bignimbus avatar charlescrtr avatar dependabot[bot] avatar joshuarrr avatar kframesa avatar meyerds avatar mvasin avatar tinder-kyleboss avatar yatsyk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.