-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |