2021-12-04 07:25:47 +01:00
|
|
|
import { CSSProperties, FC, useMemo } from 'react';
|
|
|
|
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>
|
|
|
|
{
|
|
|
|
columnCount?: number;
|
|
|
|
columnMinWidth?: number;
|
2021-12-13 06:43:53 +01:00
|
|
|
columnMinHeight?: number;
|
2021-12-04 07:25:47 +01:00
|
|
|
grow?: boolean;
|
|
|
|
inline?: boolean;
|
|
|
|
gap?: SpacingType;
|
2022-01-07 22:43:11 +01:00
|
|
|
maxContent?: boolean;
|
2021-12-04 07:25:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Grid: FC<GridProps> = props =>
|
|
|
|
{
|
2022-01-12 05:01:59 +01:00
|
|
|
const { columnCount = 0, columnMinWidth = 40, columnMinHeight = 40, grow = false, fullHeight = undefined, inline = false, gap = 2, maxContent = false, 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 = {};
|
|
|
|
|
|
|
|
if(columnCount)
|
|
|
|
{
|
|
|
|
newStyle['--bs-columns'] = columnCount.toString();
|
|
|
|
}
|
|
|
|
|
2022-01-06 03:59:46 +01:00
|
|
|
if(grow && (!columnCount || (columnCount > 1)))
|
2021-12-04 07:25:47 +01:00
|
|
|
{
|
|
|
|
newStyle['--nitro-grid-column-min-width'] = (columnMinWidth + 'px');
|
2021-12-13 06:43:53 +01:00
|
|
|
newStyle['--nitro-grid-column-min-height'] = (columnMinHeight + 'px');
|
2021-12-04 07:25:47 +01:00
|
|
|
newStyle.gridTemplateColumns = 'repeat(auto-fill, minmax(var(--nitro-grid-column-min-width, 45px), 1fr)';
|
|
|
|
}
|
|
|
|
|
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-01-07 22:43:11 +01:00
|
|
|
}, [ columnCount, columnMinWidth, columnMinHeight, grow, maxContent, style ]);
|
2021-12-13 06:43:53 +01:00
|
|
|
|
2022-01-07 22:43:11 +01:00
|
|
|
return (
|
|
|
|
<GridContextProvider value={ { isCssGrid: true } }>
|
2022-01-12 05:01:59 +01:00
|
|
|
<Base classNames={ getClassNames } style={ getStyle } fullHeight={ ((fullHeight !== undefined) ? fullHeight : !grow) } { ...rest } />
|
2022-01-07 22:43:11 +01:00
|
|
|
</GridContextProvider>
|
|
|
|
);
|
2021-12-04 07:25:47 +01:00
|
|
|
}
|