1.0.2 • Published 3 years ago

use-visibility-percentage v1.0.2

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

useVisibilityPercentage

GitHub license code style: prettier

A react hook to measure percentage of element inside the window height (window.innerHeight).

Features

  • TypeScript support 🧔
  • Lightweight 🐔

Installation

npm i use-visibilty-perctange --save

Or

yarn add use-visibility-percentage

Usage

const [percent, position] = useVisibiltyPercentage(ref, \ * { options } * \)
PropertyTypeDescription
refreact refRef to element you want to monitor
percentnumberNumber between 0 and 1 indicating visible fragment of element
positionstringOne of values ('above', 'top','center', 'bottom', 'below' )

Basic usage:

import React from 'react'
import useVisibiltyPercentage from 'use-visibilty-percentage'

const Percent = () => {
  const ref = useRef()
  const [percent] = useVisibiltyPercentage(ref)

  return (
    <div
      ref={ref}
      className='box'
      style={{ opacity: percent.toPrecision(2) }}
    />
  )
}

Using optional options:

import React from 'react'
import useVisibiltyPercentage from 'use-visibilty-percentage'

const Percent = () => {
  const ref = useRef()
  const [percent] = useVisibiltyPercentage(ref, {
    offsetTop: 20,
    offsetBottom: 20,
    throttle: 30,
  })

  return (
    <div
      ref={ref}
      className='box'
      style={{ opacity: percent.toPrecision(2) }}
    />
  )
}

Options

Provide these as second argument to hook ( useVisibiltyPerctange(ref, { options }) ).

NameTypeDefaultRequiredDescription
offsetTopnumber0falseThe top offset of window view
offsetBottomnumber0falseThe bottom offset of window view
throttlenumber16falseThe throttle time in miliseconds
1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.4

4 years ago

0.3.2

4 years ago

0.3.3

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago