bigbuffet-rw/app/soapbox/components/ui/hstack/hstack.tsx

69 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-03-21 11:09:01 -07:00
import classNames from 'classnames';
2022-07-20 06:01:36 -07:00
import React, { forwardRef } from 'react';
2022-03-21 11:09:01 -07:00
const justifyContentOptions = {
between: 'justify-between',
center: 'justify-center',
start: 'justify-start',
end: 'justify-end',
2022-07-01 13:07:16 -07:00
around: 'justify-around',
2022-03-21 11:09:01 -07:00
};
const alignItemsOptions = {
top: 'items-start',
bottom: 'items-end',
center: 'items-center',
start: 'items-start',
};
const spaces = {
'0.5': 'space-x-0.5',
1: 'space-x-1',
1.5: 'space-x-1.5',
2: 'space-x-2',
3: 'space-x-3',
4: 'space-x-4',
6: 'space-x-6',
2022-06-22 05:56:50 -07:00
8: 'space-x-8',
2022-03-21 11:09:01 -07:00
};
interface IHStack {
/** Vertical alignment of children. */
2022-03-21 11:09:01 -07:00
alignItems?: 'top' | 'bottom' | 'center' | 'start',
/** Extra class names on the <div> element. */
2022-03-21 11:09:01 -07:00
className?: string,
2022-07-20 06:01:36 -07:00
/** Children */
children?: React.ReactNode,
/** Horizontal alignment of children. */
2022-07-01 13:07:16 -07:00
justifyContent?: 'between' | 'center' | 'start' | 'end' | 'around',
/** Size of the gap between elements. */
2022-06-22 05:56:50 -07:00
space?: 0.5 | 1 | 1.5 | 2 | 3 | 4 | 6 | 8,
/** Whether to let the flexbox grow. */
2022-03-21 11:09:01 -07:00
grow?: boolean,
/** Extra CSS styles for the <div> */
style?: React.CSSProperties
2022-03-21 11:09:01 -07:00
}
/** Horizontal row of child elements. */
2022-07-20 06:01:36 -07:00
const HStack = forwardRef<HTMLDivElement, IHStack>((props, ref) => {
2022-03-21 11:09:01 -07:00
const { space, alignItems, grow, justifyContent, className, ...filteredProps } = props;
return (
<div
{...filteredProps}
2022-07-20 06:01:36 -07:00
ref={ref}
2022-03-21 11:09:01 -07:00
className={classNames('flex', {
// @ts-ignore
2022-03-21 11:09:01 -07:00
[alignItemsOptions[alignItems]]: typeof alignItems !== 'undefined',
// @ts-ignore
2022-03-21 11:09:01 -07:00
[justifyContentOptions[justifyContent]]: typeof justifyContent !== 'undefined',
// @ts-ignore
2022-03-21 11:09:01 -07:00
[spaces[space]]: typeof space !== 'undefined',
'flex-grow': grow,
}, className)}
2022-03-21 11:09:01 -07:00
/>
);
2022-07-20 06:01:36 -07:00
});
2022-03-21 11:09:01 -07:00
export default HStack;