Skip to content

Commit

Permalink
feat: add useModal custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kcwww committed Sep 24, 2024
1 parent ebb32da commit c5797a8
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 18 deletions.
13 changes: 11 additions & 2 deletions app/(landing)/_components/IntroButtonSection.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import TestModal from '@/shared/components/modals/testModal';
'use client';

import { Button } from '@/components/ui/button';
import useModal from '@/shared/hooks/useModal';

const IntroButtonSection = () => {
return <TestModal />;
const { onOpen } = useModal();

return (
<Button className="pointer-events-auto" onClick={() => onOpen('Intro')}>
Get Started
</Button>
);
};

export default IntroButtonSection;
5 changes: 0 additions & 5 deletions components/ui/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import * as React from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { X } from 'lucide-react';

import { cn } from '@/lib/utils';

Expand Down Expand Up @@ -44,10 +43,6 @@ const DialogContent = React.forwardRef<
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
));
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7",
"three": "^0.164.1",
"vaul": "^0.9.4"
"vaul": "^0.9.4",
"zustand": "5.0.0-rc.2"
},
"devDependencies": {
"@storybook/addon-essentials": "^8.1.5",
Expand Down
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
'use client';

import useModal from '@/shared/hooks/useModal';

import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogFooter,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';

const IntroduceModal = () => {
const { isOpen, onClose } = useModal();

if (!isOpen) {
return null;
}

const TestModal = () => {
return (
<Dialog>
<DialogTrigger
style={{ pointerEvents: 'auto' }}
className="text-yellow-300"
>
Open
</DialogTrigger>
<Dialog open={isOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Are you absolutely sure?</DialogTitle>
Expand All @@ -26,9 +29,12 @@ const TestModal = () => {
account and remove your data from our servers.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button onClick={() => onClose()}>Cancel</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};

export default TestModal;
export default IntroduceModal;
3 changes: 3 additions & 0 deletions shared/components/ui/UISection.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ModalProvider from '@/shared/provider/ModalProvider';

const UISection = ({ children }: { children: React.ReactNode }) => {
return (
<section
Expand All @@ -16,6 +18,7 @@ const UISection = ({ children }: { children: React.ReactNode }) => {
}}
>
{children}
<ModalProvider />
</section>
);
};
Expand Down
25 changes: 25 additions & 0 deletions shared/hooks/useModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { create } from 'zustand';

export type ModalType = 'Intro';

export interface ModalProps {
data?: null; // temp
}

interface ModalStore {
type: ModalType | null;
props: ModalProps;
isOpen: boolean;
onOpen: (type: ModalType, props?: ModalProps) => void;
onClose: () => void;
}

const useModal = create<ModalStore>((set) => ({
type: null,
props: {},
isOpen: false,
onOpen: (type, props) => set({ type, props, isOpen: true }),
onClose: () => set({ type: null, props: {}, isOpen: false }),
}));

export default useModal;
20 changes: 20 additions & 0 deletions shared/provider/ModalProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client';

import { useEffect, useState } from 'react';
import IntroduceModal from '@/shared/components/modals/IntroduceModal';

const ModalProvider = () => {
const [isMounted, setIsMounted] = useState(false);

useEffect(() => {
setIsMounted(true);
}, []);

if (!isMounted) {
return null;
}

return <IntroduceModal />;
};

export default ModalProvider;

0 comments on commit c5797a8

Please sign in to comment.