nitro-react/src/common/Grid.tsx

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-02-16 07:40:54 +01:00
import { FC, useMemo } from 'react';
import { CSSProperties } from 'styled-components';
2021-12-04 07:25:47 +01:00
import { Base, BaseProps } from './Base';
2022-02-09 04:25:01 +01:00
import { GridContextProvider } from './GridContext';
import { SpacingType } from './types';
2021-12-04 07:25:47 +01:00
export interface GridProps extends BaseProps<HTMLDivElement>
{
inline?: boolean;
gap?: SpacingType;
2022-01-07 22:43:11 +01:00
maxContent?: boolean;
2022-02-16 07:40:54 +01:00
columnCount?: number;
2021-12-04 07:25:47 +01:00
}
export const Grid: FC<GridProps> = props =>
{
2022-02-16 07:40:54 +01:00
const { inline = false, gap = 2, maxContent = false, columnCount = 0, fullHeight = true, classNames = [], style = {}, ...rest } = props;
2021-12-04 07:25:47 +01:00
const getClassNames = useMemo(() =>
{
2021-12-09 06:36:35 +01:00
const newClassNames: string[] = [];
2021-12-04 07:25:47 +01:00
if(inline) newClassNames.push('inline-grid');
else newClassNames.push('grid');
if(gap) newClassNames.push('gap-' + gap);
2022-01-12 05:01:59 +01:00
else if(gap === 0) newClassNames.push('gap-0');
2021-12-04 07:25:47 +01:00
if(classNames.length) newClassNames.push(...classNames);
return newClassNames;
2022-01-12 05:01:59 +01:00
}, [ inline, gap, classNames ]);
2021-12-04 07:25:47 +01:00
const getStyle = useMemo(() =>
{
let newStyle: CSSProperties = {};
2022-02-16 07:40:54 +01:00
if(columnCount) newStyle['--bs-columns'] = columnCount.toString();
2021-12-04 07:25:47 +01:00
2022-01-07 22:43:11 +01:00
if(maxContent) newStyle.gridTemplateRows = 'max-content';
2021-12-04 07:25:47 +01:00
if(Object.keys(style).length) newStyle = { ...newStyle, ...style };
return newStyle;
2022-02-16 07:40:54 +01:00
}, [ columnCount, maxContent, style ]);
2021-12-13 06:43:53 +01:00
2022-01-07 22:43:11 +01:00
return (
<GridContextProvider value={ { isCssGrid: true } }>
2022-02-16 07:40:54 +01:00
<Base fullHeight={ fullHeight } classNames={ getClassNames } style={ getStyle } { ...rest } />
2022-01-07 22:43:11 +01:00
</GridContextProvider>
);
2021-12-04 07:25:47 +01:00
}