Skip to content

Commit ee4f0fe

Browse files
committed
feat: 📝 Add TsDoc comments
1 parent 80f4bfa commit ee4f0fe

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/useModal.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
11
import { useState } from "react";
2-
import { IExtendedModalConfig, IModalConfig, IUseModalReturn } from "./useModal.types";
2+
import {
3+
IExtendedModalConfig,
4+
IModalConfig,
5+
IUseModalReturn,
6+
} from "./useModal.types";
37

48
const defaultConfig: IModalConfig = { open: false };
59

10+
/**
11+
* Custom React hook for dynamic modal management
12+
* @param config - The initial configuration object for the modal
13+
* @returns An object containing functions and the current modal configuration
14+
*/
615
const useModal = (config?: IModalConfig): IUseModalReturn => {
16+
/**
17+
* State variable that stores the current configuration of the modal window.
18+
*/
719
const [modalConfig, setModalConfig] = useState<IModalConfig>({
820
...defaultConfig,
921
...config,
1022
});
1123

12-
const toggleModal = () => {
24+
/**
25+
* Toggles the modal between open and closed states
26+
*/
27+
const toggleModal = () =>
1328
setModalConfig((prev) => ({ ...prev, open: !prev.open }));
14-
};
1529

30+
/**
31+
* Shows the modal
32+
*/
1633
const showModal = () => setModalConfig((prev) => ({ ...prev, open: true }));
1734

35+
/**
36+
* Hides the modal
37+
*/
1838
const hideModal = () => setModalConfig((prev) => ({ ...prev, open: false }));
1939

40+
/**
41+
* Updates the configuration of the modal (mergin with current state)
42+
* @param config - The partial configuration object to update
43+
*/
2044
const updateModalConfig = (config: Partial<IModalConfig>) =>
2145
setModalConfig((prev) => ({ ...prev, ...config }));
2246

47+
/**
48+
* Configuration object that extends the current modal configuration with a `handleClose` function.
49+
*/
2350
const extendConfig: IExtendedModalConfig = {
2451
...modalConfig,
2552
handleClose: hideModal,

src/useModal.types.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,63 @@
11
import { ReactNode, Dispatch, SetStateAction, CSSProperties } from "react";
22

3+
/**
4+
* The return type of the `useModal` hook
5+
*/
36
export interface IUseModalReturn {
7+
/** Function to toggle the open state of the modal */
48
toggleModal: () => void;
9+
/** Function to show the modal */
510
showModal: () => void;
11+
/** Function to hide the modal */
612
hideModal: () => void;
13+
/** The current configuration object for the modal */
714
modalConfig: IExtendedModalConfig;
15+
/** Function to update the configuration object for the modal (merge with prev state) */
816
updateModalConfig: (config: Partial<IModalConfig>) => void;
17+
/** Function to replace the entire configuration object for the modal */
918
setModalConfig: Dispatch<SetStateAction<IModalConfig>>;
1019
}
20+
21+
/**
22+
* Configuration object for the modal
23+
*/
1124
export interface IModalConfig {
25+
/** Determines whether the modal is open or closed */
1226
open?: boolean;
27+
/** The title of the modal */
1328
title?: string;
29+
/** Determines whether to show the close icon on the modal */
1430
showCloseIcon?: true;
31+
/** The content to display inside the modal */
1532
children?: ReactNode;
33+
/** An array of buttons to display at the bottom of the modal */
1634
buttons?: IModalButton[];
1735
}
1836

37+
/**
38+
* An extended version of the `IModalConfig` that includes a `handleClose` function
39+
*/
1940
export interface IExtendedModalConfig extends IModalConfig {
2041
handleClose: () => void;
2142
}
43+
44+
/**
45+
* The shape of any buttons you want yo use in the modal
46+
*/
2247
export interface IModalButton {
48+
/** The text to display on the button */
2349
text: string;
50+
/** Optional styling for the button */
2451
style?: CSSProperties;
52+
/** Function to call when the button is clicked */
2553
onClick?: () => void;
54+
/** Determines whether the button is disabled */
2655
disabled?: boolean;
56+
/** Determines whether to close the modal when the button is clicked */
2757
disableClose?: boolean;
2858
}
2959

60+
/**
61+
* Expose a specific type for your modal compoent props, at the moment identical to `IExtendedModalConfig`
62+
*/
3063
export type ModalProps = IExtendedModalConfig;

0 commit comments

Comments
 (0)