Skip to content

Instantly share code, notes, and snippets.

@kgregory
Last active January 18, 2022 03:40
Show Gist options
  • Save kgregory/d52e38ac9b3797133a534167bb69492f to your computer and use it in GitHub Desktop.
Save kgregory/d52e38ac9b3797133a534167bb69492f to your computer and use it in GitHub Desktop.
Stack component, vertically flex components
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