UNPKG

@mornya/react-image-libs

Version:
115 lines (114 loc) 6.37 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { useContext, useRef, useMemo, useCallback, useEffect } from 'react'; import classNames from 'classnames'; import { IntersectObserverContext } from './IntersectObserverContext'; import { usePrev, useLazyImage } from './hooks'; export var LazyImage = function (props) { var _a, _b, _c; var _d = (_a = props.style) !== null && _a !== void 0 ? _a : {}, borderRadius = _d.borderRadius, style = __rest(_d, ["borderRadius"]); var IntersectObserver = useContext(IntersectObserverContext); var imagePayload = useLazyImage(__assign(__assign({}, props), { url: props.url.replace(/^http:/, 'https:') })); var imageObserverRef = useRef(null); var imageRef = useRef(null); var imageLoadingRef = useRef(null); var isSubscribed = useRef(false); var isClickable = useMemo(function () { return !!props.onClick; }, [props.onClick]); var title = useMemo(function () { var _a; return (_a = props.title) !== null && _a !== void 0 ? _a : imagePayload.url; }, [props, imagePayload]); var prevImagePayload = usePrev(imagePayload); var onLoadedImage = useCallback(function () { var _a; if (imageRef.current) { imageRef.current.style.opacity = '1'; } if (imageLoadingRef.current) { imageLoadingRef.current.style.display = 'none'; } (_a = props.onLoaded) === null || _a === void 0 ? void 0 : _a.call(props, imagePayload); }, [props, imagePayload]); var onClickImage = useCallback(function (event) { var _a; event.preventDefault(); (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, event); }, [props]); var renderImage = useCallback(function () { var _a, _b, _c; return (_jsx("img", { ref: imageRef, src: imagePayload.url, alt: title, width: imagePayload.width, height: imagePayload.height, className: "lazy-image-payload", style: { width: (_a = props.width) !== null && _a !== void 0 ? _a : '100%', height: (_b = props.height) !== null && _b !== void 0 ? _b : 'auto', borderRadius: borderRadius, objectFit: (_c = props.fit) !== null && _c !== void 0 ? _c : 'initial', opacity: 0, pointerEvents: isClickable ? undefined : 'none', cursor: isClickable ? 'pointer' : undefined, }, onLoad: onLoadedImage })); }, [ imagePayload.url, imagePayload.width, imagePayload.height, title, borderRadius, props.width, props.height, props.fit, isClickable, onLoadedImage, ]); useEffect(function () { var target = imageObserverRef.current; if (target) { if (!isSubscribed.current) { isSubscribed.current = true; IntersectObserver.subscribe(target, function () { return imagePayload.loadImage(); }); } return function () { if (isSubscribed.current && imagePayload.loadState === 'loaded') { IntersectObserver.unsubscribe(target); } }; } }, [IntersectObserver, imagePayload]); useEffect(function () { var _a, _b; if (imagePayload.loadState !== (prevImagePayload === null || prevImagePayload === void 0 ? void 0 : prevImagePayload.loadState)) { if (imagePayload.loadState === 'loaded') { if (imageLoadingRef.current) { imageLoadingRef.current.style.display = 'none'; } (_a = props.onLoaded) === null || _a === void 0 ? void 0 : _a.call(props, imagePayload); } else if (imagePayload.loadState === 'error') { (_b = props.onError) === null || _b === void 0 ? void 0 : _b.call(props, props.url); } } }, [props, imagePayload, prevImagePayload]); return (_jsx("span", { ref: imageObserverRef, title: isClickable ? title : undefined, tabIndex: (_b = props.tabIndex) !== null && _b !== void 0 ? _b : 0, className: classNames('lazy-image', imagePayload.loadState, props.className), style: __assign(__assign({ display: 'flex', justifyContent: 'center', flexBasis: 'fit-content', position: 'relative', width: '100%', height: 'auto', minHeight: '1px' }, style), { border: '0 !important' }), onMouseOver: props.onMouseOver, onMouseLeave: props.onMouseLeave, onFocus: props.onFocus, onBlur: props.onBlur, children: _jsxs("span", { className: "lazy-image-inner", style: { display: 'inline-flex', alignItems: 'center', position: 'relative', width: '100%', height: '100%', borderRadius: borderRadius, }, children: [_jsx("span", { ref: imageLoadingRef, className: classNames('lazy-image-loading', { error: imagePayload.loadState === 'error' }), style: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }, children: imagePayload.loadState === 'error' ? props.error : (_c = props.loading) !== null && _c !== void 0 ? _c : props.beforeLoad }), imagePayload.loadState === 'loaded' && (_jsx(_Fragment, { children: isClickable ? (_jsx("button", { type: "button", "aria-label": title, style: { width: '100%', fontSize: 0 }, onClick: onClickImage, children: renderImage() })) : (renderImage()) }))] }) })); };