pleroma/app/soapbox/components/ui/stack/stack.tsx

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-02-06 10:01:03 -08:00
import clsx from 'clsx';
2022-03-21 11:09:01 -07:00
import React from 'react';
const spaces = {
2022-07-01 13:07:16 -07:00
0: 'space-y-0',
2022-10-04 12:17:51 -07:00
[0.5]: 'space-y-0.5',
2022-03-21 11:09:01 -07:00
1: 'space-y-1',
2022-10-04 12:17:51 -07:00
[1.5]: 'space-y-1.5',
2022-03-21 11:09:01 -07:00
2: 'space-y-2',
3: 'space-y-3',
4: 'space-y-4',
5: 'space-y-5',
6: 'space-y-6',
2023-03-08 11:43:16 -08:00
9: 'space-y-9',
2022-04-12 06:50:17 -07:00
10: 'space-y-10',
2022-03-21 11:09:01 -07:00
};
const justifyContentOptions = {
2023-05-30 06:02:22 -07:00
between: 'justify-between',
2022-03-21 11:09:01 -07:00
center: 'justify-center',
end: 'justify-end',
2022-03-21 11:09:01 -07:00
};
const alignItemsOptions = {
top: 'items-start',
bottom: 'items-end',
2022-03-21 11:09:01 -07:00
center: 'items-center',
start: 'items-start',
end: 'items-end',
2022-03-21 11:09:01 -07:00
};
interface IStack extends React.HTMLAttributes<HTMLDivElement> {
/** Horizontal alignment of children. */
alignItems?: keyof typeof alignItemsOptions
/** Extra class names on the element. */
className?: string
/** Vertical alignment of children. */
justifyContent?: keyof typeof justifyContentOptions
/** Size of the gap between elements. */
space?: keyof typeof spaces
2022-08-02 18:52:27 -07:00
/** Whether to let the flexbox grow. */
2022-10-04 12:08:22 -07:00
grow?: boolean
/** HTML element to use for container. */
element?: keyof JSX.IntrinsicElements
2022-03-21 11:09:01 -07:00
}
/** Vertical stack of child elements. */
const Stack = React.forwardRef<HTMLDivElement, IStack>((props, ref: React.LegacyRef<HTMLDivElement> | undefined) => {
const { space, alignItems, justifyContent, className, grow, element = 'div', ...filteredProps } = props;
const Elem = element as 'div';
2022-03-21 11:09:01 -07:00
return (
<Elem
2022-03-21 11:09:01 -07:00
{...filteredProps}
2022-08-31 11:31:44 -07:00
ref={ref}
2023-02-06 10:01:03 -08:00
className={clsx('flex flex-col', {
// @ts-ignore
2022-03-21 11:09:01 -07:00
[spaces[space]]: typeof space !== 'undefined',
// @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',
2023-02-06 10:06:44 -08:00
'grow': grow,
}, className)}
2022-03-21 11:09:01 -07:00
/>
);
2022-08-31 11:31:44 -07:00
});
2022-03-21 11:09:01 -07:00
export default Stack;