ResponsiveWrapper 是一个 WordPress 组件,用于包装子元素并保持其宽高比在调整大小时不变。它支持图片和 SVG 元素,并提供属性如 naturalWidth 和 naturalHeight 来控制固有尺寸。
import { ResponsiveWrapper } from '@wordpress/components';
const MyResponsiveWrapper = () => (
<ResponsiveWrapper naturalWidth={ 2000 } naturalHeight={ 680 }>
<img
src="https://s.w.org/style/images/about/WordPress-logotype-standard.png"
alt="WordPress"
/>
</ResponsiveWrapper>
);当包装 SVG 元素时,如果无法获取 naturalWidth 和 naturalHeight,SVG 可能会无限缩放,除非指定了 height 和 width 属性。
A wrapper component that maintains its aspect ratio when resized.
import { ResponsiveWrapper } from '@wordpress/components';
const MyResponsiveWrapper = () => (
<ResponsiveWrapper naturalWidth={ 2000 } naturalHeight={ 680 }>
<img
src="https://s.w.org/style/images/about/WordPress-logotype-standard.png"
alt="WordPress"
/>
</ResponsiveWrapper>
);
SVG elementsWhen passing an SVG element as the <ResponsiveWrapper />‘s child, make sure that it has the viewbox and the preserveAspectRatio set.
When dealing with SVGs, it may not be possible to derive its naturalWidth and naturalHeight and therefore passing them as propertied to <ResponsiveWrapper />. In this case, the SVG simply keeps scaling up to fill its container, unless the height and width attributes are specified.
children: React.ReactElementThe element to wrap.
isInline: booleanIf true, the wrapper will be span instead of div.
falsenaturalHeight: numberThe intrinsic height of the element to wrap. Will be used to determine the aspect ratio.
naturalWidth: numberThe intrinsic width of the element to wrap. Will be used to determine the aspect ratio.