UNPKG

react-aspect-ratio

Version:

React Component to maintain a consistent width-to-height ratio (aspect ratio), preventing cumulative layout shift.

33 lines (31 loc) 1.63 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _react = require("react"); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["className", "children", "ratio", "style"]; function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } } return target; } var CUSTOM_PROPERTY_NAME = '--aspect-ratio'; var DEFAULT_CLASS_NAME = 'react-aspect-ratio-placeholder'; var AspectRatio = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => { var { className = DEFAULT_CLASS_NAME, children, ratio = 1, style } = props, restProps = _objectWithoutPropertiesLoose(props, _excluded); // eslint-disable-line no-unused-vars var newStyle = _extends({}, style, { // https://github.com/roderickhsiao/react-aspect-ratio/commit/53ec15858ae186c41e70b8c14cc5a5b6e97cb6e3 [CUSTOM_PROPERTY_NAME]: "(" + ratio + ")" }); return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _extends({ className: className, style: newStyle, ref: ref }, restProps, { children: children })); }); var _default = exports.default = AspectRatio;