react-creepyface
Version:
React-Creepyface is a [React](https://reactjs.org/) component for [Creepyface](https://github.com/4lejandrito/creepyface).
113 lines • 5.27 kB
JavaScript
;
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var creepyface_1 = __importDefault(require("creepyface"));
function Creepyface(props) {
var disabled = props.disabled, src = props.src, options = props.options, imgProps = __rest(props, ["disabled", "src", "options"]);
var _a = __assign({}, options), looks = _a.looks, points = _a.points, rest = __rest(_a, ["looks", "points"]);
var imageRef = (0, react_1.useRef)(null);
var setPointProviderRef = (0, react_1.useRef)();
var pointProviderRef = (0, react_1.useRef)();
(0, react_1.useEffect)(function () {
if (!disabled && imageRef.current) {
if (src)
imageRef.current.src = src;
pointProviderRef.current = options === null || options === void 0 ? void 0 : options.points;
var cancel_1 = (0, creepyface_1.default)(imageRef.current, __assign(__assign({}, options), { onAttach: function (args) {
var _a;
setPointProviderRef.current = args.setPointProvider;
if (pointProviderRef.current !== points) {
args.setPointProvider(pointProviderRef.current);
}
(_a = options === null || options === void 0 ? void 0 : options.onAttach) === null || _a === void 0 ? void 0 : _a.call(options, args);
}, onDetach: function () {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.onDetach) === null || _a === void 0 ? void 0 : _a.call(options);
setPointProviderRef.current = undefined;
pointProviderRef.current = undefined;
} }));
return function () { return cancel_1(true); };
}
}, __spreadArray(__spreadArray(__spreadArray([
disabled,
src
], (looks || []).map(function (_a) {
var angle = _a.angle, src = _a.src;
return "".concat(angle, "-").concat(src);
}), true), Object.values(rest), true), [
Object.entries(imgProps)
.filter(function (_a) {
var name = _a[0];
return name.startsWith('data-');
})
.reduce(function (hash, _a) {
var name = _a[0], value = _a[1];
return "".concat(name, ":").concat(value, " ").concat(hash);
}, ''),
], false));
(0, react_1.useEffect)(function () {
var _a;
pointProviderRef.current = points;
(_a = setPointProviderRef.current) === null || _a === void 0 ? void 0 : _a.call(setPointProviderRef, points);
}, [points]);
return react_1.default.createElement("img", __assign({ src: src, ref: imageRef }, imgProps));
}
exports.default = Creepyface;
//# sourceMappingURL=index.js.map