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

59 lines
1.5 KiB
TypeScript
Raw Normal View History

import classNames 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',
2022-04-12 06:50:17 -07:00
10: 'space-y-10',
2022-03-21 11:09:01 -07:00
};
const justifyContentOptions = {
center: 'justify-center',
};
const alignItemsOptions = {
center: 'items-center',
};
interface IStack extends React.HTMLAttributes<HTMLDivElement> {
/** Size of the gap between elements. */
2022-10-04 12:08:22 -07:00
space?: keyof typeof spaces
/** Horizontal alignment of children. */
2022-10-04 12:08:22 -07:00
alignItems?: 'center'
/** Vertical alignment of children. */
2022-10-04 12:08:22 -07:00
justifyContent?: 'center'
/** Extra class names on the <div> element. */
2022-10-04 12:08:22 -07:00
className?: string
2022-08-02 18:52:27 -07:00
/** Whether to let the flexbox grow. */
2022-10-04 12:08:22 -07:00
grow?: boolean
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) => {
2022-08-02 18:52:27 -07:00
const { space, alignItems, justifyContent, className, grow, ...filteredProps } = props;
2022-03-21 11:09:01 -07:00
return (
<div
{...filteredProps}
2022-08-31 11:31:44 -07:00
ref={ref}
2022-03-21 11:09:01 -07:00
className={classNames('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',
2022-08-02 18:52:27 -07:00
'flex-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;