Skip to content

Commit 983d6ba

Browse files
committed
🎉 initial commit
0 parents  commit 983d6ba

34 files changed

Lines changed: 7277 additions & 0 deletions

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
lib

.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: [
5+
'@typescript-eslint',
6+
],
7+
extends: [
8+
'eslint:recommended',
9+
'plugin:@typescript-eslint/recommended',
10+
],
11+
};

.github/workflows/jest.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: CI
2+
on: push
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Install modules
9+
run: yarn
10+
- name: Run tests
11+
run: yarn jest --json --outputFile=result.json --testLocationInResults
12+
- uses: tanmen/jest-reporter@v1
13+
if: always()
14+
with:
15+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/npm-publish.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3+
4+
name: Publish package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v1
16+
with:
17+
node-version: 12
18+
registry-url: https://registry.npmjs.org
19+
- run: npm i
20+
- run: npm publish
21+
env:
22+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
lib
3+
coverage

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__test__

Readme.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# React permission gate
2+
inspired by https://isamatov.com/react-permissions-and-roles/
3+
4+
Easily render or hide pieces of UI relative to the user's access role.
5+
6+
### Supports Typescript and Flow type
7+
8+
9+
### Example
10+
full example [here](example)
11+
```javascript
12+
import { FeatureGateProvider } from 'feature-gate';
13+
14+
// define or get from api features and freeze them
15+
const features = Object.freeze({
16+
feature1: 'true',
17+
ABtest: 'A',
18+
});
19+
20+
function MyApp() {
21+
const featureFlags = {}; // get from user api
22+
23+
return (
24+
<FeatureGateProvider featureFlags={featureFlags} features={features}>
25+
<App />
26+
</FeatureGateProvider>
27+
)
28+
}
29+
```
30+
then anywhere in the app use names of features defined in the features map
31+
32+
```javascript
33+
import { FeatureGate } from 'feature-gate';
34+
35+
<FeatureGate name="feature1">
36+
<div>Component available for authorized user</div>
37+
</FeatureGate>
38+
39+
```
40+
```javascript
41+
import { FeatureSwitch } from 'feature-gate';
42+
43+
<FeatureSwitch name="ABtest" fallback={<div>B test</div>}>
44+
<div>A test</div>
45+
</FeatureSwitch>
46+
47+
```
48+
or use hook
49+
```javascript
50+
import { useFeature } from 'feature-gate';
51+
...
52+
53+
const { enabled: showFeature1 } = useFeature('feature1');
54+
// feature status for the current user
55+
...
56+
{showFeature1 && <div>Component available for authorized user</div>}
57+
```
58+
### Advanced usage
59+
A validator function can be provided
60+
```javascript
61+
import { FeatureGateProvider } from 'feature-gate';
62+
...
63+
64+
// define or get from api rules and freeze them
65+
const features = Object.freeze({
66+
feature1: 'true',
67+
ABtest: 'A',
68+
});
69+
70+
function validator({ featureFlags, features, name }) {
71+
// default validator implementation
72+
const feature = featureFlags[name];
73+
if (!feature) return false;
74+
75+
return features[name] === feature;
76+
}
77+
78+
function MyApp() {
79+
const featureFlags = {}; // get from user api
80+
81+
return (
82+
<FeatureGateProvider featureFlags={featureFlags} features={features} validator={validator}>
83+
<App />
84+
</FeatureGateProvider>
85+
)
86+
}
87+
```

example/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next"
3+
}

example/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel

example/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TypeScript Next.js example
2+
3+
This is an example of using react-feature-gate
4+
5+
## Preview
6+
7+
Preview the example live on [StackBlitz](http://stackblitz.com/):
8+
9+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/w01fgang/react-feature-gate/tree/main/example)
10+
11+
## Deploy your own
12+
13+
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
14+
15+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/w01fgang/react-feature-gate/tree/main/example&project-name=react-feature-gate&repository-name=react-feature-gate)

0 commit comments

Comments
 (0)