UNPKG

@atlaskit/motion

Version:

A set of utilities to apply motion in your application.

180 lines (178 loc) • 7.55 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.observeResizing = observeResizing; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function resolveToken(value, computed) { // Web Animations timing options need resolved values rather than CSS var() references. if (!value.startsWith('var(')) { return value; } var contents = value.slice(4, -1); var comma = contents.indexOf(','); var property = comma === -1 ? contents : contents.slice(0, comma); return computed.getPropertyValue(property.trim()).trim() || resolveToken(comma === -1 ? '' : contents.slice(comma + 1).trim(), computed); } /** * Observe natural sizes and let the browser own the animation timeline. A mutation * temporarily detaches the effect so the next observation sees the natural size. * If that size is unchanged, reattach the same effect without restarting its clock. * This keeps content mounted and preserves CSS constraints and text truncation. */ function observeResizing(element, getOptions) { // The last observed size: natural when idle, or displayed while an animation is attached. // Keep the destination separately so an interruption can start from the displayed size. var current; var running; var skipNext = false; var disconnected = false; var vertical = false; var stop = function stop() { if (running) { running.animation.onfinish = null; running.animation.cancel(); running = undefined; } }; var reobserve = function reobserve() { // Request an entry even when the natural size matches the last displayed frame. resizeObserver.unobserve(element); resizeObserver.observe(element, { box: 'border-box' }); }; var resizeObserver = new ResizeObserver(function (entries) { var _running, _running2; if (disconnected) { return; } var entry = entries.find(function (entry) { return entry.target === element; }); if (!entry) { return; } var computed = (_running = running) !== null && _running !== void 0 && _running.effect.target ? undefined : getComputedStyle(element); if (computed) { vertical = computed.writingMode.startsWith('vertical') || computed.writingMode.startsWith('sideways'); } var box = entry.borderBoxSize[0]; // Observer sizes use logical axes; animation keyframes use physical width and height. var next = { width: vertical ? box.blockSize : box.inlineSize, height: vertical ? box.inlineSize : box.blockSize }; if ((_running2 = running) !== null && _running2 !== void 0 && _running2.effect.target) { // Our own width/height animation also triggers observations. Cache its displayed size // for interruptions, but do not mistake each animation frame for a new destination. current = next; return; } var _getOptions = getOptions(), dimension = _getOptions.dimension, duration = _getOptions.duration, easing = _getOptions.easing, shouldAnimate = _getOptions.shouldAnimate, onStartMotion = _getOptions.onStartMotion, onFinishMotion = _getOptions.onFinishMotion; var changed = function changed(before) { return dimension !== 'height' && before.width !== next.width || dimension !== 'width' && before.height !== next.height; }; if (running && shouldAnimate && !skipNext && !changed(running.destination)) { // ARIA, color and other mutations can leave layout unchanged. The browser // keeps advancing this animation while its effect is detached. running.effect.target = element; return; } var previous = current; stop(); current = next; var suppressed = skipNext || !shouldAnimate; skipNext = false; if (!previous) { // The first observation establishes a baseline; mounting alone should not animate. return; } if (suppressed || !changed(previous) || !computed || typeof element.animate !== 'function') { onFinishMotion === null || onFinishMotion === void 0 || onFinishMotion(); return; } var durationValue = resolveToken(duration, computed); var parsed = parseFloat(durationValue); var durationMs = parsed * (durationValue.endsWith('ms') ? 1 : 1000); if (!Number.isFinite(durationMs) || durationMs <= 0) { onFinishMotion === null || onFinishMotion === void 0 || onFinishMotion(); return; } onStartMotion === null || onStartMotion === void 0 || onStartMotion(); var frame = function frame(size) { return _objectSpread(_objectSpread({ boxSizing: 'border-box' }, dimension !== 'height' && { width: "".concat(size.width, "px") }), dimension !== 'width' && { height: "".concat(size.height, "px") }); }; var animation = element.animate([frame(previous), frame(next)], { duration: durationMs, easing: resolveToken(easing, computed) || 'ease' }); running = { animation: animation, effect: animation.effect, destination: next }; current = previous; animation.onfinish = function () { var _getOptions$onFinishM, _getOptions2; current = next; // Remove the animation effect so normal CSS sizing controls the element again. stop(); (_getOptions$onFinishM = (_getOptions2 = getOptions()).onFinishMotion) === null || _getOptions$onFinishM === void 0 || _getOptions$onFinishM.call(_getOptions2); }; }); var mutations = new MutationObserver(function () { var _running3; if ((_running3 = running) !== null && _running3 !== void 0 && _running3.effect.target) { // Animated dimensions can hide a content change's natural size. Temporarily release // them so ResizeObserver can report the new destination without a synchronous read. // Detaching changes no inline styles and does not cancel or pause the timeline. // The observer reattaches it before paint, or replaces it if the size changed. running.effect.target = null; reobserve(); } }); mutations.observe(element, { subtree: true, childList: true, characterData: true, attributes: true }); if (element.parentElement) { mutations.observe(element.parentElement, { attributes: true }); } resizeObserver.observe(element, { box: 'border-box' }); return { suppress: function suppress() { // Accept the next natural size without animating back from the interrupted frame. skipNext = true; stop(); reobserve(); }, disconnect: function disconnect() { disconnected = true; mutations.disconnect(); resizeObserver.disconnect(); stop(); } }; }