Skip to content

Commit

Permalink
Add: modal hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Nov 22, 2024
1 parent 25d69b0 commit 96f419d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/hooks/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { PrimitiveAtom, useAtom } from "jotai";
import { useCallback } from "react";

//-- Detail Modal
/**
* データを持つモーダル
* モーダルを開いた際にデータをセットする
*
* @template T モーダルとして保持するデータの型
*/
export type DetailModal<T> = [
data: T,
/**
* モーダルを開き、データをセットする
* nullをセットするとモーダルを閉じる
* @param {T} data
* @returns {void}
*/
setData: (data: T) => void,
];

export const useDetailModal = <T>(atom: PrimitiveAtom<T>): DetailModal<T> => {
const [internalData, setInternalData] = useAtom<T>(atom);
const setData = useCallback((data: T): void => setInternalData(data), [setInternalData]);
return [internalData, setData];
};

//-- Simple Modal
/**
* Booleanで開閉するシンプルなモーダル
*/
export type SimpleModal = [isOpen: boolean, setOpen: (open: boolean) => void, toggle: () => void];

/**
* Booleanで開閉するシンプルなモーダルを作成する
* @param {boolean} initialOpen
* @returns {SimpleModal}
*/
export const useSimpleModal = (atom: PrimitiveAtom<boolean>): SimpleModal => {
const [isOpen, setOpen] = useDetailModal(atom);
const toggle = useCallback(() => setOpen(!isOpen), [isOpen, setOpen]);
return [isOpen, setOpen, toggle];
};

0 comments on commit 96f419d

Please sign in to comment.