Skip to content

Instantly share code, notes, and snippets.

@kgregory
Last active January 18, 2022 03:39
Show Gist options
  • Save kgregory/963f68663ea46bb6ba572ee34954ea96 to your computer and use it in GitHub Desktop.
Save kgregory/963f68663ea46bb6ba572ee34954ea96 to your computer and use it in GitHub Desktop.
Inline component, horizontally flex components
import * as React from "react";
import { makeStyles } from "@material-ui/core/styles";
interface InlineProps
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 ? "marginLeft" : "marginRight";
const direction = reverse ? "row-reverse" : "row";
return {
display: "flex",
flexDirection: direction,
"& > *": {
[attribute]: theme.spacing(gap)
},
"& > *:last-child": {
[attribute]: 0
}
};
}
}));
const Inline: React.FC<InlineProps> = ({
gap = 1,
reverse = false,
children,
...other
}) => {
const classes = useStyles({ gap, reverse });
return (
<div className={classes.root} {...other}>
{children}
</div>
);
};
export default Inline;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment