josenanodev-react-components-library 0.0.9 → 0.0.10
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/cjs/components/SideBar/SideBar.css +50 -0
- package/dist/cjs/components/SideBar/SideBar.d.ts +8 -0
- package/dist/cjs/components/SideBar/SideBar.js +72 -0
- package/dist/cjs/components/SideBar/types.d.ts +7 -0
- package/dist/cjs/hooks/useOutsideClick.d.ts +6 -0
- package/dist/cjs/hooks/useOutsideClick.js +25 -0
- package/dist/cjs/hooks/useResizeObserver.d.ts +7 -0
- package/dist/cjs/hooks/useResizeObserver.js +30 -0
- package/dist/esm/components/SideBar/SideBar.css +50 -0
- package/dist/esm/components/SideBar/SideBar.d.ts +8 -0
- package/dist/esm/components/SideBar/SideBar.js +44 -0
- package/dist/esm/components/SideBar/types.d.ts +7 -0
- package/dist/esm/hooks/useOutsideClick.d.ts +6 -0
- package/dist/esm/hooks/useOutsideClick.js +23 -0
- package/dist/esm/hooks/useResizeObserver.d.ts +7 -0
- package/dist/esm/hooks/useResizeObserver.js +28 -0
- package/package.json +2 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
.side-bar {
|
|
2
|
+
position: absolute;
|
|
3
|
+
top: 0;
|
|
4
|
+
height: 100%;
|
|
5
|
+
z-index: 2;
|
|
6
|
+
background-color: rgb(253, 253, 253);
|
|
7
|
+
box-shadow: 5px 0px 15px 0px rgb(206 206 206);
|
|
8
|
+
transition: all 0.5s ease-in-out;
|
|
9
|
+
animation: fade-in-side-bar 1s ease-in;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.side-bar .sidebar-close-button {
|
|
13
|
+
position: absolute;
|
|
14
|
+
top: 0;
|
|
15
|
+
height: 30px;
|
|
16
|
+
width: 30px;
|
|
17
|
+
background-color: transparent;
|
|
18
|
+
border: none;
|
|
19
|
+
outline: none;
|
|
20
|
+
transition: all 0.3s;
|
|
21
|
+
color: dimgray;
|
|
22
|
+
font-size: 30px;
|
|
23
|
+
display: flex;
|
|
24
|
+
align-items: center;
|
|
25
|
+
justify-content: center;
|
|
26
|
+
cursor: pointer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.side-bar .sidebar-close-button:hover {
|
|
30
|
+
color: var(--primary-color);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.side-bar .sidebar-close-button svg.open{
|
|
34
|
+
transform: rotate(0deg);
|
|
35
|
+
transition: all 0.5s;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.side-bar .sidebar-close-button svg.close{
|
|
39
|
+
transform: rotate(180deg);
|
|
40
|
+
transition: all 0.5s;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@keyframes fade-in-side-bar {
|
|
44
|
+
from {
|
|
45
|
+
opacity: 0;
|
|
46
|
+
}
|
|
47
|
+
to {
|
|
48
|
+
opacity: 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import "./SideBar.css";
|
|
3
|
+
import { SideBarPropsType } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* NOTE: Parent Node must have position: relative, to work correctly with the side bar
|
|
6
|
+
*/
|
|
7
|
+
declare const SideBar: ({ side, open, children, closeAction, outBoundClickClosesSideBar, }: SideBarPropsType) => JSX.Element;
|
|
8
|
+
export default SideBar;
|
|
@@ -0,0 +1,72 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const react_1 = __importStar(require("react"));
|
|
30
|
+
require("./SideBar.css");
|
|
31
|
+
//Hooks
|
|
32
|
+
const useOutsideClick_1 = __importDefault(require("../../hooks/useOutsideClick"));
|
|
33
|
+
const useResizeObserver_1 = __importDefault(require("../../hooks/useResizeObserver"));
|
|
34
|
+
//Icons
|
|
35
|
+
const bs_1 = require("react-icons/bs");
|
|
36
|
+
/**
|
|
37
|
+
* NOTE: Parent Node must have position: relative, to work correctly with the side bar
|
|
38
|
+
*/
|
|
39
|
+
const SideBar = ({ side, open = false, children, closeAction, outBoundClickClosesSideBar, }) => {
|
|
40
|
+
//Refs
|
|
41
|
+
const sideBarRef = (0, react_1.useRef)(null);
|
|
42
|
+
//Hooks
|
|
43
|
+
const [elementWidth] = (0, useResizeObserver_1.default)(sideBarRef);
|
|
44
|
+
(0, useOutsideClick_1.default)(sideBarRef, () => {
|
|
45
|
+
if (outBoundClickClosesSideBar)
|
|
46
|
+
if (closeAction) {
|
|
47
|
+
closeAction();
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
setOpenState(false);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
//useState
|
|
54
|
+
const [openState, setOpenState] = (0, react_1.useState)(open);
|
|
55
|
+
return (react_1.default.createElement("div", { ref: sideBarRef, className: "side-bar", style: {
|
|
56
|
+
[side === "left" ? "right" : "left"]: (closeAction && open) || (!closeAction && openState)
|
|
57
|
+
? `calc(100% - ${elementWidth}px)`
|
|
58
|
+
: closeAction
|
|
59
|
+
? "100%"
|
|
60
|
+
: `calc(100% - 30px)`,
|
|
61
|
+
} },
|
|
62
|
+
react_1.default.createElement("button", { style: side === "left" ? { right: 0 } : { left: 0 }, className: "sidebar-close-button", onClick: () => {
|
|
63
|
+
if (closeAction) {
|
|
64
|
+
closeAction();
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
setOpenState(!openState);
|
|
68
|
+
}
|
|
69
|
+
} }, side === "left" ? (react_1.default.createElement(bs_1.BsChevronLeft, { className: (closeAction && open) || (!closeAction && openState) ? "open" : "close" })) : (react_1.default.createElement(bs_1.BsChevronRight, { className: (closeAction && open) || (!closeAction && openState) ? "open" : "close" }))),
|
|
70
|
+
children));
|
|
71
|
+
};
|
|
72
|
+
exports.default = SideBar;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
/**
|
|
5
|
+
* Hook that alerts clicks outside of the passed ref
|
|
6
|
+
*/
|
|
7
|
+
function useOutsideClick(elementRef, onOutsideClickAction) {
|
|
8
|
+
(0, react_1.useEffect)(() => {
|
|
9
|
+
/**
|
|
10
|
+
* Alert if clicked on outside of element
|
|
11
|
+
*/
|
|
12
|
+
function handleClickOutside(event) {
|
|
13
|
+
if (elementRef.current && !elementRef.current.contains(event.target)) {
|
|
14
|
+
onOutsideClickAction();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// Bind the event listener
|
|
18
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
19
|
+
return () => {
|
|
20
|
+
// Unbind the event listener on clean up
|
|
21
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
22
|
+
};
|
|
23
|
+
}, [elementRef, onOutsideClickAction]);
|
|
24
|
+
}
|
|
25
|
+
exports.default = useOutsideClick;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
/**
|
|
3
|
+
* @returns Array [elementWidth, elementHeight], that represent the current
|
|
4
|
+
* values of width and height of elementRef
|
|
5
|
+
*/
|
|
6
|
+
declare function useResizeObserver(elementRef: React.MutableRefObject<HTMLElement | null>): number[];
|
|
7
|
+
export default useResizeObserver;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
/**
|
|
5
|
+
* @returns Array [elementWidth, elementHeight], that represent the current
|
|
6
|
+
* values of width and height of elementRef
|
|
7
|
+
*/
|
|
8
|
+
function useResizeObserver(elementRef) {
|
|
9
|
+
const [elementWidth, setElementWidth] = (0, react_1.useState)(0);
|
|
10
|
+
const [elementHeight, setElementHeight] = (0, react_1.useState)(0);
|
|
11
|
+
const observer = (0, react_1.useRef)(new ResizeObserver((entries) => {
|
|
12
|
+
const { width, height } = entries[0].contentRect;
|
|
13
|
+
setElementWidth(width);
|
|
14
|
+
setElementHeight(height);
|
|
15
|
+
}));
|
|
16
|
+
(0, react_1.useEffect)(() => {
|
|
17
|
+
if (elementRef.current !== null) {
|
|
18
|
+
observer.current.observe(elementRef.current);
|
|
19
|
+
}
|
|
20
|
+
const observerCurrentForCleanUp = observer.current;
|
|
21
|
+
const elementRefCurrentForCleanUp = elementRef.current;
|
|
22
|
+
return () => {
|
|
23
|
+
if (elementRefCurrentForCleanUp !== null) {
|
|
24
|
+
observerCurrentForCleanUp.unobserve(elementRefCurrentForCleanUp);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}, [elementRef, observer]);
|
|
28
|
+
return [elementWidth, elementHeight];
|
|
29
|
+
}
|
|
30
|
+
exports.default = useResizeObserver;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
.side-bar {
|
|
2
|
+
position: absolute;
|
|
3
|
+
top: 0;
|
|
4
|
+
height: 100%;
|
|
5
|
+
z-index: 2;
|
|
6
|
+
background-color: rgb(253, 253, 253);
|
|
7
|
+
box-shadow: 5px 0px 15px 0px rgb(206 206 206);
|
|
8
|
+
transition: all 0.5s ease-in-out;
|
|
9
|
+
animation: fade-in-side-bar 1s ease-in;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.side-bar .sidebar-close-button {
|
|
13
|
+
position: absolute;
|
|
14
|
+
top: 0;
|
|
15
|
+
height: 30px;
|
|
16
|
+
width: 30px;
|
|
17
|
+
background-color: transparent;
|
|
18
|
+
border: none;
|
|
19
|
+
outline: none;
|
|
20
|
+
transition: all 0.3s;
|
|
21
|
+
color: dimgray;
|
|
22
|
+
font-size: 30px;
|
|
23
|
+
display: flex;
|
|
24
|
+
align-items: center;
|
|
25
|
+
justify-content: center;
|
|
26
|
+
cursor: pointer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.side-bar .sidebar-close-button:hover {
|
|
30
|
+
color: var(--primary-color);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.side-bar .sidebar-close-button svg.open{
|
|
34
|
+
transform: rotate(0deg);
|
|
35
|
+
transition: all 0.5s;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.side-bar .sidebar-close-button svg.close{
|
|
39
|
+
transform: rotate(180deg);
|
|
40
|
+
transition: all 0.5s;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@keyframes fade-in-side-bar {
|
|
44
|
+
from {
|
|
45
|
+
opacity: 0;
|
|
46
|
+
}
|
|
47
|
+
to {
|
|
48
|
+
opacity: 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import "./SideBar.css";
|
|
3
|
+
import { SideBarPropsType } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* NOTE: Parent Node must have position: relative, to work correctly with the side bar
|
|
6
|
+
*/
|
|
7
|
+
declare const SideBar: ({ side, open, children, closeAction, outBoundClickClosesSideBar, }: SideBarPropsType) => JSX.Element;
|
|
8
|
+
export default SideBar;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React, { useRef, useState } from "react";
|
|
2
|
+
import "./SideBar.css";
|
|
3
|
+
//Hooks
|
|
4
|
+
import useOutsideClick from "../../hooks/useOutsideClick";
|
|
5
|
+
import useResizeObserver from "../../hooks/useResizeObserver";
|
|
6
|
+
//Icons
|
|
7
|
+
import { BsChevronLeft, BsChevronRight } from "react-icons/bs";
|
|
8
|
+
/**
|
|
9
|
+
* NOTE: Parent Node must have position: relative, to work correctly with the side bar
|
|
10
|
+
*/
|
|
11
|
+
const SideBar = ({ side, open = false, children, closeAction, outBoundClickClosesSideBar, }) => {
|
|
12
|
+
//Refs
|
|
13
|
+
const sideBarRef = useRef(null);
|
|
14
|
+
//Hooks
|
|
15
|
+
const [elementWidth] = useResizeObserver(sideBarRef);
|
|
16
|
+
useOutsideClick(sideBarRef, () => {
|
|
17
|
+
if (outBoundClickClosesSideBar)
|
|
18
|
+
if (closeAction) {
|
|
19
|
+
closeAction();
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
setOpenState(false);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
//useState
|
|
26
|
+
const [openState, setOpenState] = useState(open);
|
|
27
|
+
return (React.createElement("div", { ref: sideBarRef, className: "side-bar", style: {
|
|
28
|
+
[side === "left" ? "right" : "left"]: (closeAction && open) || (!closeAction && openState)
|
|
29
|
+
? `calc(100% - ${elementWidth}px)`
|
|
30
|
+
: closeAction
|
|
31
|
+
? "100%"
|
|
32
|
+
: `calc(100% - 30px)`,
|
|
33
|
+
} },
|
|
34
|
+
React.createElement("button", { style: side === "left" ? { right: 0 } : { left: 0 }, className: "sidebar-close-button", onClick: () => {
|
|
35
|
+
if (closeAction) {
|
|
36
|
+
closeAction();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
setOpenState(!openState);
|
|
40
|
+
}
|
|
41
|
+
} }, side === "left" ? (React.createElement(BsChevronLeft, { className: (closeAction && open) || (!closeAction && openState) ? "open" : "close" })) : (React.createElement(BsChevronRight, { className: (closeAction && open) || (!closeAction && openState) ? "open" : "close" }))),
|
|
42
|
+
children));
|
|
43
|
+
};
|
|
44
|
+
export default SideBar;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Hook that alerts clicks outside of the passed ref
|
|
4
|
+
*/
|
|
5
|
+
function useOutsideClick(elementRef, onOutsideClickAction) {
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
/**
|
|
8
|
+
* Alert if clicked on outside of element
|
|
9
|
+
*/
|
|
10
|
+
function handleClickOutside(event) {
|
|
11
|
+
if (elementRef.current && !elementRef.current.contains(event.target)) {
|
|
12
|
+
onOutsideClickAction();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// Bind the event listener
|
|
16
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
17
|
+
return () => {
|
|
18
|
+
// Unbind the event listener on clean up
|
|
19
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
20
|
+
};
|
|
21
|
+
}, [elementRef, onOutsideClickAction]);
|
|
22
|
+
}
|
|
23
|
+
export default useOutsideClick;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
/**
|
|
3
|
+
* @returns Array [elementWidth, elementHeight], that represent the current
|
|
4
|
+
* values of width and height of elementRef
|
|
5
|
+
*/
|
|
6
|
+
declare function useResizeObserver(elementRef: React.MutableRefObject<HTMLElement | null>): number[];
|
|
7
|
+
export default useResizeObserver;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useState, useRef, useEffect } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* @returns Array [elementWidth, elementHeight], that represent the current
|
|
4
|
+
* values of width and height of elementRef
|
|
5
|
+
*/
|
|
6
|
+
function useResizeObserver(elementRef) {
|
|
7
|
+
const [elementWidth, setElementWidth] = useState(0);
|
|
8
|
+
const [elementHeight, setElementHeight] = useState(0);
|
|
9
|
+
const observer = useRef(new ResizeObserver((entries) => {
|
|
10
|
+
const { width, height } = entries[0].contentRect;
|
|
11
|
+
setElementWidth(width);
|
|
12
|
+
setElementHeight(height);
|
|
13
|
+
}));
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (elementRef.current !== null) {
|
|
16
|
+
observer.current.observe(elementRef.current);
|
|
17
|
+
}
|
|
18
|
+
const observerCurrentForCleanUp = observer.current;
|
|
19
|
+
const elementRefCurrentForCleanUp = elementRef.current;
|
|
20
|
+
return () => {
|
|
21
|
+
if (elementRefCurrentForCleanUp !== null) {
|
|
22
|
+
observerCurrentForCleanUp.unobserve(elementRefCurrentForCleanUp);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}, [elementRef, observer]);
|
|
26
|
+
return [elementWidth, elementHeight];
|
|
27
|
+
}
|
|
28
|
+
export default useResizeObserver;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "josenanodev-react-components-library",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"author": "Jose Carlos Cardenas Martinez",
|
|
5
5
|
"description": "Libreria de componentes React personales",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@babel/core": "^7.20.12",
|
|
19
19
|
"@babel/preset-typescript": "^7.17.12",
|
|
20
|
+
"@babel/runtime": "^7.20.7",
|
|
20
21
|
"@storybook/addon-actions": "^6.5.15",
|
|
21
22
|
"@storybook/addon-essentials": "^6.5.15",
|
|
22
23
|
"@storybook/addon-interactions": "^6.5.15",
|