UNPKG

2.48 kBJavaScriptView Raw
1"use strict";
2'use client';
3
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6Object.defineProperty(exports, "__esModule", {
7 value: true
8});
9exports.LazyRipple = void 0;
10exports.default = useLazyRipple;
11var React = _interopRequireWildcard(require("react"));
12var _useLazyRef = _interopRequireDefault(require("@mui/utils/useLazyRef"));
13/**
14 * Lazy initialization container for the Ripple instance. This improves
15 * performance by delaying mounting the ripple until it's needed.
16 */
17class LazyRipple {
18 /** React ref to the ripple instance */
19
20 /** If the ripple component should be mounted */
21
22 /** Promise that resolves when the ripple component is mounted */
23
24 /** If the ripple component has been mounted */
25
26 /** React state hook setter */
27
28 static create() {
29 return new LazyRipple();
30 }
31 static use() {
32 /* eslint-disable */
33 const ripple = (0, _useLazyRef.default)(LazyRipple.create).current;
34 const [shouldMount, setShouldMount] = React.useState(false);
35 ripple.shouldMount = shouldMount;
36 ripple.setShouldMount = setShouldMount;
37 React.useEffect(ripple.mountEffect, [shouldMount]);
38 /* eslint-enable */
39
40 return ripple;
41 }
42 constructor() {
43 this.ref = {
44 current: null
45 };
46 this.mounted = null;
47 this.didMount = false;
48 this.shouldMount = false;
49 this.setShouldMount = null;
50 }
51 mount() {
52 if (!this.mounted) {
53 this.mounted = createControlledPromise();
54 this.shouldMount = true;
55 this.setShouldMount(this.shouldMount);
56 }
57 return this.mounted;
58 }
59 mountEffect = () => {
60 if (this.shouldMount && !this.didMount) {
61 if (this.ref.current !== null) {
62 this.didMount = true;
63 this.mounted.resolve();
64 }
65 }
66 };
67
68 /* Ripple API */
69
70 start(...args) {
71 this.mount().then(() => this.ref.current?.start(...args));
72 }
73 stop(...args) {
74 this.mount().then(() => this.ref.current?.stop(...args));
75 }
76 pulsate(...args) {
77 this.mount().then(() => this.ref.current?.pulsate(...args));
78 }
79}
80exports.LazyRipple = LazyRipple;
81function useLazyRipple() {
82 return LazyRipple.use();
83}
84function createControlledPromise() {
85 let resolve;
86 let reject;
87 const p = new Promise((resolveFn, rejectFn) => {
88 resolve = resolveFn;
89 reject = rejectFn;
90 });
91 p.resolve = resolve;
92 p.reject = reject;
93 return p;
94}
\No newline at end of file