@@ -6,7 +6,13 @@ import clsx from 'clsx';
66/**
77 * WordPress dependencies
88 */
9- import { useCallback , useEffect , useMemo , useState } from '@wordpress/element' ;
9+ import {
10+ useCallback ,
11+ useEffect ,
12+ useMemo ,
13+ useRef ,
14+ useState ,
15+ } from '@wordpress/element' ;
1016import {
1117 store as blockEditorStore ,
1218 MediaPlaceholder ,
@@ -121,8 +127,8 @@ const PlaylistEdit = ( {
121127 gradients . length > 0 || ! colorGradientSettings . disableCustomGradients ;
122128 const waveformGradientValue = waveformGradient ;
123129 const waveformBackgroundGradientValue = waveformBackgroundGradient ;
124- let waveformColorGradientChange ;
125- let waveformBackgroundColorGradientChange ;
130+ const colorGradientChangeRef = useRef ( ) ;
131+ const backgroundColorGradientChangeRef = useRef ( ) ;
126132 const onUploadError = useCallback (
127133 ( message ) => {
128134 createErrorNotice ( message , { type : 'snackbar' } ) ;
@@ -343,71 +349,89 @@ const PlaylistEdit = ( {
343349 [ setAttributes ]
344350 ) ;
345351
346- function updateWaveformColor ( colorValue ) {
347- const isSettingColor = colorValue !== undefined ;
348- if ( ! isSettingColor && waveformColorGradientChange === 'gradient' ) {
349- waveformColorGradientChange = undefined ;
350- return ;
351- }
352+ const updateWaveformColor = useCallback (
353+ ( colorValue ) => {
354+ const isSettingColor = colorValue !== undefined ;
355+ if (
356+ ! isSettingColor &&
357+ colorGradientChangeRef . current === 'gradient'
358+ ) {
359+ colorGradientChangeRef . current = undefined ;
360+ return ;
361+ }
352362
353- waveformColorGradientChange = 'color' ;
363+ colorGradientChangeRef . current = 'color' ;
354364
355- setAttributes ( {
356- waveformColor : colorValue ,
357- waveformGradient : undefined ,
358- } ) ;
359- }
365+ setAttributes ( {
366+ waveformColor : colorValue ,
367+ waveformGradient : undefined ,
368+ } ) ;
369+ } ,
370+ [ setAttributes ]
371+ ) ;
360372
361- function updateWaveformGradient ( gradientValue ) {
362- const isSettingGradient = gradientValue !== undefined ;
363- if ( ! isSettingGradient && waveformColorGradientChange === 'color' ) {
364- waveformColorGradientChange = undefined ;
365- return ;
366- }
373+ const updateWaveformGradient = useCallback (
374+ ( gradientValue ) => {
375+ const isSettingGradient = gradientValue !== undefined ;
376+ if (
377+ ! isSettingGradient &&
378+ colorGradientChangeRef . current === 'color'
379+ ) {
380+ colorGradientChangeRef . current = undefined ;
381+ return ;
382+ }
367383
368- waveformColorGradientChange = 'gradient' ;
384+ colorGradientChangeRef . current = 'gradient' ;
369385
370- setAttributes ( {
371- waveformGradient : gradientValue ,
372- waveformColor : undefined ,
373- } ) ;
374- }
386+ setAttributes ( {
387+ waveformGradient : gradientValue ,
388+ waveformColor : undefined ,
389+ } ) ;
390+ } ,
391+ [ setAttributes ]
392+ ) ;
375393
376- function updateWaveformBackgroundColor ( colorValue ) {
377- const isSettingColor = colorValue !== undefined ;
378- if (
379- ! isSettingColor &&
380- waveformBackgroundColorGradientChange === 'gradient'
381- ) {
382- waveformBackgroundColorGradientChange = undefined ;
383- return ;
384- }
394+ const updateWaveformBackgroundColor = useCallback (
395+ ( colorValue ) => {
396+ const isSettingColor = colorValue !== undefined ;
397+ if (
398+ ! isSettingColor &&
399+ backgroundColorGradientChangeRef . current === 'gradient'
400+ ) {
401+ backgroundColorGradientChangeRef . current = undefined ;
402+ return ;
403+ }
385404
386- waveformBackgroundColorGradientChange = 'color' ;
405+ backgroundColorGradientChangeRef . current = 'color' ;
387406
388- setAttributes ( {
389- waveformBackgroundColor : colorValue ,
390- waveformBackgroundGradient : undefined ,
391- } ) ;
392- }
407+ setAttributes ( {
408+ waveformBackgroundColor : colorValue ,
409+ waveformBackgroundGradient : undefined ,
410+ } ) ;
411+ } ,
412+ [ setAttributes ]
413+ ) ;
393414
394- function updateWaveformBackgroundGradient ( gradientValue ) {
395- const isSettingGradient = gradientValue !== undefined ;
396- if (
397- ! isSettingGradient &&
398- waveformBackgroundColorGradientChange === 'color'
399- ) {
400- waveformBackgroundColorGradientChange = undefined ;
401- return ;
402- }
415+ const updateWaveformBackgroundGradient = useCallback (
416+ ( gradientValue ) => {
417+ const isSettingGradient = gradientValue !== undefined ;
418+ if (
419+ ! isSettingGradient &&
420+ backgroundColorGradientChangeRef . current === 'color'
421+ ) {
422+ backgroundColorGradientChangeRef . current = undefined ;
423+ return ;
424+ }
403425
404- waveformBackgroundColorGradientChange = 'gradient' ;
426+ backgroundColorGradientChangeRef . current = 'gradient' ;
405427
406- setAttributes ( {
407- waveformBackgroundGradient : gradientValue ,
408- waveformBackgroundColor : undefined ,
409- } ) ;
410- }
428+ setAttributes ( {
429+ waveformBackgroundGradient : gradientValue ,
430+ waveformBackgroundColor : undefined ,
431+ } ) ;
432+ } ,
433+ [ setAttributes ]
434+ ) ;
411435
412436 const colorSettings = [ ] ;
413437 if ( hasColors || hasGradients ) {
0 commit comments