-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
background-panel.js
139 lines (131 loc) · 3.56 KB
/
background-panel.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* WordPress dependencies
*/
import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useCallback, Platform } from '@wordpress/element';
/**
* Internal dependencies
*/
import BackgroundImageControl from '../background-image-control';
import { useToolsPanelDropdownMenuProps } from './utils';
import { setImmutably } from '../../utils/object';
import { __ } from '@wordpress/i18n';
const DEFAULT_CONTROLS = {
backgroundImage: true,
};
/**
* Checks site settings to see if the background panel may be used.
* `settings.background.backgroundSize` exists also,
* but can only be used if settings?.background?.backgroundImage is `true`.
*
* @param {Object} settings Site settings
* @return {boolean} Whether site settings has activated background panel.
*/
export function useHasBackgroundPanel( settings ) {
return Platform.OS === 'web' && settings?.background?.backgroundImage;
}
/**
* Checks if there is a current value in the background size block support
* attributes. Background size values include background size as well
* as background position.
*
* @param {Object} style Style attribute.
* @return {boolean} Whether the block has a background size value set.
*/
export function hasBackgroundSizeValue( style ) {
return (
style?.background?.backgroundPosition !== undefined ||
style?.background?.backgroundSize !== undefined
);
}
/**
* Checks if there is a current value in the background image block support
* attributes.
*
* @param {Object} style Style attribute.
* @return {boolean} Whether the block has a background image value set.
*/
export function hasBackgroundImageValue( style ) {
return (
!! style?.background?.backgroundImage?.id ||
// Supports url() string values in theme.json.
'string' === typeof style?.background?.backgroundImage ||
!! style?.background?.backgroundImage?.url
);
}
function BackgroundToolsPanel( {
resetAllFilter,
onChange,
value,
panelId,
children,
headerLabel,
} ) {
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const resetAll = () => {
const updatedValue = resetAllFilter( value );
onChange( updatedValue );
};
return (
<ToolsPanel
label={ headerLabel }
resetAll={ resetAll }
panelId={ panelId }
dropdownMenuProps={ dropdownMenuProps }
>
{ children }
</ToolsPanel>
);
}
export default function BackgroundImagePanel( {
as: Wrapper = BackgroundToolsPanel,
value,
onChange,
inheritedValue,
settings,
panelId,
defaultControls = DEFAULT_CONTROLS,
defaultValues = {},
headerLabel = __( 'Background image' ),
} ) {
const showBackgroundImageControl = useHasBackgroundPanel( settings );
const resetBackground = () =>
onChange( setImmutably( value, [ 'background' ], {} ) );
const resetAllFilter = useCallback( ( previousValue ) => {
return {
...previousValue,
background: {},
};
}, [] );
return (
<Wrapper
resetAllFilter={ resetAllFilter }
value={ value }
onChange={ onChange }
panelId={ panelId }
headerLabel={ headerLabel }
>
{ showBackgroundImageControl && (
<ToolsPanelItem
hasValue={ () => !! value?.background }
label={ __( 'Image' ) }
onDeselect={ resetBackground }
isShownByDefault={ defaultControls.backgroundImage }
panelId={ panelId }
>
<BackgroundImageControl
value={ value }
onChange={ onChange }
settings={ settings }
inheritedValue={ inheritedValue }
defaultControls={ defaultControls }
defaultValues={ defaultValues }
/>
</ToolsPanelItem>
) }
</Wrapper>
);
}