UNPKG

@stakefish/ui

Version:

<div align="center"> <a href="https://www.npmjs.com/package/@stakefish/ui"><img src="https://gateway.pinata.cloud/ipfs/QmbZL1ceA8Yiz2pKALTg919jYx141DPUGegC9L4XpyayW5" width="300" /></a> </div>

2,063 lines (1,725 loc) 88.5 kB
import { a as __rest, _ as __assign } from '../tslib.es6-35932c2c.js'; import { jsx } from 'react/jsx-runtime'; import * as React from 'react'; import { useEffect, useState, useRef, forwardRef, useCallback, useContext, useMemo } from 'react'; import { unstable_batchedUpdates } from 'react-dom'; let updateQueue = makeQueue(); const raf = fn => schedule(fn, updateQueue); let writeQueue = makeQueue(); raf.write = fn => schedule(fn, writeQueue); let onStartQueue = makeQueue(); raf.onStart = fn => schedule(fn, onStartQueue); let onFrameQueue = makeQueue(); raf.onFrame = fn => schedule(fn, onFrameQueue); let onFinishQueue = makeQueue(); raf.onFinish = fn => schedule(fn, onFinishQueue); let timeouts = []; raf.setTimeout = (handler, ms) => { let time = raf.now() + ms; let cancel = () => { let i = timeouts.findIndex(t => t.cancel == cancel); if (~i) timeouts.splice(i, 1); __raf.count -= ~i ? 1 : 0; }; let timeout = { time, handler, cancel }; timeouts.splice(findTimeout(time), 0, timeout); __raf.count += 1; start(); return timeout; }; let findTimeout = time => ~(~timeouts.findIndex(t => t.time > time) || ~timeouts.length); raf.cancel = fn => { updateQueue.delete(fn); writeQueue.delete(fn); }; raf.sync = fn => { sync = true; raf.batchedUpdates(fn); sync = false; }; raf.throttle = fn => { let lastArgs; function queuedFn() { try { fn(...lastArgs); } finally { lastArgs = null; } } function throttled(...args) { lastArgs = args; raf.onStart(queuedFn); } throttled.handler = fn; throttled.cancel = () => { onStartQueue.delete(queuedFn); lastArgs = null; }; return throttled; }; let nativeRaf = typeof window != 'undefined' ? window.requestAnimationFrame : () => {}; raf.use = impl => nativeRaf = impl; raf.now = typeof performance != 'undefined' ? () => performance.now() : Date.now; raf.batchedUpdates = fn => fn(); raf.catch = console.error; raf.frameLoop = 'always'; raf.advance = () => { if (raf.frameLoop !== 'demand') { console.warn('Cannot call the manual advancement of rafz whilst frameLoop is not set as demand'); } else { update(); } }; let ts = -1; let sync = false; function schedule(fn, queue) { if (sync) { queue.delete(fn); fn(0); } else { queue.add(fn); start(); } } function start() { if (ts < 0) { ts = 0; if (raf.frameLoop !== 'demand') { nativeRaf(loop); } } } function loop() { if (~ts) { nativeRaf(loop); raf.batchedUpdates(update); } } function update() { let prevTs = ts; ts = raf.now(); let count = findTimeout(ts); if (count) { eachSafely(timeouts.splice(0, count), t => t.handler()); __raf.count -= count; } onStartQueue.flush(); updateQueue.flush(prevTs ? Math.min(64, ts - prevTs) : 16.667); onFrameQueue.flush(); writeQueue.flush(); onFinishQueue.flush(); } function makeQueue() { let next = new Set(); let current = next; return { add(fn) { __raf.count += current == next && !next.has(fn) ? 1 : 0; next.add(fn); }, delete(fn) { __raf.count -= current == next && next.has(fn) ? 1 : 0; return next.delete(fn); }, flush(arg) { if (current.size) { next = new Set(); __raf.count -= current.size; eachSafely(current, fn => fn(arg) && next.add(fn)); __raf.count += next.size; current = next; } } }; } function eachSafely(values, each) { values.forEach(value => { try { each(value); } catch (e) { raf.catch(e); } }); } const __raf = { count: 0, clear() { ts = -1; timeouts = []; onStartQueue = makeQueue(); updateQueue = makeQueue(); onFrameQueue = makeQueue(); writeQueue = makeQueue(); onFinishQueue = makeQueue(); __raf.count = 0; } }; function noop() {} const defineHidden = (obj, key, value) => Object.defineProperty(obj, key, { value, writable: true, configurable: true }); const is = { arr: Array.isArray, obj: a => !!a && a.constructor.name === 'Object', fun: a => typeof a === 'function', str: a => typeof a === 'string', num: a => typeof a === 'number', und: a => a === undefined }; function isEqual(a, b) { if (is.arr(a)) { if (!is.arr(b) || a.length !== b.length) return false; for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) return false; } return true; } return a === b; } const each = (obj, fn) => obj.forEach(fn); function eachProp(obj, fn, ctx) { if (is.arr(obj)) { for (let i = 0; i < obj.length; i++) { fn.call(ctx, obj[i], `${i}`); } return; } for (const key in obj) { if (obj.hasOwnProperty(key)) { fn.call(ctx, obj[key], key); } } } const toArray = a => is.und(a) ? [] : is.arr(a) ? a : [a]; function flush(queue, iterator) { if (queue.size) { const items = Array.from(queue); queue.clear(); each(items, iterator); } } const flushCalls = (queue, ...args) => flush(queue, fn => fn(...args)); let createStringInterpolator$1; let to; let colors$1 = null; let skipAnimation = false; let willAdvance = noop; const assign = globals => { if (globals.to) to = globals.to; if (globals.now) raf.now = globals.now; if (globals.colors !== undefined) colors$1 = globals.colors; if (globals.skipAnimation != null) skipAnimation = globals.skipAnimation; if (globals.createStringInterpolator) createStringInterpolator$1 = globals.createStringInterpolator; if (globals.requestAnimationFrame) raf.use(globals.requestAnimationFrame); if (globals.batchedUpdates) raf.batchedUpdates = globals.batchedUpdates; if (globals.willAdvance) willAdvance = globals.willAdvance; if (globals.frameLoop) raf.frameLoop = globals.frameLoop; }; var globals = /*#__PURE__*/Object.freeze({ __proto__: null, get createStringInterpolator () { return createStringInterpolator$1; }, get to () { return to; }, get colors () { return colors$1; }, get skipAnimation () { return skipAnimation; }, get willAdvance () { return willAdvance; }, assign: assign }); const startQueue = new Set(); let currentFrame = []; let prevFrame = []; let priority = 0; const frameLoop = { get idle() { return !startQueue.size && !currentFrame.length; }, start(animation) { if (priority > animation.priority) { startQueue.add(animation); raf.onStart(flushStartQueue); } else { startSafely(animation); raf(advance); } }, advance, sort(animation) { if (priority) { raf.onFrame(() => frameLoop.sort(animation)); } else { const prevIndex = currentFrame.indexOf(animation); if (~prevIndex) { currentFrame.splice(prevIndex, 1); startUnsafely(animation); } } }, clear() { currentFrame = []; startQueue.clear(); } }; function flushStartQueue() { startQueue.forEach(startSafely); startQueue.clear(); raf(advance); } function startSafely(animation) { if (!currentFrame.includes(animation)) startUnsafely(animation); } function startUnsafely(animation) { currentFrame.splice(findIndex(currentFrame, other => other.priority > animation.priority), 0, animation); } function advance(dt) { const nextFrame = prevFrame; for (let i = 0; i < currentFrame.length; i++) { const animation = currentFrame[i]; priority = animation.priority; if (!animation.idle) { willAdvance(animation); animation.advance(dt); if (!animation.idle) { nextFrame.push(animation); } } } priority = 0; prevFrame = currentFrame; prevFrame.length = 0; currentFrame = nextFrame; return currentFrame.length > 0; } function findIndex(arr, test) { const index = arr.findIndex(test); return index < 0 ? arr.length : index; } const colors = { transparent: 0x00000000, aliceblue: 0xf0f8ffff, antiquewhite: 0xfaebd7ff, aqua: 0x00ffffff, aquamarine: 0x7fffd4ff, azure: 0xf0ffffff, beige: 0xf5f5dcff, bisque: 0xffe4c4ff, black: 0x000000ff, blanchedalmond: 0xffebcdff, blue: 0x0000ffff, blueviolet: 0x8a2be2ff, brown: 0xa52a2aff, burlywood: 0xdeb887ff, burntsienna: 0xea7e5dff, cadetblue: 0x5f9ea0ff, chartreuse: 0x7fff00ff, chocolate: 0xd2691eff, coral: 0xff7f50ff, cornflowerblue: 0x6495edff, cornsilk: 0xfff8dcff, crimson: 0xdc143cff, cyan: 0x00ffffff, darkblue: 0x00008bff, darkcyan: 0x008b8bff, darkgoldenrod: 0xb8860bff, darkgray: 0xa9a9a9ff, darkgreen: 0x006400ff, darkgrey: 0xa9a9a9ff, darkkhaki: 0xbdb76bff, darkmagenta: 0x8b008bff, darkolivegreen: 0x556b2fff, darkorange: 0xff8c00ff, darkorchid: 0x9932ccff, darkred: 0x8b0000ff, darksalmon: 0xe9967aff, darkseagreen: 0x8fbc8fff, darkslateblue: 0x483d8bff, darkslategray: 0x2f4f4fff, darkslategrey: 0x2f4f4fff, darkturquoise: 0x00ced1ff, darkviolet: 0x9400d3ff, deeppink: 0xff1493ff, deepskyblue: 0x00bfffff, dimgray: 0x696969ff, dimgrey: 0x696969ff, dodgerblue: 0x1e90ffff, firebrick: 0xb22222ff, floralwhite: 0xfffaf0ff, forestgreen: 0x228b22ff, fuchsia: 0xff00ffff, gainsboro: 0xdcdcdcff, ghostwhite: 0xf8f8ffff, gold: 0xffd700ff, goldenrod: 0xdaa520ff, gray: 0x808080ff, green: 0x008000ff, greenyellow: 0xadff2fff, grey: 0x808080ff, honeydew: 0xf0fff0ff, hotpink: 0xff69b4ff, indianred: 0xcd5c5cff, indigo: 0x4b0082ff, ivory: 0xfffff0ff, khaki: 0xf0e68cff, lavender: 0xe6e6faff, lavenderblush: 0xfff0f5ff, lawngreen: 0x7cfc00ff, lemonchiffon: 0xfffacdff, lightblue: 0xadd8e6ff, lightcoral: 0xf08080ff, lightcyan: 0xe0ffffff, lightgoldenrodyellow: 0xfafad2ff, lightgray: 0xd3d3d3ff, lightgreen: 0x90ee90ff, lightgrey: 0xd3d3d3ff, lightpink: 0xffb6c1ff, lightsalmon: 0xffa07aff, lightseagreen: 0x20b2aaff, lightskyblue: 0x87cefaff, lightslategray: 0x778899ff, lightslategrey: 0x778899ff, lightsteelblue: 0xb0c4deff, lightyellow: 0xffffe0ff, lime: 0x00ff00ff, limegreen: 0x32cd32ff, linen: 0xfaf0e6ff, magenta: 0xff00ffff, maroon: 0x800000ff, mediumaquamarine: 0x66cdaaff, mediumblue: 0x0000cdff, mediumorchid: 0xba55d3ff, mediumpurple: 0x9370dbff, mediumseagreen: 0x3cb371ff, mediumslateblue: 0x7b68eeff, mediumspringgreen: 0x00fa9aff, mediumturquoise: 0x48d1ccff, mediumvioletred: 0xc71585ff, midnightblue: 0x191970ff, mintcream: 0xf5fffaff, mistyrose: 0xffe4e1ff, moccasin: 0xffe4b5ff, navajowhite: 0xffdeadff, navy: 0x000080ff, oldlace: 0xfdf5e6ff, olive: 0x808000ff, olivedrab: 0x6b8e23ff, orange: 0xffa500ff, orangered: 0xff4500ff, orchid: 0xda70d6ff, palegoldenrod: 0xeee8aaff, palegreen: 0x98fb98ff, paleturquoise: 0xafeeeeff, palevioletred: 0xdb7093ff, papayawhip: 0xffefd5ff, peachpuff: 0xffdab9ff, peru: 0xcd853fff, pink: 0xffc0cbff, plum: 0xdda0ddff, powderblue: 0xb0e0e6ff, purple: 0x800080ff, rebeccapurple: 0x663399ff, red: 0xff0000ff, rosybrown: 0xbc8f8fff, royalblue: 0x4169e1ff, saddlebrown: 0x8b4513ff, salmon: 0xfa8072ff, sandybrown: 0xf4a460ff, seagreen: 0x2e8b57ff, seashell: 0xfff5eeff, sienna: 0xa0522dff, silver: 0xc0c0c0ff, skyblue: 0x87ceebff, slateblue: 0x6a5acdff, slategray: 0x708090ff, slategrey: 0x708090ff, snow: 0xfffafaff, springgreen: 0x00ff7fff, steelblue: 0x4682b4ff, tan: 0xd2b48cff, teal: 0x008080ff, thistle: 0xd8bfd8ff, tomato: 0xff6347ff, turquoise: 0x40e0d0ff, violet: 0xee82eeff, wheat: 0xf5deb3ff, white: 0xffffffff, whitesmoke: 0xf5f5f5ff, yellow: 0xffff00ff, yellowgreen: 0x9acd32ff }; const NUMBER = '[-+]?\\d*\\.?\\d+'; const PERCENTAGE = NUMBER + '%'; function call(...parts) { return '\\(\\s*(' + parts.join(')\\s*,\\s*(') + ')\\s*\\)'; } const rgb = new RegExp('rgb' + call(NUMBER, NUMBER, NUMBER)); const rgba = new RegExp('rgba' + call(NUMBER, NUMBER, NUMBER, NUMBER)); const hsl = new RegExp('hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE)); const hsla = new RegExp('hsla' + call(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER)); const hex3 = /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/; const hex4 = /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/; const hex6 = /^#([0-9a-fA-F]{6})$/; const hex8 = /^#([0-9a-fA-F]{8})$/; function normalizeColor(color) { let match; if (typeof color === 'number') { return color >>> 0 === color && color >= 0 && color <= 0xffffffff ? color : null; } if (match = hex6.exec(color)) return parseInt(match[1] + 'ff', 16) >>> 0; if (colors$1 && colors$1[color] !== undefined) { return colors$1[color]; } if (match = rgb.exec(color)) { return (parse255(match[1]) << 24 | parse255(match[2]) << 16 | parse255(match[3]) << 8 | 0x000000ff) >>> 0; } if (match = rgba.exec(color)) { return (parse255(match[1]) << 24 | parse255(match[2]) << 16 | parse255(match[3]) << 8 | parse1(match[4])) >>> 0; } if (match = hex3.exec(color)) { return parseInt(match[1] + match[1] + match[2] + match[2] + match[3] + match[3] + 'ff', 16) >>> 0; } if (match = hex8.exec(color)) return parseInt(match[1], 16) >>> 0; if (match = hex4.exec(color)) { return parseInt(match[1] + match[1] + match[2] + match[2] + match[3] + match[3] + match[4] + match[4], 16) >>> 0; } if (match = hsl.exec(color)) { return (hslToRgb(parse360(match[1]), parsePercentage(match[2]), parsePercentage(match[3])) | 0x000000ff) >>> 0; } if (match = hsla.exec(color)) { return (hslToRgb(parse360(match[1]), parsePercentage(match[2]), parsePercentage(match[3])) | parse1(match[4])) >>> 0; } return null; } function hue2rgb(p, q, t) { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; } function hslToRgb(h, s, l) { const q = l < 0.5 ? l * (1 + s) : l + s - l * s; const p = 2 * l - q; const r = hue2rgb(p, q, h + 1 / 3); const g = hue2rgb(p, q, h); const b = hue2rgb(p, q, h - 1 / 3); return Math.round(r * 255) << 24 | Math.round(g * 255) << 16 | Math.round(b * 255) << 8; } function parse255(str) { const int = parseInt(str, 10); if (int < 0) return 0; if (int > 255) return 255; return int; } function parse360(str) { const int = parseFloat(str); return (int % 360 + 360) % 360 / 360; } function parse1(str) { const num = parseFloat(str); if (num < 0) return 0; if (num > 1) return 255; return Math.round(num * 255); } function parsePercentage(str) { const int = parseFloat(str); if (int < 0) return 0; if (int > 100) return 1; return int / 100; } function colorToRgba(input) { let int32Color = normalizeColor(input); if (int32Color === null) return input; int32Color = int32Color || 0; let r = (int32Color & 0xff000000) >>> 24; let g = (int32Color & 0x00ff0000) >>> 16; let b = (int32Color & 0x0000ff00) >>> 8; let a = (int32Color & 0x000000ff) / 255; return `rgba(${r}, ${g}, ${b}, ${a})`; } const createInterpolator = (range, output, extrapolate) => { if (is.fun(range)) { return range; } if (is.arr(range)) { return createInterpolator({ range, output: output, extrapolate }); } if (is.str(range.output[0])) { return createStringInterpolator$1(range); } const config = range; const outputRange = config.output; const inputRange = config.range || [0, 1]; const extrapolateLeft = config.extrapolateLeft || config.extrapolate || 'extend'; const extrapolateRight = config.extrapolateRight || config.extrapolate || 'extend'; const easing = config.easing || (t => t); return input => { const range = findRange(input, inputRange); return interpolate(input, inputRange[range], inputRange[range + 1], outputRange[range], outputRange[range + 1], easing, extrapolateLeft, extrapolateRight, config.map); }; }; function interpolate(input, inputMin, inputMax, outputMin, outputMax, easing, extrapolateLeft, extrapolateRight, map) { let result = map ? map(input) : input; if (result < inputMin) { if (extrapolateLeft === 'identity') return result;else if (extrapolateLeft === 'clamp') result = inputMin; } if (result > inputMax) { if (extrapolateRight === 'identity') return result;else if (extrapolateRight === 'clamp') result = inputMax; } if (outputMin === outputMax) return outputMin; if (inputMin === inputMax) return input <= inputMin ? outputMin : outputMax; if (inputMin === -Infinity) result = -result;else if (inputMax === Infinity) result = result - inputMin;else result = (result - inputMin) / (inputMax - inputMin); result = easing(result); if (outputMin === -Infinity) result = -result;else if (outputMax === Infinity) result = result + outputMin;else result = result * (outputMax - outputMin) + outputMin; return result; } function findRange(input, inputRange) { for (var i = 1; i < inputRange.length - 1; ++i) if (inputRange[i] >= input) break; return i - 1; } function _extends$2() { _extends$2 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$2.apply(this, arguments); } const $get = Symbol.for('FluidValue.get'); const $observers = Symbol.for('FluidValue.observers'); const hasFluidValue = arg => Boolean(arg && arg[$get]); const getFluidValue = arg => arg && arg[$get] ? arg[$get]() : arg; const getFluidObservers = target => target[$observers] || null; function callFluidObserver(observer, event) { if (observer.eventObserved) { observer.eventObserved(event); } else { observer(event); } } function callFluidObservers(target, event) { let observers = target[$observers]; if (observers) { observers.forEach(observer => { callFluidObserver(observer, event); }); } } class FluidValue { constructor(get) { this[$get] = void 0; this[$observers] = void 0; if (!get && !(get = this.get)) { throw Error('Unknown getter'); } setFluidGetter(this, get); } } const setFluidGetter = (target, get) => setHidden(target, $get, get); function addFluidObserver(target, observer) { if (target[$get]) { let observers = target[$observers]; if (!observers) { setHidden(target, $observers, observers = new Set()); } if (!observers.has(observer)) { observers.add(observer); if (target.observerAdded) { target.observerAdded(observers.size, observer); } } } return observer; } function removeFluidObserver(target, observer) { let observers = target[$observers]; if (observers && observers.has(observer)) { const count = observers.size - 1; if (count) { observers.delete(observer); } else { target[$observers] = null; } if (target.observerRemoved) { target.observerRemoved(count, observer); } } } const setHidden = (target, key, value) => Object.defineProperty(target, key, { value, writable: true, configurable: true }); const numberRegex = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/g; const colorRegex = /(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d\.]+%?\))/gi; const unitRegex = new RegExp(`(${numberRegex.source})(%|[a-z]+)`, 'i'); let namedColorRegex; const rgbaRegex = /rgba\(([0-9\.-]+), ([0-9\.-]+), ([0-9\.-]+), ([0-9\.-]+)\)/gi; const rgbaRound = (_, p1, p2, p3, p4) => `rgba(${Math.round(p1)}, ${Math.round(p2)}, ${Math.round(p3)}, ${p4})`; const createStringInterpolator = config => { if (!namedColorRegex) namedColorRegex = colors$1 ? new RegExp(`(${Object.keys(colors$1).join('|')})(?!\\w)`, 'g') : /^\b$/; const output = config.output.map(value => getFluidValue(value).replace(colorRegex, colorToRgba).replace(namedColorRegex, colorToRgba)); const keyframes = output.map(value => value.match(numberRegex).map(Number)); const outputRanges = keyframes[0].map((_, i) => keyframes.map(values => { if (!(i in values)) { throw Error('The arity of each "output" value must be equal'); } return values[i]; })); const interpolators = outputRanges.map(output => createInterpolator(_extends$2({}, config, { output }))); return input => { var _output$find; const missingUnit = !unitRegex.test(output[0]) && ((_output$find = output.find(value => unitRegex.test(value))) == null ? void 0 : _output$find.replace(numberRegex, '')); let i = 0; return output[0].replace(numberRegex, () => `${interpolators[i++](input)}${missingUnit || ''}`).replace(rgbaRegex, rgbaRound); }; }; const prefix = 'react-spring: '; const once = fn => { const func = fn; let called = false; if (typeof func != 'function') { throw new TypeError(`${prefix}once requires a function parameter`); } return (...args) => { if (!called) { func(...args); called = true; } }; }; const warnInterpolate = once(console.warn); function deprecateInterpolate() { warnInterpolate(`${prefix}The "interpolate" function is deprecated in v9 (use "to" instead)`); } const warnDirectCall = once(console.warn); function deprecateDirectCall() { warnDirectCall(`${prefix}Directly calling start instead of using the api object is deprecated in v9 (use ".start" instead), this will be removed in later 0.X.0 versions`); } function isAnimatedString(value) { return is.str(value) && (value[0] == '#' || /\d/.test(value) || value in (colors$1 || {})); } const useOnce = effect => useEffect(effect, emptyDeps); const emptyDeps = []; function useForceUpdate() { const update = useState()[1]; const mounted = useState(makeMountedRef)[0]; useOnce(mounted.unmount); return () => { if (mounted.current) { update({}); } }; } function makeMountedRef() { const mounted = { current: true, unmount: () => () => { mounted.current = false; } }; return mounted; } function useMemoOne(getResult, inputs) { const [initial] = useState(() => ({ inputs, result: getResult() })); const committed = useRef(); const prevCache = committed.current; let cache = prevCache; if (cache) { const useCache = Boolean(inputs && cache.inputs && areInputsEqual(inputs, cache.inputs)); if (!useCache) { cache = { inputs, result: getResult() }; } } else { cache = initial; } useEffect(() => { committed.current = cache; if (prevCache == initial) { initial.inputs = initial.result = undefined; } }, [cache]); return cache.result; } function areInputsEqual(next, prev) { if (next.length !== prev.length) { return false; } for (let i = 0; i < next.length; i++) { if (next[i] !== prev[i]) { return false; } } return true; } function usePrev(value) { const prevRef = useRef(); useEffect(() => { prevRef.current = value; }); return prevRef.current; } const useLayoutEffect = typeof window !== 'undefined' && window.document && window.document.createElement ? React.useLayoutEffect : React.useEffect; const $node = Symbol.for('Animated:node'); const isAnimated = value => !!value && value[$node] === value; const getAnimated = owner => owner && owner[$node]; const setAnimated = (owner, node) => defineHidden(owner, $node, node); const getPayload = owner => owner && owner[$node] && owner[$node].getPayload(); class Animated { constructor() { this.payload = void 0; setAnimated(this, this); } getPayload() { return this.payload || []; } } class AnimatedValue extends Animated { constructor(_value) { super(); this.done = true; this.elapsedTime = void 0; this.lastPosition = void 0; this.lastVelocity = void 0; this.v0 = void 0; this.durationProgress = 0; this._value = _value; if (is.num(this._value)) { this.lastPosition = this._value; } } static create(value) { return new AnimatedValue(value); } getPayload() { return [this]; } getValue() { return this._value; } setValue(value, step) { if (is.num(value)) { this.lastPosition = value; if (step) { value = Math.round(value / step) * step; if (this.done) { this.lastPosition = value; } } } if (this._value === value) { return false; } this._value = value; return true; } reset() { const { done } = this; this.done = false; if (is.num(this._value)) { this.elapsedTime = 0; this.durationProgress = 0; this.lastPosition = this._value; if (done) this.lastVelocity = null; this.v0 = null; } } } class AnimatedString extends AnimatedValue { constructor(value) { super(0); this._string = null; this._toString = void 0; this._toString = createInterpolator({ output: [value, value] }); } static create(value) { return new AnimatedString(value); } getValue() { let value = this._string; return value == null ? this._string = this._toString(this._value) : value; } setValue(value) { if (is.str(value)) { if (value == this._string) { return false; } this._string = value; this._value = 1; } else if (super.setValue(value)) { this._string = null; } else { return false; } return true; } reset(goal) { if (goal) { this._toString = createInterpolator({ output: [this.getValue(), goal] }); } this._value = 0; super.reset(); } } const TreeContext = { dependencies: null }; class AnimatedObject extends Animated { constructor(source) { super(); this.source = source; this.setValue(source); } getValue(animated) { const values = {}; eachProp(this.source, (source, key) => { if (isAnimated(source)) { values[key] = source.getValue(animated); } else if (hasFluidValue(source)) { values[key] = getFluidValue(source); } else if (!animated) { values[key] = source; } }); return values; } setValue(source) { this.source = source; this.payload = this._makePayload(source); } reset() { if (this.payload) { each(this.payload, node => node.reset()); } } _makePayload(source) { if (source) { const payload = new Set(); eachProp(source, this._addToPayload, payload); return Array.from(payload); } } _addToPayload(source) { if (TreeContext.dependencies && hasFluidValue(source)) { TreeContext.dependencies.add(source); } const payload = getPayload(source); if (payload) { each(payload, node => this.add(node)); } } } class AnimatedArray extends AnimatedObject { constructor(source) { super(source); } static create(source) { return new AnimatedArray(source); } getValue() { return this.source.map(node => node.getValue()); } setValue(source) { const payload = this.getPayload(); if (source.length == payload.length) { return payload.map((node, i) => node.setValue(source[i])).some(Boolean); } super.setValue(source.map(makeAnimated)); return true; } } function makeAnimated(value) { const nodeType = isAnimatedString(value) ? AnimatedString : AnimatedValue; return nodeType.create(value); } function getAnimatedType(value) { const parentNode = getAnimated(value); return parentNode ? parentNode.constructor : is.arr(value) ? AnimatedArray : isAnimatedString(value) ? AnimatedString : AnimatedValue; } function _extends$1() { _extends$1 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$1.apply(this, arguments); } const withAnimated = (Component, host) => { const hasInstance = !is.fun(Component) || Component.prototype && Component.prototype.isReactComponent; return forwardRef((givenProps, givenRef) => { const instanceRef = useRef(null); const ref = hasInstance && useCallback(value => { instanceRef.current = updateRef(givenRef, value); }, [givenRef]); const [props, deps] = getAnimatedState(givenProps, host); const forceUpdate = useForceUpdate(); const callback = () => { const instance = instanceRef.current; if (hasInstance && !instance) { return; } const didUpdate = instance ? host.applyAnimatedValues(instance, props.getValue(true)) : false; if (didUpdate === false) { forceUpdate(); } }; const observer = new PropsObserver(callback, deps); const observerRef = useRef(); useLayoutEffect(() => { const lastObserver = observerRef.current; observerRef.current = observer; each(deps, dep => addFluidObserver(dep, observer)); if (lastObserver) { each(lastObserver.deps, dep => removeFluidObserver(dep, lastObserver)); raf.cancel(lastObserver.update); } }); useEffect(callback, []); useOnce(() => () => { const observer = observerRef.current; each(observer.deps, dep => removeFluidObserver(dep, observer)); }); const usedProps = host.getComponentProps(props.getValue()); return React.createElement(Component, _extends$1({}, usedProps, { ref: ref })); }); }; class PropsObserver { constructor(update, deps) { this.update = update; this.deps = deps; } eventObserved(event) { if (event.type == 'change') { raf.write(this.update); } } } function getAnimatedState(props, host) { const dependencies = new Set(); TreeContext.dependencies = dependencies; if (props.style) props = _extends$1({}, props, { style: host.createAnimatedStyle(props.style) }); props = new AnimatedObject(props); TreeContext.dependencies = null; return [props, dependencies]; } function updateRef(ref, value) { if (ref) { if (is.fun(ref)) ref(value);else ref.current = value; } return value; } const cacheKey = Symbol.for('AnimatedComponent'); const createHost = (components, { applyAnimatedValues: _applyAnimatedValues = () => false, createAnimatedStyle: _createAnimatedStyle = style => new AnimatedObject(style), getComponentProps: _getComponentProps = props => props } = {}) => { const hostConfig = { applyAnimatedValues: _applyAnimatedValues, createAnimatedStyle: _createAnimatedStyle, getComponentProps: _getComponentProps }; const animated = Component => { const displayName = getDisplayName(Component) || 'Anonymous'; if (is.str(Component)) { Component = animated[Component] || (animated[Component] = withAnimated(Component, hostConfig)); } else { Component = Component[cacheKey] || (Component[cacheKey] = withAnimated(Component, hostConfig)); } Component.displayName = `Animated(${displayName})`; return Component; }; eachProp(components, (Component, key) => { if (is.arr(components)) { key = getDisplayName(Component); } animated[key] = animated(Component); }); return { animated }; }; const getDisplayName = arg => is.str(arg) ? arg : arg && is.str(arg.displayName) ? arg.displayName : is.fun(arg) && arg.name || null; function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function callProp(value, ...args) { return is.fun(value) ? value(...args) : value; } const matchProp = (value, key) => value === true || !!(key && value && (is.fun(value) ? value(key) : toArray(value).includes(key))); const resolveProp = (prop, key) => is.obj(prop) ? key && prop[key] : prop; const getDefaultProp = (props, key) => props.default === true ? props[key] : props.default ? props.default[key] : undefined; const noopTransform = value => value; const getDefaultProps = (props, transform = noopTransform) => { let keys = DEFAULT_PROPS; if (props.default && props.default !== true) { props = props.default; keys = Object.keys(props); } const defaults = {}; for (const key of keys) { const value = transform(props[key], key); if (!is.und(value)) { defaults[key] = value; } } return defaults; }; const DEFAULT_PROPS = ['config', 'onProps', 'onStart', 'onChange', 'onPause', 'onResume', 'onRest']; const RESERVED_PROPS = { config: 1, from: 1, to: 1, ref: 1, loop: 1, reset: 1, pause: 1, cancel: 1, reverse: 1, immediate: 1, default: 1, delay: 1, onProps: 1, onStart: 1, onChange: 1, onPause: 1, onResume: 1, onRest: 1, onResolve: 1, items: 1, trail: 1, sort: 1, expires: 1, initial: 1, enter: 1, update: 1, leave: 1, children: 1, onDestroyed: 1, keys: 1, callId: 1, parentId: 1 }; function getForwardProps(props) { const forward = {}; let count = 0; eachProp(props, (value, prop) => { if (!RESERVED_PROPS[prop]) { forward[prop] = value; count++; } }); if (count) { return forward; } } function inferTo(props) { const to = getForwardProps(props); if (to) { const out = { to }; eachProp(props, (val, key) => key in to || (out[key] = val)); return out; } return _extends({}, props); } function computeGoal(value) { value = getFluidValue(value); return is.arr(value) ? value.map(computeGoal) : isAnimatedString(value) ? globals.createStringInterpolator({ range: [0, 1], output: [value, value] })(1) : value; } function hasProps(props) { for (const _ in props) return true; return false; } function isAsyncTo(to) { return is.fun(to) || is.arr(to) && is.obj(to[0]); } function detachRefs(ctrl, ref) { var _ctrl$ref; (_ctrl$ref = ctrl.ref) == null ? void 0 : _ctrl$ref.delete(ctrl); ref == null ? void 0 : ref.delete(ctrl); } function replaceRef(ctrl, ref) { if (ref && ctrl.ref !== ref) { var _ctrl$ref2; (_ctrl$ref2 = ctrl.ref) == null ? void 0 : _ctrl$ref2.delete(ctrl); ref.add(ctrl); ctrl.ref = ref; } } const config = { default: { tension: 170, friction: 26 }, gentle: { tension: 120, friction: 14 }, wobbly: { tension: 180, friction: 12 }, stiff: { tension: 210, friction: 20 }, slow: { tension: 280, friction: 60 }, molasses: { tension: 280, friction: 120 } }; const linear = t => t; const defaults = _extends({}, config.default, { mass: 1, damping: 1, easing: linear, clamp: false }); class AnimationConfig { constructor() { this.tension = void 0; this.friction = void 0; this.frequency = void 0; this.damping = void 0; this.mass = void 0; this.velocity = 0; this.restVelocity = void 0; this.precision = void 0; this.progress = void 0; this.duration = void 0; this.easing = void 0; this.clamp = void 0; this.bounce = void 0; this.decay = void 0; this.round = void 0; Object.assign(this, defaults); } } function mergeConfig(config, newConfig, defaultConfig) { if (defaultConfig) { defaultConfig = _extends({}, defaultConfig); sanitizeConfig(defaultConfig, newConfig); newConfig = _extends({}, defaultConfig, newConfig); } sanitizeConfig(config, newConfig); Object.assign(config, newConfig); for (const key in defaults) { if (config[key] == null) { config[key] = defaults[key]; } } let { mass, frequency, damping } = config; if (!is.und(frequency)) { if (frequency < 0.01) frequency = 0.01; if (damping < 0) damping = 0; config.tension = Math.pow(2 * Math.PI / frequency, 2) * mass; config.friction = 4 * Math.PI * damping * mass / frequency; } return config; } function sanitizeConfig(config, props) { if (!is.und(props.decay)) { config.duration = undefined; } else { const isTensionConfig = !is.und(props.tension) || !is.und(props.friction); if (isTensionConfig || !is.und(props.frequency) || !is.und(props.damping) || !is.und(props.mass)) { config.duration = undefined; config.decay = undefined; } if (isTensionConfig) { config.frequency = undefined; } } } const emptyArray = []; class Animation { constructor() { this.changed = false; this.values = emptyArray; this.toValues = null; this.fromValues = emptyArray; this.to = void 0; this.from = void 0; this.config = new AnimationConfig(); this.immediate = false; } } function scheduleProps(callId, { key, props, defaultProps, state, actions }) { return new Promise((resolve, reject) => { var _props$cancel; let delay; let timeout; let cancel = matchProp((_props$cancel = props.cancel) != null ? _props$cancel : defaultProps == null ? void 0 : defaultProps.cancel, key); if (cancel) { onStart(); } else { if (!is.und(props.pause)) { state.paused = matchProp(props.pause, key); } let pause = defaultProps == null ? void 0 : defaultProps.pause; if (pause !== true) { pause = state.paused || matchProp(pause, key); } delay = callProp(props.delay || 0, key); if (pause) { state.resumeQueue.add(onResume); actions.pause(); } else { actions.resume(); onResume(); } } function onPause() { state.resumeQueue.add(onResume); state.timeouts.delete(timeout); timeout.cancel(); delay = timeout.time - raf.now(); } function onResume() { if (delay > 0) { timeout = raf.setTimeout(onStart, delay); state.pauseQueue.add(onPause); state.timeouts.add(timeout); } else { onStart(); } } function onStart() { state.pauseQueue.delete(onPause); state.timeouts.delete(timeout); if (callId <= (state.cancelId || 0)) { cancel = true; } try { actions.start(_extends({}, props, { callId, cancel }), resolve); } catch (err) { reject(err); } } }); } const getCombinedResult = (target, results) => results.length == 1 ? results[0] : results.some(result => result.cancelled) ? getCancelledResult(target.get()) : results.every(result => result.noop) ? getNoopResult(target.get()) : getFinishedResult(target.get(), results.every(result => result.finished)); const getNoopResult = value => ({ value, noop: true, finished: true, cancelled: false }); const getFinishedResult = (value, finished, cancelled = false) => ({ value, finished, cancelled }); const getCancelledResult = value => ({ value, cancelled: true, finished: false }); function runAsync(to, props, state, target) { const { callId, parentId, onRest } = props; const { asyncTo: prevTo, promise: prevPromise } = state; if (!parentId && to === prevTo && !props.reset) { return prevPromise; } return state.promise = (async () => { state.asyncId = callId; state.asyncTo = to; const defaultProps = getDefaultProps(props, (value, key) => key === 'onRest' ? undefined : value); let preventBail; let bail; const bailPromise = new Promise((resolve, reject) => (preventBail = resolve, bail = reject)); const bailIfEnded = bailSignal => { const bailResult = callId <= (state.cancelId || 0) && getCancelledResult(target) || callId !== state.asyncId && getFinishedResult(target, false); if (bailResult) { bailSignal.result = bailResult; bail(bailSignal); throw bailSignal; } }; const animate = (arg1, arg2) => { const bailSignal = new BailSignal(); const skipAnimationSignal = new SkipAniamtionSignal(); return (async () => { if (globals.skipAnimation) { stopAsync(state); skipAnimationSignal.result = getFinishedResult(target, false); bail(skipAnimationSignal); throw skipAnimationSignal; } bailIfEnded(bailSignal); const props = is.obj(arg1) ? _extends({}, arg1) : _extends({}, arg2, { to: arg1 }); props.parentId = callId; eachProp(defaultProps, (value, key) => { if (is.und(props[key])) { props[key] = value; } }); const result = await target.start(props); bailIfEnded(bailSignal); if (state.paused) { await new Promise(resume => { state.resumeQueue.add(resume); }); } return result; })(); }; let result; if (globals.skipAnimation) { stopAsync(state); return getFinishedResult(target, false); } try { let animating; if (is.arr(to)) { animating = (async queue => { for (const props of queue) { await animate(props); } })(to); } else { animating = Promise.resolve(to(animate, target.stop.bind(target))); } await Promise.all([animating.then(preventBail), bailPromise]); result = getFinishedResult(target.get(), true, false); } catch (err) { if (err instanceof BailSignal) { result = err.result; } else if (err instanceof SkipAniamtionSignal) { result = err.result; } else { throw err; } } finally { if (callId == state.asyncId) { state.asyncId = parentId; state.asyncTo = parentId ? prevTo : undefined; state.promise = parentId ? prevPromise : undefined; } } if (is.fun(onRest)) { raf.batchedUpdates(() => { onRest(result, target, target.item); }); } return result; })(); } function stopAsync(state, cancelId) { flush(state.timeouts, t => t.cancel()); state.pauseQueue.clear(); state.resumeQueue.clear(); state.asyncId = state.asyncTo = state.promise = undefined; if (cancelId) state.cancelId = cancelId; } class BailSignal extends Error { constructor() { super('An async animation has been interrupted. You see this error because you ' + 'forgot to use `await` or `.catch(...)` on its returned promise.'); this.result = void 0; } } class SkipAniamtionSignal extends Error { constructor() { super('SkipAnimationSignal'); this.result = void 0; } } const isFrameValue = value => value instanceof FrameValue; let nextId$1 = 1; class FrameValue extends FluidValue { constructor(...args) { super(...args); this.id = nextId$1++; this.key = void 0; this._priority = 0; } get priority() { return this._priority; } set priority(priority) { if (this._priority != priority) { this._priority = priority; this._onPriorityChange(priority); } } get() { const node = getAnimated(this); return node && node.getValue(); } to(...args) { return globals.to(this, args); } interpolate(...args) { deprecateInterpolate(); return globals.to(this, args); } toJSON() { return this.get(); } observerAdded(count) { if (count == 1) this._attach(); } observerRemoved(count) { if (count == 0) this._detach(); } _attach() {} _detach() {} _onChange(value, idle = false) { callFluidObservers(this, { type: 'change', parent: this, value, idle }); } _onPriorityChange(priority) { if (!this.idle) { frameLoop.sort(this); } callFluidObservers(this, { type: 'priority', parent: this, priority }); } } const $P = Symbol.for('SpringPhase'); const HAS_ANIMATED = 1; const IS_ANIMATING = 2; const IS_PAUSED = 4; const hasAnimated = target => (target[$P] & HAS_ANIMATED) > 0; const isAnimating = target => (target[$P] & IS_ANIMATING) > 0; const isPaused = target => (target[$P] & IS_PAUSED) > 0; const setActiveBit = (target, active) => active ? target[$P] |= IS_ANIMATING | HAS_ANIMATED : target[$P] &= ~IS_ANIMATING; const setPausedBit = (target, paused) => paused ? target[$P] |= IS_PAUSED : target[$P] &= ~IS_PAUSED; class SpringValue extends FrameValue { constructor(arg1, arg2) { super(); this.key = void 0; this.animation = new Animation(); this.queue = void 0; this.defaultProps = {}; this._state = { paused: false, pauseQueue: new Set(), resumeQueue: new Set(), timeouts: new Set() }; this._pendingCalls = new Set(); this._lastCallId = 0; this._lastToId = 0; this._memoizedDuration = 0; if (!is.und(arg1) || !is.und(arg2)) { const props = is.obj(arg1) ? _extends({}, arg1) : _extends({}, arg2, { from: arg1 }); if (is.und(props.default)) { props.default = true; } this.start(props); } } get idle() { return !(isAnimating(this) || this._state.asyncTo) || isPaused(this); } get goal() { return getFluidValue(this.animation.to); } get velocity() { const node = getAnimated(this); return node instanceof AnimatedValue ? node.lastVelocity || 0 : node.getPayload().map(node => node.lastVelocity || 0); } get hasAnimated() { return hasAnimated(this); } get isAnimating() { return isAnimating(this); } get isPaused() { return isPaused(this); } advance(dt) { let idle = true; let changed = false; const anim = this.animation; let { config, toValues } = anim; const payload = getPayload(anim.to); if (!payload && hasFluidValue(anim.to)) { toValues = toArray(getFluidValue(anim.to)); } anim.values.forEach((node, i) => { if (node.done) return; const to = node.constructor == AnimatedString ? 1 : payload ? payload[i].lastPosition : toValues[i]; let finished = anim.immediate; let position = to; if (!finished) { position = node.lastPosition; if (config.tension <= 0) { node.done = true; return; } let elapsed = node.elapsedTime += dt; const from = anim.fromValues[i]; const v0 = node.v0 != null ? node.v0 : node.v0 = is.arr(config.velocity) ? config.velocity[i] : config.velocity; let velocity; if (!is.und(config.duration)) { let p = 1; if (config.duration > 0) { if (this._memoizedDuration !== config.duration) { this._memoizedDuration = config.duration; if (node.durationProgress > 0) { node.elapsedTime = config.duration * node.durationProgress; elapsed = node.elapsedTime += dt; } } p = (config.progress || 0) + elapsed / this._memoizedDuration; p = p > 1 ? 1 : p < 0 ? 0 : p; node.durationProgress = p; } position = from + config.easing(p) * (to - from); velocity = (position - node.lastPosition) / dt; finished = p == 1; } else if (config.decay) { const decay = config.decay === true ? 0.998 : config.decay; const e = Math.exp(-(1 - decay) * elapsed); position = from + v0 / (1 - decay) * (1 - e); finished = Math.abs(node.lastPosition - position) < 0.1; velocity = v0 * e; } else { velocity = node.lastVelocity == null ? v0 : node.lastVelocity; const precision = config.precision || (from == to ? 0.005 : Math.min(1, Math.abs(to - from) * 0.001)); const restVelocity = config.restVelocity || precision / 10; const bounceFactor = config.clamp ? 0 : config.bounce; const canBounce = !is.und(bounceFactor); const isGrowing = from == to ? node.v0 > 0 : from < to; let isMoving; let isBouncing = false; const step = 1; const numSteps = Math.ceil(dt / step); for (let n = 0; n < numSteps; ++n) { isMoving = Math.abs(velocity) > restVelocity; if (!isMoving) { finished = Math.abs(to - position) <= precision; if (finished) { break; } } if (canBounce) { isBouncing = position == to || position > to == isGrowing; if (isBouncing) { velocity = -velocity * bounceFactor; position = to; } } const springForce = -config.tension * 0.000001 * (position - to); const dampingForce = -config.friction * 0.001 * velocity; const acceleration = (springForce + dampingForce) / config.mass; velocity = velocity + acceleration * step; position = position + velocity * step; } } node.lastVelocity = velocity; if (Number.isNaN(position)) { console.warn(`Got NaN while animating:`, this); finished = true; } } if (payload && !payload[i].done) { finished = false; } if (finished) { node.done = true; } else { idle = false; } if (node.setValue(position, config.round)) { changed = true; } }); const node = getAnimated(this); const currVal = node.getValue(); if (idle) { const finalVal = getFluidValue(anim.to);