Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Grid flexGap lazy check #29202

Merged
merged 4 commits into from Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions components/_util/__tests__/util.test.js
Expand Up @@ -10,7 +10,7 @@ import {
import getDataOrAriaProps from '../getDataOrAriaProps';
import Wave from '../wave';
import TransButton from '../transButton';
import { isStyleSupport, isFlexSupported } from '../styleChecker';
import { isStyleSupport } from '../styleChecker';
import { sleep } from '../../../tests/utils';

describe('Test utils function', () => {
Expand Down Expand Up @@ -208,10 +208,6 @@ describe('Test utils function', () => {
});

describe('style', () => {
it('isFlexSupported', () => {
expect(isFlexSupported).toBe(true);
});

it('isStyleSupport', () => {
expect(isStyleSupport('color')).toBe(true);
expect(isStyleSupport('not-existed')).toBe(false);
Expand Down
15 changes: 9 additions & 6 deletions components/_util/styleChecker.tsx
Expand Up @@ -12,13 +12,16 @@ export const isStyleSupport = (styleName: string | Array<string>): boolean => {
return false;
};

export const isFlexSupported = isStyleSupport(['flex', 'webkitFlex', 'Flex', 'msFlex']);

export const isFlexGapSupported = (() => {
let flexGapSupported: boolean | undefined;
export const detectFlexGapSupported = () => {
if (!canUseDocElement()) {
return false;
}

if (flexGapSupported !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

位置可以放到最上面

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放下面是为了减少 canUseDocElement 分支还得写一个赋值,节省一行代码。

return flexGapSupported;
}

// create flex container with row-gap set
const flex = document.createElement('div');
flex.style.display = 'flex';
Expand All @@ -31,8 +34,8 @@ export const isFlexGapSupported = (() => {

// append to the DOM (needed to obtain scrollHeight)
document.body.appendChild(flex);
const isSupported = flex.scrollHeight === 1; // flex container should be 1px high from the row-gap
flexGapSupported = flex.scrollHeight === 1; // flex container should be 1px high from the row-gap
document.body.removeChild(flex);

return isSupported;
})();
return flexGapSupported;
};
2 changes: 1 addition & 1 deletion components/grid/__tests__/gap.test.js
Expand Up @@ -7,7 +7,7 @@ import * as styleChecker from '../../_util/styleChecker';
jest.mock('../../_util/styleChecker', () => ({
canUseDocElement: () => true,
isStyleSupport: () => true,
isFlexGapSupported: true,
detectFlexGapSupported: () => true,
}));

describe('Grid.Gap', () => {
Expand Down
4 changes: 2 additions & 2 deletions components/grid/col.tsx
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import classNames from 'classnames';
import RowContext from './RowContext';
import { ConfigContext } from '../config-provider';
import { isFlexGapSupported } from '../_util/styleChecker';
import { detectFlexGapSupported } from '../_util/styleChecker';

// https://github.com/ant-design/ant-design/issues/14324
type ColSpanType = number | string;
Expand Down Expand Up @@ -104,7 +104,7 @@ const Col = React.forwardRef<HTMLDivElement, ColProps>((props, ref) => {
);

let mergedStyle: React.CSSProperties = { ...style };
if (gutter && !isFlexGapSupported) {
if (gutter && !detectFlexGapSupported()) {
mergedStyle = {
...(gutter[0]! > 0
? {
Expand Down
8 changes: 4 additions & 4 deletions components/grid/row.tsx
Expand Up @@ -8,7 +8,7 @@ import ResponsiveObserve, {
ScreenMap,
responsiveArray,
} from '../_util/responsiveObserve';
import { isFlexGapSupported } from '../_util/styleChecker';
import { detectFlexGapSupported } from '../_util/styleChecker';

const RowAligns = tuple('top', 'middle', 'bottom', 'stretch');
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between');
Expand Down Expand Up @@ -100,10 +100,10 @@ const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
'--row-gap'?: string | number;
} = {};

if (isFlexGapSupported) {
if (detectFlexGapSupported()) {
rowStyle = {
'--column-gap': 0,
'--row-gap': 0,
'--column-gap': '0px',
'--row-gap': '0px',
};

if (gutters[0]! > 0) {
Expand Down