soapbox/app/soapbox/components/ui/hstack/hstack.tsx

79 wiersze
2.1 KiB
TypeScript
Czysty Zwykły widok Historia

2023-02-06 18:01:03 +00:00
import clsx from 'clsx';
2022-07-20 13:01:36 +00:00
import React, { forwardRef } from 'react';
2022-03-21 18:09:01 +00:00
const justifyContentOptions = {
between: 'justify-between',
center: 'justify-center',
start: 'justify-start',
end: 'justify-end',
2022-07-01 20:07:16 +00:00
around: 'justify-around',
2022-03-21 18:09:01 +00:00
};
const alignItemsOptions = {
top: 'items-start',
bottom: 'items-end',
center: 'items-center',
start: 'items-start',
stretch: 'items-stretch',
2022-03-21 18:09:01 +00:00
};
const spaces = {
2022-10-04 19:17:26 +00:00
[0.5]: 'space-x-0.5',
2022-03-21 18:09:01 +00:00
1: 'space-x-1',
1.5: 'space-x-1.5',
2: 'space-x-2',
2.5: 'space-x-2.5',
2022-03-21 18:09:01 +00:00
3: 'space-x-3',
4: 'space-x-4',
5: 'space-x-5',
2022-03-21 18:09:01 +00:00
6: 'space-x-6',
2022-06-22 12:56:50 +00:00
8: 'space-x-8',
2022-03-21 18:09:01 +00:00
};
interface IHStack extends Pick<React.HTMLAttributes<HTMLDivElement>, 'onClick'> {
/** Vertical alignment of children. */
2022-10-04 19:08:22 +00:00
alignItems?: keyof typeof alignItemsOptions
/** Extra class names on the <div> element. */
2022-10-04 19:08:22 +00:00
className?: string
2022-07-20 13:01:36 +00:00
/** Children */
2022-10-04 19:08:22 +00:00
children?: React.ReactNode
/** Horizontal alignment of children. */
2022-10-04 19:08:22 +00:00
justifyContent?: keyof typeof justifyContentOptions
/** Size of the gap between elements. */
2022-10-04 19:08:22 +00:00
space?: keyof typeof spaces
/** Whether to let the flexbox grow. */
2022-10-04 19:08:22 +00:00
grow?: boolean
/** HTML element to use for container. */
element?: keyof JSX.IntrinsicElements
/** Extra CSS styles for the <div> */
style?: React.CSSProperties
/** Whether to let the flexbox wrap onto multiple lines. */
2022-10-04 19:08:22 +00:00
wrap?: boolean
2022-03-21 18:09:01 +00:00
}
/** Horizontal row of child elements. */
2022-07-20 13:01:36 +00:00
const HStack = forwardRef<HTMLDivElement, IHStack>((props, ref) => {
const { space, alignItems, justifyContent, className, grow, element = 'div', wrap, ...filteredProps } = props;
const Elem = element as 'div';
2022-03-21 18:09:01 +00:00
return (
<Elem
2022-03-21 18:09:01 +00:00
{...filteredProps}
2022-07-20 13:01:36 +00:00
ref={ref}
2023-02-06 18:01:03 +00:00
className={clsx('flex rtl:space-x-reverse', {
// @ts-ignore
2022-03-21 18:09:01 +00:00
[alignItemsOptions[alignItems]]: typeof alignItems !== 'undefined',
// @ts-ignore
2022-03-21 18:09:01 +00:00
[justifyContentOptions[justifyContent]]: typeof justifyContent !== 'undefined',
// @ts-ignore
2022-03-21 18:09:01 +00:00
[spaces[space]]: typeof space !== 'undefined',
2023-02-06 18:06:44 +00:00
'grow': grow,
'flex-wrap': wrap,
}, className)}
2022-03-21 18:09:01 +00:00
/>
);
2022-07-20 13:01:36 +00:00
});
2022-03-21 18:09:01 +00:00
export default HStack;