| 1 |
import { useEffect, useState } from '@wordpress/element'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
import Select from '../select'; |
| 5 |
import PRESETS from './presets'; |
| 6 |
|
| 7 |
export default function TransitionPresetsControl(props) { |
| 8 |
const { label, value, onChange } = props; |
| 9 |
|
| 10 |
const [preset, setPreset] = useState(); |
| 11 |
|
| 12 |
// Find default preset |
| 13 |
useEffect(() => { |
| 14 |
let newPreset = 'custom'; |
| 15 |
|
| 16 |
const currentReveal = { ...(value || {}) }; |
| 17 |
|
| 18 |
// Remove transition from the comparison. |
| 19 |
if (currentReveal?.transition) { |
| 20 |
delete currentReveal.transition; |
| 21 |
} |
| 22 |
|
| 23 |
Object.keys(PRESETS).forEach((slug) => { |
| 24 |
const presetData = { ...PRESETS[slug].data }; |
| 25 |
|
| 26 |
// Remove transition from the comparison. |
| 27 |
if (presetData?.transition) { |
| 28 |
delete presetData.transition; |
| 29 |
} |
| 30 |
|
| 31 |
if (JSON.stringify(currentReveal) === JSON.stringify(presetData)) { |
| 32 |
newPreset = slug; |
| 33 |
} |
| 34 |
}); |
| 35 |
|
| 36 |
setPreset(newPreset); |
| 37 |
}, [preset, value]); |
| 38 |
|
| 39 |
const presetOptions = [ |
| 40 |
...(preset === 'custom' |
| 41 |
? [ |
| 42 |
{ |
| 43 |
value: 'custom', |
| 44 |
label: __('-- Presets --', 'ghostkit'), |
| 45 |
}, |
| 46 |
] |
| 47 |
: []), |
| 48 |
...Object.keys(PRESETS).map((name) => { |
| 49 |
return { |
| 50 |
value: name, |
| 51 |
label: PRESETS[name].label, |
| 52 |
icon: PRESETS[name].icon, |
| 53 |
}; |
| 54 |
}), |
| 55 |
]; |
| 56 |
|
| 57 |
const presetValue = { |
| 58 |
value: preset, |
| 59 |
label: preset, |
| 60 |
}; |
| 61 |
|
| 62 |
// Find actual label. |
| 63 |
if (presetValue.value) { |
| 64 |
presetOptions.forEach((presetData) => { |
| 65 |
if (presetValue.value === presetData.value) { |
| 66 |
presetValue.label = presetData.label; |
| 67 |
} |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
return ( |
| 72 |
<Select |
| 73 |
label={label} |
| 74 |
value={presetValue} |
| 75 |
onChange={(val) => { |
| 76 |
if (PRESETS?.[val.value]?.data) { |
| 77 |
onChange(PRESETS[val.value].data); |
| 78 |
} |
| 79 |
}} |
| 80 |
options={presetOptions} |
| 81 |
isSearchable={false} |
| 82 |
/> |
| 83 |
); |
| 84 |
} |
| 85 |
|