npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-amphtml

v4.0.2

Published

Use amphtml components inside your React apps easily!

Downloads

5,546

Readme

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