-
Notifications
You must be signed in to change notification settings - Fork 882
/
SocialMetadataPreviewForm.js
340 lines (311 loc) · 9.95 KB
/
SocialMetadataPreviewForm.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { __ } from "@wordpress/i18n";
import { getDirectionalStyle, join } from "@yoast/helpers";
import { ReplacementVariableEditor, replacementVariablesShape } from "@yoast/replacement-variable-editor";
import { angleLeft, angleRight, colors } from "@yoast/style-guide";
import PropTypes from "prop-types";
import React, { Component, Fragment } from "react";
import styled from "styled-components";
import { ImageSelect } from "@yoast/components";
/**
* Sets the color based on whether the caret is active or not (usually hovered).
* Display css prop sets the visibility, so this only needs to switch color.
*
* @param {*} active Whether to show the active color or the hover color.
*
* @returns {string} The color of the caret. Black if active, grey otherwise.
*/
const getCaretColor = ( active ) => {
return active ? colors.$color_snippet_focus : colors.$color_snippet_hover;
};
const CaretContainer = styled.div`
position: relative;
${ props => ! props.isPremium && `
.yoast-image-select__preview {
width: 130px;
min-height: 72px;
max-height: 130px;
}
` };
`
;
CaretContainer.propTypes = {
isPremium: PropTypes.bool,
};
CaretContainer.defaultProps = {
isPremium: false,
};
const Caret = styled.div`
display: ${ props => ( props.isActive || props.isHovered ) ? "block" : "none" };
::before {
position: absolute;
top: -2px;
${ getDirectionalStyle( "left", "right" ) }: -25px;
width: 24px;
height: 24px;
background-image: url(
${ props => getDirectionalStyle(
angleRight( getCaretColor( props.isActive ) ),
angleLeft( getCaretColor( props.isActive ) ),
) }
);
color: ${ props => getCaretColor( props.isActive ) };
background-size: 24px;
background-repeat: no-repeat;
background-position: center;
content: "";
}
`;
Caret.propTypes = {
isActive: PropTypes.bool,
isHovered: PropTypes.bool,
};
Caret.defaultProps = {
isActive: false,
isHovered: false,
};
/**
* Adds Caret to a component.
* @param {React.Element} WithoutCaretComponent The component to add a Caret to.
*
* @returns {React.Element} A component with added Caret.
*/
export const withCaretStyle = ( WithoutCaretComponent ) => {
return function ComponentWithCaret( props ) {
// Define function props.
ComponentWithCaret.propTypes = {
isActive: PropTypes.bool.isRequired,
isHovered: PropTypes.bool.isRequired,
isPremium: PropTypes.bool,
};
// Destructure the props.
const {
isActive,
isHovered,
isPremium,
...withoutCaretProps
} = props;
return (
<CaretContainer isPremium={ isPremium }>
<Caret isActive={ isActive } isHovered={ isHovered } />
<WithoutCaretComponent { ...withoutCaretProps } />
</CaretContainer>
);
};
};
const ImageSelectWithCaret = withCaretStyle( ImageSelect );
/**
* A form with an image selection button, a title input field and a description field.
*
* @param {object} props The props for this component.
*
* @returns {React.Component} Returns a Fragment that contains all input fields.
*/
class SocialMetadataPreviewForm extends Component {
/**
* Constructs the component.
*
* @param {Object} props The component's props.
*
* @returns {void}
*/
constructor( props ) {
super( props );
// Binding fields to onMouseHover to prevent arrow functions in JSX props.
this.onImageEnter = props.onMouseHover.bind( this, "image" );
this.onTitleEnter = props.onMouseHover.bind( this, "title" );
this.onDescriptionEnter = props.onMouseHover.bind( this, "description" );
this.onLeave = props.onMouseHover.bind( this, "" );
this.onImageSelectBlur = props.onSelect.bind( this, "" );
this.onSelectTitleEditor = this.onSelectEditor.bind( this, "title" );
this.onSelectDescriptionEditor = this.onSelectEditor.bind( this, "description" );
this.onDeselectEditor = this.onSelectEditor.bind( this, "" );
this.onTitleEditorRef = this.onSetEditorRef.bind( this, "title" );
this.onDescriptionEditorRef = this.onSetEditorRef.bind( this, "description" );
}
/**
* Handles the onSelect function for the editors.
*
* @param {String} field The field name of the editor to focus.
*
* @returns {void}
*/
onSelectEditor( field ) {
this.props.onSelect( field );
}
/**
* Handles the onSelect function for the editors.
*
* @param {String} field The field name of the editor to focus.
* @param {String} ref The field name of the editor to focus.
*
* @returns {void}
*/
onSetEditorRef( field, ref ) {
this.props.setEditorRef( field, ref );
}
/**
* Returns the titles for the fields based on the social medium name.
*
* @param {String} socialMediumName The name of the social medium.
*
* @returns {Object} The titles for the fields.
*/
getFieldsTitles( socialMediumName ) {
if ( socialMediumName === "Twitter" ) {
return {
imageSelectTitle: __( "Twitter image", "wordpress-seo" ),
titleEditorTitle: __( "Twitter title", "wordpress-seo" ),
descEditorTitle: __( "Twitter description", "wordpress-seo" ),
};
}
if ( socialMediumName === "X" ) {
return {
imageSelectTitle: __( "X image", "wordpress-seo" ),
titleEditorTitle: __( "X title", "wordpress-seo" ),
descEditorTitle: __( "X description", "wordpress-seo" ),
};
}
return {
imageSelectTitle: __( "Social image", "wordpress-seo" ),
titleEditorTitle: __( "Social title", "wordpress-seo" ),
descEditorTitle: __( "Social description", "wordpress-seo" ),
};
}
/**
* Renders the component.
*
* @returns {React.Element} The rend
*/
render() {
const {
socialMediumName,
onSelectImageClick,
onRemoveImageClick,
title,
titleInputPlaceholder,
description,
descriptionInputPlaceholder,
onTitleChange,
onDescriptionChange,
onReplacementVariableSearchChange,
hoveredField,
activeField,
isPremium,
replacementVariables,
recommendedReplacementVariables,
imageWarnings,
imageUrl,
imageAltText,
idSuffix,
} = this.props;
const titles = this.getFieldsTitles( socialMediumName );
const imageSelected = !! imageUrl;
const imageSelectTitle = titles.imageSelectTitle;
const titleEditorTitle = titles.titleEditorTitle;
const descEditorTitle = titles.descEditorTitle;
const lowerCaseSocialMediumName = socialMediumName.toLowerCase();
return (
<Fragment>
<ImageSelectWithCaret
label={ imageSelectTitle }
onClick={ onSelectImageClick }
onRemoveImageClick={ onRemoveImageClick }
warnings={ imageWarnings }
imageSelected={ imageSelected }
onMouseEnter={ this.onImageEnter }
onMouseLeave={ this.onLeave }
isActive={ activeField === "image" }
isHovered={ hoveredField === "image" }
imageUrl={ imageUrl }
imageAltText={ imageAltText }
hasPreview={ ! isPremium }
imageUrlInputId={ join( [ lowerCaseSocialMediumName, "url-input", idSuffix ] ) }
selectImageButtonId={ join( [ lowerCaseSocialMediumName, "select-button", idSuffix ] ) }
replaceImageButtonId={ join( [ lowerCaseSocialMediumName, "replace-button", idSuffix ] ) }
removeImageButtonId={ join( [ lowerCaseSocialMediumName, "remove-button", idSuffix ] ) }
isPremium={ isPremium }
/>
<ReplacementVariableEditor
onChange={ onTitleChange }
content={ title }
placeholder={ titleInputPlaceholder }
replacementVariables={ replacementVariables }
recommendedReplacementVariables={ recommendedReplacementVariables }
type="title"
fieldId={ join( [ lowerCaseSocialMediumName, "title-input", idSuffix ] ) }
label={ titleEditorTitle }
onMouseEnter={ this.onTitleEnter }
onMouseLeave={ this.onLeave }
onSearchChange={ onReplacementVariableSearchChange }
isActive={ activeField === "title" }
isHovered={ hoveredField === "title" }
withCaret={ true }
onFocus={ this.onSelectTitleEditor }
onBlur={ this.onDeselectEditor }
editorRef={ this.onTitleEditorRef }
/>
<ReplacementVariableEditor
onChange={ onDescriptionChange }
content={ description }
placeholder={ descriptionInputPlaceholder }
replacementVariables={ replacementVariables }
recommendedReplacementVariables={ recommendedReplacementVariables }
type="description"
fieldId={ join( [ lowerCaseSocialMediumName, "description-input", idSuffix ] ) }
label={ descEditorTitle }
onMouseEnter={ this.onDescriptionEnter }
onMouseLeave={ this.onLeave }
onSearchChange={ onReplacementVariableSearchChange }
isActive={ activeField === "description" }
isHovered={ hoveredField === "description" }
withCaret={ true }
onFocus={ this.onSelectDescriptionEditor }
onBlur={ this.onDeselectEditor }
editorRef={ this.onDescriptionEditorRef }
/>
</Fragment>
);
}
}
SocialMetadataPreviewForm.propTypes = {
socialMediumName: PropTypes.oneOf( [ "Twitter", "X", "Social" ] ).isRequired,
onSelectImageClick: PropTypes.func.isRequired,
onRemoveImageClick: PropTypes.func.isRequired,
title: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
onTitleChange: PropTypes.func.isRequired,
onDescriptionChange: PropTypes.func.isRequired,
onReplacementVariableSearchChange: PropTypes.func,
isPremium: PropTypes.bool,
hoveredField: PropTypes.string,
activeField: PropTypes.string,
onSelect: PropTypes.func,
replacementVariables: replacementVariablesShape,
recommendedReplacementVariables: PropTypes.arrayOf( PropTypes.string ),
imageWarnings: PropTypes.array,
imageUrl: PropTypes.string,
imageAltText: PropTypes.string,
titleInputPlaceholder: PropTypes.string,
descriptionInputPlaceholder: PropTypes.string,
setEditorRef: PropTypes.func,
onMouseHover: PropTypes.func,
idSuffix: PropTypes.string,
};
SocialMetadataPreviewForm.defaultProps = {
replacementVariables: [],
recommendedReplacementVariables: [],
imageWarnings: [],
hoveredField: "",
activeField: "",
onSelect: () => {},
onReplacementVariableSearchChange: null,
imageUrl: "",
imageAltText: "",
titleInputPlaceholder: "",
descriptionInputPlaceholder: "",
isPremium: false,
setEditorRef: () => {},
onMouseHover: () => {},
idSuffix: "",
};
export default SocialMetadataPreviewForm;