-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathform.js
More file actions
126 lines (115 loc) · 2.67 KB
/
form.js
File metadata and controls
126 lines (115 loc) · 2.67 KB
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useRef, useEffect } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { __ } from '@wordpress/i18n';
import { Popover } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Control from './control';
export default function Form( {
title,
isVisible,
id,
idBase,
instance,
isWide,
onChangeInstance,
onChangeHasPreview,
} ) {
const ref = useRef();
const isMediumLargeViewport = useViewportMatch( 'small' );
// We only want to remount the control when the instance changes
// *externally*. For example, if the user performs an undo. To do this, we
// keep track of changes made to instance by the control itself and then
// ignore those.
const outgoingInstances = useRef( new Set() );
const incomingInstances = useRef( new Set() );
const { createNotice } = useDispatch( noticesStore );
useEffect( () => {
if ( incomingInstances.current.has( instance ) ) {
incomingInstances.current.delete( instance );
return;
}
const control = new Control( {
id,
idBase,
instance,
onChangeInstance( nextInstance ) {
outgoingInstances.current.add( instance );
incomingInstances.current.add( nextInstance );
onChangeInstance( nextInstance );
},
onChangeHasPreview,
onError( error ) {
createNotice(
'error',
error?.message ??
__(
'An error occured while fetching or updating the widget.'
)
);
},
} );
ref.current.appendChild( control.element );
return () => {
if ( outgoingInstances.current.has( instance ) ) {
outgoingInstances.current.delete( instance );
return;
}
control.destroy();
};
}, [
id,
idBase,
instance,
onChangeInstance,
onChangeHasPreview,
isMediumLargeViewport,
] );
if ( isWide && isMediumLargeViewport ) {
return (
<div
className={ classnames( {
'wp-block-legacy-widget__container': isVisible,
} ) }
>
{ isVisible && (
<h3 className="wp-block-legacy-widget__edit-form-title">
{ title }
</h3>
) }
<Popover
focusOnMount={ false }
position="middle right"
__unstableForceXAlignment
>
<div
ref={ ref }
className="wp-block-legacy-widget__edit-form"
hidden={ ! isVisible }
></div>
</Popover>
</div>
);
}
return (
<div
ref={ ref }
className="wp-block-legacy-widget__edit-form"
hidden={ ! isVisible }
>
<h3 className="wp-block-legacy-widget__edit-form-title">
{ title }
</h3>
</div>
);
}