-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcontrols.tsx
102 lines (94 loc) · 2.24 KB
/
controls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import {
StyledUnitControl,
ControlWrapper,
} from './styles/focal-point-picker-style';
import { fractionToPercentage } from './utils';
import type {
UnitControlProps,
UnitControlOnChangeCallback,
} from '../unit-control/types';
import type { FocalPointAxis, FocalPointPickerControlsProps } from './types';
const TEXTCONTROL_MIN = 0;
const TEXTCONTROL_MAX = 100;
const noop = () => {};
export default function FocalPointPickerControls( {
__nextHasNoMarginBottom,
__next40pxDefaultSize,
hasHelpText,
onChange = noop,
point = {
x: 0.5,
y: 0.5,
},
}: FocalPointPickerControlsProps ) {
const valueX = fractionToPercentage( point.x );
const valueY = fractionToPercentage( point.y );
const handleChange = (
value: Parameters< UnitControlOnChangeCallback >[ 0 ],
axis: FocalPointAxis
) => {
if ( value === undefined ) {
return;
}
const num = parseInt( value, 10 );
if ( ! isNaN( num ) ) {
onChange( { ...point, [ axis ]: num / 100 } );
}
};
return (
<ControlWrapper
className="focal-point-picker__controls"
__nextHasNoMarginBottom={ __nextHasNoMarginBottom }
hasHelpText={ hasHelpText }
gap={ 4 }
>
<FocalPointUnitControl
__next40pxDefaultSize={ __next40pxDefaultSize }
label={ __( 'Left' ) }
aria-label={ __( 'Focal point left position' ) }
value={ [ valueX, '%' ].join( '' ) }
onChange={
( ( next ) =>
handleChange(
next,
'x'
) ) as UnitControlOnChangeCallback
}
dragDirection="e"
/>
<FocalPointUnitControl
__next40pxDefaultSize={ __next40pxDefaultSize }
label={ __( 'Top' ) }
aria-label={ __( 'Focal point top position' ) }
value={ [ valueY, '%' ].join( '' ) }
onChange={
( ( next ) =>
handleChange(
next,
'y'
) ) as UnitControlOnChangeCallback
}
dragDirection="s"
/>
</ControlWrapper>
);
}
function FocalPointUnitControl( props: UnitControlProps ) {
return (
<StyledUnitControl
className="focal-point-picker__controls-position-unit-control"
labelPosition="top"
max={ TEXTCONTROL_MAX }
min={ TEXTCONTROL_MIN }
units={ [ { value: '%', label: '%' } ] }
{ ...props }
/>
);
}