-
Notifications
You must be signed in to change notification settings - Fork 23
feat(documentation): migrate collapsible to storybook v7 + lit #1656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
73a4f2d
feat(documentation): migrate collapsible to storybook v7 + lit
1ac7514
fix constants BADGE origin
18d6772
Remove unneeded import
imagoiq 3b9d895
Use mdx markdown flavored codeblock
imagoiq 47bef2f
Various fixes
imagoiq 33cda47
Remove unneeded exclude
imagoiq d8e0590
Fix headingLevel by using config for web-components with storybook-ad…
imagoiq cda255a
Add innerHTML to controls
imagoiq 5dbbf1a
Fix lead
imagoiq 7935c33
Set docs only in navigation
imagoiq 3fa1f5d
Remove hide-col-default
imagoiq 2027178
Move badge to docs
imagoiq 4a6d9e6
Address comments
imagoiq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/documentation-v7/src/stories/components/collapsible/collapsible.demo.stories.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { spread } from '@open-wc/lit-helpers'; | ||
| import { useArgs } from '@storybook/preview-api'; | ||
| import { Meta, StoryContext, StoryObj } from '@storybook/web-components'; | ||
| import { html } from 'lit'; | ||
| import { unsafeHTML } from 'lit/directives/unsafe-html.js'; | ||
|
|
||
| import { definedProperties } from '../../../utils'; | ||
|
|
||
| const meta: Meta<HTMLPostCollapsibleElement> = { | ||
| title: 'Hidden/demos/components/Collapsible', | ||
| component: 'post-collapsible', | ||
| args: { | ||
| innerHTML: `<span slot='header'>Titulum</span><p>Contentus momentus vero siteos et accusam iretea et justo.</p>`, | ||
| }, | ||
| argTypes: { | ||
| innerHTML: { | ||
| description: | ||
| 'Defines the HTML markup contained in the collapsible.<br/>' + | ||
| 'Elements with a `slot="header"` attribute are displayed in the header while others are shown in the body.', | ||
| table: { | ||
| category: 'content', | ||
| type: { | ||
| summary: 'string', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| render: (args, context) => defaultRender(args, context), | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<HTMLPostCollapsibleElement>; | ||
|
|
||
| function defaultRender( | ||
| args: HTMLPostCollapsibleElement, | ||
| context: StoryContext<HTMLPostCollapsibleElement>, | ||
| ) { | ||
| const hasHeader = args.innerHTML.indexOf('slot="header"') > -1; | ||
| const collapsibleId = `collapsible-example--${context.name.replace(/ /g, '-').toLowerCase()}`; | ||
|
|
||
| const collapsibleProperties = definedProperties({ | ||
| 'collapsed': args.collapsed, | ||
| 'heading-level': args.headingLevel, | ||
| 'id': hasHeader ? undefined : collapsibleId, | ||
| }); | ||
|
|
||
| const collapsibleComponent = html` | ||
| <post-collapsible ${spread(collapsibleProperties)}> | ||
| ${unsafeHTML(args.innerHTML)} | ||
| </post-collapsible> | ||
| `; | ||
|
|
||
| const [currentArgs, updateArgs] = useArgs(); | ||
|
|
||
| const toggleCollapse = (open?: boolean) => { | ||
| const collapsible = document.querySelector(`#${collapsibleId}`) as HTMLPostCollapsibleElement; | ||
| collapsible.toggle(open).then((isOpen: boolean) => { | ||
| if (typeof currentArgs.collapsed !== 'undefined') updateArgs({ collapsed: !isOpen }); | ||
| }); | ||
| }; | ||
|
|
||
| const togglers = [ | ||
| ['Toggle', () => toggleCollapse()], | ||
| ['Show', () => toggleCollapse(true)], | ||
| ['Hide', () => toggleCollapse(false)], | ||
| ]; | ||
|
|
||
| const togglersHtml = html` | ||
| <div class="d-flex gap-mini mb-regular"> | ||
| ${togglers.map( | ||
| ([label, listener]) => | ||
| html` | ||
| <button | ||
| aria-controls="${collapsibleId}" | ||
| aria-expanded="${!args.collapsed}" | ||
| class="btn btn-secondary" | ||
| @click="${listener}" | ||
| > | ||
| ${label} | ||
| </button> | ||
| `, | ||
| )} | ||
| </div> | ||
| `; | ||
|
|
||
| return html` | ||
| ${hasHeader ? null : togglersHtml} ${collapsibleComponent} | ||
| `; | ||
| } | ||
|
|
||
| export const Default: Story = {}; | ||
|
|
||
| export const InitiallyCollapsed: Story = { | ||
| args: { collapsed: true }, | ||
| }; | ||
|
|
||
| export const HeadingLevel: Story = { | ||
| args: { headingLevel: 6 }, | ||
| }; | ||
|
|
||
| export const IntricateContent: Story = { | ||
| args: { | ||
| innerHTML: `<p>I am part of the body</p> | ||
| <span slot='header'>Customus<em> Titulum</em></span> | ||
| <small slot='header' class='text-muted'> - I am part of the header</small> | ||
| <p>I am part of the body too!</p>`, | ||
| }, | ||
| }; | ||
|
|
||
| export const CustomTrigger: Story = { | ||
| args: { | ||
| innerHTML: `<p class='border rounded p-large'>Contentus momentus vero siteos et accusam iretea et justo.</p>`, | ||
| }, | ||
| }; |
61 changes: 61 additions & 0 deletions
61
packages/documentation-v7/src/stories/components/collapsible/collapsible.docs.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { Canvas, Controls, Meta } from '@storybook/blocks'; | ||
| import { BADGE } from '../../../../.storybook/constants'; | ||
| import * as CollapsibleStories from './collapsible.demo.stories'; | ||
|
|
||
| <Meta title="Components/Collapsible" parameters={{ badges: [BADGE.BETA, BADGE.NEEDS_REVISION] }} /> | ||
|
|
||
| # Collapsible | ||
|
|
||
| <p className="lead">Toggle the visibility of content across your project.</p> | ||
|
|
||
| The `<post-collapsible>` component is used to show and hide content. | ||
| Collapsing an element will animate the height from its current value to 0. | ||
|
|
||
| <Canvas of={CollapsibleStories.Default} /> | ||
| <Controls of={CollapsibleStories.Default} /> | ||
|
|
||
| ## Examples | ||
|
|
||
| The following examples show different use cases for the `<post-collapsible>` component. | ||
|
|
||
| ### Initially Collapsed | ||
|
|
||
| To make the collapsible content hidden by default, just use the `collapsible="true"` property. | ||
|
|
||
| <Canvas of={CollapsibleStories.InitiallyCollapsed} /> | ||
|
|
||
| ### Heading Level | ||
|
|
||
| Use the `heading-level` property to define the hierarchical level of the collapsible header within the structure of a document. | ||
|
|
||
| <Canvas of={CollapsibleStories.HeadingLevel} /> | ||
|
|
||
| ### Intricate Content | ||
|
|
||
| The collapsible component uses [HTML slot elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot) | ||
| to allow any HTML content to be used as part of its header and body.<br/> | ||
| By default, all children are appended to the component's body. | ||
| For a specific child to be displayed in the header, a `slot="header"` attribute must be added. | ||
|
|
||
| <Canvas of={CollapsibleStories.IntricateContent} /> | ||
|
|
||
| ### Custom Trigger | ||
|
|
||
| The `<post-collapsible>` component exposes a `.toggle()` method that allows to trigger the collapse programmatically. | ||
| This method is asynchronous and returns a promise that resolves with the current open state. | ||
| It optionally takes a boolean parameter that forces open when `true` or close when `false`. | ||
|
|
||
| ```typescript | ||
| const collapsible = document.querySelector('#collapsibleId') as HTMLPostCollapsibleElement; | ||
| collapsible.toggle(/* open: boolean */).then((/* isOpen: boolean */) => {}); | ||
| ``` | ||
|
|
||
| A header is then not mandatory and can be replaced by an external control. | ||
|
|
||
| In this case, to ensure good accessibility, identify the collapsible with an `id`, | ||
| then add an `aria-controls` attribute to your control element referencing this `id`. | ||
| Also make sure to add an `aria-expanded` attribute to the control element: | ||
| if the collapsible element is closed, the attribute on the control element must have a value of `aria-expanded="false"` | ||
| and `aria-expanded="true"` otherwise. | ||
|
|
||
| <Canvas of={CollapsibleStories.CustomTrigger} /> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.