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

dom-elements-guards-react

v1.0.2

Published

`dom-elements-guards-react` is a lightweight and flexible package that allows you to manage the visibility of HTML elements in a React application using a custom attribute: `data-permission-guard`. It enables you to hide or show elements without creating

Downloads

67

Readme

dom-elements-guards-react

dom-elements-guards-react is a lightweight and flexible package that allows you to manage the visibility of HTML elements in a React application using a custom attribute: data-permission-guard. It enables you to hide or show elements without creating additional components, providing simple and effective integration.

Installation

You can install the package using NPM, Yarn, or PNPM:

NPM

npm install dom-elements-guards-react

Yarn

yarn add dom-elements-guards-react

PNPM

pnpm add dom-elements-guards-react

Usage

1. Import and apply the guards

In your React component, import the applyPermissionGuards function and apply it within a useEffect hook after the DOM has been mounted.

Example:

import React, { useEffect } from 'react';
import { applyPermissionGuards } from 'dom-elements-guards-react';

const App: React.FC = () => {
  useEffect(() => {
    // Apply the guards after the component is mounted
    applyPermissionGuards();
  }, []);

  return (
    <div>
      <p data-permission-guard="true">This paragraph is visible</p>
      <p data-permission-guard="false">This paragraph is hidden</p>
    </div>
  );
};

export default App;

2. How it works

  • Simply add the data-permission-guard attribute to any HTML element in your React application.
  • If the data-permission-guard value is set to "false", the element will be hidden (display: none).
  • If the value is set to "true", the element will be visible.

Example:

<p data-permission-guard={true}>Visible</p>
<p data-permission-guard={false}>Invisible</p>

3. DOM Mutation Support

dom-elements-guards-react also monitors DOM mutations. If you dynamically add new elements with the data-permission-guard attribute, they will automatically be handled without needing to manually reapply the function.

Dynamic Example

If a new element is added after the initial load, it will be handled automatically:

useEffect(() => {
  applyPermissionGuards();

  // Dynamically adding a new element
  const newElement = document.createElement('p');
  newElement.setAttribute('data-permission-guard', 'false');
  newElement.textContent = 'I am invisible after dynamic addition!';
  document.body.appendChild(newElement);
}, []);

API

applyPermissionGuards(): void

This function scans all DOM elements with the data-permission-guard attribute and adjusts their visibility based on the value of this attribute (true or false). It also supports DOM mutations.

Use Cases

  • Dynamically manage display permissions in a React application.
  • Show/hide elements without creating complex conditional components.
  • Control visibility based on user permissions, roles, or other defined criteria in your application.

Benefits

  • Lightweight: No heavy dependencies, just a simple JavaScript/TypeScript function.
  • Easy to use: Simply add an HTML attribute to control visibility.
  • DOM mutation support: Dynamically added elements are automatically handled.
  • No component overhead: Use directly on HTML elements without creating additional components.