From 73b9c7b1b69b6a6a2ef7854fe2824e16ca625052 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Sat, 21 Jan 2023 22:25:11 +0100 Subject: [PATCH] use new Canvas API in autodocs --- .../preview-web/docs-context/DocsContext.ts | 2 +- code/ui/blocks/src/blocks/Canvas.tsx | 4 +-- code/ui/blocks/src/blocks/DocsStory.tsx | 35 +++++++------------ code/ui/blocks/src/blocks/Primary.tsx | 4 ++- code/ui/blocks/src/blocks/Stories.tsx | 8 ++--- code/ui/blocks/src/blocks/types.ts | 12 +++---- 6 files changed, 27 insertions(+), 38 deletions(-) diff --git a/code/lib/preview-api/src/modules/preview-web/docs-context/DocsContext.ts b/code/lib/preview-api/src/modules/preview-web/docs-context/DocsContext.ts index 02799288b71..3542e0a1919 100644 --- a/code/lib/preview-api/src/modules/preview-web/docs-context/DocsContext.ts +++ b/code/lib/preview-api/src/modules/preview-web/docs-context/DocsContext.ts @@ -48,7 +48,7 @@ export class DocsContext implements DocsContextProps }); } - // This docs entry references this CSF file and can syncronously load the stories, as well + // This docs entry references this CSF file and can synchronously load the stories, as well // as reference them by module export. If the CSF is part of the "component" stories, they // can also be referenced by name and are in the componentStories list. referenceCSFFile(csfFile: CSFFile) { diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 7e84314f3e8..2d20349fe0c 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -32,11 +32,11 @@ type DeprecatedCanvasProps = Omit< }; type CanvasProps = Pick & { - of: ModuleExport; + of?: ModuleExport; meta?: ModuleExports; sourceState?: 'hidden' | 'shown' | 'none'; source?: Omit; - story?: Pick; + story?: Pick; layout?: 'padded' | 'centered' | 'fullscreen'; }; diff --git a/code/ui/blocks/src/blocks/DocsStory.tsx b/code/ui/blocks/src/blocks/DocsStory.tsx index efdf5e51081..fd0e970b62f 100644 --- a/code/ui/blocks/src/blocks/DocsStory.tsx +++ b/code/ui/blocks/src/blocks/DocsStory.tsx @@ -1,41 +1,32 @@ import type { FC } from 'react'; import React from 'react'; +import type { PreparedStory } from '@storybook/types'; import { Subheading } from './Subheading'; import type { DocsStoryProps } from './types'; import { Anchor } from './Anchor'; import { Description } from './Description'; -import { Story } from './Story'; import { Canvas } from './Canvas'; +import { useOf } from './useOf'; export const DocsStory: FC = ({ - id, - name, + of, expanded = true, withToolbar = false, - parameters = {}, __forceInitialArgs = false, __primary = false, }) => { - let description; - const { docs } = parameters; - if (expanded && docs) { - description = docs.description?.story; - } - - const subheading = expanded && name; + const { story } = useOf(of, ['story']) as { type: 'story'; story: PreparedStory }; + const description = story.parameters?.docs?.description?.story; return ( - - {subheading && {subheading}} - {description && } - - - + + {expanded && ( + <> + {story.name} + {description !== undefined && } + + )} + ); }; diff --git a/code/ui/blocks/src/blocks/Primary.tsx b/code/ui/blocks/src/blocks/Primary.tsx index 8095d079323..7d5f84d06bb 100644 --- a/code/ui/blocks/src/blocks/Primary.tsx +++ b/code/ui/blocks/src/blocks/Primary.tsx @@ -12,5 +12,7 @@ export const Primary: FC = ({ name }) => { const docsContext = useContext(DocsContext); const storyId = name && docsContext.storyIdByName(name); const story = docsContext.storyById(storyId); - return story ? : null; + return story ? ( + + ) : null; }; diff --git a/code/ui/blocks/src/blocks/Stories.tsx b/code/ui/blocks/src/blocks/Stories.tsx index 76cb3a6a1e0..4accadb42ee 100644 --- a/code/ui/blocks/src/blocks/Stories.tsx +++ b/code/ui/blocks/src/blocks/Stories.tsx @@ -3,7 +3,6 @@ import React, { useContext } from 'react'; import { DocsContext } from './DocsContext'; import { DocsStory } from './DocsStory'; import { Heading } from './Heading'; -import type { DocsStoryProps } from './types'; interface StoriesProps { title?: JSX.Element | string; @@ -13,8 +12,8 @@ interface StoriesProps { export const Stories: FC = ({ title, includePrimary = true }) => { const { componentStories } = useContext(DocsContext); - let stories: DocsStoryProps[] = componentStories(); - stories = stories.filter((story) => !story.parameters?.docs?.disable); + let stories = componentStories().filter((story) => !story.parameters?.docs?.disable); + if (!includePrimary) stories = stories.slice(1); if (!stories || stories.length === 0) { @@ -24,7 +23,8 @@ export const Stories: FC = ({ title, includePrimary = true }) => { <> {title} {stories.map( - (story) => story && + (story) => + story && )} ); diff --git a/code/ui/blocks/src/blocks/types.ts b/code/ui/blocks/src/blocks/types.ts index 5aab18554b8..8a8dba5bfbc 100644 --- a/code/ui/blocks/src/blocks/types.ts +++ b/code/ui/blocks/src/blocks/types.ts @@ -1,17 +1,13 @@ +import type { ModuleExport } from '@storybook/types'; + export const PRIMARY_STORY = '^'; // TODO export type Component = any; -export interface StoryData { - id?: string; - kind?: string; - name?: string; - parameters?: any; -} - -export type DocsStoryProps = StoryData & { +export type DocsStoryProps = { + of: ModuleExport; expanded?: boolean; withToolbar?: boolean; __forceInitialArgs?: boolean;