@contentful/f36-modal 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,186 @@
1
+ import React from "react";
2
+ import { PropsWithHTMLElement, CommonProps } from "@contentful/f36-core";
3
+ import ReactModal from "react-modal";
4
+ interface ModalHeaderInternalProps extends CommonProps {
5
+ title: string;
6
+ onClose?: Function;
7
+ }
8
+ export type ModalHeaderProps = PropsWithHTMLElement<ModalHeaderInternalProps, 'div'>;
9
+ export function ModalHeader({ onClose, title, testId, className, ...otherProps }: ModalHeaderProps): React.ReactElement;
10
+ interface ModalContentInternalProps extends CommonProps {
11
+ children: React.ReactNode;
12
+ }
13
+ export type ModalContentProps = PropsWithHTMLElement<ModalContentInternalProps, 'div'>;
14
+ export function ModalContent({ testId, className, children, ...otherProps }: ModalContentProps): JSX.Element;
15
+ type ModalSizeType = 'small' | 'medium' | 'large' | 'fullWidth' | 'zen' | string | number;
16
+ type ModalPositionType = 'center' | 'top';
17
+ export interface ModalProps extends CommonProps {
18
+ /**
19
+ * When true, the dialog is shown.
20
+ */
21
+ isShown: boolean;
22
+ /**
23
+ * Function that will be run when the modal is requested to be closed, prior to actually closing.
24
+ */
25
+ onClose: ReactModal.Props['onRequestClose'];
26
+ /**
27
+ * Function that will be run after the modal has opened.
28
+ */
29
+ onAfterOpen?: ReactModal.Props['onAfterOpen'];
30
+ /**
31
+ * Additional aria attributes
32
+ */
33
+ aria?: ReactModal.Props['aria'];
34
+ /**
35
+ * Boolean indicating if clicking the overlay should close the overlay.
36
+ */
37
+ shouldCloseOnOverlayClick?: boolean;
38
+ /**
39
+ * Boolean indicating if pressing the esc key should close the overlay.
40
+ */
41
+ shouldCloseOnEscapePress?: boolean;
42
+ /**
43
+ * Indicating if modal is centered or linked to the top
44
+ */
45
+ position?: ModalPositionType;
46
+ /**
47
+ Top offset if position is 'top'
48
+ */
49
+ topOffset?: number | string;
50
+ /**
51
+ Modal title that is used in header
52
+ */
53
+ title?: string;
54
+ /**
55
+ Size of the modal window
56
+ */
57
+ size?: ModalSizeType;
58
+ /**
59
+ * Are modals higher than viewport allowed
60
+ */
61
+ allowHeightOverflow?: boolean;
62
+ /**
63
+ * Optional props to override ModalHeader behaviour
64
+ */
65
+ modalHeaderProps?: Partial<ModalHeaderProps>;
66
+ /**
67
+ * Optional props to override ModalContent behaviour
68
+ */
69
+ modalContentProps?: Partial<ModalContentProps>;
70
+ /**
71
+ * Optional property to set initial focus
72
+ */
73
+ initialFocusRef?: React.RefObject<HTMLElement>;
74
+ children: React.ReactNode | RenderModal;
75
+ }
76
+ type RenderModal = (modalProps: ModalProps) => React.ReactNode;
77
+ declare function _Modal1({ allowHeightOverflow, position, shouldCloseOnEscapePress, shouldCloseOnOverlayClick, size, testId, topOffset, aria, ...otherProps }: ModalProps): JSX.Element;
78
+ interface ModalControlsInternalProps extends CommonProps {
79
+ children: React.ReactElement[] | React.ReactElement;
80
+ }
81
+ export type ModalControlsProps = PropsWithHTMLElement<ModalControlsInternalProps, 'div'>;
82
+ export function ModalControls({ testId, className, children, ...otherProps }: ModalControlsProps): React.ReactElement;
83
+ type CompoundModal = typeof _Modal1 & {
84
+ Content: typeof ModalContent;
85
+ Header: typeof ModalHeader;
86
+ Controls: typeof ModalControls;
87
+ };
88
+ export const Modal: CompoundModal;
89
+ export interface ModalConfirmProps {
90
+ /**
91
+ * When true, the dialog is shown.
92
+ */
93
+ isShown: boolean;
94
+ /**
95
+ * Function that will be called when the confirm button is clicked. This does not close the ModalConfirm.
96
+ */
97
+ onConfirm(): void;
98
+ /**
99
+ * Function that will be called when the cancel button is clicked. This does not close the ModalConfirm.
100
+ */
101
+ onCancel: ModalProps['onClose'];
102
+ /**
103
+ Modal title that is used in header
104
+ */
105
+ title?: string;
106
+ /**
107
+ * Label of the confirm button
108
+ */
109
+ confirmLabel?: string | false;
110
+ /**
111
+ * Label of the cancel button
112
+ */
113
+ cancelLabel?: string | false;
114
+ /**
115
+ * The intent of the ModalConfirm. Used for the Button.
116
+ */
117
+ intent?: 'primary' | 'positive' | 'negative';
118
+ /**
119
+ Size of the modal window
120
+ */
121
+ size?: ModalSizeType;
122
+ /**
123
+ * Boolean indicating if clicking the overlay should close the overlay.
124
+ */
125
+ shouldCloseOnOverlayClick?: boolean;
126
+ /**
127
+ * Boolean indicating if pressing the esc key should close the overlay.
128
+ */
129
+ shouldCloseOnEscapePress?: boolean;
130
+ /**
131
+ * When true, the confirm button is set to disabled.
132
+ */
133
+ isConfirmDisabled?: boolean;
134
+ /**
135
+ * When true, the confirm button is set to loading.
136
+ */
137
+ isConfirmLoading?: boolean;
138
+ /**
139
+ * Are modals higher than viewport allowed
140
+ */
141
+ allowHeightOverflow?: boolean;
142
+ /**
143
+ * Optional props to override ModalHeader behaviour
144
+ */
145
+ modalHeaderProps?: Partial<ModalHeaderProps>;
146
+ /**
147
+ * Optional props to override ModalContent behaviour
148
+ */
149
+ modalContentProps?: Partial<ModalContentProps>;
150
+ /**
151
+ * Optional props to override ModalControl behaviour
152
+ */
153
+ modalControlsProps?: Partial<ModalControlsProps>;
154
+ /**
155
+ * Optional property to set initial focus
156
+ */
157
+ initialFocusRef?: React.RefObject<HTMLElement>;
158
+ testId?: string;
159
+ confirmTestId?: string;
160
+ cancelTestId?: string;
161
+ children: React.ReactNode;
162
+ }
163
+ export function ModalConfirm({ allowHeightOverflow, cancelLabel, cancelTestId, children, confirmLabel, confirmTestId, intent, isConfirmDisabled, isConfirmLoading, isShown, modalContentProps, modalControlsProps, modalHeaderProps, onCancel, onConfirm, shouldCloseOnEscapePress, shouldCloseOnOverlayClick, size, testId, title, initialFocusRef, }: ModalConfirmProps): JSX.Element;
164
+ interface ModalLauncherComponentRendererProps<T = any> {
165
+ isShown: boolean;
166
+ onClose: (result?: T) => void;
167
+ }
168
+ interface ModalLauncherOpenOptions {
169
+ /**
170
+ * Unique id to be used as identifier for the modal contianer
171
+ */
172
+ modalId?: string;
173
+ /**
174
+ * ms before removing the component from the tree
175
+ * @default 300
176
+ */
177
+ delay?: number;
178
+ }
179
+ declare function closeAll(): void;
180
+ declare function open<T = any>(componentRenderer: (props: ModalLauncherComponentRendererProps<T>) => JSX.Element, options?: ModalLauncherOpenOptions): Promise<T>;
181
+ export const ModalLauncher: {
182
+ open: typeof open;
183
+ closeAll: typeof closeAll;
184
+ };
185
+
186
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;ACUA,kCAAmC,SAAQ,WAAW;IACpD,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,QAAQ,CAAC;CACpB;AAED,+BAA+B,qBAC7B,wBAAwB,EACxB,KAAK,CACN,CAAC;AAEF,4BAA4B,EAC1B,OAAO,EACP,KAAK,EACL,MAA6B,EAC7B,SAAS,EACT,GAAG,UAAU,EACd,EAAE,gBAAgB,GAAG,MAAM,YAAY,CA6BvC;AEjDD,mCAAoC,SAAQ,WAAW;IACrD,QAAQ,EAAE,MAAM,SAAS,CAAC;CAC3B;AAED,gCAAgC,qBAC9B,yBAAyB,EACzB,KAAK,CACN,CAAC;AAEF,6BAA6B,EAC3B,MAA8B,EAC9B,SAAS,EACT,QAAQ,EACR,GAAG,UAAU,EACd,EAAE,iBAAiB,eAYnB;AChCD,qBACI,OAAO,GACP,QAAQ,GACR,OAAO,GACP,WAAW,GACX,KAAK,GACL,MAAM,GACN,MAAM,CAAC;AAEX,yBAAgC,QAAQ,GAAG,KAAK,CAAC;AEUjD,2BAA4B,SAAQ,WAAW;IAC7C;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,OAAO,EAAE,WAAW,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAE5C;;OAEG;IACH,WAAW,CAAC,EAAE,WAAW,KAAK,CAAC,aAAa,CAAC,CAAC;IAE9C;;OAEG;IACH,IAAI,CAAC,EAAE,WAAW,KAAK,CAAC,MAAM,CAAC,CAAC;IAEhC;;OAEG;IACH,yBAAy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sB,EACtB,YAAkD,EAClD,QAAQ,EACR,YAAwB,EACxB,aAAoD,EACpD,MAAmB,EACnB,iBAAyB,EACzB,gBAAwB,EACxB,OAAO,EACP,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,SAAS,EACT,wBAA+B,EAC/B,yBAAgC,EAChC,IAAe,EACf,MAA8B,EAC9B,KAAuB,EACvB,eAAe,GAChB,EAAE,iBAAiB,eAqDnB;AChKD,8CAA8C,CAAC,GAAG,GAAG;IACnD,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAED;IACE;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAuBD,kCAQC;AAGD,sBAAc,CAAC,GAAG,GAAG,EACnB,iBAAiB,EAAE,CACjB,KAAK,EAAE,oCAAoC,CAAC,CAAC,KAC1C,IAAI,OAAO,EAChB,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,CAAC,CAAC,CAuCZ;AAED,OAAO,MAAM;;;CAGZ,CAAC","sources":["packages/components/modal/src/ModalHeader/ModalHeader.styles.ts","packages/components/modal/src/ModalHeader/ModalHeader.tsx","packages/components/modal/src/ModalContent/ModalContent.styles.ts","packages/components/modal/src/ModalContent/ModalContent.tsx","packages/components/modal/src/types.ts","packages/components/modal/src/Modal.styles.ts","packages/components/modal/src/Modal.tsx","packages/components/modal/src/ModalControls/ModalControls.tsx","packages/components/modal/src/CompoundModal.tsx","packages/components/modal/src/ModalConfirm/ModalConfirm.tsx","packages/components/modal/src/ModalLauncher/ModalLauncher.tsx","packages/components/modal/src/index.ts","packages/components/modal/index.ts"],"sourcesContent":["import tokens from '@contentful/f36-tokens';\nimport { css } from 'emotion';\n\nexport function getModalHeaderStyles() {\n return {\n root: css({\n position: 'relative',\n padding: `${tokens.spacingM} ${tokens.spacingM} ${tokens.spacingM} ${tokens.spacingL}`,\n borderRadius: `${tokens.borderRadiusMedium} ${tokens.borderRadiusMedium} 0 0`,\n borderBottom: `1px solid ${tokens.gray300}`,\n }),\n buttonContainer: css({\n position: 'relative',\n width: tokens.spacing2Xl,\n height: tokens.spacingL,\n button: {\n position: 'absolute',\n top: `calc(-1 * ${tokens.spacing2Xs})`,\n right: 0,\n },\n }),\n };\n}\n","import React from 'react';\nimport { cx } from 'emotion';\nimport { CloseIcon } from '@contentful/f36-icons';\nimport { Flex } from '@contentful/f36-core';\nimport type { PropsWithHTMLElement, CommonProps } from '@contentful/f36-core';\nimport { Button } from '@contentful/f36-button';\nimport { Subheading } from '@contentful/f36-typography';\n\nimport { getModalHeaderStyles } from './ModalHeader.styles';\n\ninterface ModalHeaderInternalProps extends CommonProps {\n title: string;\n onClose?: Function;\n}\n\nexport type ModalHeaderProps = PropsWithHTMLElement<\n ModalHeaderInternalProps,\n 'div'\n>;\n\nexport function ModalHeader({\n onClose,\n title,\n testId = 'cf-ui-modal-header',\n className,\n ...otherProps\n}: ModalHeaderProps): React.ReactElement {\n const styles = getModalHeaderStyles();\n\n return (\n <Flex\n {...otherProps}\n className={cx(styles.root, className)}\n testId={testId}\n alignItems=\"center\"\n justifyContent=\"space-between\"\n >\n <Subheading as=\"h1\" isTruncated marginBottom=\"none\">\n {title}\n </Subheading>\n {onClose && (\n <Flex alignItems=\"center\" className={styles.buttonContainer}>\n <Button\n variant=\"transparent\"\n aria-label=\"Close\"\n startIcon={<CloseIcon size=\"small\" />}\n onClick={() => {\n onClose();\n }}\n size=\"small\"\n />\n </Flex>\n )}\n </Flex>\n );\n}\n","import { css } from 'emotion';\nimport tokens from '@contentful/f36-tokens';\n\nexport function getModalContentStyles() {\n return {\n root: css({\n padding: `${tokens.spacingM} ${tokens.spacingL}`,\n color: tokens.gray700,\n fontSize: tokens.fontSizeM,\n fontFamily: tokens.fontStackPrimary,\n lineHeight: tokens.lineHeightM,\n overflowY: 'auto',\n overflowX: 'auto',\n boxSizing: 'border-box',\n }),\n };\n}\n","import React from 'react';\nimport { cx } from 'emotion';\nimport type { PropsWithHTMLElement, CommonProps } from '@contentful/f36-core';\nimport { Box } from '@contentful/f36-core';\nimport { getModalContentStyles } from './ModalContent.styles';\n\ninterface ModalContentInternalProps extends CommonProps {\n children: React.ReactNode;\n}\n\nexport type ModalContentProps = PropsWithHTMLElement<\n ModalContentInternalProps,\n 'div'\n>;\n\nexport function ModalContent({\n testId = 'cf-ui-modal-content',\n className,\n children,\n ...otherProps\n}: ModalContentProps) {\n const styles = getModalContentStyles();\n return (\n <Box\n {...otherProps}\n as=\"div\"\n className={cx(styles.root, className)}\n testId={testId}\n >\n {children}\n </Box>\n );\n}\n","export type ModalSizeType =\n | 'small'\n | 'medium'\n | 'large'\n | 'fullWidth'\n | 'zen'\n | string\n | number;\n\nexport type ModalPositionType = 'center' | 'top';\n","import tokens from '@contentful/f36-tokens';\nimport { css, cx } from 'emotion';\nimport type { ModalPositionType, ModalSizeType } from './types';\n\nexport function getModalStyles(props: {\n size: ModalSizeType;\n position: ModalPositionType;\n allowHeightOverflow?: boolean;\n className?: string;\n}) {\n const modal = cx(\n css({\n transition: `opacity ${tokens.transitionDurationLong} ${tokens.transitionEasingDefault}, transform ${tokens.transitionDurationDefault} ${tokens.transitionEasingCubicBezier}`,\n opacity: '0.5',\n margin: tokens.spacing2Xl,\n transform: 'scale(0.85)',\n backgroundColor: tokens.colorWhite,\n borderRadius: tokens.borderRadiusMedium,\n boxShadow: tokens.boxShadowHeavy,\n maxHeight: `calc(100vh - 1rem * (100 / ${tokens.fontBaseDefault}))`,\n maxWidth: `calc(100vw - 1rem * (100 / ${tokens.fontBaseDefault}))`,\n overflow: 'hidden',\n display: 'flex',\n flexDirection: 'column',\n }),\n props.allowHeightOverflow\n ? css({\n overflow: 'auto',\n maxHeight: 'none',\n })\n : null,\n props.size === 'zen'\n ? css({\n maxWidth: 'none',\n maxHeight: 'none',\n margin: 0,\n height: '100%',\n width: '100%',\n })\n : null,\n props.className,\n );\n\n return {\n portal: css({\n display: 'block',\n }),\n base: {\n root: cx(\n css({\n zIndex: tokens.zIndexModalContent,\n position: 'relative',\n padding: 0,\n display: 'inline-block',\n margin: '0 auto',\n textAlign: 'left',\n outline: 'none',\n }),\n props.size === 'zen'\n ? css({\n width: '100%',\n height: '100%',\n })\n : null,\n ),\n afterOpen: css({\n '[data-modal-root]': {\n transform: 'scale(1)',\n opacity: '1',\n },\n }),\n beforeClose: css({\n '[data-modal-root]': {\n opacity: '0.5',\n transform: 'scale(0.85)',\n },\n }),\n },\n modalOverlay: {\n root: cx(\n css({\n display: 'flex',\n alignItems: 'baseline',\n flexWrap: 'wrap',\n top: 0,\n right: 0,\n bottom: 0,\n left: 0,\n zIndex: tokens.zIndexModal,\n opacity: 0,\n transition: `opacity ${tokens.transitionDurationDefault} ${tokens.transitionEasingDefault}`,\n position: 'fixed',\n overflowY: 'auto',\n backgroundColor: 'rgba(12, 20, 28, 0.74902)',\n textAlign: 'center',\n }),\n props.position === 'center'\n ? css({\n alignItems: 'center',\n justifyContent: 'center',\n })\n : null,\n ),\n afterOpen: css({\n opacity: '1 !important',\n }),\n beforeClose: css({\n opacity: 0,\n }),\n },\n modal,\n };\n}\n","import * as React from 'react';\nimport ReactModal from 'react-modal';\n\nimport { Box } from '@contentful/f36-core';\nimport type { CommonProps } from '@contentful/f36-core';\n\nimport { ModalHeader, ModalHeaderProps } from './ModalHeader/ModalHeader';\nimport { ModalContent, ModalContentProps } from './ModalContent/ModalContent';\nimport { getModalStyles } from './Modal.styles';\nimport type { ModalSizeType, ModalPositionType } from './types';\n\nconst ModalSizesMapper = {\n medium: '520px',\n small: '400px',\n large: '700px',\n fullWidth: '100vw',\n zen: '100vw',\n};\n\nexport interface ModalProps extends CommonProps {\n /**\n * When true, the dialog is shown.\n */\n isShown: boolean;\n\n /**\n * Function that will be run when the modal is requested to be closed, prior to actually closing.\n */\n onClose: ReactModal.Props['onRequestClose'];\n\n /**\n * Function that will be run after the modal has opened.\n */\n onAfterOpen?: ReactModal.Props['onAfterOpen'];\n\n /**\n * Additional aria attributes\n */\n aria?: ReactModal.Props['aria'];\n\n /**\n * Boolean indicating if clicking the overlay should close the overlay.\n */\n shouldCloseOnOverlayClick?: boolean;\n /**\n * Boolean indicating if pressing the esc key should close the overlay.\n */\n shouldCloseOnEscapePress?: boolean;\n /**\n * Indicating if modal is centered or linked to the top\n */\n position?: ModalPositionType;\n /**\n Top offset if position is 'top'\n */\n topOffset?: number | string;\n /**\n Modal title that is used in header\n */\n title?: string;\n /**\n Size of the modal window\n */\n size?: ModalSizeType;\n /**\n * Are modals higher than viewport allowed\n */\n allowHeightOverflow?: boolean;\n\n /**\n * Optional props to override ModalHeader behaviour\n */\n modalHeaderProps?: Partial<ModalHeaderProps>;\n\n /**\n * Optional props to override ModalContent behaviour\n */\n modalContentProps?: Partial<ModalContentProps>;\n\n /**\n * Optional property to set initial focus\n */\n initialFocusRef?: React.RefObject<HTMLElement>;\n\n children: React.ReactNode | RenderModal;\n}\n\ntype RenderModal = (modalProps: ModalProps) => React.ReactNode;\n\nfunction focusFirstWithinNode(node: HTMLElement) {\n if (node && node.querySelectorAll) {\n const elements = node.querySelectorAll('input, button');\n if (elements.length > 0) {\n const firstElement = elements[0];\n // @ts-expect-error focus might be missing\n if (typeof firstElement.focus === 'function') {\n // @ts-expect-error focus might be missing\n firstElement.focus();\n }\n }\n }\n}\n\nexport function Modal({\n allowHeightOverflow,\n position,\n shouldCloseOnEscapePress,\n shouldCloseOnOverlayClick,\n size,\n testId,\n topOffset,\n aria,\n ...otherProps\n}: ModalProps) {\n const contentRef = React.useRef<HTMLDivElement>(null);\n\n const props = {\n ...otherProps,\n allowHeightOverflow,\n position,\n shouldCloseOnEscapePress,\n shouldCloseOnOverlayClick,\n size,\n testId,\n topOffset,\n };\n\n const styles = getModalStyles({\n position,\n size,\n allowHeightOverflow,\n className: otherProps.className,\n });\n\n React.useEffect(() => {\n if (props.isShown) {\n setTimeout(() => {\n if (props.initialFocusRef && props.initialFocusRef.current) {\n if (props.initialFocusRef.current.focus) {\n props.initialFocusRef.current.focus();\n }\n } else if (contentRef.current) {\n focusFirstWithinNode(contentRef.current);\n }\n }, 100);\n }\n }, [props.isShown, props.initialFocusRef]);\n\n const renderDefault = () => {\n return (\n <React.Fragment>\n {otherProps.title && (\n <ModalHeader\n title={otherProps.title}\n onClose={props.onClose}\n {...otherProps.modalHeaderProps}\n />\n )}\n <ModalContent {...otherProps.modalContentProps}>\n {otherProps.children}\n </ModalContent>\n </React.Fragment>\n );\n };\n\n return (\n <ReactModal\n ariaHideApp={false}\n aria={aria}\n onRequestClose={props.onClose}\n isOpen={otherProps.isShown}\n onAfterOpen={props.onAfterOpen}\n shouldCloseOnEsc={shouldCloseOnEscapePress}\n shouldCloseOnOverlayClick={shouldCloseOnOverlayClick}\n shouldFocusAfterRender\n shouldReturnFocusAfterClose\n portalClassName={styles.portal}\n className={{\n base: styles.base.root,\n afterOpen: styles.base.afterOpen,\n beforeClose: styles.base.beforeClose,\n }}\n style={{\n content: {\n top: position === 'center' ? 0 : topOffset,\n },\n }}\n overlayClassName={{\n base: styles.modalOverlay.root,\n afterOpen: styles.modalOverlay.afterOpen,\n beforeClose: styles.modalOverlay.beforeClose,\n }}\n closeTimeoutMS={300}\n contentRef={(ref) => {\n contentRef.current = ref;\n }}\n >\n <Box\n testId={testId}\n style={{\n width: ModalSizesMapper[size] || size,\n }}\n className={styles.modal}\n data-modal-root\n >\n {typeof otherProps.children === 'function'\n ? otherProps.children(props)\n : renderDefault()}\n </Box>\n </ReactModal>\n );\n}\n\n// Use defaultProps instead of default values in the function to allow the\n// Storybook to import and use these values\nModal.defaultProps = {\n allowHeightOverflow: false,\n position: 'center',\n shouldCloseOnEscapePress: true,\n shouldCloseOnOverlayClick: true,\n size: 'medium',\n testId: 'cf-ui-modal',\n topOffset: '50px',\n};\n","import React from 'react';\n\nimport type { PropsWithHTMLElement, CommonProps } from '@contentful/f36-core';\nimport { Flex } from '@contentful/f36-core';\nimport { ButtonGroup } from '@contentful/f36-button';\n\ninterface ModalControlsInternalProps extends CommonProps {\n children: React.ReactElement[] | React.ReactElement;\n}\n\nexport type ModalControlsProps = PropsWithHTMLElement<\n ModalControlsInternalProps,\n 'div'\n>;\n\nexport function ModalControls({\n testId = 'cf-ui-modal-controls',\n className,\n children,\n ...otherProps\n}: ModalControlsProps): React.ReactElement {\n return (\n <Flex\n {...otherProps}\n className={className}\n testId={testId}\n flexDirection=\"row\"\n justifyContent=\"flex-end\"\n margin=\"spacingL\"\n marginTop=\"none\"\n >\n <ButtonGroup variant=\"spaced\" spacing=\"spacingS\">\n {children}\n </ButtonGroup>\n </Flex>\n );\n}\n","import { Modal as OriginalModal } from './Modal';\nimport { ModalContent } from './ModalContent/ModalContent';\nimport { ModalHeader } from './ModalHeader/ModalHeader';\nimport { ModalControls } from './ModalControls/ModalControls';\n\ntype CompoundModal = typeof OriginalModal & {\n Content: typeof ModalContent;\n Header: typeof ModalHeader;\n Controls: typeof ModalControls;\n};\n\nexport const Modal = OriginalModal as CompoundModal;\nModal.Content = ModalContent;\nModal.Header = ModalHeader;\nModal.Controls = ModalControls;\n","import React from 'react';\n\nimport { Modal } from '../CompoundModal';\nimport { ModalProps } from '../Modal';\nimport type { ModalSizeType } from '../types';\nimport type { ModalHeaderProps } from '../ModalHeader/ModalHeader';\nimport type { ModalContentProps } from '../ModalContent/ModalContent';\nimport type { ModalControlsProps } from '../ModalControls/ModalControls';\nimport { Button } from '@contentful/f36-button';\n\nexport interface ModalConfirmProps {\n /**\n * When true, the dialog is shown.\n */\n isShown: boolean;\n /**\n * Function that will be called when the confirm button is clicked. This does not close the ModalConfirm.\n */\n onConfirm(): void;\n /**\n * Function that will be called when the cancel button is clicked. This does not close the ModalConfirm.\n */\n onCancel: ModalProps['onClose'];\n /**\n Modal title that is used in header\n */\n title?: string;\n /**\n * Label of the confirm button\n */\n confirmLabel?: string | false;\n /**\n * Label of the cancel button\n */\n cancelLabel?: string | false;\n /**\n * The intent of the ModalConfirm. Used for the Button.\n */\n intent?: 'primary' | 'positive' | 'negative';\n /**\n Size of the modal window\n */\n size?: ModalSizeType;\n /**\n * Boolean indicating if clicking the overlay should close the overlay.\n */\n shouldCloseOnOverlayClick?: boolean;\n /**\n * Boolean indicating if pressing the esc key should close the overlay.\n */\n shouldCloseOnEscapePress?: boolean;\n /**\n * When true, the confirm button is set to disabled.\n */\n isConfirmDisabled?: boolean;\n /**\n * When true, the confirm button is set to loading.\n */\n isConfirmLoading?: boolean;\n /**\n * Are modals higher than viewport allowed\n */\n allowHeightOverflow?: boolean;\n\n /**\n * Optional props to override ModalHeader behaviour\n */\n modalHeaderProps?: Partial<ModalHeaderProps>;\n\n /**\n * Optional props to override ModalContent behaviour\n */\n modalContentProps?: Partial<ModalContentProps>;\n\n /**\n * Optional props to override ModalControl behaviour\n */\n modalControlsProps?: Partial<ModalControlsProps>;\n\n /**\n * Optional property to set initial focus\n */\n initialFocusRef?: React.RefObject<HTMLElement>;\n\n testId?: string;\n confirmTestId?: string;\n cancelTestId?: string;\n children: React.ReactNode;\n}\n\nexport function ModalConfirm({\n allowHeightOverflow = false,\n cancelLabel = 'Cancel',\n cancelTestId = 'cf-ui-modal-confirm-cancel-button',\n children,\n confirmLabel = 'Confirm',\n confirmTestId = 'cf-ui-modal-confirm-confirm-button',\n intent = 'positive',\n isConfirmDisabled = false,\n isConfirmLoading = false,\n isShown,\n modalContentProps,\n modalControlsProps,\n modalHeaderProps,\n onCancel,\n onConfirm,\n shouldCloseOnEscapePress = true,\n shouldCloseOnOverlayClick = true,\n size = 'medium',\n testId = 'cf-ui-modal-confirm',\n title = 'Are you sure?',\n initialFocusRef,\n}: ModalConfirmProps) {\n const cancelRef = React.useRef(null);\n\n const confirmButton = confirmLabel ? (\n <Button\n testId={confirmTestId}\n isDisabled={isConfirmDisabled}\n isLoading={isConfirmLoading}\n variant={intent}\n size=\"small\"\n onClick={() => onConfirm()}\n >\n {confirmLabel}\n </Button>\n ) : null;\n\n const cancelButton = cancelLabel ? (\n <Button\n testId={cancelTestId}\n variant=\"secondary\"\n onClick={onCancel}\n size=\"small\"\n ref={initialFocusRef || cancelRef}\n >\n {cancelLabel}\n </Button>\n ) : null;\n\n return (\n <Modal\n testId={testId}\n isShown={isShown}\n onClose={onCancel}\n size={size}\n shouldCloseOnOverlayClick={shouldCloseOnOverlayClick}\n shouldCloseOnEscapePress={shouldCloseOnEscapePress}\n allowHeightOverflow={allowHeightOverflow}\n initialFocusRef={cancelRef}\n >\n {() => {\n return (\n <React.Fragment>\n <Modal.Header title={title || ''} {...modalHeaderProps} />\n <Modal.Content {...modalContentProps}>{children}</Modal.Content>\n <Modal.Controls {...modalControlsProps}>\n {cancelButton}\n {confirmButton}\n </Modal.Controls>\n </React.Fragment>\n );\n }}\n </Modal>\n );\n}\n","/* global Promise */\nimport ReactDOM from 'react-dom';\n\n// @todo: change any to unknown\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface ModalLauncherComponentRendererProps<T = any> {\n isShown: boolean;\n onClose: (result?: T) => void;\n}\n\ninterface ModalLauncherOpenOptions {\n /**\n * Unique id to be used as identifier for the modal contianer\n */\n modalId?: string;\n /**\n * ms before removing the component from the tree\n * @default 300\n */\n delay?: number;\n}\n\ninterface CloseModalData {\n delay: number;\n render: (args: ModalLauncherComponentRendererProps<any>) => void;\n currentConfig: ModalLauncherComponentRendererProps<any>;\n}\n\nconst getRoot = (rootElId: string): HTMLElement => {\n // Reuse the container if we find it\n let rootDom = document.getElementById(rootElId);\n if (rootDom !== null) {\n return rootDom;\n }\n\n // Otherwise create it\n rootDom = document.createElement('div');\n rootDom.setAttribute('id', rootElId);\n document.body.appendChild(rootDom);\n return rootDom;\n};\n\nconst openModalsIds: Map<string, CloseModalData> = new Map();\nfunction closeAll() {\n openModalsIds.forEach(async ({ render, currentConfig, delay }, rootElId) => {\n const config = { ...currentConfig, isShown: false };\n render(config);\n await new Promise((resolveDelay) => setTimeout(resolveDelay, delay));\n ReactDOM.unmountComponentAtNode(getRoot(rootElId));\n openModalsIds.delete(rootElId);\n });\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction open<T = any>(\n componentRenderer: (\n props: ModalLauncherComponentRendererProps<T>,\n ) => JSX.Element,\n options: ModalLauncherOpenOptions = {},\n): Promise<T> {\n options = { delay: 300, ...options };\n\n // Allow components to specify if they wish to reuse the modal container\n const rootElId = `modals-root${options.modalId || Date.now()}`;\n const rootDom = getRoot(rootElId);\n\n return new Promise((resolve) => {\n let currentConfig = { onClose, isShown: true };\n\n function render({\n onClose,\n isShown,\n }: ModalLauncherComponentRendererProps<T>) {\n ReactDOM.render(componentRenderer({ onClose, isShown }), rootDom);\n }\n\n async function onClose(arg?: T) {\n currentConfig = {\n ...currentConfig,\n isShown: false,\n };\n render(currentConfig);\n await new Promise((resolveDelay) =>\n setTimeout(resolveDelay, options.delay),\n );\n ReactDOM.unmountComponentAtNode(rootDom);\n rootDom.remove();\n openModalsIds.delete(rootElId);\n resolve(arg);\n }\n\n render(currentConfig);\n openModalsIds.set(rootElId, {\n render,\n currentConfig,\n delay: options.delay,\n });\n });\n}\n\nexport const ModalLauncher = {\n open,\n closeAll,\n};\n","export { Modal } from './CompoundModal';\nexport type { ModalProps } from './Modal';\nexport { ModalConfirm } from './ModalConfirm/ModalConfirm';\nexport type { ModalConfirmProps } from './ModalConfirm/ModalConfirm';\nexport { ModalContent } from './ModalContent/ModalContent';\nexport type { ModalContentProps } from './ModalContent/ModalContent';\nexport { ModalControls } from './ModalControls/ModalControls';\nexport type { ModalControlsProps } from './ModalControls/ModalControls';\nexport { ModalHeader } from './ModalHeader/ModalHeader';\nexport type { ModalHeaderProps } from './ModalHeader/ModalHeader';\nexport { ModalLauncher } from './ModalLauncher/ModalLauncher';\n","export * from './src/index';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@contentful/f36-modal",
3
+ "version": "4.0.0",
4
+ "description": "Forma 36: Modal component",
5
+ "scripts": {
6
+ "build": "parcel build index.ts"
7
+ },
8
+ "dependencies": {
9
+ "@babel/runtime": "^7.6.2",
10
+ "@contentful/f36-button": "^4.0.0",
11
+ "@contentful/f36-core": "^4.0.0",
12
+ "@contentful/f36-icons": "^4.0.0",
13
+ "@contentful/f36-tokens": "^4.0.0",
14
+ "@contentful/f36-typography": "^4.0.0",
15
+ "@types/react-modal": "^3.12.1",
16
+ "emotion": "^10.0.17",
17
+ "react-modal": "^3.14.3"
18
+ },
19
+ "peerDependencies": {
20
+ "react": ">=16.8",
21
+ "react-dom": ">=16.8"
22
+ },
23
+ "license": "MIT",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "main": "dist/main.js",
28
+ "module": "dist/module.js",
29
+ "types": "dist/types.d.ts",
30
+ "source": "src/index.ts",
31
+ "sideEffects": false,
32
+ "browserslist": "extends @contentful/browserslist-config",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/contentful/forma-36"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "gitHead": "01d931c77410619d05cd00dbb3b5acc5d8ae2cdf"
41
+ }