UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

435 lines (432 loc) • 17.6 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Animators_native_exports = {}; __export(Animators_native_exports, { KeyframeAnimator: () => KeyframeAnimator, PhaseAnimator: () => PhaseAnimator }); module.exports = __toCommonJS(Animators_native_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_react = require("react"); var import_animatorStyle = require("./animatorStyle.native.js"); var import_animatorTypes = require("./animatorTypes.native.js"); var import_containerChildren = require("./containerChildren.native.js"); var import_controlTypes = require("./generated/controlTypes.native.js"); var import_OneNativeKeyframeAnimatorNativeComponent = __toESM(require("./specs/OneNativeKeyframeAnimatorNativeComponent.native.js"), 1); var import_OneNativePhaseAnimatorNativeComponent = __toESM(require("./specs/OneNativePhaseAnimatorNativeComponent.native.js"), 1); var keyframeFieldLimit = 8; var nextAnimatorId = 1; function useAnimatorId() { var id = (0, import_react.useRef)(0); if (id.current === 0) id.current = nextAnimatorId++; return id.current; } function useTriggerCount(trigger) { var [state, setState] = (0, import_react.useState)({ trigger, count: 0 }); if (Object.is(state.trigger, trigger)) return state.count; var next = { trigger, count: (state.count + 1) % 2147483647 }; setState(next); return next.count; } function typeName(type) { if (typeof type === "string") return type; if (typeof type === "function" || typeof type === "object" && type !== null) { var named = type; return named.displayName || named.name || "a component"; } return String(type); } function sameValue(a, b) { if (Object.is(a, b)) return true; if (typeof a === "function" && typeof b === "function") return true; if (/* @__PURE__ */ (0, import_react.isValidElement)(a) || /* @__PURE__ */ (0, import_react.isValidElement)(b)) return false; if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every(function(item, index) { return sameValue(item, b[index]); }); if (a && b && typeof a === "object" && typeof b === "object") { var keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]); return [...keys].every(function(key) { return sameValue(a[key], b[key]); }); } return false; } function zipContent(nodes, owner, mark) { var first = nodes[0]; if (Array.isArray(first)) { if (!nodes.every(function(node) { return Array.isArray(node) && node.length === first.length; })) throw new Error(`${owner} content must render the same children for every ${unit(owner)}`); return first.map(function(_, index) { return zipContent(nodes.map(function(node) { return node[index]; }), owner, mark); }); } if (!/* @__PURE__ */ (0, import_react.isValidElement)(first)) { if (!nodes.every(function(node) { return sameValue(node, first); })) throw new Error(`${owner} content must render the same children for every ${unit(owner)}`); return first; } var elements = nodes; if (!elements.every(function(node) { return /* @__PURE__ */ (0, import_react.isValidElement)(node) && node.type === first.type && node.key === first.key; })) throw new Error(`${owner} content must render the same children for every ${unit(owner)}`); var props = first.props; var keys = new Set(elements.flatMap(function(element) { return Object.keys(element.props); })); var next = {}; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { var _loop = function() { var key = _step.value; var values = elements.map(function(element) { return element.props[key]; }); if (key === "children") { if (typeof values[0] === "function") return "continue"; next.children = zipContent(values, owner, mark); } else if (key === "swiftStyle") { if (!values.every(function(value) { return sameValue(value, values[0]); })) next.swiftStyle = mark(values); } else if (!values.every(function(value) { return sameValue(value, values[0]); })) { throw new Error(`${owner} content can vary only swiftStyle by ${unit(owner)}; ${typeName(first.type)}.${key} changed`); } }; for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) _loop(); } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } if (!("children" in next) && !("swiftStyle" in next)) return first; return /* @__PURE__ */ (0, import_react.cloneElement)(first, { ...props, ...next }); } function unit(owner) { return owner === "Swift.PhaseAnimator" ? "phase" : "value"; } function PhaseAnimator(param) { var { phases, trigger, animation, children, style, ...props } = param; var id = useAnimatorId(); var count = useTriggerCount(trigger); if (typeof children !== "function") throw new Error("Swift.PhaseAnimator takes a content function of its phase; pass (phase) => content"); if (phases === null || typeof phases !== "object" || !(Symbol.iterator in phases)) throw new Error("Swift.PhaseAnimator phases must be a sequence"); var list = Array.from(phases); if (list.length === 0) throw new Error("Swift.PhaseAnimator phases must not be empty"); if (animation !== void 0 && typeof animation !== "function") throw new Error("Swift.PhaseAnimator animation must be a function of the phase"); var animations = list.map(function(phase) { var value = animation ? animation(phase) : "default"; if (value !== null && !import_controlTypes.sdkAnimationValues.includes(value)) throw new Error(`Swift.PhaseAnimator animation must return null or one of ${import_controlTypes.sdkAnimationValues.join(", ")}`); return value; }); var content = zipContent(list.map(function(phase) { return children(phase); }), "Swift.PhaseAnimator", function(samples) { return (0, import_animatorStyle.animatedSwiftStyle)({ id, kind: "phase", owner: "Swift.PhaseAnimator", samples: samples.map(function(sample) { return sample !== null && sample !== void 0 ? sample : {}; }) }); }); (0, import_containerChildren.assertOneNativeChildren)(content, "Swift.PhaseAnimator"); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_OneNativePhaseAnimatorNativeComponent.default, { ...props, style: [{ alignSelf: "stretch" }, style], animatorId: id, animations: JSON.stringify(animations), hasTrigger: trigger !== void 0, trigger: count, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_containerChildren.InsideContainer, { value: true, children: content }) }); } function valueFields(value, owner, what) { var fields = typeof value === "number" ? ["self"] : value && typeof value === "object" ? Object.keys(value) : []; var numbers = typeof value === "number" ? [value] : fields.map(function(field) { return value[field]; }); if (fields.length === 0 || !numbers.every(function(item) { return typeof item === "number" && Number.isFinite(item); })) throw new Error(`${owner} ${what} must be a finite number or a record of finite numbers`); if (fields.length > keyframeFieldLimit) throw new Error(`${owner} ${what} supports at most ${keyframeFieldLimit} fields`); return fields; } function vectorOf(value, fields, owner, what) { if (typeof value === "number" && fields.length === 1 && fields[0] === "self") return [value]; var actual = valueFields(value, owner, what); if (actual.length !== fields.length || !fields.every(function(field) { return actual.includes(field); })) throw new Error(`${owner} ${what} must have the fields ${fields.join(", ")}`); return fields.map(function(field) { return value[field]; }); } function valueFrom(vector, fields, scalar) { return scalar ? vector[0] : Object.fromEntries(fields.map(function(field, index) { return [field, vector[index]]; })); } function duration(value, owner, required) { if (value === void 0 && !required) return void 0; if (typeof value !== "number" || !Number.isFinite(value) || value < 0) throw new Error(`${owner} keyframe duration must be a finite, non-negative number`); return value; } function curveOf(curve, owner) { if (curve === void 0) return void 0; if (typeof curve === "string") { if (!import_animatorTypes.unitCurves.includes(curve)) throw new Error(`${owner} timingCurve must be one of ${import_animatorTypes.unitCurves.join(", ")} or a bezier`); return { name: curve }; } var points = [curve === null || curve === void 0 ? void 0 : curve.startControlPoint, curve === null || curve === void 0 ? void 0 : curve.endControlPoint]; if (!points.every(function(point) { return point && Number.isFinite(point.x) && Number.isFinite(point.y); })) throw new Error(`${owner} bezier timingCurve needs finite startControlPoint and endControlPoint`); return { startControlPoint: [curve.startControlPoint.x, curve.startControlPoint.y], endControlPoint: [curve.endControlPoint.x, curve.endControlPoint.y] }; } function springOf(spring, owner) { if (spring === void 0) return void 0; var finite = function finite2(value) { return value === void 0 || typeof value === "number" && Number.isFinite(value); }; if (typeof spring === "string") { if (!import_animatorTypes.springPresets.includes(spring)) throw new Error(`${owner} spring must be one of ${import_animatorTypes.springPresets.join(", ")} or a Spring initializer`); return { kind: spring }; } var record = spring !== null && spring !== void 0 ? spring : {}; if (!Object.values(record).every(function(value) { return typeof value === "boolean" || finite(value); })) throw new Error(`${owner} spring values must be finite numbers`); if ("stiffness" in record || "damping" in record || "mass" in record) { if (record.stiffness === void 0 || record.damping === void 0) throw new Error(`${owner} spring needs stiffness and damping`); return { kind: "physical", ...record }; } if ("settlingDuration" in record) { if (record.dampingRatio === void 0) throw new Error(`${owner} spring needs dampingRatio`); return { kind: "settling", ...record }; } if ("response" in record) { if (record.dampingRatio === void 0) throw new Error(`${owner} spring needs dampingRatio`); return { kind: "response", ...record }; } return { kind: "duration", ...record }; } function nativeKeyframe(frame, field, read, owner) { var pick = function pick2(value, what) { return value === void 0 ? void 0 : read(value, what)[field]; }; switch (frame === null || frame === void 0 ? void 0 : frame.kind) { case "linear": return { kind: "linear", to: pick(frame.to, "LinearKeyframe value"), duration: duration(frame.duration, owner, true), timingCurve: curveOf(frame.timingCurve, owner) }; case "spring": return { kind: "spring", to: pick(frame.to, "SpringKeyframe value"), duration: duration(frame.duration, owner, false), spring: springOf(frame.spring, owner), startVelocity: pick(frame.startVelocity, "SpringKeyframe startVelocity") }; case "cubic": return { kind: "cubic", to: pick(frame.to, "CubicKeyframe value"), duration: duration(frame.duration, owner, true), startVelocity: pick(frame.startVelocity, "CubicKeyframe startVelocity"), endVelocity: pick(frame.endVelocity, "CubicKeyframe endVelocity") }; case "move": return { kind: "move", to: pick(frame.to, "MoveKeyframe value") }; default: throw new Error(`${owner} keyframes must be LinearKeyframe, SpringKeyframe, CubicKeyframe or MoveKeyframe`); } } function nativeTracks(result, fields, scalar, owner) { if (!Array.isArray(result)) throw new Error(`${owner} keyframes must return an array of tracks or keyframes`); var tracks = result.every(function(item) { return (item === null || item === void 0 ? void 0 : item.kind) === "track"; }) ? result : [{ kind: "track", content: result }]; var byField = /* @__PURE__ */ new Map(); var add = function add2(field, frames) { if (byField.has(field)) throw new Error(`${owner} keyframes animate ${fields[field]} in more than one track; one track per field is bridged`); byField.set(field, frames); }; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { var _loop = function() { var track = _step.value; if ((track === null || track === void 0 ? void 0 : track.kind) !== "track" || !Array.isArray(track.content)) throw new Error(`${owner} keyframes must not mix tracks and keyframes`); if (track.keyPath === void 0) { var read = function read2(value, what) { return vectorOf(value, fields, owner, what); }; fields.forEach(function(_, field2) { return add(field2, track.content.map(function(frame) { return nativeKeyframe(frame, field2, read, owner); })); }); } else { var field = scalar ? -1 : fields.indexOf(track.keyPath); if (field < 0) throw new Error(`${owner} KeyframeTrack keyPath ${track.keyPath} is not a field of the value`); var read1 = function read2(value, what) { if (typeof value !== "number" || !Number.isFinite(value)) throw new Error(`${owner} ${what} on ${track.keyPath} must be a finite number`); return fields.map(function() { return value; }); }; add(field, track.content.map(function(frame) { return nativeKeyframe(frame, field, read1, owner); })); } }; for (var _iterator = tracks[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) _loop(); } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return [...byField].map(function(param) { var [field, keyframes] = param; return { field, keyframes }; }); } function KeyframeAnimator(param) { var { initialValue, repeating, trigger, keyframes, children, style, ...props } = param; var owner = "Swift.KeyframeAnimator"; var id = useAnimatorId(); var count = useTriggerCount(trigger); if (typeof children !== "function") throw new Error(`${owner} takes a content function of its value; pass (value) => content`); if (typeof keyframes !== "function") throw new Error(`${owner} keyframes must be a function of the value`); if (trigger !== void 0 && repeating !== void 0) throw new Error(`${owner} takes repeating or trigger, not both`); if (repeating !== void 0 && typeof repeating !== "boolean") throw new Error(`${owner} repeating must be a boolean`); var scalar = typeof initialValue === "number"; var fields = valueFields(initialValue, owner, "initialValue"); var initial = vectorOf(initialValue, fields, owner, "initialValue"); var samples = [ initial, ...fields.map(function(_, field) { return initial.map(function(value, index) { return index === field ? value + 1 : value; }); }), initial.map(function(value, index) { return value + .37 * (index + 1) + .25; }) ]; var tracks = nativeTracks(keyframes(initialValue), fields, scalar, owner); var checkTracks = nativeTracks(keyframes(valueFrom(samples.at(-1), fields, scalar)), fields, scalar, owner); if (JSON.stringify(tracks) !== JSON.stringify(checkTracks)) throw new Error(`${owner} keyframes must not depend on their value argument; it is not bridged`); var content = zipContent(samples.map(function(vector) { return children(valueFrom(vector, fields, scalar)); }), owner, function(styles) { return (0, import_animatorStyle.animatedSwiftStyle)({ id, kind: "keyframe", owner, samples: styles.map(function(sample) { return sample !== null && sample !== void 0 ? sample : {}; }), values: samples }); }); (0, import_containerChildren.assertOneNativeChildren)(content, owner); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_OneNativeKeyframeAnimatorNativeComponent.default, { ...props, style: [{ alignSelf: "stretch" }, style], animatorId: id, initialValue: JSON.stringify(initial), tracks: JSON.stringify(tracks), repeating: repeating !== null && repeating !== void 0 ? repeating : true, hasTrigger: trigger !== void 0, trigger: count, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_containerChildren.InsideContainer, { value: true, children: content }) }); } //# sourceMappingURL=Animators.native.js.map