Last active
January 18, 2022 03:40
-
-
Save kgregory/d52e38ac9b3797133a534167bb69492f to your computer and use it in GitHub Desktop.
Stack component, vertically flex components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import { makeStyles } from "@material-ui/core/styles"; | |
interface StackProps | |
extends React.DetailedHTMLProps< | |
React.HTMLAttributes<HTMLDivElement>, | |
HTMLDivElement | |
> { | |
gap?: number; | |
reverse?: boolean; | |
} | |
const useStyles = makeStyles((theme) => ({ | |
root: ({ gap, reverse }: { gap: number; reverse: boolean }) => { | |
const attribute = reverse ? "marginTop" : "marginBottom"; | |
const direction = reverse ? "column-reverse" : "column"; | |
return { | |
display: "flex", | |
flexDirection: direction, | |
"& > *": { | |
[attribute]: theme.spacing(gap) | |
}, | |
"& > *:last-child": { | |
[attribute]: 0 | |
} | |
}; | |
} | |
})); | |
const Stack: React.FC<StackProps> = ({ | |
gap = 1, | |
reverse = false, | |
children, | |
...other | |
}) => { | |
const classes = useStyles({ gap, reverse }); | |
return ( | |
<div className={classes.root} {...other}> | |
{children} | |
</div> | |
); | |
}; | |
export default Stack; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment