use-wave
Version:
The material-ripple hook for React that actually works
189 lines (174 loc) • 7.18 kB
JavaScript
;
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(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 DEFAULT_OPTIONS = {
color: 'currentColor',
initialOpacity: 0.2,
finalOpacity: 0.1,
duration: 0.4,
easing: 'ease-out',
cancellationPeriod: 75
};
var createContainer = function (_a) {
var borderTopLeftRadius = _a.borderTopLeftRadius, borderTopRightRadius = _a.borderTopRightRadius, borderBottomLeftRadius = _a.borderBottomLeftRadius, borderBottomRightRadius = _a.borderBottomRightRadius;
var waveContainer = document.createElement('div');
waveContainer.style.top = '0';
waveContainer.style.left = '0';
waveContainer.style.width = '100%';
waveContainer.style.height = '100%';
waveContainer.style.position = 'absolute';
waveContainer.style.borderRadius = borderTopLeftRadius + " " + borderTopRightRadius + " " + borderBottomRightRadius + " " + borderBottomLeftRadius;
waveContainer.style.overflow = 'hidden';
waveContainer.style.pointerEvents = 'none';
waveContainer.style.webkitMaskImage = '-webkit-radial-gradient(white, black)';
return waveContainer;
};
var createWaveElement = function (x, y, size, options) {
var waveElement = document.createElement('div');
waveElement.style.position = 'absolute';
waveElement.style.width = size + "px";
waveElement.style.height = size + "px";
waveElement.style.top = y + "px";
waveElement.style.left = x + "px";
waveElement.style.background = options.color;
waveElement.style.borderRadius = '50%';
waveElement.style.opacity = "" + options.initialOpacity;
waveElement.style.transform = "translate(-50%,-50%) scale(0)";
waveElement.style.transition = "transform " + options.duration + "s " + options.easing + ", opacity " + options.duration + "s " + options.easing;
return waveElement;
};
function magnitude(x1, y1, x2, y2) {
var deltaX = x1 - x2;
var deltaY = y1 - y2;
return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
}
function getDistanceToFurthestCorner(x, y, _a) {
var width = _a.width, height = _a.height;
var topLeft = magnitude(x, y, 0, 0);
var topRight = magnitude(x, y, width, 0);
var bottomLeft = magnitude(x, y, 0, height);
var bottomRight = magnitude(x, y, width, height);
return Math.max(topLeft, topRight, bottomLeft, bottomRight);
}
var getRelativePointer = function (_a, _b) {
var x = _a.x, y = _a.y;
var top = _b.top, left = _b.left;
return ({
x: x - left,
y: y - top
});
};
var WAVE_COUNT = 'useWaveCountInternal';
function incrementWaveCount(el) {
var count = getWaveCount(el);
setWaveCount(el, count + 1);
}
function decrementWaveCount(el) {
var count = getWaveCount(el);
setWaveCount(el, count - 1);
}
function setWaveCount(el, count) {
el.dataset[WAVE_COUNT] = count.toString();
}
function getWaveCount(el) {
var _a;
return parseInt((_a = el.dataset[WAVE_COUNT]) !== null && _a !== void 0 ? _a : '0', 10);
}
function deleteWaveCount(el) {
delete el.dataset[WAVE_COUNT];
}
var wave = function (event, el, options) {
var rect = el.getBoundingClientRect();
var computedStyles = window.getComputedStyle(el);
var _a = getRelativePointer(event, rect), x = _a.x, y = _a.y;
var size = 2.05 * getDistanceToFurthestCorner(x, y, rect);
var waveContainer = createContainer(computedStyles);
var waveEl = createWaveElement(x, y, size, options);
incrementWaveCount(el);
var originalPositionValue = '';
if (computedStyles.position === 'static') {
if (el.style.position)
originalPositionValue = el.style.position;
el.style.position = 'relative';
}
waveContainer.appendChild(waveEl);
el.appendChild(waveContainer);
var shouldDissolveWave = false;
var releaseWave = function (e) {
if (typeof e !== 'undefined') {
document.removeEventListener('pointerup', releaseWave);
document.removeEventListener('pointercancel', releaseWave);
}
if (shouldDissolveWave)
dissolveWave();
else
shouldDissolveWave = true;
};
var dissolveWave = function () {
waveEl.style.transition = 'opacity 150ms linear';
waveEl.style.opacity = '0';
setTimeout(function () {
waveContainer.remove();
decrementWaveCount(el);
if (getWaveCount(el) === 0) {
deleteWaveCount(el);
el.style.position = originalPositionValue;
}
}, 150);
};
document.addEventListener('pointerup', releaseWave);
document.addEventListener('pointercancel', releaseWave);
var token = setTimeout(function () {
document.removeEventListener('pointercancel', cancelWave);
requestAnimationFrame(function () {
waveEl.style.transform = "translate(-50%,-50%) scale(1)";
waveEl.style.opacity = "" + options.finalOpacity;
setTimeout(function () { return releaseWave(); }, options.duration * 1000);
});
}, options.cancellationPeriod);
var cancelWave = function () {
clearTimeout(token);
waveContainer.remove();
document.removeEventListener('pointerup', releaseWave);
document.removeEventListener('pointercancel', releaseWave);
document.removeEventListener('pointercancel', cancelWave);
};
document.addEventListener('pointercancel', cancelWave);
};
var elementMap = new WeakMap();
function register(el) {
el.addEventListener('pointerdown', function (event) {
wave(event, el, __assign(__assign({}, DEFAULT_OPTIONS), elementMap.get(el)));
});
}
function useWave(options) {
if (options === void 0) { options = {}; }
return function (el) {
if (!el)
return;
if (!elementMap.has(el))
register(el);
elementMap.set(el, options);
};
}
module.exports = useWave;