| 1 |
import { BaseControl } from '@wordpress/components'; |
| 2 |
import { useState } from '@wordpress/element'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
|
| 5 |
import DropdownPicker from '../dropdown-picker'; |
| 6 |
import ToggleGroup from '../toggle-group'; |
| 7 |
import { |
| 8 |
EasingBezierEditor, |
| 9 |
EasingControls, |
| 10 |
} from '../transition-easing-controls'; |
| 11 |
import EASING_DEFAULT from '../transition-easing-controls/default'; |
| 12 |
import { SpringControls, SpringEditor } from '../transition-spring-controls'; |
| 13 |
import SPRING_DEFAULT from '../transition-spring-controls/default'; |
| 14 |
|
| 15 |
export default function TransitionSelector(props) { |
| 16 |
const { |
| 17 |
label, |
| 18 |
value, |
| 19 |
onChange, |
| 20 |
enableEasing = true, |
| 21 |
enableSpring = true, |
| 22 |
enableDelayControl = true, |
| 23 |
allowReset = false, |
| 24 |
} = props; |
| 25 |
|
| 26 |
const [isOpenTransition, setIsOpenTransition] = useState(false); |
| 27 |
|
| 28 |
let easingValue = value?.easing; |
| 29 |
if (!easingValue || easingValue.length !== 4) { |
| 30 |
easingValue = EASING_DEFAULT.easing; |
| 31 |
} |
| 32 |
|
| 33 |
let buttonLabel = ''; |
| 34 |
const resetButton = allowReset && value && ( |
| 35 |
<span |
| 36 |
className="ghostkit-component-transition-selector-reset" |
| 37 |
onClick={(e) => { |
| 38 |
// Reset. |
| 39 |
e.preventDefault(); |
| 40 |
e.stopPropagation(); |
| 41 |
|
| 42 |
setIsOpenTransition(false); |
| 43 |
|
| 44 |
onChange(undefined); |
| 45 |
}} |
| 46 |
onKeyDown={() => {}} |
| 47 |
role="button" |
| 48 |
tabIndex={0} |
| 49 |
> |
| 50 |
<svg |
| 51 |
xmlns="http://www.w3.org/2000/svg" |
| 52 |
viewBox="0 0 24 24" |
| 53 |
width="24" |
| 54 |
height="24" |
| 55 |
aria-hidden="true" |
| 56 |
focusable="false" |
| 57 |
> |
| 58 |
<path d="M12 13.06l3.712 3.713 1.061-1.06L13.061 12l3.712-3.712-1.06-1.06L12 10.938 8.288 7.227l-1.061 1.06L10.939 12l-3.712 3.712 1.06 1.061L12 13.061z" /> |
| 59 |
</svg> |
| 60 |
</span> |
| 61 |
); |
| 62 |
|
| 63 |
if (value?.type === 'easing') { |
| 64 |
buttonLabel = ( |
| 65 |
<> |
| 66 |
<EasingBezierEditor variant="preview" value={easingValue} /> |
| 67 |
{__('Easing', 'ghostkit')} |
| 68 |
{resetButton} |
| 69 |
</> |
| 70 |
); |
| 71 |
} else if (value?.type === 'spring') { |
| 72 |
buttonLabel = ( |
| 73 |
<> |
| 74 |
<SpringEditor variant="preview" value={value} /> |
| 75 |
{__('Spring', 'ghostkit')} |
| 76 |
{resetButton} |
| 77 |
</> |
| 78 |
); |
| 79 |
} else { |
| 80 |
buttonLabel = ( |
| 81 |
<> |
| 82 |
<SpringEditor |
| 83 |
variant="preview" |
| 84 |
value={{ |
| 85 |
type: 'spring', |
| 86 |
stiffness: 930, |
| 87 |
damping: 40, |
| 88 |
mass: 6, |
| 89 |
delay: 0, |
| 90 |
}} |
| 91 |
backgroundColor="#c3c3c3" |
| 92 |
/> |
| 93 |
{__('Add…', 'ghostkit')} |
| 94 |
</> |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
if (!enableEasing && !enableSpring) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
return ( |
| 103 |
<BaseControl id={label} label={label}> |
| 104 |
<DropdownPicker |
| 105 |
label={buttonLabel} |
| 106 |
className="ghostkit-component-transition-selector" |
| 107 |
contentClassName="ghostkit-component-transition-selector-content" |
| 108 |
isOpenTransition={isOpenTransition} |
| 109 |
onToggle={(isOpen) => { |
| 110 |
setIsOpenTransition(isOpen); |
| 111 |
}} |
| 112 |
onClick={(onToggle) => { |
| 113 |
// Toggle dropdown. |
| 114 |
if (value?.type === 'spring' || value?.type === 'easing') { |
| 115 |
onToggle(); |
| 116 |
|
| 117 |
// Add spring transition. |
| 118 |
} else { |
| 119 |
const addTransition = { |
| 120 |
type: 'spring', |
| 121 |
...SPRING_DEFAULT, |
| 122 |
}; |
| 123 |
|
| 124 |
delete addTransition.label; |
| 125 |
|
| 126 |
onChange(addTransition); |
| 127 |
} |
| 128 |
}} |
| 129 |
> |
| 130 |
{enableEasing && enableSpring && ( |
| 131 |
<ToggleGroup |
| 132 |
value={value?.type || 'spring'} |
| 133 |
options={[ |
| 134 |
{ |
| 135 |
label: __('Easing', 'ghostkit'), |
| 136 |
value: 'easing', |
| 137 |
}, |
| 138 |
{ |
| 139 |
label: __('Spring', 'ghostkit'), |
| 140 |
value: 'spring', |
| 141 |
}, |
| 142 |
]} |
| 143 |
onChange={(val) => { |
| 144 |
const defaultTransition = |
| 145 |
val === 'easing' |
| 146 |
? { type: 'easing', ...EASING_DEFAULT } |
| 147 |
: { type: 'spring', ...SPRING_DEFAULT }; |
| 148 |
|
| 149 |
delete defaultTransition.label; |
| 150 |
|
| 151 |
onChange(defaultTransition); |
| 152 |
}} |
| 153 |
isBlock |
| 154 |
/> |
| 155 |
)} |
| 156 |
{enableSpring && value?.type !== 'easing' && ( |
| 157 |
<SpringControls |
| 158 |
value={value} |
| 159 |
onChange={(val) => { |
| 160 |
onChange(val); |
| 161 |
}} |
| 162 |
enableDelayControl={enableDelayControl} |
| 163 |
/> |
| 164 |
)} |
| 165 |
{enableEasing && value?.type === 'easing' && ( |
| 166 |
<EasingControls |
| 167 |
value={value} |
| 168 |
onChange={(val) => { |
| 169 |
onChange(val); |
| 170 |
}} |
| 171 |
enableDelayControl={enableDelayControl} |
| 172 |
/> |
| 173 |
)} |
| 174 |
</DropdownPicker> |
| 175 |
</BaseControl> |
| 176 |
); |
| 177 |
} |
| 178 |
|