Socket
Socket
Sign inDemoInstall

@vovikilelik/use-html

Package Overview
Dependencies
6
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vovikilelik/use-html

Hook and utils for DOM manipulations


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

It is cover for html-js utils.

Abstract

The use-html package includes a react hook that allows you to access an HTML element and a set of html-js utilities for manipulating the DOM.

Features

  • Manipulation with DOM.
  • Call to clear function before new rendering.

For more features see html-js library.

Example

You can use the useHtml hook to access a DOM element. The builder function implements element management. Inside you can use the html-js utility.

export const Component: React.FC = () => {
  const ref = useHtml(element => { /* DOM manipulations */ });
  
  return <div ref={ref} />;
};

useHtml accepts a method that can return a cleanup function.

const builder: HtmlBuilder<HTMLDivElement> = element => {
  const unsubscriber = subscribe(element, {
    click: () => console.log('Click')
  });

  /* DOM manipulations */

  return unsubscriber;
};

export const Component: React.FC = () => {
  const ref = useHtml(builder);
  
  return <div ref={ref} />;
};

Uses

This package contains several hooks that will help you access DOM elements:

  • useHtml() - base hook.
  • useLayoutHtml() - like useLayoutEffect().

Its usage is similar to useRef().

const builder = () => { ... };

export const Component: React.FC = () => {
  const ref = useHtml(builder);
  
  return <div ref={ref} />;
};

The builder function is passed as an argument, which implements DOM manipulation and returns a cleanup function.

const builder: HtmlBuilder<HTMLDivElement> = element => {
  /* DOM manipulations */
  return () => { /* clean function */ };
};

You can pass multiple builders as an array.

const ref = useHtml([builder1, builder2, builder3]);

Transferred builder need to be memoized or store in variable.

const builder = () => { ... };
/* or const builder = [builder1, builder2, builder3]; */

export const Component: React.FC = () => {
  const ref = useHtml(builder);
  ...
};

You can also pass dependencies as the second argument, if necessary.

const ref = useHtml(builder, [deps1, deps2]);

Lastly, use the html-js utility in builders.

Keywords

FAQs

Last updated on 06 Nov 2023

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc