/
border.js
123 lines (105 loc) · 3.24 KB
/
border.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* WordPress dependencies
*/
import { getBlockSupport } from '@wordpress/blocks';
import { PanelBody } from '@wordpress/components';
import { Platform } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import InspectorControls from '../components/inspector-controls';
import useSetting from '../components/use-setting';
import { BorderColorEdit } from './border-color';
import { BorderRadiusEdit } from './border-radius';
import { BorderStyleEdit } from './border-style';
import { BorderWidthEdit } from './border-width';
export const BORDER_SUPPORT_KEY = '__experimentalBorder';
export function BorderPanel( props ) {
const isDisabled = useIsBorderDisabled( props );
const isSupported = hasBorderSupport( props.name );
const isColorSupported =
useSetting( 'border.customColor' ) &&
hasBorderSupport( props.name, 'color' );
const isRadiusSupported =
useSetting( 'border.customRadius' ) &&
hasBorderSupport( props.name, 'radius' );
const isStyleSupported =
useSetting( 'border.customStyle' ) &&
hasBorderSupport( props.name, 'style' );
const isWidthSupported =
useSetting( 'border.customWidth' ) &&
hasBorderSupport( props.name, 'width' );
if ( isDisabled || ! isSupported ) {
return null;
}
return (
<InspectorControls>
<PanelBody
className="block-editor-hooks__border-controls"
title={ __( 'Border' ) }
initialOpen={ false }
>
{ ( isWidthSupported || isStyleSupported ) && (
<div className="block-editor-hooks__border-controls-row">
{ isWidthSupported && <BorderWidthEdit { ...props } /> }
{ isStyleSupported && <BorderStyleEdit { ...props } /> }
</div>
) }
{ isColorSupported && <BorderColorEdit { ...props } /> }
{ isRadiusSupported && <BorderRadiusEdit { ...props } /> }
</PanelBody>
</InspectorControls>
);
}
/**
* Determine whether there is block support for border properties.
*
* @param {string} blockName Block name.
* @param {string} feature Border feature to check support for.
*
* @return {boolean} Whether there is support.
*/
export function hasBorderSupport( blockName, feature = 'any' ) {
if ( Platform.OS !== 'web' ) {
return false;
}
const support = getBlockSupport( blockName, BORDER_SUPPORT_KEY );
if ( support === true ) {
return true;
}
if ( feature === 'any' ) {
return !! (
support?.color ||
support?.radius ||
support?.width ||
support?.style
);
}
return !! support?.[ feature ];
}
/**
* Check whether serialization of border classes and styles should be skipped.
*
* @param {string|Object} blockType Block name or block type object.
*
* @return {boolean} Whether serialization of border properties should occur.
*/
export function shouldSkipSerialization( blockType ) {
const support = getBlockSupport( blockType, BORDER_SUPPORT_KEY );
return support?.__experimentalSkipSerialization;
}
/**
* Determines if all border support features have been disabled.
*
* @return {boolean} If border support is completely disabled.
*/
const useIsBorderDisabled = () => {
const configs = [
! useSetting( 'border.customColor' ),
! useSetting( 'border.customRadius' ),
! useSetting( 'border.customStyle' ),
! useSetting( 'border.customWidth' ),
];
return configs.every( Boolean );
};