AMP for JavaScripters

AMP for JavaScripters
Implementing Interactive Interfaces with AMP
Weston Ruter & Felix Arntz | #JSforWPConf
Agenda
The AMP Framework
AMP Components
AMP Actions and Events
01
02
03
AMP State and Bindings
AMP Script
04
05
The AMP Framework
AMP is a web component framework to
easily create user-first websites.
AMP is an HTML Framework
Interactivity in AMP
Developers using AMP
AMP Framework Technologies
• AMP is an open source project.
• AMP has an open governance model.
• AMP is built on the open web.
Democratizing Performance
A Minimal AMP Document
<!DOCTYPE html>
<html amp>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="self.html">
<style amp-boilerplate>…</style>
<noscript><style amp-boilerplate>…</style></noscript>
<script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>
Hello world.
</body>
</html>
ValidatorWeb Components CDN & Cache
AMP Components
Types of Elements Used in AMP
Built-in
HTML Tags
Optimized
Replacements
Custom
Components
<p> <amp-img> <amp-accordion>
Optimized Replacements
<img> → <amp-img> / <amp-anim>
<iframe> → <amp-iframe>
<audio> → <amp-audio>
<video> → <amp-video>
Custom Components
Component Example
<amp-carousel>
<amp-carousel type="carousel" ...>
<a class="project-highlight" href="...">
<amp-img src="..." ...></amp-img>
</a>
<!-- ... -->
</amp-carousel>
Component Example
<amp-image-slider>
<amp-image-slider width="300" height="200" layout="responsive">
<amp-img
src="https://upload.wikimedia.org/.../Whole_world_-_land_and_oceans.jpg"
alt="Earth at Day"
layout="fill"></amp-img>
<amp-img
src="https://upload.wikimedia.org/.../City_Lights_2012_-_Flat_map_crop.jpg"
alt="Earth at night"
layout="fill"></amp-img>
</amp-image-slider>
Component Example
<amp-image-lightbox>
<amp-image-lightbox id="lightbox1" layout="nodisplay">
</amp-image-lightbox>
<!-- ... -->
<figure>
<amp-img on="tap:lightbox1.open" role="button" tabindex="0"
src="..." alt="Picture of a dog" width="300" height="246"></amp-img>
<figcaption>Border Collie.</figcaption>
</figure>
<!-- ... -->
Component Example
<amp-list
layout="fixed-height"
height="100"
src="https://blog.amp.dev/wp-json/wp/v2/posts/"
items="."
>
<template type="amp-mustache">
<div>
<a href="{{link}}">{{{title.rendered}}}</a>
</div>
</template>
</amp-list>
<amp-list>
Mobile Menu: amp-sidebar
<button id="toggleNavMenu" on="tap:navMenu.toggle" aria-controls="navMenu">
Toggle Menu
</button>
<amp-sidebar id="navMenu" layout="nodisplay" side="left">
<button id="closeNavMenu" on="tap:navMenu.close">
Close Menu
</button>
<nav>
<ul>
<li><a href="...">...</a></li>
<!-- ... -->
</ul>
</nav>
</amp-sidebar>
AMP Actions and Events
Component API
AMP Events AMP Actions
Triggered by the user. Triggered by an AMP event.
Abstraction of native DOM events. Similar to methods on DOM elements.
on="{event}:{elementId}.{action}[({arg}={value}...)],{event}:{elementId}…"
<button id="toggleNavMenu" on="tap:navMenu.toggle" aria-controls="navMenu">
Toggle Menu
</button>
Global Events and Actions
Events
● tap
Actions
● show
● hide
● toggleVisibility
● toggleClass
● scrollTo
● focus
amp.dev/documentation/guides-and-tutorials/learn/amp-actions-and-events
#globally-defined-events-and-actions
Element-Specific Events and Actions
Events
● change (<input>)
● submit (<form>)
● slideChange (<amp-carousel>)
● sidebarOpen (<amp-sidebar>)
● ...
Actions
● submit (<form>)
● play (<amp-audio>)
● goToSlide (<amp-carousel>)
● open (<amp-sidebar>)
● ...
amp.dev/documentation/guides-and-tutorials/learn/amp-actions-and-events
#element-specific-events
Mobile Menu: toggleVisibility
<button id="toggleNavMenu" on="tap:navMenu.toggleVisibility" ...>
Toggle Menu
</button>
<nav id="navMenu" hidden>
<ul>
<li><a href="...">...</a></li>
<!-- ... -->
</ul>
</nav>
AMP State and Bindings
• State
• Expressions
• Bindings
amp-bind
<amp-state id="count">
<script type="application/json">0</script>
</amp-state>
Count: <output [text]="count">0</output>
<button on="tap:AMP.setState( { count: count + 1 } )">Increment</button>
Example: Click Counter
<amp-state id="cart">
<script type="application/json">
{ "quantity": 0 }
</script>
</amp-state>
Quantity: <input [value]="cart.quantity" type="number" value="0" readonly>
<button on="tap:AMP.setState( { cart:{ quantity: cart.quantity + 1 } } )">
More
</button>
<button on="tap:AMP.setState( { cart:{ quantity: max( 0, cart.quantity - 1 ) } } )"
disabled [disabled]="cart.quantity == 0">
Less
</button>
Example: Shopping Cart Quantity
<amp-state id="navExpanded">
<script type="application/json">true</script>
</amp-state>
...
<amp-state id="cart">
<script type="application/json">{"quantity":2}</script>
</amp-state>
...
<amp-state id="skus">
<script type="application/json">["123", "456", "789"]</script>
</amp-state>
Multiple amp-state elements
Mobile Menu: amp-bind
<amp-state id="navMenuExpanded">
<script type="application/json">false</script>
</amp-state>
<button id="toggleNavMenu" on="tap:AMP.setState({ navMenuExpanded: ! navMenuExpanded })"
aria-controls="navMenu" aria-expanded="false"
[aria-expanded]="navMenuExpanded ? 'true' : 'false'"
[text]="navMenuExpanded ? 'Close Menu' : 'Open Menu'"
>Open Menu</button>
<nav id="navMenu" hidden [hidden]="!navMenuExpanded">
<ul>
<li><a href="...">...</a></li>
<!-- ... -->
</ul>
</nav>
AMP for JavaScripters
AMP for JavaScripters
AMP Script
• Script runs in Web Worker sandbox
• WorkerDOM library exposes DOM APIs
• Built for modern frameworks (e.g. React, Vue)
amp-script
Mobile Menu via amp-script
<amp-script src="https://example.com/.../nav-menu-via-amp-script.js">
<button id="toggleNavMenu" aria-controls="navMenu" aria-expanded="false"
data-open-text="Open Menu" data-close-text="Close Menu"
>Open Menu</button>
<nav id="navMenu" class="hidden">
<ul>
<li><a href="...">...</a></li>
<!-- ... -->
</ul>
</nav>
</amp-script>
const button = document.getElementById( 'toggleNavMenu' );
const navMenu = document.getElementById( 'navMenu' );
button.addEventListener( 'click', () => {
const hidden = ! navMenu.classList.contains( 'hidden' );
navMenu.classList.toggle( 'hidden', hidden );
button.setAttribute( 'aria-expanded', hidden ? 'false' : 'true' );
button.textContent = hidden ? button.getAttribute( 'data-open-text' ) : button.getAttribute( 'data-close-text' );
} );
• Escape hatch
• Limit of 150KB
• Script limited to DOM in scope of container
• Not all DOM APIs are supported (yet)
amp-script: Restrictions
Canvas Drawing via amp-script
<amp-script src="canvas-drawing-with-amp-script.js" width="400" height="400">
<button>Start drawing!</button>
</amp-script>
Password checker via amp-script
amp.dev/documentation/guides-and-tutorials/develop/
custom-javascript-tutorial
w.org/plugins/amp
AMP
in WordPress
amp.dev
amp-wp.org
Learn More
github.com/westonruter/javascriptforwp-conference-amp-examples
Thank You
Felix Arntz
@felixarntz
Weston Ruter
@westonruter
1 of 40

Recommended

php tutorial - By Bally Chohan by
php tutorial - By Bally Chohanphp tutorial - By Bally Chohan
php tutorial - By Bally Chohanballychohanuk
1.1K views11 slides
AMP in WordPress, the WordPress Way by
AMP in WordPress, the WordPress WayAMP in WordPress, the WordPress Way
AMP in WordPress, the WordPress WayAlberto Medina
232 views52 slides
Xaml novinky ve Windows 10 by
Xaml novinky ve Windows 10Xaml novinky ve Windows 10
Xaml novinky ve Windows 10Jiri Danihelka
778 views27 slides
jQuery SUG Group Introduction by
jQuery SUG Group IntroductionjQuery SUG Group Introduction
jQuery SUG Group IntroductionAndrew Chalkley
579 views11 slides
Word press dreamweaver by
Word press dreamweaverWord press dreamweaver
Word press dreamweaverkmawk
817 views55 slides
Web Os Hands On by
Web Os Hands OnWeb Os Hands On
Web Os Hands On360|Conferences
822 views11 slides

More Related Content

What's hot

Redirect subdomain to webmail by
Redirect subdomain to webmailRedirect subdomain to webmail
Redirect subdomain to webmailKaviyarasu Pugaz
651 views3 slides
Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16 by
Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16
Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16Sean Malseed
1.3K views41 slides
Accelerated Mobile Pages (AMP) & How it will Impact your Business by
Accelerated Mobile Pages (AMP) & How it will Impact your BusinessAccelerated Mobile Pages (AMP) & How it will Impact your Business
Accelerated Mobile Pages (AMP) & How it will Impact your BusinessHarshavardhan MP
1.3K views13 slides
Ionic by Example by
Ionic by ExampleIonic by Example
Ionic by ExampleMichal Haták
671 views51 slides
J Query Presentation by
J Query PresentationJ Query Presentation
J Query PresentationVishal Kumar
2.3K views31 slides
Accelerated Mobile Pages by
Accelerated Mobile PagesAccelerated Mobile Pages
Accelerated Mobile PagesJeremy Green
1.5K views26 slides

What's hot(8)

Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16 by Sean Malseed
Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16
Get AMP'ed for Accelerated Mobile Pages - SEO Grail Philadelphia 1/20/16
Sean Malseed1.3K views
Accelerated Mobile Pages (AMP) & How it will Impact your Business by Harshavardhan MP
Accelerated Mobile Pages (AMP) & How it will Impact your BusinessAccelerated Mobile Pages (AMP) & How it will Impact your Business
Accelerated Mobile Pages (AMP) & How it will Impact your Business
Harshavardhan MP1.3K views
J Query Presentation by Vishal Kumar
J Query PresentationJ Query Presentation
J Query Presentation
Vishal Kumar2.3K views
Accelerated Mobile Pages by Jeremy Green
Accelerated Mobile PagesAccelerated Mobile Pages
Accelerated Mobile Pages
Jeremy Green1.5K views
V Legakis Presentation by VLegakis
V Legakis PresentationV Legakis Presentation
V Legakis Presentation
VLegakis213 views

Similar to AMP for JavaScripters

Once Source to Rule Them All by
Once Source to Rule Them AllOnce Source to Rule Them All
Once Source to Rule Them AllDavid Yeiser
2.2K views212 slides
Xxx by
XxxXxx
Xxxsyfwan
8.6K views30 slides
Accelerated Mobile Pages (AMP) in Magento by
Accelerated Mobile Pages (AMP) in MagentoAccelerated Mobile Pages (AMP) in Magento
Accelerated Mobile Pages (AMP) in MagentoMagecom UK Limited
870 views52 slides
WPF - An introduction by
WPF - An introductionWPF - An introduction
WPF - An introductionSharada Gururaj
3.5K views40 slides
SMC304 Serverless Orchestration with AWS Step Functions by
SMC304 Serverless Orchestration with AWS Step FunctionsSMC304 Serverless Orchestration with AWS Step Functions
SMC304 Serverless Orchestration with AWS Step FunctionsAmazon Web Services
932 views54 slides
HTML5 New and Improved by
HTML5   New and ImprovedHTML5   New and Improved
HTML5 New and ImprovedTimothy Fisher
6.8K views46 slides

Similar to AMP for JavaScripters(20)

Once Source to Rule Them All by David Yeiser
Once Source to Rule Them AllOnce Source to Rule Them All
Once Source to Rule Them All
David Yeiser2.2K views
Xxx by syfwan
XxxXxx
Xxx
syfwan8.6K views
SMC304 Serverless Orchestration with AWS Step Functions by Amazon Web Services
SMC304 Serverless Orchestration with AWS Step FunctionsSMC304 Serverless Orchestration with AWS Step Functions
SMC304 Serverless Orchestration with AWS Step Functions
Progressive Web Apps: o melhor da Web appficada by Caelum
Progressive Web Apps: o melhor da Web appficadaProgressive Web Apps: o melhor da Web appficada
Progressive Web Apps: o melhor da Web appficada
Caelum75.8K views
Java Web Development with Stripes by Samuel Santos
Java Web Development with StripesJava Web Development with Stripes
Java Web Development with Stripes
Samuel Santos4.8K views
Building iPad apps with Flex - 360Flex by danielwanja
Building iPad apps with Flex - 360FlexBuilding iPad apps with Flex - 360Flex
Building iPad apps with Flex - 360Flex
danielwanja3.4K views
Breaking SAP portal (HashDays) by ERPScan
Breaking SAP portal (HashDays)Breaking SAP portal (HashDays)
Breaking SAP portal (HashDays)
ERPScan603 views
Developing Applications for WebOS by Chuq Von Rospach
Developing Applications for WebOSDeveloping Applications for WebOS
Developing Applications for WebOS
Chuq Von Rospach2.4K views
Course CodeSchool - Shaping up with Angular.js by Vinícius de Moraes
Course CodeSchool - Shaping up with Angular.jsCourse CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.js
Vinícius de Moraes3.8K views
Overview of ASP.Net by software outsourcing company india by Jignesh Aakoliya
Overview of ASP.Net by software outsourcing company indiaOverview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company india
Jignesh Aakoliya143 views
The Future of the Web - Cold Front conference 2016 by Robert Nyman
The Future of the Web - Cold Front conference 2016The Future of the Web - Cold Front conference 2016
The Future of the Web - Cold Front conference 2016
Robert Nyman13.2K views
Take Your XPages Development to the Next Level by balassaitis
Take Your XPages Development to the Next LevelTake Your XPages Development to the Next Level
Take Your XPages Development to the Next Level
balassaitis3.2K views
URL Hacking 101: An Easy Way to Streamline Processes in Salesforce by Configero
URL Hacking 101: An Easy Way to Streamline Processes in Salesforce URL Hacking 101: An Easy Way to Streamline Processes in Salesforce
URL Hacking 101: An Easy Way to Streamline Processes in Salesforce
Configero11.1K views
Demystifying S-Controls and AJAX by dreamforce2006
Demystifying S-Controls and AJAXDemystifying S-Controls and AJAX
Demystifying S-Controls and AJAX
dreamforce20061K views

More from Felix Arntz

Tackling performance in the WordPress ecosystem at scale by
Tackling performance in the WordPress ecosystem at scaleTackling performance in the WordPress ecosystem at scale
Tackling performance in the WordPress ecosystem at scaleFelix Arntz
82 views32 slides
Enhancing performance in an open-source CMS ecosystem by
Enhancing performance in an open-source CMS ecosystemEnhancing performance in an open-source CMS ecosystem
Enhancing performance in an open-source CMS ecosystemFelix Arntz
656 views46 slides
The WordPress Performance Team by
The WordPress Performance TeamThe WordPress Performance Team
The WordPress Performance TeamFelix Arntz
563 views11 slides
Accessing APIs using OAuth on the federated (WordPress) web by
Accessing APIs using OAuth on the federated (WordPress) webAccessing APIs using OAuth on the federated (WordPress) web
Accessing APIs using OAuth on the federated (WordPress) webFelix Arntz
251 views31 slides
Leveraging the Power of Custom Elements in Gutenberg by
Leveraging the Power of Custom Elements in GutenbergLeveraging the Power of Custom Elements in Gutenberg
Leveraging the Power of Custom Elements in GutenbergFelix Arntz
2.1K views35 slides
Introduction to Web Components by
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web ComponentsFelix Arntz
167 views40 slides

More from Felix Arntz(7)

Tackling performance in the WordPress ecosystem at scale by Felix Arntz
Tackling performance in the WordPress ecosystem at scaleTackling performance in the WordPress ecosystem at scale
Tackling performance in the WordPress ecosystem at scale
Felix Arntz82 views
Enhancing performance in an open-source CMS ecosystem by Felix Arntz
Enhancing performance in an open-source CMS ecosystemEnhancing performance in an open-source CMS ecosystem
Enhancing performance in an open-source CMS ecosystem
Felix Arntz656 views
The WordPress Performance Team by Felix Arntz
The WordPress Performance TeamThe WordPress Performance Team
The WordPress Performance Team
Felix Arntz563 views
Accessing APIs using OAuth on the federated (WordPress) web by Felix Arntz
Accessing APIs using OAuth on the federated (WordPress) webAccessing APIs using OAuth on the federated (WordPress) web
Accessing APIs using OAuth on the federated (WordPress) web
Felix Arntz251 views
Leveraging the Power of Custom Elements in Gutenberg by Felix Arntz
Leveraging the Power of Custom Elements in GutenbergLeveraging the Power of Custom Elements in Gutenberg
Leveraging the Power of Custom Elements in Gutenberg
Felix Arntz2.1K views
Introduction to Web Components by Felix Arntz
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web Components
Felix Arntz167 views
Web Policies & Reporting by Felix Arntz
Web Policies & ReportingWeb Policies & Reporting
Web Policies & Reporting
Felix Arntz146 views

Recently uploaded

The Dark Web : Hidden Services by
The Dark Web : Hidden ServicesThe Dark Web : Hidden Services
The Dark Web : Hidden ServicesAnshu Singh
22 views24 slides
ARNAB12.pdf by
ARNAB12.pdfARNAB12.pdf
ARNAB12.pdfArnabChakraborty499766
5 views83 slides
hamro digital logics.pptx by
hamro digital logics.pptxhamro digital logics.pptx
hamro digital logics.pptxtupeshghimire
11 views36 slides
Affiliate Marketing by
Affiliate MarketingAffiliate Marketing
Affiliate MarketingNavin Dhanuka
21 views30 slides
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx by
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptxCracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptxLeasedLinesQuote
5 views8 slides
cis5-Project-11a-Harry Lai by
cis5-Project-11a-Harry Laicis5-Project-11a-Harry Lai
cis5-Project-11a-Harry Laiharrylai126
9 views11 slides

Recently uploaded(13)

The Dark Web : Hidden Services by Anshu Singh
The Dark Web : Hidden ServicesThe Dark Web : Hidden Services
The Dark Web : Hidden Services
Anshu Singh22 views
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx by LeasedLinesQuote
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptxCracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx
cis5-Project-11a-Harry Lai by harrylai126
cis5-Project-11a-Harry Laicis5-Project-11a-Harry Lai
cis5-Project-11a-Harry Lai
harrylai1269 views
40th TWNIC Open Policy Meeting: APNIC PDP update by APNIC
40th TWNIC Open Policy Meeting: APNIC PDP update40th TWNIC Open Policy Meeting: APNIC PDP update
40th TWNIC Open Policy Meeting: APNIC PDP update
APNIC106 views
WITS Deck by W.I.T.S.
WITS DeckWITS Deck
WITS Deck
W.I.T.S.36 views
40th TWNIC Open Policy Meeting: A quick look at QUIC by APNIC
40th TWNIC Open Policy Meeting: A quick look at QUIC40th TWNIC Open Policy Meeting: A quick look at QUIC
40th TWNIC Open Policy Meeting: A quick look at QUIC
APNIC109 views
40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download by APNIC
40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download
40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download
APNIC112 views
ATPMOUSE_융합2조.pptx by kts120898
ATPMOUSE_융합2조.pptxATPMOUSE_융합2조.pptx
ATPMOUSE_융합2조.pptx
kts12089835 views
Penetration Testing for Cybersecurity Professionals by 211 Check
Penetration Testing for Cybersecurity ProfessionalsPenetration Testing for Cybersecurity Professionals
Penetration Testing for Cybersecurity Professionals
211 Check49 views

AMP for JavaScripters

  • 1. AMP for JavaScripters Implementing Interactive Interfaces with AMP Weston Ruter & Felix Arntz | #JSforWPConf
  • 2. Agenda The AMP Framework AMP Components AMP Actions and Events 01 02 03 AMP State and Bindings AMP Script 04 05
  • 4. AMP is a web component framework to easily create user-first websites.
  • 5. AMP is an HTML Framework
  • 6. Interactivity in AMP Developers using AMP AMP Framework Technologies
  • 7. • AMP is an open source project. • AMP has an open governance model. • AMP is built on the open web. Democratizing Performance
  • 8. A Minimal AMP Document <!DOCTYPE html> <html amp> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <link rel="canonical" href="self.html"> <style amp-boilerplate>…</style> <noscript><style amp-boilerplate>…</style></noscript> <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body> Hello world. </body> </html>
  • 11. Types of Elements Used in AMP Built-in HTML Tags Optimized Replacements Custom Components <p> <amp-img> <amp-accordion>
  • 12. Optimized Replacements <img> → <amp-img> / <amp-anim> <iframe> → <amp-iframe> <audio> → <amp-audio> <video> → <amp-video>
  • 14. Component Example <amp-carousel> <amp-carousel type="carousel" ...> <a class="project-highlight" href="..."> <amp-img src="..." ...></amp-img> </a> <!-- ... --> </amp-carousel>
  • 15. Component Example <amp-image-slider> <amp-image-slider width="300" height="200" layout="responsive"> <amp-img src="https://upload.wikimedia.org/.../Whole_world_-_land_and_oceans.jpg" alt="Earth at Day" layout="fill"></amp-img> <amp-img src="https://upload.wikimedia.org/.../City_Lights_2012_-_Flat_map_crop.jpg" alt="Earth at night" layout="fill"></amp-img> </amp-image-slider>
  • 16. Component Example <amp-image-lightbox> <amp-image-lightbox id="lightbox1" layout="nodisplay"> </amp-image-lightbox> <!-- ... --> <figure> <amp-img on="tap:lightbox1.open" role="button" tabindex="0" src="..." alt="Picture of a dog" width="300" height="246"></amp-img> <figcaption>Border Collie.</figcaption> </figure> <!-- ... -->
  • 18. Mobile Menu: amp-sidebar <button id="toggleNavMenu" on="tap:navMenu.toggle" aria-controls="navMenu"> Toggle Menu </button> <amp-sidebar id="navMenu" layout="nodisplay" side="left"> <button id="closeNavMenu" on="tap:navMenu.close"> Close Menu </button> <nav> <ul> <li><a href="...">...</a></li> <!-- ... --> </ul> </nav> </amp-sidebar>
  • 19. AMP Actions and Events
  • 20. Component API AMP Events AMP Actions Triggered by the user. Triggered by an AMP event. Abstraction of native DOM events. Similar to methods on DOM elements. on="{event}:{elementId}.{action}[({arg}={value}...)],{event}:{elementId}…" <button id="toggleNavMenu" on="tap:navMenu.toggle" aria-controls="navMenu"> Toggle Menu </button>
  • 21. Global Events and Actions Events ● tap Actions ● show ● hide ● toggleVisibility ● toggleClass ● scrollTo ● focus amp.dev/documentation/guides-and-tutorials/learn/amp-actions-and-events #globally-defined-events-and-actions
  • 22. Element-Specific Events and Actions Events ● change (<input>) ● submit (<form>) ● slideChange (<amp-carousel>) ● sidebarOpen (<amp-sidebar>) ● ... Actions ● submit (<form>) ● play (<amp-audio>) ● goToSlide (<amp-carousel>) ● open (<amp-sidebar>) ● ... amp.dev/documentation/guides-and-tutorials/learn/amp-actions-and-events #element-specific-events
  • 23. Mobile Menu: toggleVisibility <button id="toggleNavMenu" on="tap:navMenu.toggleVisibility" ...> Toggle Menu </button> <nav id="navMenu" hidden> <ul> <li><a href="...">...</a></li> <!-- ... --> </ul> </nav>
  • 24. AMP State and Bindings
  • 25. • State • Expressions • Bindings amp-bind
  • 26. <amp-state id="count"> <script type="application/json">0</script> </amp-state> Count: <output [text]="count">0</output> <button on="tap:AMP.setState( { count: count + 1 } )">Increment</button> Example: Click Counter
  • 27. <amp-state id="cart"> <script type="application/json"> { "quantity": 0 } </script> </amp-state> Quantity: <input [value]="cart.quantity" type="number" value="0" readonly> <button on="tap:AMP.setState( { cart:{ quantity: cart.quantity + 1 } } )"> More </button> <button on="tap:AMP.setState( { cart:{ quantity: max( 0, cart.quantity - 1 ) } } )" disabled [disabled]="cart.quantity == 0"> Less </button> Example: Shopping Cart Quantity
  • 28. <amp-state id="navExpanded"> <script type="application/json">true</script> </amp-state> ... <amp-state id="cart"> <script type="application/json">{"quantity":2}</script> </amp-state> ... <amp-state id="skus"> <script type="application/json">["123", "456", "789"]</script> </amp-state> Multiple amp-state elements
  • 29. Mobile Menu: amp-bind <amp-state id="navMenuExpanded"> <script type="application/json">false</script> </amp-state> <button id="toggleNavMenu" on="tap:AMP.setState({ navMenuExpanded: ! navMenuExpanded })" aria-controls="navMenu" aria-expanded="false" [aria-expanded]="navMenuExpanded ? 'true' : 'false'" [text]="navMenuExpanded ? 'Close Menu' : 'Open Menu'" >Open Menu</button> <nav id="navMenu" hidden [hidden]="!navMenuExpanded"> <ul> <li><a href="...">...</a></li> <!-- ... --> </ul> </nav>
  • 33. • Script runs in Web Worker sandbox • WorkerDOM library exposes DOM APIs • Built for modern frameworks (e.g. React, Vue) amp-script
  • 34. Mobile Menu via amp-script <amp-script src="https://example.com/.../nav-menu-via-amp-script.js"> <button id="toggleNavMenu" aria-controls="navMenu" aria-expanded="false" data-open-text="Open Menu" data-close-text="Close Menu" >Open Menu</button> <nav id="navMenu" class="hidden"> <ul> <li><a href="...">...</a></li> <!-- ... --> </ul> </nav> </amp-script> const button = document.getElementById( 'toggleNavMenu' ); const navMenu = document.getElementById( 'navMenu' ); button.addEventListener( 'click', () => { const hidden = ! navMenu.classList.contains( 'hidden' ); navMenu.classList.toggle( 'hidden', hidden ); button.setAttribute( 'aria-expanded', hidden ? 'false' : 'true' ); button.textContent = hidden ? button.getAttribute( 'data-open-text' ) : button.getAttribute( 'data-close-text' ); } );
  • 35. • Escape hatch • Limit of 150KB • Script limited to DOM in scope of container • Not all DOM APIs are supported (yet) amp-script: Restrictions
  • 36. Canvas Drawing via amp-script <amp-script src="canvas-drawing-with-amp-script.js" width="400" height="400"> <button>Start drawing!</button> </amp-script>
  • 37. Password checker via amp-script amp.dev/documentation/guides-and-tutorials/develop/ custom-javascript-tutorial