From 3554de33af776cf0bec05d45a592e9e4cd73eff2 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 19 Apr 2023 17:15:29 +0900 Subject: [PATCH] feat: add PopupManager for hide all currently visible popups --- App.tsx | 15 ++++++++++----- packages/v1/factory/index.ts | 10 ++++++++++ packages/v1/index.ts | 2 ++ packages/v1/manager/index.ts | 25 +++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 packages/v1/manager/index.ts diff --git a/App.tsx b/App.tsx index e00767b..0f99ce1 100644 --- a/App.tsx +++ b/App.tsx @@ -1,23 +1,28 @@ import React, { useEffect } from 'react'; +import { + createPopup, + AlertPopupUI, + ActionSheetUI, + PopupManager, +} from './packages/v1'; // import StorybookUIRoot from './storybook'; -import { View } from 'react-native'; -import { createPopup, AlertPopupUI } from './packages/v1'; const Alert = createPopup(AlertPopupUI); +const ActionSheet = createPopup(ActionSheetUI); export default function App() { // useEffect(() => { // Alert.show({ title: 'please!!', message: 'helpme' }); - + // ActionSheet.show({ actions: [] }); // setTimeout(() => { - // Alert.hide(); + // PopupManager.hideAll(); // }, 3000); // }, []); return ( <> - + ); } diff --git a/packages/v1/factory/index.ts b/packages/v1/factory/index.ts index 60eb3fd..e25ed83 100644 --- a/packages/v1/factory/index.ts +++ b/packages/v1/factory/index.ts @@ -1,4 +1,5 @@ import React, { createElement } from 'react'; +import PopupManager from '../manager'; import Provider from '../provider'; import { PopupContext } from '../types'; @@ -27,6 +28,8 @@ export function createPopup(Component: React.FC) { await internalRef.current.hide(); } + PopupManager.registerRef(genUniqueComponentName(Component), internalRef); + return { show, hide, @@ -38,3 +41,10 @@ export function createPopup(Component: React.FC) { ), }; } + +function genUniqueComponentName>(Component: T) { + const unique = Math.round(Math.random() * 1234567890); + return `${ + Component.name || Component.displayName || `GlobalComponent` + }${unique}`; +} diff --git a/packages/v1/index.ts b/packages/v1/index.ts index 7c438c2..04e78ad 100644 --- a/packages/v1/index.ts +++ b/packages/v1/index.ts @@ -42,3 +42,5 @@ export { usePopupContext } from './context'; * animation hooks */ export * from './hooks'; + +export { default as PopupManager } from './manager'; diff --git a/packages/v1/manager/index.ts b/packages/v1/manager/index.ts new file mode 100644 index 0000000..0c9440f --- /dev/null +++ b/packages/v1/manager/index.ts @@ -0,0 +1,25 @@ +import { PopupContext } from '../types'; + +class PopupManager { + private map = new Map>>(); + + public registerRef( + name: string, + ref: React.RefObject>, + ): void { + if (this.map.has(name)) return; + this.map.set(name, ref); + } + + /** + * hide all current visible popups + */ + public async hideAll(): Promise { + await Promise.all( + Array.from(this.map).map(([_, popup]) => popup.current?.hide()), + ); + return true; + } +} + +export default new PopupManager();