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

@derhackt/react-permissions

v0.2.3

Published

<!-- Improved compatibility of back to top link: See: https://github.com/othneildrew/Best-README-Template/pull/73 --> <a name="readme-top"></a> <!-- *** Thanks for checking out the Best-README-Template. If you have a suggestion *** that would make thi

Downloads

4

Readme

GitHub contributors GitHub forks GitHub stars GitHub watchers GitHub issues

GitHub release Npm package yearly downloads Types included MIT license

About The Project

react-permmissions can be used to easily manage permissions globally. The package supports the basic permissions view, create, update and delete. The indication of these permissions is done via an object. The configuration and use, as well as retrieving the permissions for the developer should be simplified. This happens via a component which is responsible for rendering embedded components. Furthermore, the package contains a use-hook, through which one gets access to all permissions.

*The readme will be extended in future

Built With

  • Visual Studio Code VisualStudioCode
  • JavaScript JavaScript
  • TypeScript TypeScript
  • ReactJS ReactJS
  • NPM NPM
  • ESLint ESLint

Getting Started

The package can be downloaded via npm and yarn. After download and added to your project you have access to all compnents, hooks and types.

Prerequisites

This package is only for use in ReactJS version 18.2.0 or higher.

Functionality in lower versions is not guaranteed and will not be supported in the future.

We also recommend using TypeScript version 4.9.4 or higher. All types are integrated.

Installation

  1. Install NPM packages

NPM

  • npm
npm install @derhackt/react-permissions@latest
  • yarn
yarn add @derhackt/react-permissions@latest
  1. Wrap your project into the PermissionProvider
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

import { PermissionProvider } from "@derhackt/react-permissions";
import { permissionConfig } from "./permissionconfig"

const root = ReactDOM.createRoot(
    document.getElementById("root") as HTMLElement
);
root.render(
    <React.StrictMode>
        <PermissionProvider config={permissionConfig}>
            <App />
        </PermissionProvider>
    </React.StrictMode>
);
  1. Add your config-file (example: permissionConfig.ts)
import type { TypePermissionContext } from "@derhackt/react-permissions/dist/esm/types/components/provider/PermissionProvider.types";

export const config: TypePermissionContext["config"] = {
    current: {
        permissions: {
            admin: {
                view: true,
                create: true,
                delete: true,
                update: true,
            },
            user: {
                view: true,
                create: false,
                delete: false,
                update: false,
            },
            // You can even include restriction which have no permissions as a default permission.
            // The handlers set not given permissions to false by default without to declare it.
            guest: {
                view: false,
                create: false,
                delete: false,
                update: false,
            }
        },
    },
    no_permissions_needed: true,
    // ...other optional settings
};

Optional:

  • You can import types from the @derhackt/react-permissions/dist/esm/types folder.
  • You can add more settings to your config:

fallback_component component to fallback when view permission is restricted. Type: JSX.Element

Example:

    fallback_component: <div>You don´t have permissions to view this page</div>

If you use react-router-dom you can add the navigation component from there to redirect

own_permission_name Object to change the default naming of the permissions. Type: Record<string, string>

Example:

    own_permission_name: {
        view: "i_can_see";
        create: "i_can_add";
        update: "i_can_change";
        delete: "i_can_remove";
    }

Usage

  • Permission component

The permission component can be used to restrict view permissions. It will automatic render the fallback component from the config if the view permission is restricted.

To use the component with the noPermissionsNeeded property the setting no_permissions_needed in the config have to set to true

Example:

import { Permission } from "@derhackt/react-permissions";

// ...

<Permission noPermissionsNeeded>
    <p>no permission needed. even 'guest' can see this text.</p>
</Permission>
<Permission permissions={["admin"]}>
    <p>only 'admin' have view permission</p>
</Permission>
<Permission permissions={["user"]}>
    <p>only 'user' have view permission</p>
</Permission>
<Permission permissions={["admin", "user"]}>
    <p>'admin' and 'user' have view permission</p>
</Permission>
  • usePermission hook

The hook can be used to restrict serveral handlings and functions. It also return a disable state for easy embbing into disabled-props of components like <button>

Example: usePermission-hook example

Roadmap

  • [x] Adding provider
  • [x] Adding component
  • [x] Adding hook
  • [x] Adding types
  • [x] Adding README.md
  • [ ] Adding DocJS
  • [ ] Adding more usefull components
  • [ ] Adding logged in restriction
  • [ ] Adding native support for react-redux
  • [ ] Adding native support for react-router-dom
  • [ ] Adding native support for component-libraries like MUI
  • [ ] Getting inspired by the community

See the open issues for a full list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Thanks to all lovely contributers

Made with contributors-img.

License

Distributed under the MIT License. See LICENSE.txt for more information.


ReactJS and ReactJS-logo are under Creative Common Attribution 4.0 International license ReactJS-License

Logo modified by AnnoDomine