import React from "react";
import { Classes, OverlaysProvider } from "@blueprintjs/core";
import { Meta } from "@storybook/react";
import {
AlertDialog,
Button,
ModalContext,
ModalContextProps,
ModalSize,
SimpleDialog,
Spacing,
useModalContext,
} from "./../../../../index";
/**
* `ModalContext` can be used as provider to track a stack of modals.
* Always use it when you open modal from inside other modals.
* Otherise screen readers may not recognize the correct modal to work with.
*
* ```(Javascript)
* import { ModalContext, SimpleDialog } from "@eccenca/gui-elements";
*
* const ContextTemplate = () => {
* const { setModalOpen, openModalStack } = useModalContext();
* return (
*
*
*
*
*
* );
* };
*
* const OtherModal = () => {
* const modalContext = React.useContext(ModalContext);
* return (
*
*
* {(modalContext.openModalStack ?? []).map((modalId, idx) => (
* -
* {idx + 1}. {modalId}
*
* ))}
*
*
* );
* };
* ```
*/
export default {
title: "Components/Dialog/ModalContext",
decorators: [
(Story) => (
),
],
} as Meta;
export const Usage = () => {
const { setModalOpen, openModalStack } = useModalContext();
return (
);
};
/** Component for nested modals. */
const ExampleModal = ({
id,
size,
children,
modalType = "simple",
}: {
id?: string;
size: ModalSize;
modalType?: "simple" | "alert";
children?: React.HTMLAttributes["children"];
}) => {
const [isOpen, setIsOpen] = React.useState(true);
const [portalElement, setPortalElement] = React.useState();
React.useEffect(() => {
setPortalElement(document.getElementById("modalPortal")!);
}, []);
const DialogType = modalType === "alert" ? AlertDialog : SimpleDialog;
return (
setIsOpen(false)}>
Close
}
size={size}
isOpen={isOpen}
usePortal={true}
portalContainer={portalElement}
hasBackdrop={true}
onOpened={() => {
// workaround, Blueprint attach a class to body tht prevents scrolling, probably it is attached to the wrong portal
document.body.classList.remove(Classes.OVERLAY_OPEN);
}}
>
{children}
);
};
const InnerModal = () => {
return ;
};
const MiddleModal = () => {
return (
);
};
/** Shows the current stack of open modals. */
const TrackingContent = () => {
const modalContext = React.useContext(ModalContext);
return (
{(modalContext.openModalStack() ?? []).map((modalId, idx) => (
-
{idx + 1}. {modalId}
))}
);
};