2024-01-23 20:43:55 +08:00
|
|
|
import { unit, type CSSObject } from '@ant-design/cssinjs';
|
2023-08-28 22:10:47 +08:00
|
|
|
|
2023-11-17 16:25:47 +08:00
|
|
|
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
|
2023-11-29 17:23:45 +08:00
|
|
|
import { genStyleHooks, mergeToken } from '../../theme/internal';
|
2022-04-08 15:24:07 +08:00
|
|
|
|
2023-11-17 16:25:47 +08:00
|
|
|
export interface ComponentToken {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
interface GridRowToken extends FullToken<'Grid'> {
|
|
|
|
//
|
|
|
|
}
|
2022-03-31 12:45:41 +08:00
|
|
|
|
2022-04-08 15:24:07 +08:00
|
|
|
interface GridColToken extends FullToken<'Grid'> {
|
2022-03-31 12:45:41 +08:00
|
|
|
gridColumns: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================== Row-Shared ==============================
|
|
|
|
const genGridRowStyle: GenerateStyle<GridRowToken> = (token): CSSObject => {
|
2022-04-08 15:24:07 +08:00
|
|
|
const { componentCls } = token;
|
2022-03-31 12:45:41 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
// Grid system
|
2022-04-08 15:24:07 +08:00
|
|
|
[componentCls]: {
|
2022-03-31 12:45:41 +08:00
|
|
|
display: 'flex',
|
|
|
|
flexFlow: 'row wrap',
|
2022-07-29 15:56:11 +08:00
|
|
|
minWidth: 0,
|
2022-03-31 12:45:41 +08:00
|
|
|
|
|
|
|
'&::before, &::after': {
|
|
|
|
display: 'flex',
|
|
|
|
},
|
|
|
|
|
|
|
|
'&-no-wrap': {
|
|
|
|
flexWrap: 'nowrap',
|
|
|
|
},
|
|
|
|
|
|
|
|
// The origin of the X-axis
|
|
|
|
'&-start': {
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
},
|
|
|
|
|
|
|
|
// The center of the X-axis
|
|
|
|
'&-center': {
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
|
|
|
|
// The opposite of the X-axis
|
|
|
|
'&-end': {
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
},
|
|
|
|
|
|
|
|
'&-space-between': {
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
},
|
|
|
|
|
2023-04-06 19:43:32 +08:00
|
|
|
'&-space-around': {
|
2022-03-31 12:45:41 +08:00
|
|
|
justifyContent: 'space-around',
|
|
|
|
},
|
|
|
|
|
2023-04-06 19:43:32 +08:00
|
|
|
'&-space-evenly': {
|
|
|
|
justifyContent: 'space-evenly',
|
|
|
|
},
|
|
|
|
|
2022-03-31 12:45:41 +08:00
|
|
|
// Align at the top
|
|
|
|
'&-top': {
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
},
|
|
|
|
|
|
|
|
// Align at the center
|
|
|
|
'&-middle': {
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
|
|
|
|
'&-bottom': {
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// ============================== Col-Shared ==============================
|
|
|
|
const genGridColStyle: GenerateStyle<GridColToken> = (token): CSSObject => {
|
2022-04-08 15:24:07 +08:00
|
|
|
const { componentCls } = token;
|
2022-03-31 12:45:41 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
// Grid system
|
2022-04-08 15:24:07 +08:00
|
|
|
[componentCls]: {
|
2022-03-31 12:45:41 +08:00
|
|
|
position: 'relative',
|
|
|
|
maxWidth: '100%',
|
|
|
|
// Prevent columns from collapsing when empty
|
|
|
|
minHeight: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const genLoopGridColumnsStyle = (token: GridColToken, sizeCls: string): CSSObject => {
|
2024-01-23 20:43:55 +08:00
|
|
|
const { prefixCls, componentCls, gridColumns } = token;
|
2022-03-31 12:45:41 +08:00
|
|
|
|
|
|
|
const gridColumnsStyle: CSSObject = {};
|
|
|
|
for (let i = gridColumns; i >= 0; i--) {
|
|
|
|
if (i === 0) {
|
2023-08-29 15:48:03 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-${i}`] = {
|
2022-03-31 12:45:41 +08:00
|
|
|
display: 'none',
|
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}-push-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
insetInlineStart: 'auto',
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}-pull-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
insetInlineEnd: 'auto',
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-push-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
insetInlineStart: 'auto',
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-pull-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
insetInlineEnd: 'auto',
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-offset-${i}`] = {
|
2023-03-19 16:18:27 +08:00
|
|
|
marginInlineStart: 0,
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-order-${i}`] = {
|
2022-03-31 12:45:41 +08:00
|
|
|
order: 0,
|
|
|
|
};
|
|
|
|
} else {
|
2023-08-29 15:48:03 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-${i}`] = [
|
|
|
|
// https://github.com/ant-design/ant-design/issues/44456
|
|
|
|
// Form set `display: flex` on Col which will override `display: block`.
|
|
|
|
// Let's get it from css variable to support override.
|
|
|
|
{
|
|
|
|
['--ant-display' as any]: 'block',
|
|
|
|
// Fallback to display if variable not support
|
|
|
|
display: 'block',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: 'var(--ant-display)',
|
|
|
|
flex: `0 0 ${(i / gridColumns) * 100}%`,
|
|
|
|
maxWidth: `${(i / gridColumns) * 100}%`,
|
|
|
|
},
|
|
|
|
];
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-push-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
insetInlineStart: `${(i / gridColumns) * 100}%`,
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-pull-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
insetInlineEnd: `${(i / gridColumns) * 100}%`,
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-offset-${i}`] = {
|
2022-04-19 11:27:09 +08:00
|
|
|
marginInlineStart: `${(i / gridColumns) * 100}%`,
|
2022-03-31 12:45:41 +08:00
|
|
|
};
|
2022-04-08 15:24:07 +08:00
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-order-${i}`] = {
|
2022-03-31 12:45:41 +08:00
|
|
|
order: i,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 20:43:55 +08:00
|
|
|
// Flex CSS Var
|
|
|
|
gridColumnsStyle[`${componentCls}${sizeCls}-flex`] = {
|
|
|
|
flex: `var(--${prefixCls}${sizeCls}-flex)`,
|
|
|
|
};
|
|
|
|
|
2022-03-31 12:45:41 +08:00
|
|
|
return gridColumnsStyle;
|
|
|
|
};
|
|
|
|
|
|
|
|
const genGridStyle = (token: GridColToken, sizeCls: string): CSSObject =>
|
|
|
|
genLoopGridColumnsStyle(token, sizeCls);
|
|
|
|
|
|
|
|
const genGridMediaStyle = (
|
|
|
|
token: GridColToken,
|
|
|
|
screenSize: number,
|
|
|
|
sizeCls: string,
|
|
|
|
): CSSObject => ({
|
2023-11-17 16:25:47 +08:00
|
|
|
[`@media (min-width: ${unit(screenSize)})`]: {
|
2022-03-31 12:45:41 +08:00
|
|
|
...genGridStyle(token, sizeCls),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-17 16:25:47 +08:00
|
|
|
export const prepareRowComponentToken: GetDefaultToken<'Grid'> = () => ({});
|
2022-03-31 12:45:41 +08:00
|
|
|
|
2023-11-17 16:25:47 +08:00
|
|
|
export const prepareColComponentToken: GetDefaultToken<'Grid'> = () => ({});
|
|
|
|
|
|
|
|
// ============================== Export ==============================
|
2023-11-29 17:23:45 +08:00
|
|
|
export const useRowStyle = genStyleHooks('Grid', genGridRowStyle, prepareRowComponentToken);
|
2023-11-17 16:25:47 +08:00
|
|
|
|
2023-11-29 17:23:45 +08:00
|
|
|
export const useColStyle = genStyleHooks(
|
2023-11-17 16:25:47 +08:00
|
|
|
'Grid',
|
|
|
|
(token) => {
|
|
|
|
const gridToken: GridColToken = mergeToken<GridColToken>(token, {
|
|
|
|
gridColumns: 24, // Row is divided into 24 parts in Grid
|
|
|
|
});
|
|
|
|
const gridMediaSizesMap = {
|
|
|
|
'-sm': gridToken.screenSMMin,
|
|
|
|
'-md': gridToken.screenMDMin,
|
|
|
|
'-lg': gridToken.screenLGMin,
|
|
|
|
'-xl': gridToken.screenXLMin,
|
|
|
|
'-xxl': gridToken.screenXXLMin,
|
|
|
|
} as const;
|
|
|
|
type GridMediaSize = keyof typeof gridMediaSizesMap;
|
|
|
|
return [
|
|
|
|
genGridColStyle(gridToken),
|
|
|
|
genGridStyle(gridToken, ''),
|
|
|
|
genGridStyle(gridToken, '-xs'),
|
|
|
|
Object.keys(gridMediaSizesMap)
|
2024-04-01 15:49:45 +08:00
|
|
|
.map((key) => genGridMediaStyle(gridToken, gridMediaSizesMap[key as GridMediaSize], key))
|
2023-11-17 16:25:47 +08:00
|
|
|
.reduce((pre, cur) => ({ ...pre, ...cur }), {}),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
prepareColComponentToken,
|
|
|
|
);
|