AMP Conf 2020: Save the date!
AMP
  • websites

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 the toolbar attribute and toolbar-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 the toolbar-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>
需要进一步说明?

如果此页面的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。

前往 Stack Overflow
一项无法解释的功能?

AMP 项目强烈鼓励您参与其中并作出贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您对热衷问题做出单次贡献。

在 GitHub 上编辑实例