From 62b28514513d9a385e9e07aa443b87fc42568b5f Mon Sep 17 00:00:00 2001 From: Keshav Varadachari Date: Tue, 7 Sep 2021 12:58:06 -0400 Subject: [PATCH] docs(amp-accordion): add readme docs for 1.0 amp-accordion --- docs/spec/amp-html-responsive-attributes.md | 18 +- docs/spec/bento-common-props.md | 27 + extensions/amp-accordion/1.0/README.md | 632 ++++++++++++++++++ .../amp-accordion/1.0/storybook/Basic.amp.js | 2 +- 4 files changed, 669 insertions(+), 10 deletions(-) create mode 100644 docs/spec/bento-common-props.md create mode 100644 extensions/amp-accordion/1.0/README.md diff --git a/docs/spec/amp-html-responsive-attributes.md b/docs/spec/amp-html-responsive-attributes.md index fcf4684f022c..0498196b4d66 100644 --- a/docs/spec/amp-html-responsive-attributes.md +++ b/docs/spec/amp-html-responsive-attributes.md @@ -43,12 +43,12 @@ The following `amp-base-carousel` will show `3` slides at a time when width is g You may apply responsive attribute values to the following components: -- [amp-accordion](./../extensions/amp-accordion/amp-accordion.md#animate) -- [amp-base-carousel](./../extensions/amp-base-carousel/amp-base-carousel.md#media-queries) -- [amp-fit-text](./../extensions/amp-fit-text/amp-fit-text.md#media-queries) -- [amp-inline-gallery-pagination](./../extensions/amp-inline-gallery/amp-inline-gallery.md#include-pagination-indicators) -- [amp-inline-gallery-thumbnails](./../extensions/amp-inline-gallery/amp-inline-gallery.md#include-pagination-thumbnails) -- [amp-lightbox](./../extensions/amp-lightbox/amp-lightbox.md#animation) -- [amp-selector](./../extensions/amp-selector/amp-selector.md#keyboard-select-mode) -- [amp-stream-gallery](./../extensions/amp-stream-gallery/amp-stream-gallery.md#media-queries) -- [amp-video](./../extensions/amp-video/amp-video.md#rotate-to-fullscreen) +- [amp-accordion](./../../extensions/amp-accordion/amp-accordion.md#animate) +- [amp-base-carousel](./../../extensions/amp-base-carousel/amp-base-carousel.md#media-queries) +- [amp-fit-text](./../../extensions/amp-fit-text/amp-fit-text.md#media-queries) +- [amp-inline-gallery-pagination](./../../extensions/amp-inline-gallery/amp-inline-gallery.md#include-pagination-indicators) +- [amp-inline-gallery-thumbnails](./../../extensions/amp-inline-gallery/amp-inline-gallery.md#include-pagination-thumbnails) +- [amp-lightbox](./../../extensions/amp-lightbox/amp-lightbox.md#animation) +- [amp-selector](./../../extensions/amp-selector/amp-selector.md#keyboard-select-mode) +- [amp-stream-gallery](./../../extensions/amp-stream-gallery/amp-stream-gallery.md#media-queries) +- [amp-video](./../../extensions/amp-video/amp-video.md#rotate-to-fullscreen) diff --git a/docs/spec/bento-common-props.md b/docs/spec/bento-common-props.md new file mode 100644 index 000000000000..6488333a2018 --- /dev/null +++ b/docs/spec/bento-common-props.md @@ -0,0 +1,27 @@ +# Common Props in Preact/React Bento Components + +The following are common props that should be be supported in al Bento Preact/React Bento Components when possible. + +## `as` + +Allows consumer to customize the html tag that is used to implement a component. + +## `class` (Preact only) + +Allows consumer to add class(es) to the given component. + +## `className` (React only) + +Allows consumer to add class(es) to the given component. + +## `id` + +Allows consumer to set the `id` attribute on the given component. + +## `role` + +Allows consumer to set the `role` attribute on the given component to control accessibility characteristics. + +## `tabIndex` + +Allows consumer to set the `tabIndex` attribute on the given component to control tab order. diff --git a/extensions/amp-accordion/1.0/README.md b/extensions/amp-accordion/1.0/README.md new file mode 100644 index 000000000000..d4b2a15f78c2 --- /dev/null +++ b/extensions/amp-accordion/1.0/README.md @@ -0,0 +1,632 @@ +--- +$category@: layout +formats: + - websites +teaser: + text: A stacked list of headers that collapse or expand content sections with user interaction. +experimental: true +bento: true +--- + +# Bento Accordion + +## Usage + +The Bento Accordion component allows you to display collapsible and expandable +content sections. This component provides a way for viewers to glance at the +content outline and jump to any section. Effective use reduces scrolling needs +on mobile devices. + +Use Bento Accordion as a web component ([``](#web-component)), or a Preact/React functional component ([``](#preact/react-component)). + +- A Bento Accordion accepts one or more `
` elements as its direct + children. +- Each `
` must contain exactly two direct children. +- The first child in a `
` is the heading for that section of the + Bento Accordion. It must be a heading element such as `

-

` or + `
`. +- The second child in a `
` is the expandable/collapsible content. + - It can be any tag allowed in [AMP HTML](https://github.com/ampproject/amphtml/blob/main/docs/spec/amp-html-format.md). +- A click or tap on a `
` heading expands or collapses the section. +- A Bento Accordion with a defined `id` preserves the collapsed or expanded + state of each section while the user remains on your domain. + +### Web Component + +You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. Or use the light-weight pre-upgrade styles available inline. See [Layout and style](#layout-and-style). + +The examples below demonstrate use of the `` web component. + +#### Example: Import via npm + +[example preview="top-frame" playground="false"] + +Install via npm: + +```sh +npm install @ampproject/bento-accordion +``` + +```javascript +import '@ampproject/bento-accordion'; +``` + +[/example] + +#### Example: Include via ` + + + + +
+

Section 1

+

Content in section 1.

+
+
+

Section 2

+
Content in section 2.
+
+
+

Section 3

+
Content in section 3.
+
+
+ + +``` + +[/example] + +#### Interactivity and API usage + +Bento enabled components in standalone use are highly interactive through their API. The `bento-accordion` component API is accessible by including the following script tag in your document: + +```javascript +await customElements.whenDefined('bento-accordion'); +const api = await accordion.getApi(); +``` + +##### Actions + +**toggle()** +The `toggle` action switches the `expanded` and `collapsed` states of +`bento-accordion` sections. When called with no arguments, it toggles all sections +of the accordion. To specify a specific section, add the `section` argument and +use its corresponding `id` as the value. + +```html + +
+

Section 1

+

Bunch of awesome content

+
+
+

Section 2

+
Bunch of awesome content
+
+
+

Section 3

+
Bunch of awesome content
+
+
+ + + +``` + +**expand()** +The `expand` action expands the sections of the `bento-accordion`. If a section +is already expanded, it stays expanded. When called with no arguments, it +expands all sections of the accordion. To specify a section, add the `section` argument, and use its corresponding `id` as the value. + +```html + + + +``` + +**collapse()** +The `collapse` action collapses the sections of the `bento-accordion`. If a +section is already collapsed, it stays collapsed. When called with no arguments, +it collapses all sections of the accordion. To specify a section, add the +`section` argument, and use its corresponding `id` as the value. + +```html + + + +``` + +##### Events + +The `bento-accordion` API allows you to register and respond to the following events: + +**expand** +This event is triggered when an accordion section is expanded and is dispatched from the expanded section. + +See below for example. + +**collapse** +This event is triggered when an accordion section is collapsed and is dispatched from the collapsed section. + +In the example below, `section 1` listens for the `expand` event and expands `section 2` when it is expanded. `section 2` listens for the `collapse` event and collapses `section 1` when it is collapsed. + +See below for example. + +```html + +
+

Section 1

+
Puppies are cute.
+
+
+

Section 2

+
Kittens are furry.
+
+
+ + +``` + +#### Layout and style + +Each Bento component has a small CSS library you must include to guarantee proper loading without [content shifts](https://web.dev/cls/). Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles. + +```html + +``` + +Alternatively, you may also make the light-weight pre-upgrade styles available inline: + +```html + +``` + +#### Attributes + +##### animate + +Include the `animate` attribute in `` to add a "roll down" +animation when the content is expanded and "roll up" animation when collapsed. + +This attribute can be configured to based on a [media query](./../../../docs/spec/amp-html-responsive-attributes.md). + +[example preview="top-frame" playground="true" imports="bento-accordion:1.0"] + +```html + +
+

Section 1

+

Content in section 1.

+
+
+

Section 2

+
Content in section 2.
+
+
+

Section 3

+
Content in section 2.
+
+
+``` + +[/example] + +##### expanded + +Apply the `expanded` attribute to a nested `
` to expand that section when the page loads. + +[example preview="top-frame" playground="true" imports="bento-accordion:1.0"] + +```html + +
+

Section 1

+

Bunch of awesome content

+
+
+

Section 2

+
Bunch of awesome content
+
+
+

Section 3

+
Bunch of awesome expanded content
+
+
+``` + +[/example] + +##### expand-single-section + +Allow only one section to expand at a time by applying the `expand-single-section` attribute to the `` element. This means if a user taps on a collapsed `
`, it will expand and collapse other expanded `
`'s. + +[example preview="top-frame" playground="true" imports="bento-accordion:1.0"] + +```html + +
+

Section 1

+

Content in section 1.

+
+
+

Section 2

+
Content in section 2.
+
+
+

Section 3

+ +
+
+``` + +[/example] + +#### Styling + +You may use the `bento-accordion` element selector to style the accordion +freely. + +Keep the following points in mind when you style an amp-accordion: + +- `bento-accordion` elements are always `display: block`. +- `float` cannot style a `
`, heading, nor content elements. +- An expanded section applies the `expanded` attribute to the `
` + element. +- The content element is clear-fixed with `overflow: hidden` and hence cannot + have scrollbars. +- Margins of the ``, `
`, heading, and content elements + are set to `0`, but can be overridden in custom styles. +- Both the header and content elements are `position: relative`. + +### Preact/React Component + +The examples below demonstrates use of the `` as a functional component usable with the Preact or React libraries. + +#### Example: Import via npm + +[example preview="top-frame" playground="false"] + +Install via npm: + +```sh +npm install @ampproject/bento-accordion +``` + +```javascript +import React from 'react'; +import { BentoAccordion } from '@ampproject/bento-accordion/react'; +import '@ampproject/bento-accordion/styles.css'; + +function App() { + return ( + + +

Section 1

+ Content 1 +
+ + +

Section 2

+ Content 2 +
+ + +

Section 3

+ Content 3 +
+
+ ); +} +``` + +[/example] + +#### Interactivity and API usage + +Bento components are highly interactive through their API. The `BentoAccordion` component API is accessible by passing a `ref`: + +```javascript +import React, {createRef} from 'react'; +const ref = createRef(); + +function App() { + return ( + + +

Section 1

+ Content 1 +
+ + +

Section 2

+ Content 2 +
+ + +

Section 3

+ Content 3 +
+
+ ); +} +``` + +##### Actions + +The `BentoAccordion` API allows you to perform the following actions: + +**toggle()** +The `toggle` action switches the `expanded` and `collapsed` states of +`bento-accordion` sections. When called with no arguments, it toggles all sections +of the accordion. To specify a specific section, add the `section` argument and +use its corresponding `id` as the value. + +```javascript +ref.current.toggle(); +ref.current.toggle('section1'); +``` + +**expand()** +The `expand` action expands the sections of the `bento-accordion`. If a section +is already expanded, it stays expanded. When called with no arguments, it +expands all sections of the accordion. To specify a section, add the `section` argument, and use its corresponding `id` as the value. + +```javascript +ref.current.expand(); +ref.current.expand('section1'); +``` + +**collapse()** +The `collapse` action collapses the sections of the `bento-accordion`. If a +section is already collapsed, it stays collapsed. When called with no arguments, +it collapses all sections of the accordion. To specify a section, add the +`section` argument, and use its corresponding `id` as the value. + +```javascript +ref.current.collapse(); +ref.current.collapse('section1'); +``` + +##### Events + +The Bento Accordion API allows you to respond to the following events: + +**onExpandStateChange** + +This event is triggered on a section when an accordion section is expanded or collpased and is dispatched from the expanded section. + +See below for example. + +**onCollapse** + +This event is triggered on a section when an accordion section is collapsed and is dispatched from the collapsed section. + +In the example below, `section 1` listens for the `expand` event and expands `section 2` when it is expanded. `section 2` listens for the `collapse` event and collapses `section 1` when it is collapsed. + +See below for example. + +```jsx + + expanded ? alert('section1 expanded') : alert('section1 collapsed') + }> +

Section 1

+ Content 1 +
+ + expanded ? alert('section2 expanded') : alert('section2 collapsed'} + }> +

Section 2

+ Content 2 +
+ + expanded ? alert('section3 expanded') : alert('section3 collapsed'} + }> +

Section 3

+ Content 3 +
+
+``` + +#### Layout and style + +**Container type** + +The `BentoAccordion` component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children via a desired CSS layout (such as one defined with `height`, `width`, `aspect-ratio`, or other such properties). These can be applied inline: + +```jsx + + ... + +``` + +Or via `className`: + +```jsx + + ... + +``` + +```css +.custom-styles { + background-color: red; +} +``` + +#### Props + +##### BentoAccordion + +###### animate + +If true, then uses "roll-down" / "roll-up" animation during the expansion and collapse of each section +Default: `false` + +###### expandSingleSection + +If true, then expanding 1 section will automatically collapse all other sections: +Default: `false` + +##### BentoAccordionSection + +###### animate + +If true, then uses "roll-down" / "roll-up" animation during the expansion and collapse the section +Default: `false` + +###### expanded + +If true, expands the section. +Default: `false` + +###### onExpandStateChange + +```typescript +(expanded: boolean): void +``` + +Callback to listen for expand state changes. Takes a boolean flag as parameter indicating whether the section was just expanded (`false` indicates it was collapsed) + +##### BentoAccordionHeader + +##### Common props + +This component supports the [common props](../../../docs/spec/bento-common-props.md) for React and Preact components. + +BentoAccordionHeader does not yet support any custom props + +##### BentoAccordionContent + +##### Common props + +This component supports the [common props](../../../docs/spec/bento-common-props.md) for React and Preact components. + +BentoAccordionContent does not yet support any custom props + +#### Styling + +You may use the `BentoAccordion` element selector to style the carousel freely. + +## Version notes diff --git a/extensions/amp-accordion/1.0/storybook/Basic.amp.js b/extensions/amp-accordion/1.0/storybook/Basic.amp.js index 49989f7ee979..55bb4f446b64 100644 --- a/extensions/amp-accordion/1.0/storybook/Basic.amp.js +++ b/extensions/amp-accordion/1.0/storybook/Basic.amp.js @@ -18,7 +18,7 @@ export default { export const _default = (args) => { return (
- +

Section 1

Puppies are cute.