|
1 | 1 | import classNames from "classnames"; |
2 | 2 | import styles from "./styles/index.module.scss"; |
3 | | -import React, { useCallback, useMemo, useState } from "react"; |
| 3 | +import React, { useCallback, useEffect, useMemo, useState } from "react"; |
4 | 4 | import { ModalContext, type ModalContextProps, type ModalContextValue } from "./context"; |
5 | 5 | import { Card } from "@react-ck/card"; |
6 | 6 | import { Overlay } from "@react-ck/overlay"; |
@@ -40,33 +40,51 @@ export const Modal = ({ |
40 | 40 | [setContextValue], |
41 | 41 | ); |
42 | 42 |
|
| 43 | + // Invoke open / close handlers |
| 44 | + useEffect(() => { |
| 45 | + if (internalOpen) onOpen?.(); |
| 46 | + else onClose?.(); |
| 47 | + }, [internalOpen, onClose, onOpen]); |
| 48 | + |
| 49 | + // Synchronize internal state with external state |
| 50 | + useEffect(() => { |
| 51 | + setInternalOpen(open); |
| 52 | + }, [open]); |
| 53 | + |
43 | 54 | return ( |
44 | | - <Layer elevation="overlay"> |
45 | | - <div {...otherProps} className={classNames(styles.root, className)}> |
46 | | - <Overlay /> |
47 | | - <Card className={styles.card}> |
48 | | - <ModalContext.Provider value={contextProps}> |
49 | | - {props.header && ( |
50 | | - <header |
51 | | - {...props.header} |
52 | | - className={classNames(styles.header, props.header.className)}> |
53 | | - <Text type="h3" as="p" margin={false}> |
54 | | - {props.header.heading} |
55 | | - </Text> |
| 55 | + internalOpen && ( |
| 56 | + <Layer elevation="overlay"> |
| 57 | + <div {...otherProps} className={classNames(styles.root, className)}> |
| 58 | + <Overlay |
| 59 | + className={classNames(dismissable && styles.clickable_overlay)} |
| 60 | + onClick={handleClose} |
| 61 | + /> |
| 62 | + <Card className={styles.card}> |
| 63 | + <ModalContext.Provider value={contextProps}> |
| 64 | + {props.header && ( |
| 65 | + <header |
| 66 | + {...props.header} |
| 67 | + className={classNames(styles.header, props.header.className)}> |
| 68 | + <Text type="h3" as="p" margin={false}> |
| 69 | + {props.header.heading} |
| 70 | + </Text> |
56 | 71 |
|
57 | | - <Button skin="ghost" icon={<Icon name="close" />} /> |
58 | | - </header> |
59 | | - )} |
60 | | - <main className={styles.content}>{children}</main> |
61 | | - {props.footer && ( |
62 | | - <footer |
63 | | - {...props.footer} |
64 | | - className={classNames(styles.footer, props.footer.className)} |
65 | | - /> |
66 | | - )} |
67 | | - </ModalContext.Provider> |
68 | | - </Card> |
69 | | - </div> |
70 | | - </Layer> |
| 72 | + {dismissable && ( |
| 73 | + <Button skin="ghost" icon={<Icon name="close" />} onClick={handleClose} /> |
| 74 | + )} |
| 75 | + </header> |
| 76 | + )} |
| 77 | + <main className={styles.content}>{children}</main> |
| 78 | + {props.footer && ( |
| 79 | + <footer |
| 80 | + {...props.footer} |
| 81 | + className={classNames(styles.footer, props.footer.className)} |
| 82 | + /> |
| 83 | + )} |
| 84 | + </ModalContext.Provider> |
| 85 | + </Card> |
| 86 | + </div> |
| 87 | + </Layer> |
| 88 | + ) |
71 | 89 | ); |
72 | 90 | }; |
0 commit comments