npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-body-direction

v1.0.9

Published

A hook to detect change in the body direction style.

Downloads

23

Readme

useBodyDirection

Usage

import useBodyDirection from "use-body-direction";
import { useEffect } from "react";

export default function App() {
  const direction = useBodyDirection();

  useEffect(() => {
    console.log(`The current body direction is '${directoin}'`);
  }, [direction]);
} 

Use Body Direction Hook for React

An example to introduce the MutationObserver class, the original code is available in the src folder as a React Hook which allows the user to detect any change in style property of the body element, especially when change is related to direction style property.

Listen to a DOM attribute change!

A method which introduces how to observe changes on an HTML DOM Element attribute. Sometimes you need to Observe the change of an HTML DOM Element attribute, such as style.

MutationObserver class

MutationObserver class handles a callback which called after a specific moment, the object which created by that is an observer and receives an HTML Element and an attribute to listen.

const observer = new MutationObserver(handleStyleChanged);
observer.observe(document.body, {
  attributeFilter: ["style"],
});

Here the point is detecting body style property changes.

Callback

A callback is triggered when change is detected on filtered attribute, the first argument owns the target DOM element here there is a need to check if the change was being repeated or a new in case of avoid from update UI or do a task twice but useless!

let prevDirection;

function handleStyleChanged(mutations: MutationRecord[]) {
  const newDirection = (mutations[0].target as HTMLBodyElement).style.direction;

  if (newDirection !== prevDirection) {
    prevDirection = document.body.style.direction;
    // Update UI or do somthing!
  }
}

Disconnecting Observer

Be aware of disconnecting created observer! To prevent from the memory leak, disconnect the observer at the end of the work.

observer.disconnect();