|
| 1 | +import { Button } from '../button'; |
| 2 | +import { classNames, useDOMRef } from '../utils'; |
| 3 | +import { CloseOutline, Icon } from '../icon'; |
| 4 | +import { DialogContext, DialogContextValue } from './context'; |
| 5 | +import { DOMRef } from '@react-types/shared'; |
| 6 | +import { FocusScope } from '@react-aria/focus'; |
| 7 | +import { mergeProps } from '@react-aria/utils'; |
| 8 | +import React, { useContext } from 'react'; |
| 9 | +import { useDialog } from '@react-aria/dialog'; |
| 10 | +import { DialogProps } from '../types/dialog'; |
| 11 | +import { Heading } from '../content'; |
| 12 | +import { css } from '@emotion/core'; |
| 13 | +import theme from '../theme'; |
| 14 | + |
| 15 | +const dialogCSS = css` |
| 16 | + outline: none; |
| 17 | + &.ac-dialog--slideOver { |
| 18 | + &.ac-dialog--small { |
| 19 | + width: 400px; |
| 20 | + } |
| 21 | + &.ac-dialog--medium { |
| 22 | + width: 700px; |
| 23 | + } |
| 24 | + &.ac-dialog--large { |
| 25 | + width: 900px; |
| 26 | + } |
| 27 | + } |
| 28 | +`; |
| 29 | +let sizeMap = { |
| 30 | + S: 'small', |
| 31 | + M: 'medium', |
| 32 | + L: 'large', |
| 33 | + fullscreen: 'fullscreen', |
| 34 | + fullscreenTakeover: 'fullscreenTakeover', |
| 35 | +}; |
| 36 | + |
| 37 | +function Dialog(props: DialogProps, ref: DOMRef) { |
| 38 | + let { type = 'modal', ...contextProps } = |
| 39 | + useContext(DialogContext) || ({} as DialogContextValue); |
| 40 | + let { |
| 41 | + children, |
| 42 | + isDismissable = contextProps.isDismissable, |
| 43 | + onDismiss = contextProps.onClose, |
| 44 | + size, |
| 45 | + title, |
| 46 | + extra, |
| 47 | + } = props; |
| 48 | + let domRef = useDOMRef(ref); |
| 49 | + size = size || 'S'; |
| 50 | + let sizeVariant = sizeMap[size]; |
| 51 | + const { dialogProps, titleProps } = useDialog( |
| 52 | + mergeProps(contextProps, props), |
| 53 | + domRef |
| 54 | + ); |
| 55 | + |
| 56 | + return ( |
| 57 | + <FocusScope contain restoreFocus> |
| 58 | + <section |
| 59 | + {...dialogProps} |
| 60 | + className={classNames('ac-dialog', { |
| 61 | + [`ac-dialog--${sizeVariant}`]: sizeVariant, |
| 62 | + [`ac-dialog--${type}`]: type, |
| 63 | + 'ac-dialog--dismissable': isDismissable, |
| 64 | + })} |
| 65 | + ref={domRef} |
| 66 | + css={dialogCSS} |
| 67 | + > |
| 68 | + <Heading |
| 69 | + level={2} |
| 70 | + {...titleProps} |
| 71 | + css={css` |
| 72 | + display: flex; |
| 73 | + flex-direction: row; |
| 74 | + justify-content: space-between; |
| 75 | + align-items: center; |
| 76 | + padding: ${theme.spacing.padding8}px ${theme.spacing.padding16}px; |
| 77 | + border-bottom: 1px solid ${theme.colors.gray500}; |
| 78 | + `} |
| 79 | + > |
| 80 | + {title} |
| 81 | + <div |
| 82 | + css={css` |
| 83 | + display: flex; |
| 84 | + flex-direction: row; |
| 85 | + .ac-dialog__dismiss-button { |
| 86 | + margin-left: ${theme.spacing.margin8}px; |
| 87 | + } |
| 88 | + `} |
| 89 | + > |
| 90 | + {extra} |
| 91 | + {isDismissable && ( |
| 92 | + <Button |
| 93 | + variant="default" |
| 94 | + aria-label="dismiss" |
| 95 | + onClick={onDismiss} |
| 96 | + icon={<Icon svg={<CloseOutline />} />} |
| 97 | + size="compact" |
| 98 | + className="ac-dialog__dismiss-button" |
| 99 | + /> |
| 100 | + )} |
| 101 | + </div> |
| 102 | + </Heading> |
| 103 | + {children} |
| 104 | + </section> |
| 105 | + </FocusScope> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Dialogs are windows containing contextual information, tasks, or workflows that appear over the user interface. |
| 111 | + * Depending on the kind of Dialog, further interactions may be blocked until the Dialog is acknowledged. |
| 112 | + */ |
| 113 | +let _Dialog = React.forwardRef(Dialog); |
| 114 | +export { _Dialog as Dialog }; |
0 commit comments