|
| 1 | +import Button from "@material-ui/core/Button" |
| 2 | +import Popover from "@material-ui/core/Popover" |
| 3 | +import { makeStyles } from "@material-ui/core/styles" |
| 4 | +import { CloseDropdown, OpenDropdown } from "components/DropdownArrows/DropdownArrows" |
| 5 | +import { DropdownContent } from "components/DropdownButton/DropdownContent/DropdownContent" |
| 6 | +import { FC, ReactNode, useRef, useState } from "react" |
| 7 | +import { CancelButton } from "./ActionCtas" |
| 8 | + |
| 9 | +export interface DropdownButtonProps { |
| 10 | + primaryAction: ReactNode |
| 11 | + secondaryActions: Array<{ action: string; button: ReactNode }> |
| 12 | + canCancel: boolean |
| 13 | + handleCancel?: () => void |
| 14 | +} |
| 15 | + |
| 16 | +export const DropdownButton: FC<DropdownButtonProps> = ({ |
| 17 | + primaryAction, |
| 18 | + secondaryActions, |
| 19 | + canCancel, |
| 20 | + handleCancel, |
| 21 | +}) => { |
| 22 | + const styles = useStyles() |
| 23 | + const anchorRef = useRef<HTMLButtonElement>(null) |
| 24 | + const [isOpen, setIsOpen] = useState(false) |
| 25 | + const id = isOpen ? "action-popover" : undefined |
| 26 | + |
| 27 | + return ( |
| 28 | + <span className={styles.buttonContainer}> |
| 29 | + {/* primary workspace CTA */} |
| 30 | + <span data-testid="primary-cta" className={styles.primaryCta}> |
| 31 | + {primaryAction} |
| 32 | + </span> |
| 33 | + {canCancel && handleCancel ? ( |
| 34 | + <CancelButton handleAction={handleCancel} /> |
| 35 | + ) : ( |
| 36 | + <> |
| 37 | + {/* popover toggle button */} |
| 38 | + <Button |
| 39 | + data-testid="workspace-actions-button" |
| 40 | + aria-controls="workspace-actions-menu" |
| 41 | + aria-haspopup="true" |
| 42 | + className={styles.dropdownButton} |
| 43 | + ref={anchorRef} |
| 44 | + disabled={!secondaryActions.length} |
| 45 | + onClick={() => { |
| 46 | + setIsOpen(true) |
| 47 | + }} |
| 48 | + > |
| 49 | + {isOpen ? <CloseDropdown /> : <OpenDropdown />} |
| 50 | + </Button> |
| 51 | + <Popover |
| 52 | + classes={{ paper: styles.popoverPaper }} |
| 53 | + id={id} |
| 54 | + open={isOpen} |
| 55 | + anchorEl={anchorRef.current} |
| 56 | + onClose={() => setIsOpen(false)} |
| 57 | + onBlur={() => setIsOpen(false)} |
| 58 | + anchorOrigin={{ |
| 59 | + vertical: "bottom", |
| 60 | + horizontal: "right", |
| 61 | + }} |
| 62 | + transformOrigin={{ |
| 63 | + vertical: "top", |
| 64 | + horizontal: "right", |
| 65 | + }} |
| 66 | + > |
| 67 | + {/* secondary workspace CTAs */} |
| 68 | + <DropdownContent secondaryActions={secondaryActions} /> |
| 69 | + </Popover> |
| 70 | + </> |
| 71 | + )} |
| 72 | + </span> |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +const useStyles = makeStyles((theme) => ({ |
| 77 | + buttonContainer: { |
| 78 | + border: `1px solid ${theme.palette.divider}`, |
| 79 | + borderRadius: `${theme.shape.borderRadius}px`, |
| 80 | + display: "inline-flex", |
| 81 | + }, |
| 82 | + dropdownButton: { |
| 83 | + border: "none", |
| 84 | + borderLeft: `1px solid ${theme.palette.divider}`, |
| 85 | + borderRadius: `0px ${theme.shape.borderRadius}px ${theme.shape.borderRadius}px 0px`, |
| 86 | + minWidth: "unset", |
| 87 | + width: "63px", // matching cancel button so button grouping doesn't grow in size |
| 88 | + "& .MuiButton-label": { |
| 89 | + marginRight: "8px", |
| 90 | + }, |
| 91 | + }, |
| 92 | + primaryCta: { |
| 93 | + [theme.breakpoints.down("sm")]: { |
| 94 | + width: "100%", |
| 95 | + |
| 96 | + "& > *": { |
| 97 | + width: "100%", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + popoverPaper: { |
| 102 | + padding: `${theme.spacing(1)}px ${theme.spacing(2)}px ${theme.spacing(1)}px`, |
| 103 | + }, |
| 104 | +})) |
0 commit comments