다이얼로그 구현하기
본 문서에서는
@muvel-plugins/modal플러그인을 사용하여 다이얼로그를 구현하는 방법을 설명합니다. 사이드바도@muvel-plugins/drawer플러그인을 사용하여 거의 유사한 방식으로 구현할 수 있습니다.
다이얼로그 등록하기
모달은 보통 아래 3단계로 연결합니다.
modal.tsx에서 모달 정의plugin.ts의setup에서 등록dispose에서 등록 해제
1) 모달 정의 (modal.tsx)
import { Dialog } from "@chakra-ui/react"
import { defineModal, modalId } from "@muvel-plugins/modal"
type SearchModalProps = {
keyword: string
}
const SearchModal: React.FC<SearchModalProps> = (props) => {
return (
<Dialog.Content maxW="600px" w="90vw">
{/* 실제 폼/콘텐츠 */}
{props.keyword}
</Dialog.Content>
)
}
export const searchModalId = modalId<SearchModalProps>("search-modal")
export const searchModal = defineModal<SearchModalProps>(searchModalId, {
component: SearchModal,
props: {
root: { placement: "center" },
},
})modalId<T>()의T가open(id, props)호출 시 props 타입 안정성을 보장합니다.defineModal<T>()로 모달 컴포넌트와 기본 dialog props를 함께 정의합니다.
2) 플러그인에서 등록 (plugin.ts)
import { modalPluginId } from "@muvel-plugins/modal"
setup: async (mgr, ctx) => {
const modalApi = mgr.use(ctx, modalPluginId)
const disposer = modalApi.register(searchModal)
return { disposer }
},
dispose: async (state) => {
state.disposer()
},novel-search 플러그인도 같은 방식으로 setup에서 등록 후 dispose에서 해제합니다.
다이얼로그 사용하기
모달 호출은 보통 API 함수에서 래핑해서 외부에 공개합니다.
api: {
openSearch: (ctx, keyword: string) => {
const modalApi = ctx.api(modalPluginId)
modalApi.open(searchModalId, { keyword })
},
closeSearch: (ctx) => {
const modalApi = ctx.api(modalPluginId)
modalApi.close(searchModalId)
},
}클라이언트 React 컴포넌트에서는 해당 플러그인의 API를 usePluginApi(...)로 가져와 호출하면 됩니다.
const myApi = usePluginApi(myPluginId)
myApi.openSearch("muvel")타이핑 포인트
ModalApi는ctx를 받는 원본 API 시그니처입니다.BoundModalApi는 외부에서 사용하는 타입으로,ctx가 제거된 버전입니다.- 예:
open(ctx, id, props)->open(id, props)
- 예:
BoundApi는 보통 추론되지만, 제네릭/오버로드가 복잡하면definePlugin<Api, State, BoundApi>형태로 명시해 타입을 고정하세요.
체크리스트
dependencies에modalPluginId를 선언했는지 확인modalId<T>와defineModal<T>의 제네릭T가 실제 props와 일치하는지 확인setup의register와dispose의 해제가 짝을 이루는지 확인- 호출부에서
open(id, props)의 props 타입이 정확히 맞는지 확인
Last updated on