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 pathComponentsExample.js
229 lines (213 loc) · 6.55 KB
/
ComponentsExample.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
import { __ } from "@wordpress/i18n";
import { Alert, CourseDetails, FullHeightCard, StarRating, Warning } from "@yoast/components";
import { getCourseFeed, getDirectionalStyle, makeOutboundLink } from "@yoast/helpers";
import React from "react";
import styled from "styled-components";
const Container = styled.div`
max-width: 1024px;
margin: 0 auto;
padding: 24px;
box-sizing: border-box;
`;
// Use this only for testing purposes in the standalone examples.
const WordPressStylesEmulator = styled.div`
/* Emulate WordPress font metrics. */
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
font-size: 13px;
line-height: 1.4em;
/* Emulate WordPress outline reset. */
a, div {
outline: 0;
}
/* Emulate WordPress list styles. */
li {
margin-bottom: 6px;
}
`;
const CoursesList = styled.ul`
display: flex;
flex-wrap: wrap;
list-style-type: none;
padding: 0;
/* Max 5 cards per row. */
max-width: 1520px;
`;
const CourseListItem = styled.li`
/* Higher specificity to override WordPress margins. */
&& {
flex: 0 0 288px;
margin: ${ getDirectionalStyle( "0 16px 16px 0", "0 0 16px 16px" ) };
}
`;
const CornerstoneLink = makeOutboundLink();
const NonYoastLink = makeOutboundLink();
const YoastLink = makeOutboundLink();
const YoastShortLink = makeOutboundLink();
const YoastLinkCustomRel = makeOutboundLink();
/**
* Renders the yoast-component Components Examples.
*
* @returns {ReactElement} The Components example container component.
*/
export default class ComponentsExample extends React.Component {
/**
* Creates the components and initializes its state.
*/
constructor() {
super();
this.state = {
courses: null,
isAlertDismissed: false,
};
this.getFeed( "free" );
this.onAlertDismissed = () => {
this.setState( { isAlertDismissed: true } );
};
}
/**
* Fetches data from the yoast.com feed, parses it and sets it to the state.
*
* @param {String} version Active Yoast SEO version.
*
* @returns {void}
*/
getFeed( version ) {
// Developer note: this link should -not- be converted to a shortlink.
getCourseFeed( "https://yoast.com/?feed=courses&license=" + version )
.then( ( feed ) => {
feed.items = feed.items.map( ( item ) => {
return item;
} );
this.setState( { courses: feed.items } );
} )
/* eslint-disable-next-line no-console */
.catch( error => console.error( error ) );
}
/**
* Converts the relevant data in a course to a header object.
*
* @param {Object} course The course to create a header for.
*
* @returns {Object} The header object.
*/
getHeaderData( course ) {
return {
image: course.image,
title: course.title,
link: course.link,
};
}
/**
* Converts the relevant data from a course for the ctaButton to an object.
*
* @param {string} course The course to create a ctaButton for.
*
* @returns {Object} The data object for the ctaButton.
*/
getButtonData( course ) {
return {
ctaButtonCopy: course.ctaButtonCopy,
ctaButtonType: course.ctaButtonType,
ctaButtonUrl: course.ctaButtonUrl,
};
}
/**
* Renders a Yoast Alert.
*
* @returns {React.Element} The rendered alert.
*/
renderAlert( type ) {
return <Alert key={ type } type={ type }>
{ `This is an Alert of type: "${ type }".` }
<br />
You can add some content.
Including a link <YoastShortLink href="https://yoa.st/why-permalinks/">yoa.st/why-permalinks</YoastShortLink>
</Alert>;
}
updateStars( event ) {
this.setState({
input: event.target.value
} );
}
/**
* Renders all the Component examples.
*
* @returns {React.Element} The rendered list of the Component examples.
*/
render() {
const courses = this.state.courses;
/* eslint-disable react/jsx-no-target-blank */
return (
<React.Fragment>
<Container>
<h2>Yoast alerts</h2>
{ [ "error", "info", "success", "warning" ].map( this.renderAlert ) }
{ ! this.state.isAlertDismissed && <Alert type="info" onDismissed={ this.onAlertDismissed }>
This is the dismissable variant.
<br />
You will have to wrap it in order to do actually dismiss it.
<br />
Which is currently done through the state of this example.
</Alert> }
<h2>Yoast warning</h2>
<Warning
message={ [
"This is a warning message that also accepts arrays, so you can pass links such as ",
<CornerstoneLink
key="1"
href="https://yoa.st/metabox-help-cornerstone"
>cornerstone content</CornerstoneLink>,
", for example.",
<p key="2">This spans to multiple lines.</p>,
] }
/>
<h2>Star rating</h2>
<i>Accepts a rating from 0-5 and colors the stars yellow accordingly</i>
<StarRating rating={ 3.5 } />
<h2>Outbound links</h2>
<p>
<NonYoastLink href="http://www.example.org">example.org</NonYoastLink>
<br /><small>expected: target="_blank" rel="noopener" and visually hidden message</small>
</p>
<p>
<YoastLink href="https://yoast.com">yoast.com</YoastLink>
<br /><small>expected: target="_blank" and visually hidden message</small>
</p>
<p>
<YoastShortLink href="https://yoa.st/why-permalinks/">yoa.st/why-permalinks</YoastShortLink>
<br /><small>expected: target="_blank" and visually hidden message</small>
</p>
<p>
<YoastLinkCustomRel href="https://yoast.com/" rel="bookmark nofollow">yoast.com (custom rel attribute)</YoastLinkCustomRel>
<br /><small>expected: target="_blank" rel="bookmark nofollow" and visually hidden message</small>
</p>
</Container>
<h2>Courses overview cards</h2>
<p>Full width example to test the cards wrapping.</p>
<WordPressStylesEmulator>
{ courses && <CoursesList>
{ courses.map( course =>
<CourseListItem key={ course.id }>
<FullHeightCard
className={ "CourseCard" }
id={ course.id }
header={ this.getHeaderData( course ) }
banner={ course.isFree === "true" ? { text: __( "Free", "yoast-components" ) } : null }
>
<CourseDetails
description={ course.content }
courseUrl={ course.link }
readMoreLinkText={ course.readMoreLinkText }
ctaButtonData={ this.getButtonData( course ) }
isBundle={ course.isBundle }
/>
</FullHeightCard>
</CourseListItem>
) }
</CoursesList> }
</WordPressStylesEmulator>
</React.Fragment>
);
/* eslint-enable react/jsx-no-target-blank */
}
}