2022-08-31 02:35:06 -07:00
|
|
|
import classNames from 'clsx';
|
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',
|
2022-05-23 12:14:42 -07:00
|
|
|
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 = {
|
2022-10-04 12:17:26 -07:00
|
|
|
[0.5]: 'space-x-0.5',
|
2022-03-21 11:09:01 -07:00
|
|
|
1: 'space-x-1',
|
|
|
|
1.5: 'space-x-1.5',
|
|
|
|
2: 'space-x-2',
|
2022-11-25 09:04:11 -08:00
|
|
|
2.5: 'space-x-2.5',
|
2022-03-21 11:09:01 -07:00
|
|
|
3: 'space-x-3',
|
|
|
|
4: 'space-x-4',
|
2022-11-25 09:04:11 -08:00
|
|
|
5: 'space-x-5',
|
2022-03-21 11:09:01 -07:00
|
|
|
6: 'space-x-6',
|
2022-06-22 05:56:50 -07:00
|
|
|
8: 'space-x-8',
|
2022-03-21 11:09:01 -07:00
|
|
|
};
|
|
|
|
|
2022-11-20 10:33:53 -08:00
|
|
|
interface IHStack extends Pick<React.HTMLAttributes<HTMLDivElement>, 'onClick'> {
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Vertical alignment of children. */
|
2022-10-04 12:08:22 -07:00
|
|
|
alignItems?: keyof typeof alignItemsOptions
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Extra class names on the <div> element. */
|
2022-10-04 12:08:22 -07:00
|
|
|
className?: string
|
2022-07-20 06:01:36 -07:00
|
|
|
/** Children */
|
2022-10-04 12:08:22 -07:00
|
|
|
children?: React.ReactNode
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Horizontal alignment of children. */
|
2022-10-04 12:08:22 -07:00
|
|
|
justifyContent?: keyof typeof justifyContentOptions
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Size of the gap between elements. */
|
2022-10-04 12:08:22 -07:00
|
|
|
space?: keyof typeof spaces
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Whether to let the flexbox grow. */
|
2022-10-04 12:08:22 -07:00
|
|
|
grow?: boolean
|
2022-11-05 03:46:15 -07:00
|
|
|
/** HTML element to use for container. */
|
|
|
|
element?: keyof JSX.IntrinsicElements,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Extra CSS styles for the <div> */
|
2022-03-23 05:40:21 -07:00
|
|
|
style?: React.CSSProperties
|
2022-09-05 06:18:15 -07:00
|
|
|
/** Whether to let the flexbox wrap onto multiple lines. */
|
2022-10-04 12:08:22 -07:00
|
|
|
wrap?: boolean
|
2022-03-21 11:09:01 -07:00
|
|
|
}
|
|
|
|
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Horizontal row of child elements. */
|
2022-07-20 06:01:36 -07:00
|
|
|
const HStack = forwardRef<HTMLDivElement, IHStack>((props, ref) => {
|
2022-11-05 03:46:15 -07:00
|
|
|
const { space, alignItems, justifyContent, className, grow, element = 'div', wrap, ...filteredProps } = props;
|
|
|
|
|
|
|
|
const Elem = element as 'div';
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
return (
|
2022-11-05 03:46:15 -07:00
|
|
|
<Elem
|
2022-03-21 11:09:01 -07:00
|
|
|
{...filteredProps}
|
2022-07-20 06:01:36 -07:00
|
|
|
ref={ref}
|
2022-11-25 09:04:11 -08:00
|
|
|
className={classNames('flex rtl:space-x-reverse', {
|
2022-03-24 12:27:27 -07:00
|
|
|
// @ts-ignore
|
2022-03-21 11:09:01 -07:00
|
|
|
[alignItemsOptions[alignItems]]: typeof alignItems !== 'undefined',
|
2022-03-24 12:27:27 -07:00
|
|
|
// @ts-ignore
|
2022-03-21 11:09:01 -07:00
|
|
|
[justifyContentOptions[justifyContent]]: typeof justifyContent !== 'undefined',
|
2022-03-24 12:27:27 -07:00
|
|
|
// @ts-ignore
|
2022-03-21 11:09:01 -07:00
|
|
|
[spaces[space]]: typeof space !== 'undefined',
|
|
|
|
'flex-grow': grow,
|
2022-09-05 06:18:15 -07:00
|
|
|
'flex-wrap': wrap,
|
2022-03-24 12:27:27 -07:00
|
|
|
}, 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;
|