This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathApp.js
299 lines (284 loc) · 6.9 KB
/
App.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
import React from "react";
import styled, { ThemeProvider } from "styled-components";
import { setLocaleData } from "@wordpress/i18n";
import ButtonsWrapper from "./ButtonsWrapper";
import ButtonsOldWrapper from "./ButtonsOldWrapper";
import ComponentsExample from "./ComponentsExample";
import ContentAnalysis from "./ContentAnalysisWrapper";
import DashboardWidget from "./DashboardWidgetWrapper";
import KeywordExample from "./KeywordExample";
import WordListWrapper from "./WordListWrapper";
import SidebarCollapsibleWrapper from "./SidebarCollapsibleWrapper";
import SnippetEditor from "./SnippetEditorExample";
import SvgIconsWrapper from "./SvgIconsWrapper";
import UIControlsWrapper from "./UIControlsWrapper";
import Wizard from "./WizardWrapper";
import { Loader } from "@yoast/components";
import FacebookPreviewExample from "./FacebookPreviewExample";
import TwitterPreviewExample from "./TwitterPreviewExample";
import LinkSuggestionsWrapper from "./LinkSuggestionsExample";
import WordOccurrencesWrapper from "./WordOccurrencesWrapper";
import MultiStepProgressWrapper from "./MultiStepProgressWrapper";
import SocialPreviewFormWrapper from "./SocialPreviewFormWrapper";
import SocialPreviewEditorWrapper from "./SocialPreviewEditorWrapper";
import InputsWrapper from "./InputsWrapper";
import ImageSelectWrapper from "./ImageSelectWrapper";
// Setup empty translations to prevent Jed error.
setLocaleData( { "": {} }, "yoast-components" );
const components = [
{
id: "snippet-preview",
name: "Snippet preview",
component: <SnippetEditor />,
},
{
id: "wizard",
name: "Wizard",
component: <Wizard />,
},
{
id: "loader",
name: "Loader",
component: <Loader />,
},
{
id: "content-analysis",
name: "Content Analysis",
component: <ContentAnalysis />,
},
{
id: "dashboard-widget",
name: "Dashboard Widget",
component: <DashboardWidget />,
},
{
id: "ui-controls",
name: "UI Controls",
component: <UIControlsWrapper />,
},
{
id: "keyword-example",
name: "Keyword",
component: <KeywordExample />,
},
{
id: "sidebar-collapsible",
name: "Collapsibles",
component: <SidebarCollapsibleWrapper />,
},
{
id: "wordlist",
name: "WordList",
component: <WordListWrapper />,
},
{
id: "linkSuggestions",
name: "LinkSuggestions",
component: <LinkSuggestionsWrapper />,
},
{
id: "buttons-old",
name: "Buttons (old)",
component: <ButtonsOldWrapper />,
},
{
id: "svg-icons",
name: "SVG Icons",
component: <SvgIconsWrapper />,
},
{
id: "components-example",
name: "Components",
component: <ComponentsExample />,
},
{
id: "facebookpreview-example",
name: "FacebookPreview",
component: <FacebookPreviewExample />,
},
{
id: "twitterpreview-example",
name: "TwitterPreview",
component: <TwitterPreviewExample />,
},
{
id: "wordoccurrences-example",
name: "WordOccurrences",
component: <WordOccurrencesWrapper />,
},
{
id: "multi-step-progress",
name: "Multi step progress",
component: <MultiStepProgressWrapper />,
},
{
id: "social-preview-data-form",
name: "Social Preview data form",
component: <SocialPreviewFormWrapper />,
},
{
id: "social-preview",
name: "Social Preview",
component: <SocialPreviewEditorWrapper />,
},
{
id: "buttons",
name: "Buttons",
component: <ButtonsWrapper />,
},
{
id: "inputs",
name: "Inputs",
component: <InputsWrapper />,
},
{
id: "image-select",
name: "Image Select",
component: <ImageSelectWrapper />,
},
];
const LanguageDirectionContainer = styled.div`
text-align: center;
`;
/**
* Represents the React Example App.
*/
class App extends React.Component {
/**
* Constructs the App container.
*/
constructor() {
super();
this.state = {
activeComponent: localStorage.getItem( "active-component" ) || "buttons",
isRtl: false,
};
this.changeLanguageDirection = this.changeLanguageDirection.bind( this );
}
/**
* Get the active component.
*
* @returns {string} The active component.
*/
getContent() {
const activeComponent = this.state.activeComponent;
for ( var i = 0; i < components.length; i++ ) {
if ( activeComponent === components[ i ].id ) {
return <div key={ components[ i ].id }>{ components[ i ].component }</div>;
}
}
}
/**
* Sets the activeComponent in the state.
*
* @param {string} activeComponent The active component id.
*
* @returns {void}
*/
navigate( activeComponent ) {
this.setState( {
activeComponent: activeComponent,
}, () => {
localStorage.setItem( "active-component", activeComponent );
} );
}
/**
* Renders the button for each component section.
*
* @param {string} id The component id.
* @param {string} title The component name.
*
* @returns {ReactElement} The button.
*/
renderButton( id, title ) {
const isActive = this.state.activeComponent === id;
const style = {};
if ( isActive ) {
style.backgroundColor = "#006671";
style.color = "#FFF";
style.borderRadius = "5px";
style.border = "1px solid white";
style.outline = "none";
}
return (
<button style={ style } key={ id } type="button" onClick={ this.navigate.bind( this, id ) }>
{ title }
</button>
);
}
/**
* Renders the menu.
*
* @returns {ReactElement} The navigation menu.
*/
getMenu() {
return (
<nav style={ { textAlign: "center" } }>
{
components.map( config => {
return this.renderButton( config.id, config.name );
} )
}
<p style={ { fontSize: "0.8em", margin: "5px 0" } }>
For redux devtools press <strong>Ctrl + H</strong>,
to change position press <strong>Ctrl + Q</strong>
</p>
</nav>
);
}
/**
* Renders a switch language directionality button.
*
* @returns {ReactElement} The rendered button.
*/
renderLanguageDirectionButton() {
return (
<button type="button" onClick={ this.changeLanguageDirection }>
Change language direction
</button>
);
}
/**
* Changes the language direction state.
*
* @returns {void}
*/
changeLanguageDirection() {
this.setState( {
isRtl: ! this.state.isRtl,
} );
}
/**
* Sets the language direction based on what direction is in the current state after the component did update.
*
* @param {Object} prevProps The old props.
* @param {Object} prevState The old state.
*
* @returns {void}
*/
componentDidUpdate( prevProps, prevState ) {
// Set and update the <html> element dir attribute based on the current language direction.
if ( prevState.isRtl !== this.state.isRtl ) {
document.documentElement.setAttribute( "dir", this.state.isRtl ? "rtl" : "ltr" );
}
}
/**
* Renders the example app.
*
* @returns {ReactElement} The rendered App.
*/
render() {
return (
<ThemeProvider theme={ { isRtl: this.state.isRtl } }>
<div>
{ this.getMenu() }
<LanguageDirectionContainer>
{ this.renderLanguageDirectionButton() }
</LanguageDirectionContainer>
{ this.getContent() }
</div>
</ThemeProvider>
);
}
}
export default App;