AMP
  • email

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>

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>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기