UNPKG

@mui/material

Version:

Material UI is an open-source React component library that implements Google's Material Design. It's comprehensive and can be used in production out of the box.

95 lines (86 loc) 3.16 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.LazyRipple = void 0; exports.default = useLazyRipple; var React = _interopRequireWildcard(require("react")); var _useLazyRef = _interopRequireDefault(require("@mui/utils/useLazyRef")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** * Lazy initialization container for the Ripple instance. This improves * performance by delaying mounting the ripple until it's needed. */ class LazyRipple { /** React ref to the ripple instance */ /** If the ripple component should be mounted */ /** Promise that resolves when the ripple component is mounted */ /** If the ripple component has been mounted */ /** React state hook setter */ static create() { return new LazyRipple(); } static use() { /* eslint-disable */ const ripple = (0, _useLazyRef.default)(LazyRipple.create).current; const [shouldMount, setShouldMount] = React.useState(false); ripple.shouldMount = shouldMount; ripple.setShouldMount = setShouldMount; React.useEffect(ripple.mountEffect, [shouldMount]); /* eslint-enable */ return ripple; } constructor() { this.ref = { current: null }; this.mounted = null; this.didMount = false; this.shouldMount = false; this.setShouldMount = null; } mount() { if (!this.mounted) { this.mounted = createControlledPromise(); this.shouldMount = true; this.setShouldMount(this.shouldMount); } return this.mounted; } mountEffect = () => { if (this.shouldMount && !this.didMount) { if (this.ref.current !== null) { this.didMount = true; this.mounted.resolve(); } } }; /* Ripple API */ start(...args) { this.mount().then(() => this.ref.current?.start(...args)); } stop(...args) { this.mount().then(() => this.ref.current?.stop(...args)); } pulsate(...args) { this.mount().then(() => this.ref.current?.pulsate(...args)); } } exports.LazyRipple = LazyRipple; function useLazyRipple() { return LazyRipple.use(); } function createControlledPromise() { let resolve; let reject; const p = new Promise((resolveFn, rejectFn) => { resolve = resolveFn; reject = rejectFn; }); p.resolve = resolve; p.reject = reject; return p; }