diff --git a/components/masonry/Masonry.tsx b/components/masonry/Masonry.tsx index fb684a7286..0257fe1318 100644 --- a/components/masonry/Masonry.tsx +++ b/components/masonry/Masonry.tsx @@ -16,6 +16,7 @@ import useCSSVarCls from '../config-provider/hooks/useCSSVarCls'; import type { RowProps } from '../grid'; import useBreakpoint from '../grid/hooks/useBreakpoint'; import useGutter from '../grid/hooks/useGutter'; +import { genCssVar } from '../theme/util/genStyleUtils'; import useDelay from './hooks/useDelay'; import usePositions from './hooks/usePositions'; import type { ItemHeightData } from './hooks/usePositions'; @@ -92,9 +93,12 @@ const Masonry = React.forwardRef((props, ref) => { } = useComponentConfig('masonry'); const prefixCls = getPrefixCls('masonry', customizePrefixCls); + const rootPrefixCls = getPrefixCls(); const rootCls = useCSSVarCls(prefixCls); const [hashId, cssVarCls] = useStyle(prefixCls, rootCls); + const [varName, varRef] = genCssVar(rootPrefixCls, 'masonry'); + // ======================= Refs ======================= const containerRef = React.useRef(null); @@ -231,12 +235,7 @@ const Masonry = React.forwardRef((props, ref) => { cssVarCls, { [`${prefixCls}-rtl`]: direction === 'rtl' }, )} - style={{ - height: totalHeight, - ...mergedStyles.root, - ...contextStyle, - ...style, - }} + style={{ height: totalHeight, ...mergedStyles.root, ...contextStyle, ...style }} // Listen for image events onLoad={collectItemSize} onError={collectItemSize} @@ -263,9 +262,9 @@ const Masonry = React.forwardRef((props, ref) => { const { column: columnIndex = 0 } = position; const itemStyle: CSSProperties = { - '--item-width': `calc((100% + ${horizontalGutter}px) / ${columnCount})`, - insetInlineStart: `calc(var(--item-width) * ${columnIndex})`, - width: `calc(var(--item-width) - ${horizontalGutter}px)`, + [varName('item-width')]: `calc((100% + ${horizontalGutter}px) / ${columnCount})`, + insetInlineStart: `calc(${varRef('item-width')} * ${columnIndex})`, + width: `calc(${varRef('item-width')} - ${horizontalGutter}px)`, top: position.top, position: 'absolute', }; diff --git a/components/masonry/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/masonry/__tests__/__snapshots__/demo-extend.test.ts.snap index aefd45412a..dfe079ddf3 100644 --- a/components/masonry/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/masonry/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -7,7 +7,7 @@ exports[`renders components/masonry/demo/basic.tsx extend context correctly 1`] >
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
sample
{ triggerResize(document.body.querySelector('.ant-masonry')!); @@ -198,15 +200,14 @@ describe('Masonry', () => { }); describe('gutter', () => { + const [varName] = genCssVar(defaultPrefixCls, 'masonry'); const getGutter = () => { const itemElements = document.body.querySelectorAll('.ant-masonry-item'); - const horizontalGutter = itemElements[0].style - .getPropertyValue('--item-width') - .match(/\d+px/)![0]; - const verticalGutter = itemElements[2].style.top.match(/\d+px/)![0]; - - return [Number.parseInt(horizontalGutter, 10), Number.parseInt(verticalGutter, 10)]; + .getPropertyValue(varName('item-width')) + .match(/\d+px/)?.[0]; + const verticalGutter = itemElements[2].style.top.match(/\d+px/)?.[0]; + return [Number.parseInt(horizontalGutter!, 10), Number.parseInt(verticalGutter!, 10)]; }; const renderGutter = (gutter: MasonryProps['gutter']) => (