react-essentials-functions 1.0.0
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/LICENCE +21 -0
- package/README.md +13 -0
- package/build/components/ConditionnalWrapper.d.ts +7 -0
- package/build/components/ConditionnalWrapper.js +11 -0
- package/build/components/index.d.ts +1 -0
- package/build/components/index.js +17 -0
- package/build/hooks/index.d.ts +6 -0
- package/build/hooks/index.js +22 -0
- package/build/hooks/useDimensions.d.ts +5 -0
- package/build/hooks/useDimensions.js +35 -0
- package/build/hooks/useSafeFetch.d.ts +1 -0
- package/build/hooks/useSafeFetch.js +10 -0
- package/build/hooks/useSafeState.d.ts +1 -0
- package/build/hooks/useSafeState.js +20 -0
- package/build/hooks/useScript.d.ts +1 -0
- package/build/hooks/useScript.js +16 -0
- package/build/hooks/useTheme.d.ts +2 -0
- package/build/hooks/useTheme.js +22 -0
- package/build/hooks/useWindowDimensions.d.ts +4 -0
- package/build/hooks/useWindowDimensions.js +27 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +18 -0
- package/package.json +93 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Maxime Golfier
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
export declare type ConditionnalWrapperProps = {
|
|
3
|
+
condition: boolean;
|
|
4
|
+
wrapper: (children: React.ReactNode) => React.ReactNode;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
};
|
|
7
|
+
export declare const ConditionalWrapper: ({ condition, wrapper, children, }: ConditionnalWrapperProps) => React.ReactNode;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConditionalWrapper = void 0;
|
|
4
|
+
const ConditionalWrapper = ({ condition, wrapper, children, }) => (condition ? wrapper(children) : children);
|
|
5
|
+
exports.ConditionalWrapper = ConditionalWrapper;
|
|
6
|
+
// <ConditionalWrapper
|
|
7
|
+
// condition={link}
|
|
8
|
+
// wrapper={children => <a href={link}>{children}</a>}
|
|
9
|
+
// >
|
|
10
|
+
// .....
|
|
11
|
+
// </ConditionalWrapper>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ConditionnalWrapper';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ConditionnalWrapper"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useDimensions"), exports);
|
|
18
|
+
__exportStar(require("./useSafeFetch"), exports);
|
|
19
|
+
__exportStar(require("./useSafeState"), exports);
|
|
20
|
+
__exportStar(require("./useScript"), exports);
|
|
21
|
+
__exportStar(require("./useTheme"), exports);
|
|
22
|
+
__exportStar(require("./useWindowDimensions"), exports);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDimensions = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useDimensions(targetRef) {
|
|
6
|
+
const getDimensions = () => {
|
|
7
|
+
return {
|
|
8
|
+
width: targetRef.current ? targetRef.current.offsetWidth : 0,
|
|
9
|
+
height: targetRef.current ? targetRef.current.offsetHeight : 0,
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
const [dimensions, setDimensions] = (0, react_1.useState)(getDimensions);
|
|
13
|
+
const handleResize = () => {
|
|
14
|
+
setDimensions(getDimensions());
|
|
15
|
+
};
|
|
16
|
+
(0, react_1.useEffect)(() => {
|
|
17
|
+
window.addEventListener('resize', handleResize);
|
|
18
|
+
window.addEventListener('scroll', handleResize);
|
|
19
|
+
window.addEventListener('load', handleResize);
|
|
20
|
+
return () => {
|
|
21
|
+
window.removeEventListener('resize', handleResize);
|
|
22
|
+
window.removeEventListener('scroll', handleResize);
|
|
23
|
+
window.addEventListener('load', handleResize);
|
|
24
|
+
};
|
|
25
|
+
}, []);
|
|
26
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
27
|
+
handleResize();
|
|
28
|
+
}, []);
|
|
29
|
+
return dimensions;
|
|
30
|
+
}
|
|
31
|
+
exports.useDimensions = useDimensions;
|
|
32
|
+
// const targetRef = useRef() // then set a ref to your component
|
|
33
|
+
// const size = useDimensions(targetRef)
|
|
34
|
+
// ....
|
|
35
|
+
// return (div ref={targetRef}> .... <p>{size.width} - {size.height} </p> .... </div>);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useSafeFetch(): (url: string, options: Record<any, any>) => Promise<Response>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSafeFetch = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useSafeFetch() {
|
|
6
|
+
const abortController = (0, react_1.useRef)(new AbortController());
|
|
7
|
+
(0, react_1.useEffect)(() => () => abortController.current.abort(), []);
|
|
8
|
+
return (0, react_1.useCallback)((url, options) => fetch(url, { signal: abortController.current.signal, ...options }), []);
|
|
9
|
+
}
|
|
10
|
+
exports.useSafeFetch = useSafeFetch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useSafeState(initialValue?: null): (((value: any) => void) | null)[];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSafeState = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useSafeState(initialValue = null) {
|
|
6
|
+
const isMounted = (0, react_1.useRef)(true);
|
|
7
|
+
const [state, setState] = (0, react_1.useState)(initialValue);
|
|
8
|
+
(0, react_1.useEffect)(() => {
|
|
9
|
+
return () => {
|
|
10
|
+
isMounted.current = false;
|
|
11
|
+
};
|
|
12
|
+
}, []);
|
|
13
|
+
const setStateSafe = (0, react_1.useCallback)((value) => {
|
|
14
|
+
if (isMounted.current) {
|
|
15
|
+
setState(value);
|
|
16
|
+
}
|
|
17
|
+
}, []);
|
|
18
|
+
return [state, setStateSafe];
|
|
19
|
+
}
|
|
20
|
+
exports.useSafeState = useSafeState;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useScript: (url: string) => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useScript = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useScript = (url) => {
|
|
6
|
+
(0, react_1.useEffect)(() => {
|
|
7
|
+
const script = document.createElement('script');
|
|
8
|
+
script.src = url;
|
|
9
|
+
script.async = true;
|
|
10
|
+
document.body.appendChild(script);
|
|
11
|
+
return () => {
|
|
12
|
+
document.body.removeChild(script);
|
|
13
|
+
};
|
|
14
|
+
}, [url]);
|
|
15
|
+
};
|
|
16
|
+
exports.useScript = useScript;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useTheme = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useTheme = () => {
|
|
6
|
+
const [theme, setTheme] = (0, react_1.useState)('light');
|
|
7
|
+
const [mountedComponent, setMountedComponent] = (0, react_1.useState)(false);
|
|
8
|
+
const setMode = (mode) => {
|
|
9
|
+
window.localStorage.setItem('theme', mode);
|
|
10
|
+
setTheme(mode);
|
|
11
|
+
};
|
|
12
|
+
const themeToggler = () => {
|
|
13
|
+
theme === 'light' ? setMode('dark') : setMode('light');
|
|
14
|
+
};
|
|
15
|
+
(0, react_1.useEffect)(() => {
|
|
16
|
+
const localTheme = window.localStorage.getItem('theme');
|
|
17
|
+
localTheme ? setTheme(localTheme) : setMode('light');
|
|
18
|
+
setMountedComponent(true);
|
|
19
|
+
}, []);
|
|
20
|
+
return [theme, themeToggler, mountedComponent];
|
|
21
|
+
};
|
|
22
|
+
exports.useTheme = useTheme;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
function useWindowDimensions() {
|
|
5
|
+
const hasWindow = typeof window !== 'undefined';
|
|
6
|
+
function getWindowDimensions() {
|
|
7
|
+
const width = hasWindow ? window.innerWidth : null;
|
|
8
|
+
const height = hasWindow ? window.innerHeight : null;
|
|
9
|
+
return {
|
|
10
|
+
width,
|
|
11
|
+
height,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const [windowDimensions, setWindowDimensions] = (0, react_1.useState)(getWindowDimensions());
|
|
15
|
+
(0, react_1.useEffect)(() => {
|
|
16
|
+
if (hasWindow) {
|
|
17
|
+
// eslint-disable-next-line no-inner-declarations
|
|
18
|
+
function handleResize() {
|
|
19
|
+
setWindowDimensions(getWindowDimensions());
|
|
20
|
+
}
|
|
21
|
+
window.addEventListener('resize', handleResize);
|
|
22
|
+
return () => window.removeEventListener('resize', handleResize);
|
|
23
|
+
}
|
|
24
|
+
}, [hasWindow]);
|
|
25
|
+
return windowDimensions;
|
|
26
|
+
}
|
|
27
|
+
exports.default = useWindowDimensions;
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./components"), exports);
|
|
18
|
+
__exportStar(require("./hooks"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-essentials-functions",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "maxgfr",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "A collection of zero-dependencies useful hooks and components for React",
|
|
7
|
+
"main": "./build/index.js",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/maxgfr/react-essentials-functions.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/maxgfr/react-essentials-functions/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/maxgfr/react-essentials-functions#readme",
|
|
16
|
+
"files": [
|
|
17
|
+
"build"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"hooks",
|
|
21
|
+
"react",
|
|
22
|
+
"components",
|
|
23
|
+
"useful"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "nodemon",
|
|
27
|
+
"develop": "node -r @swc-node/register ./src/index.ts",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch",
|
|
30
|
+
"clean": "rimraf build",
|
|
31
|
+
"build": "tsc -p tsconfig.build.json",
|
|
32
|
+
"build:watch": "tsc -w -p tsconfig.build.json",
|
|
33
|
+
"build:swc": "swc ./src -d build",
|
|
34
|
+
"build:swc:watch": "swc ./src -d build -w",
|
|
35
|
+
"lint": "eslint ./src --ext .ts,.tsx",
|
|
36
|
+
"prettier": "prettier --write './src/**/*.{ts,js,json,tsx,jsx}'",
|
|
37
|
+
"storybook:start": "start-storybook -p 6006",
|
|
38
|
+
"storybook:build": "build-storybook",
|
|
39
|
+
"storybook:deploy": "npm run storybook:build && gh-pages -d storybook-static",
|
|
40
|
+
"storybook:deploy:action": "npm run storybook:build && gh-pages -d storybook-static -u \"github-actions-bot <support+actions@github.com>\"",
|
|
41
|
+
"release": "semantic-release"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": "*",
|
|
45
|
+
"react-dom": "*"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@babel/core": "^7.20.2",
|
|
49
|
+
"@semantic-release/changelog": "^6.0.2",
|
|
50
|
+
"@semantic-release/commit-analyzer": "^9.0.2",
|
|
51
|
+
"@semantic-release/git": "^10.0.1",
|
|
52
|
+
"@semantic-release/github": "^8.0.6",
|
|
53
|
+
"@semantic-release/npm": "^9.0.1",
|
|
54
|
+
"@semantic-release/release-notes-generator": "^10.0.3",
|
|
55
|
+
"@storybook/addon-actions": "^6.5.13",
|
|
56
|
+
"@storybook/addon-essentials": "^6.5.13",
|
|
57
|
+
"@storybook/addon-interactions": "^6.5.13",
|
|
58
|
+
"@storybook/addon-links": "^6.5.13",
|
|
59
|
+
"@storybook/builder-webpack4": "^6.5.13",
|
|
60
|
+
"@storybook/manager-webpack4": "^6.5.13",
|
|
61
|
+
"@storybook/react": "^6.5.13",
|
|
62
|
+
"@storybook/testing-library": "^0.0.13",
|
|
63
|
+
"@swc-node/register": "1.5.4",
|
|
64
|
+
"@swc/cli": "0.1.57",
|
|
65
|
+
"@swc/core": "1.3.19",
|
|
66
|
+
"@swc/jest": "0.2.23",
|
|
67
|
+
"@swc/plugin-styled-jsx": "^1.5.20",
|
|
68
|
+
"@testing-library/jest-dom": "^5.16.5",
|
|
69
|
+
"@testing-library/react": "^13.4.0",
|
|
70
|
+
"@testing-library/user-event": "^14.4.3",
|
|
71
|
+
"@types/jest": "29.2.3",
|
|
72
|
+
"@types/node": "18.11.9",
|
|
73
|
+
"@types/react": "^18.0.25",
|
|
74
|
+
"@types/react-dom": "^18.0.8",
|
|
75
|
+
"@typescript-eslint/eslint-plugin": "5.43.0",
|
|
76
|
+
"@typescript-eslint/parser": "5.43.0",
|
|
77
|
+
"babel-loader": "^9.0.0",
|
|
78
|
+
"eslint": "8.28.0",
|
|
79
|
+
"eslint-config-prettier": "8.5.0",
|
|
80
|
+
"eslint-plugin-jest": "27.1.5",
|
|
81
|
+
"eslint-plugin-jsx-a11y": "^6.6.1",
|
|
82
|
+
"eslint-plugin-react": "^7.31.10",
|
|
83
|
+
"eslint-plugin-storybook": "^0.6.7",
|
|
84
|
+
"gh-pages": "^4.0.0",
|
|
85
|
+
"jest": "29.3.1",
|
|
86
|
+
"jest-environment-jsdom": "^29.3.0",
|
|
87
|
+
"nodemon": "2.0.20",
|
|
88
|
+
"prettier": "2.7.1",
|
|
89
|
+
"rimraf": "3.0.2",
|
|
90
|
+
"semantic-release": "^19.0.5",
|
|
91
|
+
"typescript": "^4.8.4"
|
|
92
|
+
}
|
|
93
|
+
}
|