UNPKG

@kennarddh/spinning-prize-wheel

Version:
816 lines (799 loc) 25.2 kB
import Ue, { forwardRef as gr, useState as ie, useCallback as Te, useImperativeHandle as mr, useEffect as Ye, useMemo as br } from "react"; import J from "styled-components"; var le = {}, yr = { get exports() { return le; }, set exports(a) { le = a; } }, q = {}; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Le; function Er() { if (Le) return q; Le = 1; var a = Ue, s = Symbol.for("react.element"), c = Symbol.for("react.fragment"), g = Object.prototype.hasOwnProperty, _ = a.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, C = { key: !0, ref: !0, __self: !0, __source: !0 }; function O(T, v, $) { var b, w = {}, S = null, A = null; $ !== void 0 && (S = "" + $), v.key !== void 0 && (S = "" + v.key), v.ref !== void 0 && (A = v.ref); for (b in v) g.call(v, b) && !C.hasOwnProperty(b) && (w[b] = v[b]); if (T && T.defaultProps) for (b in v = T.defaultProps, v) w[b] === void 0 && (w[b] = v[b]); return { $$typeof: s, type: T, key: S, ref: A, props: w, _owner: _.current }; } return q.Fragment = c, q.jsx = O, q.jsxs = O, q; } var z = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Ne; function Rr() { return Ne || (Ne = 1, process.env.NODE_ENV !== "production" && function() { var a = Ue, s = Symbol.for("react.element"), c = Symbol.for("react.portal"), g = Symbol.for("react.fragment"), _ = Symbol.for("react.strict_mode"), C = Symbol.for("react.profiler"), O = Symbol.for("react.provider"), T = Symbol.for("react.context"), v = Symbol.for("react.forward_ref"), $ = Symbol.for("react.suspense"), b = Symbol.for("react.suspense_list"), w = Symbol.for("react.memo"), S = Symbol.for("react.lazy"), A = Symbol.for("react.offscreen"), Y = Symbol.iterator, ce = "@@iterator"; function fe(e) { if (e === null || typeof e != "object") return null; var r = Y && e[Y] || e[ce]; return typeof r == "function" ? r : null; } var j = a.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function m(e) { { for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++) t[n - 1] = arguments[n]; B("error", e, t); } } function B(e, r, t) { { var n = j.ReactDebugCurrentFrame, u = n.getStackAddendum(); u !== "" && (r += "%s", t = t.concat([u])); var l = t.map(function(i) { return String(i); }); l.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, l); } } var D = !1, L = !1, y = !1, de = !1, G = !1, N; N = Symbol.for("react.module.reference"); function H(e) { return !!(typeof e == "string" || typeof e == "function" || e === g || e === C || G || e === _ || e === $ || e === b || de || e === A || D || L || y || typeof e == "object" && e !== null && (e.$$typeof === S || e.$$typeof === w || e.$$typeof === O || e.$$typeof === T || e.$$typeof === v || e.$$typeof === N || e.getModuleId !== void 0)); } function ve(e, r, t) { var n = e.displayName; if (n) return n; var u = r.displayName || r.name || ""; return u !== "" ? t + "(" + u + ")" : t; } function p(e) { return e.displayName || "Context"; } function h(e) { if (e == null) return null; if (typeof e.tag == "number" && m("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case g: return "Fragment"; case c: return "Portal"; case C: return "Profiler"; case _: return "StrictMode"; case $: return "Suspense"; case b: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case T: var r = e; return p(r) + ".Consumer"; case O: var t = e; return p(t._context) + ".Provider"; case v: return ve(e, e.render, "ForwardRef"); case w: var n = e.displayName || null; return n !== null ? n : h(e.type) || "Memo"; case S: { var u = e, l = u._payload, i = u._init; try { return h(i(l)); } catch { return null; } } } return null; } var E = Object.assign, F = 0, I, U, K, X, Z, Q, P; function ee() { } ee.__reactDisabledLog = !0; function pe() { { if (F === 0) { I = console.log, U = console.info, K = console.warn, X = console.error, Z = console.group, Q = console.groupCollapsed, P = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: ee, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } F++; } } function Je() { { if (F--, F === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: E({}, e, { value: I }), info: E({}, e, { value: U }), warn: E({}, e, { value: K }), error: E({}, e, { value: X }), group: E({}, e, { value: Z }), groupCollapsed: E({}, e, { value: Q }), groupEnd: E({}, e, { value: P }) }); } F < 0 && m("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var he = j.ReactCurrentDispatcher, ge; function re(e, r, t) { { if (ge === void 0) try { throw Error(); } catch (u) { var n = u.stack.trim().match(/\n( *(at )?)/); ge = n && n[1] || ""; } return ` ` + ge + e; } } var me = !1, te; { var Be = typeof WeakMap == "function" ? WeakMap : Map; te = new Be(); } function xe(e, r) { if (!e || me) return ""; { var t = te.get(e); if (t !== void 0) return t; } var n; me = !0; var u = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var l; l = he.current, he.current = null, pe(); try { if (r) { var i = function() { throw Error(); }; if (Object.defineProperty(i.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(i, []); } catch (k) { n = k; } Reflect.construct(e, [], i); } else { try { i.call(); } catch (k) { n = k; } e.call(i.prototype); } } else { try { throw Error(); } catch (k) { n = k; } e(); } } catch (k) { if (k && n && typeof k.stack == "string") { for (var o = k.stack.split(` `), R = n.stack.split(` `), f = o.length - 1, d = R.length - 1; f >= 1 && d >= 0 && o[f] !== R[d]; ) d--; for (; f >= 1 && d >= 0; f--, d--) if (o[f] !== R[d]) { if (f !== 1 || d !== 1) do if (f--, d--, d < 0 || o[f] !== R[d]) { var x = ` ` + o[f].replace(" at new ", " at "); return e.displayName && x.includes("<anonymous>") && (x = x.replace("<anonymous>", e.displayName)), typeof e == "function" && te.set(e, x), x; } while (f >= 1 && d >= 0); break; } } } finally { me = !1, he.current = l, Je(), Error.prepareStackTrace = u; } var M = e ? e.displayName || e.name : "", Me = M ? re(M) : ""; return typeof e == "function" && te.set(e, Me), Me; } function Ge(e, r, t) { return xe(e, !1); } function He(e) { var r = e.prototype; return !!(r && r.isReactComponent); } function ne(e, r, t) { if (e == null) return ""; if (typeof e == "function") return xe(e, He(e)); if (typeof e == "string") return re(e); switch (e) { case $: return re("Suspense"); case b: return re("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case v: return Ge(e.render); case w: return ne(e.type, r, t); case S: { var n = e, u = n._payload, l = n._init; try { return ne(l(u), r, t); } catch { } } } return ""; } var ae = Object.prototype.hasOwnProperty, Ce = {}, Oe = j.ReactDebugCurrentFrame; function oe(e) { if (e) { var r = e._owner, t = ne(e.type, e._source, r ? r.type : null); Oe.setExtraStackFrame(t); } else Oe.setExtraStackFrame(null); } function Ke(e, r, t, n, u) { { var l = Function.call.bind(ae); for (var i in e) if (l(e, i)) { var o = void 0; try { if (typeof e[i] != "function") { var R = Error((n || "React class") + ": " + t + " type `" + i + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[i] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw R.name = "Invariant Violation", R; } o = e[i](r, i, n, t, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (f) { o = f; } o && !(o instanceof Error) && (oe(u), m("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", n || "React class", t, i, typeof o), oe(null)), o instanceof Error && !(o.message in Ce) && (Ce[o.message] = !0, oe(u), m("Failed %s type: %s", t, o.message), oe(null)); } } } var Xe = Array.isArray; function be(e) { return Xe(e); } function Ze(e) { { var r = typeof Symbol == "function" && Symbol.toStringTag, t = r && e[Symbol.toStringTag] || e.constructor.name || "Object"; return t; } } function Qe(e) { try { return Se(e), !1; } catch { return !0; } } function Se(e) { return "" + e; } function je(e) { if (Qe(e)) return m("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", Ze(e)), Se(e); } var V = j.ReactCurrentOwner, er = { key: !0, ref: !0, __self: !0, __source: !0 }, Pe, ke, ye; ye = {}; function rr(e) { if (ae.call(e, "ref")) { var r = Object.getOwnPropertyDescriptor(e, "ref").get; if (r && r.isReactWarning) return !1; } return e.ref !== void 0; } function tr(e) { if (ae.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function nr(e, r) { if (typeof e.ref == "string" && V.current && r && V.current.stateNode !== r) { var t = h(V.current.type); ye[t] || (m('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref', h(V.current.type), e.ref), ye[t] = !0); } } function ar(e, r) { { var t = function() { Pe || (Pe = !0, m("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "key", { get: t, configurable: !0 }); } } function or(e, r) { { var t = function() { ke || (ke = !0, m("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "ref", { get: t, configurable: !0 }); } } var ir = function(e, r, t, n, u, l, i) { var o = { $$typeof: s, type: e, key: r, ref: t, props: i, _owner: l }; return o._store = {}, Object.defineProperty(o._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(o, "_self", { configurable: !1, enumerable: !1, writable: !1, value: n }), Object.defineProperty(o, "_source", { configurable: !1, enumerable: !1, writable: !1, value: u }), Object.freeze && (Object.freeze(o.props), Object.freeze(o)), o; }; function ur(e, r, t, n, u) { { var l, i = {}, o = null, R = null; t !== void 0 && (je(t), o = "" + t), tr(r) && (je(r.key), o = "" + r.key), rr(r) && (R = r.ref, nr(r, u)); for (l in r) ae.call(r, l) && !er.hasOwnProperty(l) && (i[l] = r[l]); if (e && e.defaultProps) { var f = e.defaultProps; for (l in f) i[l] === void 0 && (i[l] = f[l]); } if (o || R) { var d = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; o && ar(i, d), R && or(i, d); } return ir(e, o, R, u, n, V.current, i); } } var Ee = j.ReactCurrentOwner, $e = j.ReactDebugCurrentFrame; function W(e) { if (e) { var r = e._owner, t = ne(e.type, e._source, r ? r.type : null); $e.setExtraStackFrame(t); } else $e.setExtraStackFrame(null); } var Re; Re = !1; function _e(e) { return typeof e == "object" && e !== null && e.$$typeof === s; } function Fe() { { if (Ee.current) { var e = h(Ee.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function sr(e) { { if (e !== void 0) { var r = e.fileName.replace(/^.*[\\\/]/, ""), t = e.lineNumber; return ` Check your code at ` + r + ":" + t + "."; } return ""; } } var Ie = {}; function lr(e) { { var r = Fe(); if (!r) { var t = typeof e == "string" ? e : e.displayName || e.name; t && (r = ` Check the top-level render call using <` + t + ">."); } return r; } } function Ae(e, r) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var t = lr(r); if (Ie[t]) return; Ie[t] = !0; var n = ""; e && e._owner && e._owner !== Ee.current && (n = " It was passed a child from " + h(e._owner.type) + "."), W(e), m('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', t, n), W(null); } } function De(e, r) { { if (typeof e != "object") return; if (be(e)) for (var t = 0; t < e.length; t++) { var n = e[t]; _e(n) && Ae(n, r); } else if (_e(e)) e._store && (e._store.validated = !0); else if (e) { var u = fe(e); if (typeof u == "function" && u !== e.entries) for (var l = u.call(e), i; !(i = l.next()).done; ) _e(i.value) && Ae(i.value, r); } } } function cr(e) { { var r = e.type; if (r == null || typeof r == "string") return; var t; if (typeof r == "function") t = r.propTypes; else if (typeof r == "object" && (r.$$typeof === v || r.$$typeof === w)) t = r.propTypes; else return; if (t) { var n = h(r); Ke(t, e.props, "prop", n, e); } else if (r.PropTypes !== void 0 && !Re) { Re = !0; var u = h(r); m("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", u || "Unknown"); } typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && m("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function fr(e) { { for (var r = Object.keys(e.props), t = 0; t < r.length; t++) { var n = r[t]; if (n !== "children" && n !== "key") { W(e), m("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), W(null); break; } } e.ref !== null && (W(e), m("Invalid attribute `ref` supplied to `React.Fragment`."), W(null)); } } function We(e, r, t, n, u, l) { { var i = H(e); if (!i) { var o = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (o += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var R = sr(u); R ? o += R : o += Fe(); var f; e === null ? f = "null" : be(e) ? f = "array" : e !== void 0 && e.$$typeof === s ? (f = "<" + (h(e.type) || "Unknown") + " />", o = " Did you accidentally export a JSX literal instead of a component?") : f = typeof e, m("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", f, o); } var d = ur(e, r, t, u, l); if (d == null) return d; if (i) { var x = r.children; if (x !== void 0) if (n) if (be(x)) { for (var M = 0; M < x.length; M++) De(x[M], e); Object.freeze && Object.freeze(x); } else m("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."); else De(x, e); } return e === g ? fr(d) : cr(d), d; } } function dr(e, r, t) { return We(e, r, t, !0); } function vr(e, r, t) { return We(e, r, t, !1); } var pr = vr, hr = dr; z.Fragment = g, z.jsx = pr, z.jsxs = hr; }()), z; } (function(a) { process.env.NODE_ENV === "production" ? a.exports = Er() : a.exports = Rr(); })(yr); const ue = le.jsx, _r = le.jsxs, Tr = J.div` display: flex; justify-content: center; align-items: center; flex-direction: column; `, wr = J.div` width: 50px; height: 50px; border: 10px solid black; border-top: none; border-left: none; rotate: 45deg; translate: 0 -50%; `, xr = J.div` background: conic-gradient( ${({ colors: a }) => a.map( (s, c) => `${s} ${c / a.length * 100}% ${c / a.length * 100 + 1 / a.length * 100}%` ).join(",")} ); width: ${({ size: a }) => a}px; height: ${({ size: a }) => a}px; border-radius: 50%; position: relative; display: flex; justify-content: center; align-items: center; transition: rotate cubic-bezier(0, -0.14, 0, 1) ${({ duration: a }) => a}s; rotate: ${({ rotation: a }) => a}deg; `, Cr = J.div` width: 75%; height: 75%; position: absolute; display: flex; justify-content: center; `, Or = J.p` rotate: ${({ rotation: a }) => a}deg; font-size: ${({ fontSize: a }) => a}px; width: fit-content; `, se = (a, s) => Math.floor(Math.random() * (s - a + 1) + a), Ve = (a) => { const c = Math.random() * 1e5 / 1e3, { result: g } = Object.entries(a).reduce( (_, [C, O]) => (_.result === null && c > 100 - O - _.acc && (_.result = C), _.acc += O, _), { acc: 0, result: null } ); return g; }, we = () => `hsla(${Math.random() * 360}, 100%, 50%, 1)`, qe = (a) => { const s = Math.floor(Math.random() * a.length); return a[s]; }, Sr = (a, s) => { let c; do c = qe(a); while (c === s); return c; }, ze = (a) => { const s = [...a]; for (let c = s.length - 1; c > 0; c -= 1) { const g = Math.floor(Math.random() * (c + 1)); [s[c], s[g]] = [s[g], s[c]]; } return s; }, jr = ({ rotateDuration: a, rarityGroups: s, choices: c, onEndRotate: g, onStartRotate: _, onReset: C, withoutArrow: O, fullRotationAddBeforeDestination: T, counterClockwise: v, groupColorByRarity: $, resetRotateDuration: b = 0, shuffleChoices: w, size: S, fontSize: A = 16, needReset: Y }, ce) => { const [fe, j] = ie(0), [m, B] = ie(a), [D, L] = ie(!1), [y, de] = ie([]), G = Te(() => T ? T && typeof T == "function" ? T() : T : se(2, 5), [T]), N = Te(() => { if (D) return; const p = 360 * G(), h = Ve( Object.entries(s).reduce( (P, [ee, pe]) => (P[ee] = pe.rarity, P), {} ) ), E = y.filter( (P) => P.rarityGroup === h ), F = se( 0, E.length - 1 ), I = y.findIndex( (P) => P.id === E[F].id ), U = v ? -1 : 1, K = U * -1 * (360 / y.length * I + 360 / y.length / 2), X = se(0, 360 / y.length * 0.35), Z = Math.random() > 0.5 ? -1 : 1, Q = U * (K + p + X * Z); _ && _(y[I].id), j(Q), a !== 0 ? setTimeout(() => { L(!0), g && g(y[I].id); }, a * 1e3) : (Y && L(!0), g && g(y[I].id)); }, [ D, G, s, y, v, _, a, g, Y ]), H = Te(() => { D && (L(!1), B(b), j(0), C && C(), setTimeout(() => { B(a); }, 0)); }, [D, C, b, a]); mr( ce, () => ({ Reset: H, Rotate: N }), [H, N] ), Ye(() => { if (new Set(c.map((p) => p.id)).size !== c.length) throw new Error("Choices id cannot be duplicate"); de(w ? ze(c) : c); }, [c, w]), Ye(() => { let p = 0; for (const h of Object.values(s)) { if (h.rarity < 0) throw new Error("Rarity must not be smaller than 0"); p += h.rarity; } if (p !== 100) throw new Error("Sum of rarities must be equal to 100"); }, [s]); const ve = br(() => Object.entries(s).reduce( (p, [h, { color: E }]) => (p[h] = E || we(), p), {} ), [s]); return /* @__PURE__ */ _r(Tr, { children: [ O ? null : /* @__PURE__ */ ue(wr, {}), /* @__PURE__ */ ue( xr, { colors: y.map((p) => $ ? ve[p.rarityGroup] : p.color ? p.color : we()), duration: m, rotation: fe, size: S, children: y.map(({ label: p, id: h }, E) => /* @__PURE__ */ ue(Cr, { children: /* @__PURE__ */ ue( Or, { rotation: E * (360 / y.length) + 180 / y.length, fontSize: A, children: p } ) }, h)) } ) ] }); }, $r = gr(jr), Fr = { Array: { Pick: qe, PickUnique: Sr, Shuffle: ze }, Random: { RandomBetween: se, RandomColor: we, WeightedRandom: Ve } }; export { Fr as Utils, $r as default }; //# sourceMappingURL=spinning-prize-wheel.es.js.map