Skip to content

Commit

Permalink
Merge pull request #217 from storybookjs/feature/blocks-collapse-event
Browse files Browse the repository at this point in the history
Add `onCollapsedChange` prop for Doc Block
  • Loading branch information
yannbf authored Dec 1, 2023
2 parents 8ff94a0 + 4e50696 commit 90597f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/examples/stories/internal/blocks/0.common.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ Also, you can customize the text shown when the block is collapsed with `placeho
placeholder="Foo"
/>

You can listen to changes to collapsed state via `onCollapsedChange` event callback (see browser console for working demo).

<Source
language="jsx"
code={`<IFrame
url="https://www.wikipedia.org"
height={100}
onCollapsedChange={(newValue, oldValue) => {
console.log("onCollapsedChange", { newValue, oldValue });
}}
/>`}
/>

<IFrame
url="https://www.wikipedia.org"
height={100}
onCollapsedChange={(newValue, oldValue) => {
console.log("onCollapsedChange", { newValue, oldValue });
}}
/>

## Open in new tab

By default, Design Doc Blocks show an `Open in new tab` to allow users to open the embed content in a new tab.
Expand Down
18 changes: 17 additions & 1 deletion packages/storybook-addon-designs/src/blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ export interface BlocksCommonProps {
* @default true
*/
showLink?: boolean;

/**
* **Doc Block Props**
*
* Will be called when a user changed collapse/expand state of the block.
*/
onCollapsedChange?(newValue: boolean, oldValue: boolean): void;
}

export const DocBlockBase: FC<BlocksCommonProps> = ({
Expand All @@ -118,6 +125,7 @@ export const DocBlockBase: FC<BlocksCommonProps> = ({
defaultCollapsed = false,
placeholder,
showLink = true,
onCollapsedChange,
...rest
}) => {
const [collapsed, setCollapsed] = useState(!!defaultCollapsed);
Expand All @@ -136,7 +144,15 @@ export const DocBlockBase: FC<BlocksCommonProps> = ({
actionItems={[
collapsable && {
title: collapsed ? "Show" : "Hide",
onClick: () => setCollapsed((v) => !v),
onClick: () => {
const newValue = !collapsed;

if (onCollapsedChange) {
onCollapsedChange(newValue, collapsed);
}

setCollapsed(newValue);
},
},
showOpenInNewTab && {
title: "Open in new tab",
Expand Down

0 comments on commit 90597f0

Please sign in to comment.