Skip to content

Commit

Permalink
feat: add PopupManager for hide all currently visible popups
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongshin committed Apr 19, 2023
1 parent db98176 commit 3554de3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
15 changes: 10 additions & 5 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<View></View>
<Alert.Portal />
<ActionSheet.Portal />
</>
);
}
10 changes: 10 additions & 0 deletions packages/v1/factory/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { createElement } from 'react';
import PopupManager from '../manager';
import Provider from '../provider';
import { PopupContext } from '../types';

Expand Down Expand Up @@ -27,6 +28,8 @@ export function createPopup<T>(Component: React.FC<T>) {
await internalRef.current.hide();
}

PopupManager.registerRef(genUniqueComponentName(Component), internalRef);

return {
show,
hide,
Expand All @@ -38,3 +41,10 @@ export function createPopup<T>(Component: React.FC<T>) {
),
};
}

function genUniqueComponentName<T extends React.FC<any>>(Component: T) {
const unique = Math.round(Math.random() * 1234567890);
return `${
Component.name || Component.displayName || `GlobalComponent`
}${unique}`;
}
2 changes: 2 additions & 0 deletions packages/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ export { usePopupContext } from './context';
* animation hooks
*/
export * from './hooks';

export { default as PopupManager } from './manager';
25 changes: 25 additions & 0 deletions packages/v1/manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PopupContext } from '../types';

class PopupManager {
private map = new Map<string, React.RefObject<PopupContext<any>>>();

public registerRef(
name: string,
ref: React.RefObject<PopupContext<any>>,
): void {
if (this.map.has(name)) return;
this.map.set(name, ref);
}

/**
* hide all current visible popups
*/
public async hideAll(): Promise<boolean> {
await Promise.all(
Array.from(this.map).map(([_, popup]) => popup.current?.hide()),
);
return true;
}
}

export default new PopupManager();

0 comments on commit 3554de3

Please sign in to comment.