From b97ab23f52c4363e152e413f758d54805546cbaf Mon Sep 17 00:00:00 2001 From: dominicleo Date: Sun, 12 Mar 2023 01:43:32 +0800 Subject: [PATCH] chore: modal --- packages/zarm/src/modal/index.ts | 9 ++++++--- packages/zarm/src/modal/methods.tsx | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 packages/zarm/src/modal/methods.tsx diff --git a/packages/zarm/src/modal/index.ts b/packages/zarm/src/modal/index.ts index c0d25d308..31361fcc4 100644 --- a/packages/zarm/src/modal/index.ts +++ b/packages/zarm/src/modal/index.ts @@ -1,11 +1,14 @@ -import Modal from './Modal'; +import attachPropertiesToComponent from '../utils/attachPropertiesToComponent'; import alert from './Alert'; import confirm from './Confirm'; -import attachPropertiesToComponent from '../utils/attachPropertiesToComponent'; +import Modal from './Modal'; +import { show, clear } from './methods'; -export type { ModalProps, ModalCssVars } from './Modal'; +export type { ModalCssVars, ModalProps } from './Modal'; export default attachPropertiesToComponent(Modal, { + show, + clear, alert, confirm, }); diff --git a/packages/zarm/src/modal/methods.tsx b/packages/zarm/src/modal/methods.tsx new file mode 100644 index 000000000..3fd58000c --- /dev/null +++ b/packages/zarm/src/modal/methods.tsx @@ -0,0 +1,27 @@ +import * as React from 'react'; +import { ImperativeHandler, renderImperatively } from '../utils/dom'; +import Modal, { ModalProps } from './Modal'; + +export type ModalShowProps = Omit; + +export type ModalShowHandler = Pick; + +const closeFn = new Set<() => void>(); + +export const show = (props: ModalShowProps) => { + const handler: ModalShowHandler = renderImperatively( + { + closeFn.delete(handler.close); + props.afterClose?.(); + }} + />, + ); + closeFn.add(handler.close); + return handler; +}; + +export const clear = () => { + closeFn.forEach((close) => close()); +};