1.0.2 • Published 1 year ago

use-target v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

useTarget

A custom React hook for scrolling to a specific target element on the page.

Installation

To install useTarget, you can use NPM or Yarn:

npm install use-target
yarn add use-target

Usage

To use useTarget, import it into your component and call it with the ID of the target element you want to scroll to:

jsx

import useTarget from 'use-target';

  

const MyComponent = () => {

const handleClick = useTarget('my-target');

  

return (

<button onClick={handleClick}>Scroll to target</button>

<div id="my-target">This is the target element</div>

);

};

The useTarget hook returns a click handler function that, when called, will smoothly scroll the page to the target element with the specified ID.

Options

useTarget also accepts an optional second argument for configuration options:

jsx

const handleClick = useTarget('my-target', { speed: 1000, offset: 100 });

speed

The speed at which the page scrolls to the target element, in milliseconds. Default is 500.

offset

The vertical offset from the top of the target element to scroll to. This can be useful if you want to scroll to a target element that is partially obscured by a fixed header or other element. Default is 0.

License

useTarget is released under the MIT License.