UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

119 lines (118 loc) • 4.67 kB
/** * DevExtreme (esm/__internal/core/utils/ink_ripple.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import $ from "../../../core/renderer"; import { getOuterHeight, getOuterWidth } from "../../../core/utils/size"; const INKRIPPLE_CLASS = "dx-inkripple"; const INKRIPPLE_WAVE_CLASS = "dx-inkripple-wave"; const INKRIPPLE_SHOWING_CLASS = "dx-inkripple-showing"; const INKRIPPLE_HIDING_CLASS = "dx-inkripple-hiding"; const DEFAULT_WAVE_SIZE_COEFFICIENT = 2; const MAX_WAVE_SIZE = 4e3; const ANIMATION_DURATION = 300; const HOLD_ANIMATION_DURATION = 1e3; const DEFAULT_WAVE_INDEX = 0; const getDurations = useHoldAnimation => ({ showingScale: useHoldAnimation ? 1e3 : 300, hidingScale: 300, hidingOpacity: 300 }); export const initConfig = function() { let config = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}; const { useHoldAnimation: useHoldAnimation, waveSizeCoefficient: waveSizeCoefficient, isCentered: isCentered, wavesNumber: wavesNumber } = config; return { waveSizeCoefficient: waveSizeCoefficient ?? 2, isCentered: isCentered ?? false, wavesNumber: wavesNumber ?? 1, durations: getDurations(useHoldAnimation ?? true) } }; const getInkRipple = element => { let result = element.children(".dx-inkripple"); if (0 === result.length) { result = $("<div>").addClass("dx-inkripple").appendTo(element) } return result }; const getWaves = (element, wavesNumber) => { const inkRipple = getInkRipple($(element)); const result = inkRipple.children(".dx-inkripple-wave").toArray(); for (let i = result.length; i < (wavesNumber ?? 0); i += 1) { const $currentWave = $("<div>").appendTo(inkRipple).addClass("dx-inkripple-wave"); result.push($currentWave[0]) } return $(result) }; const getWaveStyleConfig = (args, config) => { const element = $(config.element); const elementWidth = getOuterWidth(element); const elementHeight = getOuterHeight(element); const elementDiagonal = Math.trunc(Math.sqrt(elementWidth * elementWidth + elementHeight * elementHeight)); const waveSize = Math.min(4e3, Math.trunc(elementDiagonal * args.waveSizeCoefficient)); let left = 0; let top = 0; if (args.isCentered) { left = (elementWidth - waveSize) / 2; top = (elementHeight - waveSize) / 2 } else { const { event: event } = config; const position = element.offset(); const x = ((null === event || void 0 === event ? void 0 : event.pageX) ?? 0) - ((null === position || void 0 === position ? void 0 : position.left) ?? 0); const y = ((null === event || void 0 === event ? void 0 : event.pageY) ?? 0) - ((null === position || void 0 === position ? void 0 : position.top) ?? 0); left = x - waveSize / 2; top = y - waveSize / 2 } return { left: left, top: top, height: waveSize, width: waveSize } }; const hideSelectedWave = $wave => { $wave.removeClass("dx-inkripple-hiding").css("transitionDuration", "") }; const showingWaveHandler = (args, $wave) => { const durationCss = `${args.durations.showingScale}ms`; $wave.addClass("dx-inkripple-showing").css("transitionDuration", durationCss) }; export const showWave = (args, config) => { const $wave = getWaves(config.element, args.wavesNumber).eq(config.wave ?? 0); clearTimeout(args.hidingTimeout); hideSelectedWave($wave); $wave.css(getWaveStyleConfig(args, config)); args.showingTimeout = setTimeout(() => showingWaveHandler(args, $wave), 0) }; export const hideWave = (args, config) => { clearTimeout(args.showingTimeout); const $wave = getWaves(config.element, config.wavesNumber).eq(config.wave ?? 0); const { durations: durations } = args; const durationCss = `${durations.hidingScale}ms, ${durations.hidingOpacity}ms`; $wave.addClass("dx-inkripple-hiding").removeClass("dx-inkripple-showing").css("transitionDuration", durationCss); const animationDuration = Math.max(durations.hidingScale, durations.hidingOpacity); args.hidingTimeout = setTimeout(() => hideSelectedWave($wave), animationDuration) }; export const render = config => { const inkRippleConfig = initConfig(config); return { showWave: waveConfig => showWave(inkRippleConfig, waveConfig), hideWave: waveConfig => hideWave(inkRippleConfig, waveConfig) } };