@ttoss/react-feature-flags 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.js +66 -0
- package/dist/index.d.ts +26 -0
- package/package.json +3 -3
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
|
|
3
|
+
// src/useFeatureFlag.ts
|
|
4
|
+
import * as React2 from "react";
|
|
5
|
+
|
|
6
|
+
// src/FeatureFlagsProvider.tsx
|
|
7
|
+
import * as React from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
var FeatureFlagsContext = React.createContext({
|
|
10
|
+
features: [],
|
|
11
|
+
setFeatures: () => {}
|
|
12
|
+
});
|
|
13
|
+
var FeatureFlagsProvider = ({
|
|
14
|
+
children,
|
|
15
|
+
loadFeatures
|
|
16
|
+
}) => {
|
|
17
|
+
const [features, setFeatures] = React.useState([]);
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
loadFeatures?.().then(features2 => {
|
|
20
|
+
return setFeatures(features2);
|
|
21
|
+
});
|
|
22
|
+
}, [loadFeatures]);
|
|
23
|
+
return /* @__PURE__ */jsx(FeatureFlagsContext.Provider, {
|
|
24
|
+
value: {
|
|
25
|
+
features,
|
|
26
|
+
setFeatures
|
|
27
|
+
},
|
|
28
|
+
children
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/useFeatureFlag.ts
|
|
33
|
+
var useFeatureFlag = name => {
|
|
34
|
+
const {
|
|
35
|
+
features
|
|
36
|
+
} = React2.useContext(FeatureFlagsContext);
|
|
37
|
+
return features.includes(name);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/FeatureFlag.tsx
|
|
41
|
+
import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
|
|
42
|
+
var FeatureFlag = ({
|
|
43
|
+
name,
|
|
44
|
+
children,
|
|
45
|
+
fallback = null
|
|
46
|
+
}) => {
|
|
47
|
+
const isEnabled = useFeatureFlag(name);
|
|
48
|
+
if (!isEnabled) {
|
|
49
|
+
return fallback;
|
|
50
|
+
}
|
|
51
|
+
return /* @__PURE__ */jsx2(Fragment, {
|
|
52
|
+
children
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/useUpdateFeatures.ts
|
|
57
|
+
import * as React3 from "react";
|
|
58
|
+
var useUpdateFeatures = () => {
|
|
59
|
+
const {
|
|
60
|
+
setFeatures
|
|
61
|
+
} = React3.useContext(FeatureFlagsContext);
|
|
62
|
+
return {
|
|
63
|
+
updateFeatures: setFeatures
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export { FeatureFlag, FeatureFlagsProvider, useFeatureFlag, useUpdateFeatures };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
|
|
4
|
+
interface FeatureFlagProps {
|
|
5
|
+
name: FeatureFlags;
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
fallback?: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
declare const FeatureFlag: ({ name, children, fallback, }: FeatureFlagProps) => string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null;
|
|
10
|
+
|
|
11
|
+
declare const FeatureFlagsProvider: ({ children, loadFeatures, }: {
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
loadFeatures?: () => Promise<FeatureFlags[]>;
|
|
14
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
15
|
+
|
|
16
|
+
declare const useFeatureFlag: (name: FeatureFlags) => boolean;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* If you want to update the feature flags on React environment
|
|
20
|
+
* (after the initial render and providers), you can use this hook.
|
|
21
|
+
*/
|
|
22
|
+
declare const useUpdateFeatures: () => {
|
|
23
|
+
updateFeatures: (features: FeatureFlags[]) => void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { FeatureFlag, FeatureFlagsProvider, useFeatureFlag, useUpdateFeatures };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-feature-flags",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "React Feature Flags",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"jest": "^29.7.0",
|
|
31
31
|
"react": "^18.3.1",
|
|
32
32
|
"tsup": "^8.3.0",
|
|
33
|
-
"@ttoss/
|
|
34
|
-
"@ttoss/
|
|
33
|
+
"@ttoss/test-utils": "^2.1.16",
|
|
34
|
+
"@ttoss/config": "^1.34.0"
|
|
35
35
|
},
|
|
36
36
|
"keywords": [
|
|
37
37
|
"feature-flags",
|