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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-access-level

v1.0.3

Published

<h3 align="center"> An awesome library for access level in <code>ReactJS</code> </h3>

Downloads

14

Readme

React ACL

Features

  • Show component if has an role;
  • Edit style of component if has or not an role;
  • Enable if else components for show or not components based in authorities;
  • Provide one way for set inital roles;
  • Edit roles in runtime.

Usage

Around your main component put ReactACLProvider like that.

import React from 'react';
import { ReactACLProvider } from "react-access-level"

function App() {
  return (
    <ReactACLProvider>
      <div>
        my App
      </div>
    </ReactACLProvider>
  );
}

export default App;

The component ReactACLProvider can receive 3 properties:

  • extractInitialRoles: () => string[].

This function extract the initials roles of application, default is [].

  • extractInitialUser: () => object | boolean | string | null.

This function extract the initial user state, default is true.

  • defaultUnauthorizedComponent: React.ReactElement.

This is the default component for show if user not have access.

An user can have roles even not is logged.

import React from 'react';
import { ReactACLProvider } from "react-access-level"
import Home from './Home';

function App() {
  function getInitialUser() {
    return true
  }

  function getInitialRoles() {
    console.log('here')
    return ["user:create", "user:read", "user:update"]
  }

  return (
    <ReactACLProvider
      extractInitialUser={getInitialUser}
      extractInitialRoles={getInitialRoles}
      defaultUnauthorizedComponent={
      <span 
        style={{
          background: 'red',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          width: 400,
          height: 200
        }}>
        DENIED ACCESS 
      </span>}
    >
      <Home />
    </ReactACLProvider>
  );
}

export default App;

Manager Rules

The help component for manager access level was:

  • Logged: Show an component if user is logged;
  • NotLogged: Show an component if user is not logged;
  • Can: Show an component if user have the access.
import {
  Logged,
  NotLogged,
  Can
} from "react-access-level"

Simple example.

export default function Home() {
  return (
    <div>
      <Logged>
        <p>user logged</p>
      </Logged>
      <NotLogged>
        <p>user not logged</p>
      </NotLogged>
    </div>
  )
}

Can component.

<Can>
  <p>no effect</p>
</Can>

<Can resource="user" authority="create">
  <button>Create user</button>
</Can>

With render props

<Can resource="user" authority="destroy">
  {accept => <button disabled={!accept}>delete user</button>}
</Can>

Custom rule.

import { and, or } from "react-access-level"
<Can match={and("user:create", or("user:delete", "user:update"))}>
  <p>it's ok!!</p>
</Can>

Custom unauthorized component

<Logged showAnauthorizedComponent>
  <p>user logged</p>
</Logged>

<Logged otherwiseComponent={<span>DENIED</span>}>
  <p>user logged</p>
</Logged>

<Can resource="user" authority="destroy" showAnauthorizedComponent>
  <button>delete user</button>
</Can>

<Can 
  resource="user"
  authority="destroy"
  otherwiseComponent={<b>CAN'T DELETE USER</b>}
>
  <button>delete user</button>
</Can>

Use hooks.

import React from "react"
import { 
  useLogged, 
  useRules, 
  useLogin, 
  useLogout,
  useManagerRoles
} from "react-access-level"

export default function Hook() {
  const logged = useLogged()
  const roles = useRoles()
  const login = useLogin()
  const logout = useLogout()

  const { addRole, updateRole, deleteRole } = useManagerRoles()

  return (
    <div id="app">
      <h1>{logged ? 'logged' : 'not logged'}</h1>
      <ul id="roles-list">
        {roles.map(role => <li key={role}>{role}</li>)}
      </ul>

      <button
        id="btn-login"
        onClick={() => login(true)}
      >
        Login
      </button>
      <button
        id="btn-logout"
        onClick={() => logout()}
      >
        Logout
      </button>

      <button
        id="btn-addrole"
        onClick={() => addRole('user:read')}
      >
        Add role
      </button>

      <button
        id="btn-updaterole"
        onClick={() => updateRole('user:create', 'user:add')}
      >
        Update role
      </button>
      <button
        id="btn-deleterole"
        onClick={() => deleteRole('user:create')}
      >
        Update role
      </button>
    </div>
  )
}

The component that uses Components or custom hooks must be have an children of ReactACLProvider.