• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    TypeScript
  • Created over 8 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚡️ Use amphtml components inside your React apps easily!

react-amphtml

Use amphtml components inside your React apps easily!

Usage

react-amphtml exports React components and functions to easily create AMP HTML pages. Each exported React component has a TypeScript interface and PropTypes derived from AMP HTML's own validator rules to speed up development and make it safer. Boilerplate and the inclusion of AMP directive-specific scripts is all handled for you!

// All AMP elements
import * as Amp from 'react-amphtml';

// Helper render props for actions and bindings
import * as AmpHelpers from 'react-amphtml/helpers';

// Components and functions to render pages
import {
  AmpScripts,
  AmpScriptsManager,
  headerBoilerplate,
} from 'react-amphtml/setup';

Amp Components

import * as Amp from 'react-amphtml';
// ...
<Amp.AmpCarousel {...props} />

The main file exported by react-amphtml contains all of the AMP HTML directives as React components. This includes the custom element amp-* directives, normal HTML directives with validations required by AMP, and some components with added functionality: Html, AmpState (amp-state directive) and Script.

To see a list of available components and their relative documentation see the official AMP components documentation: The AMP component catalogue.

Amp Helpers

import * as Amp from 'react-amphtml';
import * as AmpHelpers from 'react-amphtml/helpers';

// Example of attaching actions to elements
<AmpHelpers.Action events={{...}}>
  {(props) => (
    <button type="button" {...props}>
      Do Something
    </button>
  )}
</AmpHelpers.Action>

// Example of using state and bindings together
const defaultHeading = {
  text: 'Hello, World!',
};
// ...
<Amp.AmpState specName="amp-state" id="heading">
  {defaultHeading}
</Amp.AmpState>
<AmpHelpers.Bind text="heading.text">
  {(props): ReactElement => <h1 {...props}>{defaultHeading.text}</h1>}
</AmpHelpers.Bind>

The helpers file contains render prop components that help add AMP attribute directives for actions and bindings. Wondering what actions and bindings are all about? Check out these official guides on the subjects:

Amp Setup

import * as Amp from 'react-amphtml';
import {
  AmpScripts,
  AmpScriptsManager,
  headerBoilerplate,
} from 'react-amphtml/setup';

const ampScripts = new AmpScripts();

const bodyContent = renderToStaticMarkup(
  <AmpScriptsManager ampScripts={ampScripts}>
    <div>
      <Amp.AmpImg
        specName="default"
        src="/"
        width={0}
        height={0}
        layout="responsive"
        alt="test"
      />
      <Amp.AmpAccordion />
    </div>
  </AmpScriptsManager>,
);

/* eslint-disable react/no-danger */
const html = renderToStaticMarkup(
  <Amp.Html>
    <head>
      {headerBoilerplate('/')}
      <title>react-amphtml</title>
      {ampScripts.getScriptElements()}
    </head>
    <body dangerouslySetInnerHTML={{ __html: bodyContent }} />
  </Amp.Html>,
);
/* eslint-enable */

const htmlPage = `
  <!doctype html>
  ${html}
`;

The setup file makes creating pages for AMP HTML a breeze. It helps insert all the necessary boilerplate and also the scripts needed for AMP directives.

The code is based on the requirements from AMP documented in Create your AMP HTML page: Required mark-up.

Examples

Full Example

Go checkout ampreact!

If you are looking for an example that is in combination with one or more of these tools:

ampreact gives a very nice setup to get started with or learn from!

Simple Example

For simple usage examples of react-amphtml, check the Jest unit tests in react-amphtml/src/__tests__/react-amphtml.spec.tsx. The best test to look at is can server-side render valid html for a good complete usage of react-amphtml.

Development

About

The code for react-amphtml is generated from AMP HTML's own validator via amphtml-validator-rules.

Want to learn about AMP HTML validation? See the guide: Validate AMP pages.

Need to run the validator? Use either the online tool The AMP Validator or the npm package amphtml-validator.

Commands

Use the following commands to develop on react-amphtml.

  • npm run codegen: Create components based on AMP HTML's validator. This must be done at least once prior to running npm run build, and can be done afterwards anytime code in codegen is modified.

  • npm run build: Bundles the source files into dist.

  • npm run typecheck: Uses TypeScript to ensure type safety. Should be run after running npm run build to check the files in dist that are bundled.

  • npm run lint: Use ESLint to check source files.

  • npm run test: Use Jest to run tests.

Resources

More Repositories

1

pokemonsay

👾 Pokemon version of cowsay with CLI and API.
JavaScript
401
star
2

ampreact

⚡⚛ AMP HTML, Next.js, React, styled-components, GraphQL, and TypeScript — The most trendy website boilerplate possible.
TypeScript
228
star
3

hyper-sync-settings

🔃 Sync Hyper.app settings with Github.
TypeScript
123
star
4

bluster

A Rust crate for implementing BLE (Bluetooth Low Energy) peripherals
Rust
110
star
5

hyper-tab-icons

💅 Icons in the header tabs for the current running process in Hyper.app.
JavaScript
92
star
6

sous-vide

🍲 Node.js API to control Anova water circulators.
JavaScript
69
star
7

wasm-brotli

🗜 WebAssembly compiled Brotli library
TypeScript
66
star
8

hyper-transparent-bg

🌫 An emulated transparent background for HyperTerm!
JavaScript
36
star
9

threejs-water

Three.js implementation of water with real-time caustics.
JavaScript
30
star
10

xpc-connection-rs

XPC connection bindings for Rust
Rust
29
star
11

lastpass-node

🔐 Lastpass client for Node.js
JavaScript
26
star
12

font-family-merger

Merge font variations together to make a big, happy, font family!
Python
21
star
13

nixos-rpi-sd-image

A convenient way to create custom Raspberry Pi NixOS SD images.
Nix
21
star
14

wasm-zopfli

🗜 WebAssembly compiled Zopfli library
JavaScript
16
star
15

awesome-rust-keyboard-firmware

A list of keyboard firmwares written in Rust and related resources.
13
star
16

hyperterm-lastpass

⚫️ Lastpass plugin for autofilling passwords in HyperTerm.
JavaScript
13
star
17

bitwise-rotation

🔄 Rotate values with a bitwise rotation.
JavaScript
10
star
18

rollup-webpack-loader

🍭 Add rollup to your webpack.
JavaScript
9
star
19

node-fonttools

🖋 Native bindings to `fonttools` to decompile and compile fonts.
C++
8
star
20

keycode

A Rust crate for translating keycodes based on Chrome's mapping of keys.
Rust
8
star
21

grav-plugin-dropbox

A highly alpha, yet working Dropbox plugin for Grav.
PHP
8
star
22

eyeliner

👁 A CSS inliner for making emails.
Rust
8
star
23

proton-c

Board Support Crate for the Proton C.
Rust
6
star
24

docker-preprocessors

📬Easy way to preprocess resources and assets with Docker.
JavaScript
5
star
25

node-eyeliner

Native bindings for eyeliner, a CSS inliner for making emails.
Rust
4
star
26

pm2-loggly

PM2 Remote Logging to Loggly
JavaScript
4
star
27

bleboard

Simple app to make any keyboard bluetooth enabled
Rust
4
star
28

pixelmatch-rs

Pixel-level image comparison
Rust
4
star
29

zopfli-rs

Bindings to `zopfli` for deflate, gzip, and zlib compression.
Rust
4
star
30

bitrate

Extension traits that add convenience methods for working with bitrates and frequencies.
Rust
4
star
31

jsxn-rs

Put JSX in your JSON and JSON in your JSX!
Rust
4
star
32

docker-intranet

Easy way to get started with a small intranet.
PHP
4
star
33

hyper-custom-plugins

🛠 Create Hyper.app plugins from Hyper.app's configuration file.
JavaScript
4
star
34

linter-american

🇺🇸 Make American spelling great again.
JavaScript
3
star
35

mk20d7-hal

Rust embedded-hal implementation crate for the MK20D7 processor
Rust
3
star
36

tokei-aas

Tokei as a Service
Rust
3
star
37

nur-packages

My personal NUR repository
Nix
2
star
38

karl-the-fog

🌫 Creates a "stylized fog" using an HTML5 canvas.
JavaScript
2
star
39

moment.rs

Rust
2
star
40

wasm-module-preprocessors

💎 Easy way to preprocess WebAssembly (wasm) modules.
JavaScript
2
star
41

rust-apigpio

C
2
star
42

zerocater-slack

Simple personal foray into slack slash commands using my newly created ZeroCater client.
JavaScript
2
star
43

inro

🌸 Probably the easiest way to create a responsive UI.
JavaScript
2
star
44

amphtml-validator-rules

🔏Get all the rules that `amphtml-validator` uses.
JavaScript
2
star
45

MagentoOrderCancelEmailNotification

When an order is cancelled a notification of this even will be emailed to magento's support email.
PHP
2
star
46

zerocater-node

🍱 Node.js client for ZeroCater.
JavaScript
2
star
47

image-xterm-loader

🖥 Load images with Webpack as XTerm 256-color compatible strings.
JavaScript
2
star
48

grav-plugin-redirects2

Grav plugin to redirect on a per page basis.
PHP
1
star
49

git-changelog

Quick Node.js script to output a CSV changelog of git commits
JavaScript
1
star
50

node-jsxn

Put JSX in your JSON and JSON in your JSX!
Rust
1
star
51

rust-lib-bin-crate-example

Shows that a Rust module can be both a `lib` and a `bin`.
Rust
1
star
52

jira-absolute-dates

⌛ Chrome extension that shows JIRA absolute dates.
JavaScript
1
star
53

babel-plugin-ad-lib

🔥 A Babel plugin to make your code lit AF
JavaScript
1
star
54

log-scrollback-ui-template

JavaScript
1
star
55

domshot

Render HTML + CSS to images quickly
Rust
1
star
56

docker-tweets

Container for spinning up an oauth server to retrieve tweets.
1
star
57

crowd-control

⚠️ Control promises in a queue with optional timeouts.
JavaScript
1
star
58

nginx-indicator

🚨 Nginx up-indicator, click to start server
JavaScript
1
star
59

node-find-synology

Use find.synology.com's APIs for finding your local Synology NASs
JavaScript
1
star
60

gdo-server

Integrate Security+ 2.0 garage doors w/ MQTT
Python
1
star
61

git-webfont-service

🖇 Serve fonts from a git repo.
JavaScript
1
star
62

sketch-expander

🆙 Expand Sketch files to make them easy to version control.
JavaScript
1
star
63

mk20d7

Rust device crate generated with svd2rust for MK20D7 processors
Rust
1
star
64

hyper-greeting

🙋 Add a nice greeting to each new session created in Hyper.
JavaScript
1
star
65

raspberry-pi-server-poc

🔑 A quick proof of concept that a server (will be a Raspberry Pi) can be used as an upstream server behind a VPN (OpenVPN in this case) from another server (planning on using DigitalOcean).
Shell
1
star
66

linter-rust-cargo

Rust Cargo linter that's fast.
JavaScript
1
star
67

advent-of-code-2022

Zig
1
star
68

npm-reinstall-global-packages

Simple Node.js script to reinstall all npm global packages (convenient for when you change Node.js versions)
JavaScript
1
star
69

npm-dependency-injection

💉 A very dumb version of dependency injection for npm.
JavaScript
1
star
70

rocky-watchface-tutorial-part1-webpack-example

📖 Example of how to use Webpack with the Pebble smartwatch.
JavaScript
1
star