1
1
<div align =" center " >
2
- <h1 ><strong >RAMPT 3 </strong ></h1 >
3
- <div align =" center " >Server side rendering using Preact + ModularCSS/JSCSS + AMP-custom-elements. </div >
2
+ <h1 ><strong >RAMPT v4 </strong ></h1 >
3
+ <div align =" center " >< p >AMP components aliases and shims for React SSR 16+ & styled-components v3</ p > </div >
4
4
</div >
5
5
6
6
<br />
@@ -30,185 +30,117 @@ Write AMP pages using React syntaxt right the way and style with your preferred
30
30
## Contents
31
31
32
32
- [ Usage] ( #usage )
33
+ - [ Demo] ( #demo )
33
34
- [ API] ( #api )
34
35
- [ Configuration] ( #configuration )
35
36
- [ Contribute] ( #contributing )
36
37
37
38
38
39
## Usage
39
40
40
- ### Install from NPM
41
+ ### Install
41
42
42
- - ` yarn add react-amp-template`
43
+ - ` npm i react-amp-template`
43
44
44
45
### Static Render
45
46
46
47
``` javascript
47
- import { h } from ' preact'
48
- import render from ' react-amp-template'
49
-
50
- const metaContent = {
51
- ' http-equiv' : " origin-trial" ,
52
- ' data-expires' : ' 2020-01-01' ,
53
- ' data-feature' : " Web Share" ,
54
- ' content' : " Ajcrk411RcpUCQ3ovgC8le4e7Te/1kARZsW5Hd/OCnW6vIHTs5Kcq1PaABs7SzcrtfvT0TIlFh9Vdb5xWi9LiQsAAABSeyJvcmlnaW4iOiJodHRwczovL2FtcGJ5ZXhhbXBsZS5jb206NDQzIiwiZmVhdHVyZSI6IldlYlNoYXJlIiwiZXhwaXJ5IjoxNDkxMzM3MDEwfQ=="
55
- }
56
-
57
- const SocialShare = (props , { head }) => {
58
- head .append (' amp-social-share' )
59
- return < amp- social- share {... props} / >
60
- }
48
+ import React , { Fragment } from ' react'
49
+ import styled from ' styled-components'
50
+ import { renderToString , AMP } from ' react-amp-template'
61
51
62
- const Body = (props , { head }) => {
63
- // Add required meta tags to head at any moment.
64
- head .append (' meta' , metaContent)
65
- head .append (' link' , {rel: ' amphtml' , ' href' : " https://www.example.com/url/to/amp/document.html" })
66
- return (
67
- < body>
68
- < h1> Preact for AMP is welcome as well! < / h1>
69
- < SocialShare width= " 40" height= " 40" type= " email" / >
70
- < SocialShare width= " 40" height= " 40" type= " facebook"
71
- data- param- app_id= " 254325784911610" / >
72
- < SocialShare width= " 40" height= " 40" type= " gplus" / >
73
- < SocialShare width= " 40" height= " 40" type= " linkedin" / >
74
- < SocialShare width= " 40" height= " 40" type= " whatsapp" / >
75
- < SocialShare width= " 40" height= " 40" type= " twitter" / >
76
- < / body>
77
- )
78
- }
52
+ const { Title , Link , Carousel } = AMP
79
53
80
-
81
- render (< Body / > , {
82
- title: ' Rendering AMP with preact' ,
83
- canonical: ' https://my-website.com' ,
84
- })
85
- ```
86
-
87
- ## Styled components
88
-
89
- ``` javascript
90
- import React from ' react'
91
- import render from ' react-amp-template'
92
-
93
- const Body = ({ children, ... props }, { head }) =>
94
- < body {... props}>
95
- < Button> Normal< / Button>
96
- < ThemeProvider theme= {theme}>
97
- < Button> Themed< / Button>
98
- < / ThemeProvider>
99
- {children}
100
- < / body>
101
-
102
-
103
- const StyledBody = styled (Body)`
104
- background: papayawhip;
105
- @media (max-width: 700px) {
106
- background: palevioletred;
107
- }
54
+ const Body = styled .body `
55
+ margin: 0 1rem;
108
56
`
109
57
110
- render (< StyledBody / > , {
111
- title: ' Styling AMP with StyledComponents' ,
112
- canonical: ' https://canonical.com' ,
113
- styleManager: ' styled-components' ,
114
- })
58
+ const App = ({ title }) => (
59
+ < Fragment>
60
+ < Title> {title}< / Title>
61
+ < Link rel= " canonical" href= " http://localhost" / >
62
+ < Body>
63
+ < h1> Hello World< / h1>
64
+ < Carousel lightbox width= " 400" height= " 300" layout= " responsive" type= " slides" >
65
+ < amp- img src= " /img/image1.jpg" width= " 400" height= " 300" layout= " responsive" >< / amp- img>
66
+ < amp- img src= " /img/image2.jpg" width= " 400" height= " 300" layout= " responsive" >< / amp- img>
67
+ < amp- img src= " /img/image3.jpg" width= " 400" height= " 300" layout= " responsive" >< / amp- img>
68
+ < / Carousel>
69
+ < / Body>
70
+ < / Fragment>
71
+ )
72
+
73
+ export default props => renderToString (< App title= " AMP demo" / > )
115
74
```
116
75
117
- [ See more here] ( https://github.com/Ariel-Rodriguez/react-amp-template/tree/master/examples )
76
+
77
+ ## Demo [ here] ( https://github.com/Ariel-Rodriguez/react-amp-template/tree/master/examples )
118
78
119
79
120
80
## API
121
81
122
- ### Template
82
+ ### renderToString
123
83
124
84
``` javascript
125
- import { Template } from ' react-amp-template'
126
- const template = new Template ({ title, canonical, styleManager })
85
+ /**
86
+ * Transform React component into HTML AMP format.
87
+ *
88
+ * @returns {String} html
89
+ * @param {Class|Object} body React component to render
90
+ * @param {Object} options Settings
91
+ * @property {string} options.cdnURI absolute URL to AMP CDN
92
+ * @property {string} options.boilerplate HTML string which contains AMP boilerplate styles
93
+ * @property {object} options.extensions Key map of references to specify an extension version
94
+ * @property {object} options.extensions.default default version for all amp-extensions e.g '0.1'
95
+ * @property {object} options.extensions.extension [extension-name]
96
+ ** specify custom version for derived extension e.g: 'amp-sticky-ad': '1.0'
97
+ import { renderToString } from 'react-amp-template'
127
98
```
128
99
129
- ### Options
130
-
131
- - * title* | Add meta title with [ _ title_ ]
132
- - * canonical* | Add link canonical with href to [ _ canonical_ ]
133
- - * styleManager* | {string | function}. Allowed ` aphrodite ` ` styled-components ` ` undefined ` ` function `
134
-
135
-
136
- ### Context
137
-
138
- Every children element receives the high order ` context ` .
139
- It allows components to add custom content to Head.
100
+ #### AMP components
140
101
102
+ ```javascript
103
+ import { AMP } from 'react-amp-template'
141
104
142
- #### AMP component
105
+ const AdUnit = () => <AMP.AdUnit />
106
+ ```
107
+ - RAMPT provides shorthands for amp-custom-elements. A \[ get \] operation on { AMP } module returns Node element and automatically registers the `<script />` tag required by AMP.
143
108
144
- Ensure to add the amp-custom script required.
145
- RAMPT prevents duplicated scripts automatically.
109
+ - The following components could be used in case of need to ad elements into `<head>` element
146
110
147
111
```javascript
148
- const Element = (props , context ) => {
149
- context .head .append (' amp-sidebar' )
150
- context .head .append (' amp-sidebar' ) // <- it won't be added
151
- return ...
152
- }
112
+ <Link /> <Meta /> <Title /> <Script /> <Tag _name="custom-tag" />
153
113
```
154
114
155
- ` <script async custom-element="amp-sidebar" src="https://cdn.ampproject.org/v0/amp-sidebar-0.1.js"></script> `
156
-
157
- By default every amp-script address to version 0.1. However it can be customized.
115
+ - By default every amp-script address to version 0.1. However it can be customized.
158
116
159
117
```javascript
160
- const Element = (props , context ) => {
161
- context .head .append (' amp-sticky-ad' , { version: ' 1.0' })
162
- return ...
163
- }
118
+ renderToString(<App />, {
119
+ extensions: {
120
+ default: 0.2,
121
+ 'amp-sticky-unit': 1.0,
122
+ }
123
+ })
164
124
```
165
125
166
- ` <script async custom-element="amp-sticky-ad" src="https://cdn.ampproject.org/v0/amp-sticky-ad-0.1.js"></script> `
167
-
168
-
169
126
#### LD+JSON
170
127
171
128
```javascript
172
- const Element = ( props , context ) => {
173
- context . head . append ( ' ldjson ' , { id : ' myjson ' , content : {foo : ' bar ' }})
174
- return ...
175
- }
129
+ <AMP.Script
130
+ type="application/ld+json"
131
+ dangerouslySetInnerHTML={{ __html: JSON.stringify(json) }}
132
+ />
176
133
```
177
134
178
- ` <script type="application/json" id="myjson">{foo:bar}</script> `
179
-
180
-
181
- #### Mustache
182
-
183
- ``` javascript
184
- const Element = (props , { head }) => {
185
- head .append (' amp-list' )
186
- head .append (' amp-mustache' )
187
-
188
- return (
189
- < amp- list src= " https://ampbyexample.com/json/cart.json"
190
- layout= " fixed-height"
191
- height= " 56" >
192
- < template type= " amp-mustache"
193
- dangerousllySetInnerHTML= {{ __html: " Hi {{fullname}}!" }}>
194
- < / template>
195
- < / amp- list>
196
- )
197
- }
198
- ```
199
135
200
136
## Configuration
201
137
202
- RAMPT renders to string thanks to [ preact-render-to-string] ( https://github.com/developit/preact-render-to-string )
203
-
204
- A system bundler is recommended in order to make it work with React and Styled components.
205
- Take a look for [ this webpack configuration for RAMPT examples] ( https://github.com/Ariel-Rodriguez/react-amp-template/blob/master/config/webpack.config.babel.js )
206
-
207
- ### Babel & Webpack
138
+ ### Babel
139
+ - Setup the environment as recomends React and Styled-Components server rendering.
208
140
209
141
#### React | Styled Components
210
142
211
- ` yarn add --dev babel-plugin-styled-components babel-preset-react babel-plugin-module-resolver `
143
+ `npm i -D babel-plugin-styled-components babel-preset-react`
212
144
213
145
```json
214
146
{
@@ -218,62 +150,22 @@ Take a look for [this webpack configuration for RAMPT examples](https://github.c
218
150
],
219
151
"plugins": [
220
152
["babel-plugin-styled-components", { "ssr": true }]
221
- [" module-resolver" , {
222
- "root" : [" ." ],
223
- "alias" : {
224
- "react" : " preact-compat" ,
225
- "react-dom" : " preact-compat"
226
- }
227
- }]
228
153
]
229
154
}
230
155
```
231
156
232
- - Add Webpack alias
233
-
234
- ``` javascript
235
- resolve: {
236
- extensions: [' .jsx' , ' .js' ],
237
- modules: [
238
- path .resolve (__dirname , " src" ),
239
- path .resolve (__dirname , ' node_modules' )
240
- ],
241
- alias: {
242
- ' react' : ' preact-compat' ,
243
- ' react-dom' : ' preact-compat' ,
244
- }
245
- }
246
- ...
247
- ```
248
-
249
- - Include styled-component in the bundle in order to bypass React alias into Preact
250
-
251
- ``` javascript
252
- externals: [ nodeExternals ({
253
- whitelist: [' styled-components' ]
254
- })]
255
- ```
256
-
257
- ## Preact
258
-
259
- ``` json
260
- {
261
- "presets" : [
262
- " stage-0" ,
263
- " preact"
264
- ]
265
- }
266
- ```
267
157
268
158
## Contributing
269
159
270
- - ` $git clone git@github.com:Ariel-Rodriguez/react-amp-template.git `
271
- - ` yarn `
272
- - ` yarn dev `
160
+ - Fork the repository
161
+ - `npm install`
162
+ - `npm run dev`
163
+ - Create pull request
273
164
274
165
### Build examples
275
166
276
- - ` yarn examples `
167
+ - `cd examples/simple-server`
168
+ - `npm install && npm start`
277
169
278
170
## License
279
171
0 commit comments