UNPKG

@bianic-ui/image

Version:

React component for progressive image loading

95 lines (84 loc) 3.03 kB
function _extends() { _extends = Object.assign || 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 = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { bianic, forwardRef } from "@bianic-ui/system"; import { omit, __DEV__ } from "@bianic-ui/utils"; import * as React from "react"; import { useImage } from "./use-image"; var NativeImage = /*#__PURE__*/React.forwardRef(function NativeImage(props, ref) { var { htmlWidth, htmlHeight } = props, rest = _objectWithoutPropertiesLoose(props, ["htmlWidth", "htmlHeight"]); return /*#__PURE__*/React.createElement("img", _extends({ width: htmlWidth, height: htmlHeight, ref: ref }, rest)); }); /** * React component that renders an image with support * for fallbacks * * @see Docs https://bianic-ui.com/components/image */ export var Image = /*#__PURE__*/forwardRef(function Image(props, ref) { var { fallbackSrc, fallback, src, align, fit, loading, ignoreFallback, crossOrigin } = props, rest = _objectWithoutPropertiesLoose(props, ["fallbackSrc", "fallback", "src", "align", "fit", "loading", "ignoreFallback", "crossOrigin"]); /** * Defer to native `img` tag if `loading` prop is passed * @see https://gitlab.com/bia-design/bianic-ui/issues/1027 */ var shouldIgnore = loading != null || ignoreFallback; var status = useImage(_extends({}, props, { ignoreFallback: shouldIgnore })); var shared = _extends({ ref, objectFit: fit, objectPosition: align }, shouldIgnore ? rest : omit(rest, ["onError", "onLoad"])); if (status !== "loaded") { /** * If user passed a custom fallback component, * let's render it here. */ if (fallback) return fallback; return /*#__PURE__*/React.createElement(bianic.img, _extends({ as: NativeImage, className: "bianic-image__placeholder", src: fallbackSrc }, shared)); } return /*#__PURE__*/React.createElement(bianic.img, _extends({ as: NativeImage, src: src, crossOrigin: crossOrigin, loading: loading, className: "bianic-image" }, shared)); }); /** * Fallback component for most SSR users who want to use the native `img` with * support for bianic props */ export var Img = /*#__PURE__*/forwardRef(function Img(props, ref) { return /*#__PURE__*/React.createElement(bianic.img, _extends({ ref: ref, as: NativeImage, className: "bianic-image" }, props)); }); if (__DEV__) { Image.displayName = "Image"; } //# sourceMappingURL=image.js.map