|
| 1 | +import { makeStyles } from "@material-ui/core/styles" |
| 2 | +import { fade } from "@material-ui/core/styles/colorManipulator" |
| 3 | +import Typography from "@material-ui/core/Typography" |
| 4 | +import React from "react" |
| 5 | +import { SectionAction } from "./Action" |
| 6 | + |
| 7 | +type SectionLayout = "fixed" | "fluid" |
| 8 | + |
| 9 | +export interface SectionProps { |
| 10 | + title?: React.ReactNode | string |
| 11 | + description?: React.ReactNode |
| 12 | + toolbar?: React.ReactNode |
| 13 | + alert?: React.ReactNode |
| 14 | + layout?: SectionLayout |
| 15 | + children?: React.ReactNode |
| 16 | +} |
| 17 | + |
| 18 | +type SectionFC = React.FC<SectionProps> & { Action: typeof SectionAction } |
| 19 | + |
| 20 | +export const Section: SectionFC = ({ title, description, toolbar, alert, children, layout = "fixed" }) => { |
| 21 | + const styles = useStyles({ layout }) |
| 22 | + return ( |
| 23 | + <div className={styles.root}> |
| 24 | + <div className={styles.inner}> |
| 25 | + {(title || description) && ( |
| 26 | + <div className={styles.header}> |
| 27 | + <div> |
| 28 | + {title && <Typography variant="h4">{title}</Typography>} |
| 29 | + {description && typeof description === "string" && ( |
| 30 | + <Typography className={styles.description}>{description}</Typography> |
| 31 | + )} |
| 32 | + {description && typeof description !== "string" && ( |
| 33 | + <div className={styles.description}>{description}</div> |
| 34 | + )} |
| 35 | + </div> |
| 36 | + {toolbar && <div>{toolbar}</div>} |
| 37 | + </div> |
| 38 | + )} |
| 39 | + {alert && <div className={styles.alert}>{alert}</div>} |
| 40 | + {children} |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +// Sub-components |
| 47 | +Section.Action = SectionAction |
| 48 | + |
| 49 | +const useStyles = makeStyles((theme) => ({ |
| 50 | + root: { |
| 51 | + backgroundColor: theme.palette.background.paper, |
| 52 | + boxShadow: `0px 18px 12px 6px ${fade(theme.palette.common.black, 0.02)}`, |
| 53 | + marginBottom: theme.spacing(1), |
| 54 | + padding: theme.spacing(6), |
| 55 | + }, |
| 56 | + inner: ({ layout }: { layout: SectionLayout }) => ({ |
| 57 | + maxWidth: layout === "fluid" ? "100%" : 500, |
| 58 | + }), |
| 59 | + alert: { |
| 60 | + marginBottom: theme.spacing(1), |
| 61 | + }, |
| 62 | + header: { |
| 63 | + marginBottom: theme.spacing(4), |
| 64 | + display: "flex", |
| 65 | + flexDirection: "row", |
| 66 | + justifyContent: "space-between", |
| 67 | + }, |
| 68 | + description: { |
| 69 | + color: theme.palette.text.secondary, |
| 70 | + fontSize: 16, |
| 71 | + marginTop: theme.spacing(2), |
| 72 | + }, |
| 73 | +})) |
0 commit comments