Socket
Book a DemoInstallSign in
Socket

react-html-refs

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-html-refs

React html hooks to help us better manage html elements on React

1.0.2
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

react-html-refs

A collection of react hooks/providers to help you scroll by through life and focus on the right stuff.

Tired of React.RefObject chains all-over your project?
Tired of document.querySelector?.('.meaning-of-life') in a React app?
Do you want to manually focus on a html input across the application or scroll the user to a specific element? Yeah, me neither

Well then this project is for you!

With the code below you can manage all your html element related needs in your React app, just:

  • Wrap your React app in a Destinations provider, with your collection of useful handlers
  • Use the useDestination hook or Destination component to register html element destinations
  • Use useDestination to apply code to your registered destinations

See the example:

import React from 'react';
import { render } from 'react-dom';

import { useDestination, Destinations, Destination, Handler, chain, DebugLogger, silence, wrapUnknown } from 'react-html-refs';

type DestinationType = 'A';
type HandlerCategory = 'Focus' | 'Scroll';

const App = (): JSX.Element => {
    const { handle } = useDestination<DestinationType, HandlerCategory>('A');

    return (
        <>
            <Destination<DestinationType> destinationId="A">I am a destination</Destination>
            <button onClick={() => handle(wrapUnknown((el) => el.scrollIntoView({ behavior: 'smooth' })))}>Scroll!</button>
            <button onClick={() => handle('Focus')}>Scroll!</button>
        </>
    );
};

render(
    <Destinations<HandlerCategory>
        defaulHandler={chain(
            silence(wrapUnknown((el) => el.scrollIntoView({ behavior: 'smooth' }))),
            silence(wrapUnknown((el) => el.scrollTo({ behavior: 'smooth' }))),
            wrapUnknown((el) => window.scrollTo(0, el.offsetTop)),
        )}
        handlers={{
            Scroll: wrapUnknown((el) => window.scrollTo(0, el.offsetTop)),
            Focus: wrapUnknown((el) => el.focus()),
        }}
        logger={new DebugLogger()}
    >
        <App />
    </Destinations>,
    document.querySelector('#app'),
);

If you are using typescript, I strongly recommend strongly typing your ids and handler categories so its easier to manage them.

Keywords

react

FAQs

Package last updated on 23 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.