Skip to content

Commit

Permalink
feat :: useModal
Browse files Browse the repository at this point in the history
  • Loading branch information
dutexion committed May 4, 2024
1 parent 437caff commit d6626ad
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/hooks/useModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useCallback, useState } from 'react';
import styled from '@emotion/styled';
import { theme } from '@/style/theme';

export type useModalReturnType = {
isVisible: string | boolean;
onShow: (arg?: string | undefined) => void;
onClose: () => void;
ModalWrapper: ({
width,
height,
children,
}: {
width: number;
height: number;
children: React.ReactNode;
}) => JSX.Element;
};

export type useModalProps = {
defaultVisible?: string | boolean;
};

export const useModal = ({ defaultVisible = false }: useModalProps = {}): useModalReturnType => {
const [isVisible, setIsVisible] = useState(defaultVisible);

const onShow = useCallback((arg?: string) => {
setIsVisible(arg || true);
}, []);

const onClose = useCallback(() => {
setIsVisible(false);
}, []);

const ModalWrapper = useCallback(
({ width, height, children }: { width: number; height: number; children: React.ReactNode }): JSX.Element => {
const onClick = (event: React.MouseEvent<HTMLDivElement>) => {
if (event.target === event.currentTarget) {
onClose();
}
};

return (
<ModalBackground onClick={onClick}>
<Wrapper width={width} height={height}>
{children}
</Wrapper>
</ModalBackground>
);
},
[onClose],
);

return {
isVisible,
onShow,
onClose,
ModalWrapper,
};
};

const ModalBackground = styled.div`
width: 100vw;
height: calc(100vh + 52px);
background-color: rgba(0, 0, 0, 0.5);
position: fixed;
top: 0;
left: 0;
z-index: 999;
display: flex;
justify-content: center;
align-items: center;
`;

const Wrapper = styled.div<{ width: number; height: number }>`
width: ${({ width }) => width}px;
height: ${({ height }) => height}px;
background-color: ${theme.color.gray1};
border-radius: 8px;
${theme.effect.shadow3};
`;

0 comments on commit d6626ad

Please sign in to comment.