react-photoswipe-gallery
Version:
React component wrapper around PhotoSwipe
69 lines • 2.11 kB
JavaScript
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 { useRef, useEffect, useMemo, useCallback } from 'react';
import PropTypes from 'prop-types';
import { useApiContext } from "./hooks.js";
import { NoRefError } from "./no-ref-error.js";
/**
* Gallery item
*
* Should be a children of Gallery component
*/
export const Item = _a => {
var {
children
} = _a,
restProps = __rest(_a, ["children"]);
const ref = useRef(null);
const {
remove,
set,
handleClick,
close,
isRefRegistered
} = useApiContext();
const refCallback = useCallback(node => {
ref.current = node;
set(ref, restProps);
}, [set, ...Object.values(restProps)]);
const open = useCallback(event => {
if (!isRefRegistered(ref)) {
throw new NoRefError();
}
handleClick(ref, null, null, event);
}, [handleClick, isRefRegistered]);
const childrenFnProps = useMemo(() => ({
ref: refCallback,
open,
close
}), [refCallback, open, close]);
useEffect(() => {
return () => {
if (ref.current === null) {
remove(ref);
}
};
}, [remove]);
return children(childrenFnProps);
};
Item.propTypes = {
children: PropTypes.func.isRequired,
original: PropTypes.string,
originalSrcset: PropTypes.string,
thumbnail: PropTypes.string,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
alt: PropTypes.string,
caption: PropTypes.string,
content: PropTypes.element,
html: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
cropped: PropTypes.bool,
sourceId: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
};