Skip to content
Open
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
43 changes: 21 additions & 22 deletions src/components/ui/custom-portal/CustomPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ interface PortalBaseValue {
element: (close: () => void) => JSX.Element,
id?: string,
toggle?: boolean
) => number | undefined;
closePortal: (index: number) => void;
) => string | undefined;
closePortal: (id: string) => void;
closePortalById: (id: string) => void;
isPortalOpened: (id: string) => boolean;
openedPortals: () => {
Expand Down Expand Up @@ -63,6 +63,7 @@ interface Item {
const [elements, setElements] = createStore<Item[]>([]);

export const toast = (body: string, title?: string, icon?: string) => {
const id = "toast-" + Math.random();
setElements((e) => [
...e,
{
Expand All @@ -74,7 +75,7 @@ export const toast = (body: string, title?: string, icon?: string) => {
title={title || "Alert"}
/>
),
id: "toast-" + Math.random()
id: id
}
]);
};
Expand All @@ -85,6 +86,7 @@ export const prompt = (
icon?: string,
onSubmit?: (value: string) => void
) => {
const id = "prompt-" + Math.random();
setElements((e) => [
...e,
{
Expand All @@ -98,7 +100,7 @@ export const prompt = (
prompt
/>
),
id: "prompt-" + Math.random()
id: id
}
]);
};
Expand Down Expand Up @@ -128,34 +130,28 @@ export function CustomPortalProvider(props: CustomPortalProps) {
id?: string,
toggle?: boolean
) => {
const portalId = id || "portal-" + Math.random();
if (id && toggle) {
if (isPortalOpened(id)) {
closePortalById(id);
return;
}
}
if (id && isPortalOpened(id)) return;
setElements([...elements, { element, id }]);
return elements.length - 1;
setElements([...elements, { element, id: portalId }]);
return portalId;
};

const closePortal = async (index: number) => {
const closePortal = async (id: string) => {
const index = elements.findIndex((e) => e.id === id);
const element = elements[index];
if (!element) return;
if (element?.closing) return;
setElements(index, "closing", true);
await element?.customCloseHandler?.();
setElements(produce((elements) => elements.splice(index, 1)));
};
const closePortalById = async (id: string) => {
const element = elements.find((e) => e.id === id);
if (!element) return;
if (element?.closing) return;
const index = elements.findIndex((e) => e.id === id);
setElements(index, "closing", true);
await element?.customCloseHandler?.();
setElements(elements.filter((e) => e.id !== id));
};
const closePortalById = closePortal;

const isPortalOpened = (id: string) =>
elements.find((e) => e.id === id) !== undefined;
Expand Down Expand Up @@ -183,10 +179,9 @@ export function CustomPortalProvider(props: CustomPortalProps) {
{props.children}
</div>
<For each={elements}>
{(item, i) => (
{(item) => (
<PortalItem
item={item}
i={i()}
closePortal={closePortal}
setElements={setElements}
/>
Expand All @@ -198,16 +193,20 @@ export function CustomPortalProvider(props: CustomPortalProps) {

const PortalItem = (props: {
item: Item;
i: number;
closePortal: (index: number) => void;
closePortal: (id: string) => void;
setElements: SetStoreFunction<Item[]>;
}) => {
const close = () => {
props.closePortal(props.i);
if (props.item.id) {
props.closePortal(props.item.id);
}
};

const setCustomCloseHandler = (customCloseHandler: () => void) => {
props.setElements(props.i, "customCloseHandler", () => customCloseHandler);
const index = elements.findIndex((e) => e.id === props.item.id);
if (index !== -1) {
props.setElements(index, "customCloseHandler", () => customCloseHandler);
}
};

return (
Expand Down