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.
toolbar
elements. In this example, toolbar
elements are best viewed by resizing 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>
Desktop Sidebar
The amp-sidebar can be used as a permanent navigation bar on the main page. The toolbar target (aside element) is defined as a left hand navigation element on the page.
<main> <aside id="target-element-desktop" class="desktop-sidebar">... </aside> </main>
Using a media query, the toolbar target can be made visible only when the browser window is greater than a certain width. Using CSS flex on the parent element of the target allows us to position the target on the left side of the page.
@media (min-width: 784px) { main { display: flex; flex-direction: row; } }
The amp-sidebar uses the nav toolbar target to populate it's content into this navigation element. It identifies the nav toolbar target by matching the id
attribute (defined in the aside element above) with the toolbar-target
attribute within the nav
element.
<amp-sidebar id="sidebar-desktop" class="desktop-sidebar" layout="nodisplay" side="left">
<h1>Desktop Sidebar</h1>
<button on="tap:sidebar-desktop.close">Close sidebar</button>
<nav toolbar="(min-width: 784px)" toolbar-target="target-element-desktop">
<ul>
<li>Nav item A</li>
<li>Nav item B</li>
<li>Nav item C</li>
<li>Nav item D</li>
</ul>
</nav>
</amp-sidebar>
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