amp-sidebar
Introduction
The amp-sidebar
component provides a way to display meta content intended for temporary access (navigation links, buttons, menus, etc.). The sidebar can be revealed by a button tap while the main content remains visually underneath. However, optional attributes that accept media queries can be used to display meta content in other parts of the site. Child <nav toolbar="(media query)" toolbar-target="elementID">
will create toolbar
elements that allow for content within the sidebar to be displayed on other parts of the main content.
You must be able to resize your browser window in order to notice the effects of toolbar
elements. In this example, toolbar
elements are best viewed by resiving your window from mobile to desktop.
Setup
Import the amp-sidebar
component.
<script async custom-element="amp-sidebar" src="https://cdn.ampproject.org/v0/amp-sidebar-0.1.js"></script>
Basic usage
The amp-sidebar
should be a direct child of the <body>
. It must have a layout of nodisplay
. amp-sidebar
supports the following actions: toggle
, open
and close
.
<amp-sidebar id="sidebar" class="sample-sidebar" layout="nodisplay" side="right"> <h3>Sidebar</h3> <button on="tap:sidebar.close">Close sidebar</button> <button on="tap:sidebar.toggle">Toggle sidebar</button> </amp-sidebar> <button on="tap:sidebar.toggle">Toggle sidebar</button> <button on="tap:sidebar.open">Open sidebar</button>
Toolbar
toolbar
enables elements within the amp-sidebar
to be displayed on other parts of the <body>
. This is useful for responsive design, including navigation bars, social footers, etc.
toolbar
elements have their own requirements:
- The sidebar may implement toolbars by adding
nav
elements with thetoolbar
attribute andtoolbar-target
attribute. - The
nav
element must be a child of<amp-sidebar>
and follow this format:<nav toolbar="(media-query)" toolbar-target="elementID">
. - The
nav
containing the toolbar attribute must only contain a single<ul>
element, that contains<li>
elements. - The
<li>
elements may contain any valid HTML elements (supported by AMP), or any of the AMP elements that<amp-sidebar>
supports. - Toolbar behavior is only applied while the
toolbar
attribute media-query is valid. Also, an element with thetoolbar-target
attribute id must exist on the page for the toolbar to be applied.
<amp-sidebar id="sidebar-left" class="sample-sidebar" layout="nodisplay" side="left"> <h3>Sidebar</h3> <button on="tap:sidebar-left.close">Close sidebar</button> <nav toolbar="(min-width: 784px)" toolbar-target="target-element-left"> <ul> <li>Nav item 1</li> <li>Nav item 2</li> </ul> </nav> <ul> <li>Nav item 3</li> <li>Nav item 4</li> </ul> </amp-sidebar> <button on="tap:sidebar-left.toggle">Toggle sidebar</button> <div id="target-element-left"> </div>
Styled toolbar
The toolbar
element within the <amp-sidebar>
element, will have classes applied to the element depending if the toolbar-target
element is shown or hidden. This is useful for applying different styles on the toolbar
element and the toolbar-target
element. The classes are amp-sidebar-toolbar-target-shown
, and amp-sidebar-toolbar-target-hidden
. The class amp-sidebar-toolbar-target-shown
is applied to the toolbar
element when the toolbar-target
element is shown. The class amp-sidebar-toolbar-target-hidden
is applied to the toolbar
element when the toolbar-target
element is hidden.
.amp-sidebar-toolbar-target-shown { display: none; }
<amp-sidebar id="sidebar-right" class="sample-sidebar" layout="nodisplay" side="right"> <h3>Sidebar</h3> <button on="tap:sidebar-right.close">Close sidebar</button> <nav toolbar="(min-width: 784px)" toolbar-target="target-element-right"> <ul> <li>Nav item 1</li> <li>Nav item 2</li> </ul> </nav> <ul> <li>Nav item 3</li> <li>Nav item 4</li> </ul> </amp-sidebar> <button on="tap:sidebar-right.toggle">Toggle sidebar</button> <div id="target-element-right"> </div>
If the explanations on this page don't cover all of your questions feel free to reach out to other AMP users to discuss your exact use case.
Go to Stack Overflow An unexplained feature?The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.
Edit sample on GitHub-
Written by @juliantoledo