| 1 |
import { |
| 2 |
__experimentalToolsPanelItem as ExperimentalToolsPanelItem, |
| 3 |
__experimentalUnitControl as ExperimentalUnitControl, |
| 4 |
__stableToolsPanelItem as StableToolsPanelItem, |
| 5 |
__stableUnitControl as StableUnitControl, |
| 6 |
} from '@wordpress/components'; |
| 7 |
import { addFilter } from '@wordpress/hooks'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
|
| 10 |
import ResponsiveToggle from '../../../components/responsive-toggle'; |
| 11 |
import useResponsive from '../../../hooks/use-responsive'; |
| 12 |
import useStyles from '../../../hooks/use-styles'; |
| 13 |
|
| 14 |
const ToolsPanelItem = StableToolsPanelItem || ExperimentalToolsPanelItem; |
| 15 |
const UnitControl = StableUnitControl || ExperimentalUnitControl; |
| 16 |
|
| 17 |
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks'; |
| 18 |
|
| 19 |
function PositionWidthTools(props) { |
| 20 |
const { getStyle, hasStyle, setStyles, resetStyles } = useStyles(props); |
| 21 |
|
| 22 |
const { device, allDevices } = useResponsive(); |
| 23 |
|
| 24 |
let hasWidth = false; |
| 25 |
|
| 26 |
['', ...Object.keys(allDevices)].forEach((thisDevice) => { |
| 27 |
hasWidth = hasWidth || hasStyle('width', thisDevice); |
| 28 |
}); |
| 29 |
|
| 30 |
return ( |
| 31 |
<ToolsPanelItem |
| 32 |
label={__('Width', 'ghostkit')} |
| 33 |
hasValue={() => !!hasWidth} |
| 34 |
onDeselect={() => { |
| 35 |
resetStyles(['width'], true); |
| 36 |
}} |
| 37 |
isShownByDefault={false} |
| 38 |
> |
| 39 |
<UnitControl |
| 40 |
label={ |
| 41 |
<> |
| 42 |
{__('Width', 'ghostkit')} |
| 43 |
<ResponsiveToggle |
| 44 |
checkActive={(checkMedia) => { |
| 45 |
return hasStyle('width', checkMedia); |
| 46 |
}} |
| 47 |
/> |
| 48 |
</> |
| 49 |
} |
| 50 |
value={getStyle('width', device)} |
| 51 |
onChange={(val) => { |
| 52 |
setStyles({ width: val }, device); |
| 53 |
}} |
| 54 |
labelPosition="edge" |
| 55 |
__unstableInputWidth="70px" |
| 56 |
units={[ |
| 57 |
{ value: 'px', label: 'px' }, |
| 58 |
{ value: '%', label: '%' }, |
| 59 |
{ value: 'em', label: 'em' }, |
| 60 |
{ value: 'rem', label: 'rem' }, |
| 61 |
{ value: 'vw', label: 'vw' }, |
| 62 |
{ value: 'vh', label: 'vh' }, |
| 63 |
]} |
| 64 |
min={0} |
| 65 |
/> |
| 66 |
</ToolsPanelItem> |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
addFilter( |
| 71 |
'ghostkit.extension.position.tools', |
| 72 |
'ghostkit/extension/position/tools/width', |
| 73 |
(children, { props }) => { |
| 74 |
const hasWidthSupport = |
| 75 |
hasBlockSupport(props.name, ['ghostkit', 'position', 'width']) || |
| 76 |
getBlockSupport(props.name, ['ghostkit', 'position']) === true; |
| 77 |
|
| 78 |
if (!hasWidthSupport) { |
| 79 |
return children; |
| 80 |
} |
| 81 |
|
| 82 |
return ( |
| 83 |
<> |
| 84 |
{children} |
| 85 |
<PositionWidthTools {...props} /> |
| 86 |
</> |
| 87 |
); |
| 88 |
} |
| 89 |
); |
| 90 |
|