Skip to content

l0c0luke/react-amphtml

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-amphtml

Use amphtml components inside your React apps easily!

Usage

import * as amphtml from 'react-amphtml';

amphtml.Amp

An object containing React components that correspond to all built-ins and extensions of amphtml.

The properties of this object are the names of each built-in or extension of amphtml, but with the amp- prefix removed, and camel-cased, instead of kebab-cased.

amphtml.Amp[Amp Component]([props][, context])

Amp Components will accept any props given to them. If AmpScriptsManager is passing an instance of AmpScripts through context, these components will add the appropriate <script /> tag to it.

amphtml.AmpScript(props)

A component used to generate amphtml <script /> tags to be rendered in the <head /> of the document. More than likely, you will not need to use this, as these are created by an AmpScripts instance.

Class: amphtml.AmpScripts

A class that is used to keep track of generated amphtml <script /> tags. An instance of this should be given to AmpScriptsManager for Amp components to utilize through context.

new AmpScripts()

No constructor options.

ampScripts.addComponent(component)

Used to add a new <script /> tag for use in the <head /> of the document.

  • component <string>

    The name of the component (ex "amp-youtube").

ampScripts.getScriptElements()

Returns an array of AmpScripts, <script /> tags, to be rendered in the <head /> of the document.

amphtml.AmpScriptsManager(props)

  • props <Object>

    • children <Component>

    • ampScripts <AmpScripts>

A component that passes an instance of AmpScripts as context to Amp components.

amphtml.headerBoilerplate(href)

An function that returns an array of components for use in the <head /> of the document. href is the canonical reference to the source page. The array should include everything that amphtml validates for, excluding the scripts generated by AmpScripts.

Special amphtml Components

Some amphtml components do not lend well to react or the way in which react-amphtml was written. In these cases, the following components have been added to make things easier.

amphtml.Amp.Bind

amp-bind is a special extension of amphtml which allows for defining a state object, using amp-state, and also [*] bound attributes. JSX does not like any props or attributes which use [*] notation so Amp.Bind can be used to make it easier to read in these cases.

Example

Target HTML
<p [text]="'Hello ' + foo">Hello World</p>
JSX
<Amp.Bind text="'Hello ' + foo">
  <p>Hello World</p>
</Amp.Bind>

amphtml.Amp.State

amp-state, a component included in the amp-bind component script, requires a single <script type="application/json" /> element, with JSON as a child. In React, the requires using dangerouslySetInnerHTML. To make this easier, Amp.State does the heavy lifting.

Example

Target HTML
<amp-state id="allAnimals">
  <script type="application/json">
    {
      "currentAnimal": "dog"
    }
  </script>
</amp-state>
JSX
<Amp.State id="allAnimals">
  {{
    currentAnimal: 'dog',
  }}
</Amp.State>

amphtml.Amp.Action

The on attribute is used to install event handlers on elements. The events that are supported depend on the element. — AMP Docs

on is an attribute that is commonly used for actions, not just in amphtml repo, but React too. React will remove any on attributes from elements and try to set the appropriate event handlers, but Amp.Action, has a small work-around to prevent this from happening. It's also a bit easier to write the syntax for the events and actions that are to be applied to an element.

Example

Target HTML
<button on="tap:AMP.setState({ foo: 'amp-bind' })">
JSX
<Amp.Action
  events={{
    tap: ['AMP.setState({ foo: "amp-bind" })'],
  }}
>
  <button />
</Amp.Action>

Resources

About

⚡️ Use amphtml components inside your React apps easily!

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%