nitro-react/src/common/Flex.tsx

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-12-04 07:25:47 +01:00
import { FC, useMemo } from 'react';
import { Base, BaseProps } from './Base';
2022-02-09 04:25:01 +01:00
import { AlignItemType, AlignSelfType, JustifyContentType, SpacingType } from './types';
2021-12-04 07:25:47 +01:00
export interface FlexProps extends BaseProps<HTMLDivElement>
{
column?: boolean;
reverse?: boolean;
gap?: SpacingType;
center?: boolean;
2022-02-01 07:58:28 +01:00
alignSelf?: AlignSelfType;
2021-12-04 07:25:47 +01:00
alignItems?: AlignItemType;
justifyContent?: JustifyContentType;
}
export const Flex: FC<FlexProps> = props =>
{
2022-03-08 06:11:57 +01:00
const { display = 'flex', column = undefined, reverse = false, gap = null, center = false, alignSelf = null, alignItems = null, justifyContent = null, classNames = [], ...rest } = props;
2021-12-04 07:25:47 +01:00
const getClassNames = useMemo(() =>
{
const newClassNames: string[] = [];
if(column)
{
if(reverse) newClassNames.push('flex-column-reverse');
else newClassNames.push('flex-column');
}
else
{
if(reverse) newClassNames.push('flex-row-reverse');
}
if(gap) newClassNames.push('gap-' + gap);
2022-02-01 07:58:28 +01:00
if(alignSelf) newClassNames.push('align-self-' + alignSelf);
2021-12-29 17:45:05 +01:00
if(alignItems) newClassNames.push('align-items-' + alignItems);
2021-12-04 07:25:47 +01:00
2021-12-29 17:45:05 +01:00
if(justifyContent) newClassNames.push('justify-content-' + justifyContent);
if(!alignItems && !justifyContent && center) newClassNames.push('align-items-center', 'justify-content-center');
2021-12-04 07:25:47 +01:00
if(classNames.length) newClassNames.push(...classNames);
return newClassNames;
2022-03-08 06:11:57 +01:00
}, [ column, reverse, gap, center, alignSelf, alignItems, justifyContent, classNames ]);
2021-12-04 07:25:47 +01:00
2022-03-08 06:11:57 +01:00
return <Base display={ display } classNames={ getClassNames } { ...rest } />;
2021-12-04 07:25:47 +01:00
}