Skip to content

Commit d8542bc

Browse files
committed
feat: 🎉 Initial commit
0 parents  commit d8542bc

File tree

5 files changed

+290
-0
lines changed

5 files changed

+290
-0
lines changed

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Dependency directories
11+
node_modules/
12+
13+
# TypeScript cache
14+
*.tsbuildinfo
15+
16+
# Optional npm cache directory
17+
.npm
18+
19+
# Optional REPL history
20+
.node_repl_history
21+
22+
# Output of 'npm pack'
23+
*.tgz
24+
25+
# Yarn Integrity file
26+
.yarn-integrity
27+
28+
# dotenv environment variable files
29+
.env
30+
.env.development.local
31+
.env.test.local
32+
.env.production.local
33+
.env.local
34+
35+
# Stores VSCode versions used for testing VSCode extensions
36+
.vscode-test
37+
38+
# yarn v2
39+
.yarn/cache
40+
.yarn/unplugged
41+
.yarn/build-state.yml
42+
.yarn/install-state.gz
43+
.pnp.*

package-lock.json

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "react-use-modal",
3+
"version": "0.0.1",
4+
"description": "A custom hook for easy and quick modal management for React",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/dennib/useModal.git"
12+
},
13+
"keywords": [
14+
"react",
15+
"typescript",
16+
"modal",
17+
"dialog"
18+
],
19+
"author": "Denni Bevilacqua",
20+
"license": "ISC",
21+
"bugs": {
22+
"url": "https://github.com/dennib/useModal/issues"
23+
},
24+
"homepage": "https://github.com/dennib/useModal#readme",
25+
"devDependencies": {
26+
"@types/react": "^18.0.28",
27+
"react": "^18.2.0",
28+
"typescript": "^4.9.5"
29+
}
30+
}

src/useModal.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useState } from 'react';
2+
import { IModalConfig, IUseModalReturn } from './useModal.types';
3+
4+
const defaultConfig: IModalConfig = { open: false };
5+
6+
const useModal = (config?: IModalConfig): IUseModalReturn => {
7+
const [modalConfig, setModalConfig] = useState<IModalConfig>({
8+
...defaultConfig,
9+
...config,
10+
});
11+
12+
const toggleModal = () => {
13+
setModalConfig(prev => ({ ...prev, open: !prev.open }));
14+
};
15+
16+
const showModal = () => setModalConfig(prev => ({ ...prev, open: true }));
17+
18+
const hideModal = () => setModalConfig(prev => ({ ...prev, open: false }));
19+
20+
const updateModalConfig = (config: Partial<IModalConfig>) =>
21+
setModalConfig(prev => ({ ...prev, ...config }));
22+
23+
const extendConfig = {
24+
...modalConfig,
25+
handleClose: hideModal,
26+
};
27+
28+
return {
29+
toggleModal,
30+
showModal,
31+
hideModal,
32+
modalConfig: extendConfig,
33+
updateModalConfig,
34+
setModalConfig,
35+
};
36+
};
37+
export { useModal };

src/useModal.types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ReactNode, Dispatch, SetStateAction, CSSProperties } from "react";
2+
3+
export interface IUseModalReturn {
4+
toggleModal: () => void;
5+
showModal: () => void;
6+
hideModal: () => void;
7+
modalConfig: IModalConfig;
8+
updateModalConfig: (config: Partial<IModalConfig>) => void;
9+
setModalConfig: Dispatch<SetStateAction<IModalConfig>>;
10+
}
11+
export interface IModalConfig {
12+
open?: boolean;
13+
title?: string;
14+
centerContent?: boolean;
15+
showActionsDivider?: boolean;
16+
showCloseIcon?: true;
17+
children?: ReactNode;
18+
buttons?: IModalButton[];
19+
fullWidthButtons?: true;
20+
stretchButtons?: boolean;
21+
}
22+
23+
export interface IModalButton {
24+
text: string;
25+
style: CSSProperties;
26+
onClick?: () => void;
27+
disabled?: boolean;
28+
disableClose?: boolean;
29+
}

0 commit comments

Comments
 (0)