Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: #1 Modal 전역 상태관리 #31

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useState } from 'react'
import { RouterProvider } from 'react-router-dom'
import styled, { ThemeProvider } from 'styled-components'
import { Helmet, HelmetProvider } from 'react-helmet-async'
import ModalContainer from '~modals/ModalContainer'

function App() {
//* 다크모드 확장성 고려
Expand All @@ -28,6 +29,7 @@ function App() {
<RouterProvider router={router} />
</MobileWrapper>
<PWABadge />
<ModalContainer />
</ThemeProvider>
</HelmetProvider>
</>
Expand Down
8 changes: 8 additions & 0 deletions src/modals/ModalContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as S from './styles'
import { useModalStore } from '~stores/modalStore'

export default function ModalContainer() {
const { modalList } = useModalStore()

return <>{modalList.length ? <S.ModalWrapper>{...modalList}</S.ModalWrapper> : null}</>
}
11 changes: 11 additions & 0 deletions src/modals/ModalContainer/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { styled } from 'styled-components'

export const ModalWrapper = styled.div`
position: fixed;
top: 0;
left: 0;
width: 100dvw;
height: 100dvh;
background-color: rgba(0, 0, 0, 0.4);
z-index: 999;
`
22 changes: 22 additions & 0 deletions src/stores/modalStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ReactNode } from 'react'
import { create } from 'zustand'

interface ModalStore {
modalList: ReactNode[]
pushModal: (modal: ReactNode) => void
popModal: () => void
clearModal: () => void
}

export const useModalStore = create<ModalStore>(set => ({
modalList: [],
pushModal: modal =>
set(state => ({
modalList: [...state.modalList, modal],
})),
popModal: () =>
set(state => ({
modalList: state.modalList.slice(0, -1),
})),
clearModal: () => set({ modalList: [] }),
}))
Loading