feat: Grid support cssVar (#45855)

* feat: Grid support cssVar

* fix: fix

* fix: fix

---------

Signed-off-by: lijianan <574980606@qq.com>
This commit is contained in:
lijianan 2023-11-17 16:25:47 +08:00 committed by GitHub
parent 3871763950
commit 2954c3f60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 35 deletions

View File

@ -5,6 +5,7 @@ import type { LiteralUnion } from '../_util/type';
import { ConfigContext } from '../config-provider'; import { ConfigContext } from '../config-provider';
import RowContext from './RowContext'; import RowContext from './RowContext';
import { useColStyle } from './style'; import { useColStyle } from './style';
import { useColCssVar } from './style/cssVar';
// https://github.com/ant-design/ant-design/issues/14324 // https://github.com/ant-design/ant-design/issues/14324
type ColSpanType = number | string; type ColSpanType = number | string;
@ -67,7 +68,9 @@ const Col = React.forwardRef<HTMLDivElement, ColProps>((props, ref) => {
} = props; } = props;
const prefixCls = getPrefixCls('col', customizePrefixCls); const prefixCls = getPrefixCls('col', customizePrefixCls);
const [wrapSSR, hashId] = useColStyle(prefixCls);
const [, hashId] = useColStyle(prefixCls);
const wrapCSSVar = useColCssVar(prefixCls);
let sizeClassObj = {}; let sizeClassObj = {};
sizes.forEach((size) => { sizes.forEach((size) => {
@ -126,7 +129,7 @@ const Col = React.forwardRef<HTMLDivElement, ColProps>((props, ref) => {
} }
} }
return wrapSSR( return wrapCSSVar(
<div {...others} style={{ ...mergedStyle, ...style }} className={classes} ref={ref}> <div {...others} style={{ ...mergedStyle, ...style }} className={classes} ref={ref}>
{children} {children}
</div>, </div>,

View File

@ -7,6 +7,7 @@ import { ConfigContext } from '../config-provider';
import RowContext from './RowContext'; import RowContext from './RowContext';
import type { RowContextState } from './RowContext'; import type { RowContextState } from './RowContext';
import { useRowStyle } from './style'; import { useRowStyle } from './style';
import { useRowCssVar } from './style/cssVar';
const RowAligns = ['top', 'middle', 'bottom', 'stretch'] as const; const RowAligns = ['top', 'middle', 'bottom', 'stretch'] as const;
const RowJustify = [ const RowJustify = [
@ -146,7 +147,10 @@ const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
}; };
const prefixCls = getPrefixCls('row', customizePrefixCls); const prefixCls = getPrefixCls('row', customizePrefixCls);
const [wrapSSR, hashId] = useRowStyle(prefixCls);
const [, hashId] = useRowStyle(prefixCls);
const wrapCSSVar = useRowCssVar(prefixCls);
const gutters = getGutter(); const gutters = getGutter();
const classes = classNames( const classes = classNames(
prefixCls, prefixCls,
@ -180,7 +184,7 @@ const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
[gutterH, gutterV, wrap], [gutterH, gutterV, wrap],
); );
return wrapSSR( return wrapCSSVar(
<RowContext.Provider value={rowContext}> <RowContext.Provider value={rowContext}>
<div {...others} className={classes} style={{ ...rowStyle, ...style }} ref={ref}> <div {...others} className={classes} style={{ ...rowStyle, ...style }} ref={ref}>
{children} {children}

View File

@ -0,0 +1,6 @@
import { prepareColComponentToken, prepareRowComponentToken } from '.';
import { genCSSVarRegister } from '../../theme/internal';
export const useColCssVar = genCSSVarRegister<'Grid'>('Grid', prepareColComponentToken);
export const useRowCssVar = genCSSVarRegister<'Grid'>('Grid', prepareRowComponentToken);

View File

@ -1,9 +1,15 @@
import type { CSSObject } from '@ant-design/cssinjs'; import { unit, type CSSObject } from '@ant-design/cssinjs';
import type { FullToken, GenerateStyle } from '../../theme/internal'; import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal'; import { genComponentStyleHook, mergeToken } from '../../theme/internal';
interface GridRowToken extends FullToken<'Grid'> {} export interface ComponentToken {
//
}
interface GridRowToken extends FullToken<'Grid'> {
//
}
interface GridColToken extends FullToken<'Grid'> { interface GridColToken extends FullToken<'Grid'> {
gridColumns: number; gridColumns: number;
@ -156,35 +162,40 @@ const genGridMediaStyle = (
screenSize: number, screenSize: number,
sizeCls: string, sizeCls: string,
): CSSObject => ({ ): CSSObject => ({
[`@media (min-width: ${screenSize}px)`]: { [`@media (min-width: ${unit(screenSize)})`]: {
...genGridStyle(token, sizeCls), ...genGridStyle(token, sizeCls),
}, },
}); });
export const prepareRowComponentToken: GetDefaultToken<'Grid'> = () => ({});
export const prepareColComponentToken: GetDefaultToken<'Grid'> = () => ({});
// ============================== Export ============================== // ============================== Export ==============================
export const useRowStyle = genComponentStyleHook('Grid', (token) => [genGridRowStyle(token)]); export const useRowStyle = genComponentStyleHook('Grid', genGridRowStyle, prepareRowComponentToken);
export const useColStyle = genComponentStyleHook('Grid', (token) => { export const useColStyle = genComponentStyleHook(
const gridToken: GridColToken = mergeToken<GridColToken>(token, { 'Grid',
gridColumns: 24, // Row is divided into 24 parts in Grid (token) => {
}); const gridToken: GridColToken = mergeToken<GridColToken>(token, {
gridColumns: 24, // Row is divided into 24 parts in Grid
const gridMediaSizesMap = { });
'-sm': gridToken.screenSMMin, const gridMediaSizesMap = {
'-md': gridToken.screenMDMin, '-sm': gridToken.screenSMMin,
'-lg': gridToken.screenLGMin, '-md': gridToken.screenMDMin,
'-xl': gridToken.screenXLMin, '-lg': gridToken.screenLGMin,
'-xxl': gridToken.screenXXLMin, '-xl': gridToken.screenXLMin,
}; '-xxl': gridToken.screenXXLMin,
} as const;
return [ type GridMediaSize = keyof typeof gridMediaSizesMap;
genGridColStyle(gridToken), return [
genGridStyle(gridToken, ''), genGridColStyle(gridToken),
genGridStyle(gridToken, '-xs'), genGridStyle(gridToken, ''),
Object.keys(gridMediaSizesMap) genGridStyle(gridToken, '-xs'),
.map((key: keyof typeof gridMediaSizesMap) => Object.keys(gridMediaSizesMap)
genGridMediaStyle(gridToken, gridMediaSizesMap[key], key), .map((key: GridMediaSize) => genGridMediaStyle(gridToken, gridMediaSizesMap[key], key))
) .reduce((pre, cur) => ({ ...pre, ...cur }), {}),
.reduce((pre, cur) => ({ ...pre, ...cur }), {}), ];
]; },
}); prepareColComponentToken,
);

View File

@ -24,6 +24,7 @@ import type { ComponentToken as EmptyComponentToken } from '../../empty/style';
import type { ComponentToken as FlexComponentToken } from '../../flex/style'; import type { ComponentToken as FlexComponentToken } from '../../flex/style';
import type { ComponentToken as FloatButtonComponentToken } from '../../float-button/style'; import type { ComponentToken as FloatButtonComponentToken } from '../../float-button/style';
import type { ComponentToken as FormComponentToken } from '../../form/style'; import type { ComponentToken as FormComponentToken } from '../../form/style';
import type { ComponentToken as GridComponentToken } from '../../grid/style';
import type { ComponentToken as ImageComponentToken } from '../../image/style'; import type { ComponentToken as ImageComponentToken } from '../../image/style';
import type { ComponentToken as InputNumberComponentToken } from '../../input-number/style'; import type { ComponentToken as InputNumberComponentToken } from '../../input-number/style';
import type { ComponentToken as InputComponentToken } from '../../input/style'; import type { ComponentToken as InputComponentToken } from '../../input/style';
@ -87,7 +88,7 @@ export interface ComponentTokenMap {
Flex?: FlexComponentToken; Flex?: FlexComponentToken;
FloatButton?: FloatButtonComponentToken; FloatButton?: FloatButtonComponentToken;
Form?: FormComponentToken; Form?: FormComponentToken;
Grid?: {}; Grid?: GridComponentToken;
Image?: ImageComponentToken; Image?: ImageComponentToken;
Input?: InputComponentToken; Input?: InputComponentToken;
InputNumber?: InputNumberComponentToken; InputNumber?: InputNumberComponentToken;

View File

@ -29,7 +29,7 @@ console.error = (msg: any) => {
async function checkCSSVar() { async function checkCSSVar() {
await generateCssinjs({ await generateCssinjs({
key: 'check', key: 'check',
ignore: ['grid', 'pagination', 'calendar'], ignore: ['pagination', 'calendar'],
render(Component: any) { render(Component: any) {
ReactDOMServer.renderToString( ReactDOMServer.renderToString(
<StyleProvider linters={[NaNLinter]}> <StyleProvider linters={[NaNLinter]}>