Skip to content

docs: migrate Storybook to v9 #7466

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ packages/main/tmp
packages/*/src/generated/

.nx

debug-storybook.log
2 changes: 1 addition & 1 deletion .storybook/components/ArgTypesWithNote.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgTypes } from '@storybook/blocks';
import { ArgTypes } from '@storybook/addon-docs/blocks';
import MessageStripDesign from '@ui5/webcomponents/dist/types/MessageStripDesign.js';
import { MessageStrip } from '@ui5/webcomponents-react';
import type { ComponentProps, ReactNode } from 'react';
Expand Down
2 changes: 1 addition & 1 deletion .storybook/components/CommandsAndQueries.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Heading, Markdown } from '@storybook/blocks';
import { Heading, Markdown } from '@storybook/addon-docs/blocks';
import { Tag as WCRTag } from '@ui5/webcomponents-react';
import dedent from 'dedent';
import { Fragment } from 'react';
Expand Down Expand Up @@ -88,11 +88,11 @@
}

interface Members {
global: any[];

Check warning on line 91 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
inner: any[];

Check warning on line 92 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
instance: any[];

Check warning on line 93 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
events: any[];

Check warning on line 94 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
static: any[];

Check warning on line 95 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}

interface FunctionMetadata {
Expand All @@ -100,14 +100,14 @@
tags: Tag[];
loc: Loc;
context: Context;
augments: any[];

Check warning on line 103 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
examples: Example[];
implements: any[];

Check warning on line 105 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
params: Param[];
properties: any[];

Check warning on line 107 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
returns: Return[];
sees: any[];

Check warning on line 109 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
throws: any[];

Check warning on line 110 in .storybook/components/CommandsAndQueries.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
todos: any[];
yields: any[];
name: string;
Expand Down
4 changes: 2 additions & 2 deletions .storybook/components/ControlsWithNote.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controls } from '@storybook/blocks';
import { Controls } from '@storybook/addon-docs/blocks';
import MessageStripDesign from '@ui5/webcomponents/dist/types/MessageStripDesign.js';
import { MessageStrip } from '@ui5/webcomponents-react';
import type { ComponentProps, ReactNode } from 'react';
Expand Down Expand Up @@ -26,7 +26,7 @@ export function ControlsWithNote(props: ComponentProps<typeof Controls> & Contro
</MessageStrip>
)}
<Controls {...rest} />
<DomRefTable />
<DomRefTable of={props.of} />
</div>
);
}
35 changes: 15 additions & 20 deletions .storybook/components/DocsHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Description, DocsContext, Subtitle, Title } from '@storybook/addon-docs';
import type { Controls } from '@storybook/addon-docs/blocks';
import { Description, Subtitle, Title, useOf } from '@storybook/addon-docs/blocks';
import ButtonDesign from '@ui5/webcomponents/dist/types/ButtonDesign.js';
import MessageStripDesign from '@ui5/webcomponents/dist/types/MessageStripDesign.js';
import copyIcon from '@ui5/webcomponents-icons/dist/copy.js';
Expand All @@ -13,21 +14,14 @@ import {
ThemeProvider,
} from '@ui5/webcomponents-react';
import { clsx } from 'clsx';
import { useContext } from 'react';
import type { ComponentProps } from 'react';
import { useGetSubComponentsOfModule } from '../utils';
import classes from './DocsHeader.module.css';
import { GitHubLogo } from './GitHub-Mark';
import { Import } from './Import';
import { TableOfContent } from './TableOfContent';

const Links = () => {
const docsContext = useContext(DocsContext);
const isChart = docsContext.componentStories().at(0).id.startsWith('charts-');

// const filePath = docsContext.parameters.fileName.replace(/^\.\//, '');
// const folderPath = filePath.substr(0, filePath.lastIndexOf('/'));

// const githubUrl = `https://github.com/SAP/ui5-webcomponents-react/tree/main/${folderPath}`;
const Links = ({ isChart }: { isChart?: boolean }) => {
const githubUrl = `https://github.com/SAP/ui5-webcomponents-react`;

const packageName = `@ui5/webcomponents-react${isChart ? '-charts' : ''}`;
Expand All @@ -50,14 +44,15 @@ interface InfoTableProps {
mergeSubComponents?: boolean;
isChart?: boolean;
experimental?: boolean;
of: ComponentProps<typeof Controls>['of'];
}

export const InfoTable = ({ since, subComponents, mergeSubComponents }: InfoTableProps) => {
const context = useContext(DocsContext);
const groups = context.componentStories().at(0).kind.split('/');
const moduleName = groups[groups.length - 1].replace('(experimental)', '').trim();
export const InfoTable = ({ of, since, subComponents, mergeSubComponents }: InfoTableProps) => {
const context = useOf<'meta'>(of);
const { csfFile, preparedMeta } = context;
const moduleName = csfFile.meta.component.displayName;

const wcSubComponents = useGetSubComponentsOfModule(moduleName.replace('V2', ''));
const wcSubComponents = useGetSubComponentsOfModule(moduleName.replace('V2', ''), preparedMeta.tags);
const subComps = mergeSubComponents
? [...(subComponents ?? []), ...(wcSubComponents ?? [])]
: (subComponents ?? wcSubComponents);
Expand All @@ -81,7 +76,7 @@ export const InfoTable = ({ since, subComponents, mergeSubComponents }: InfoTabl
<Label>Usage</Label>
</th>
<td data-import-cell={supportsClipboardApi}>
<Import />
<Import moduleNames={[moduleName]} componentId={preparedMeta.componentId} />
{supportsClipboardApi && (
<Button
design={ButtonDesign.Transparent}
Expand Down Expand Up @@ -110,7 +105,7 @@ export const InfoTable = ({ since, subComponents, mergeSubComponents }: InfoTabl
<Label>Subcomponents</Label>
</th>
<td data-import-cell={supportsClipboardApi}>
<Import moduleNames={subComps} />
<Import moduleNames={subComps} componentId={preparedMeta.componentId} />
{supportsClipboardApi && (
<Button
design={ButtonDesign.Transparent}
Expand All @@ -129,17 +124,17 @@ export const InfoTable = ({ since, subComponents, mergeSubComponents }: InfoTabl
);
};

export const DocsHeader = ({ since, subComponents, mergeSubComponents, isChart, experimental }: InfoTableProps) => {
export const DocsHeader = ({ of, since, subComponents, mergeSubComponents, isChart, experimental }: InfoTableProps) => {
return (
<ThemeProvider>
<FlexBox alignItems={FlexBoxAlignItems.Center} className={classes.titleRow}>
<Title />
{experimental && <Label className={classes.experimentalLabel}>experimental</Label>}
<span style={{ flexGrow: 1 }} />
<Links />
<Links isChart={isChart} />
</FlexBox>
<Subtitle />
<InfoTable since={since} subComponents={subComponents} mergeSubComponents={mergeSubComponents} />
<InfoTable of={of} since={since} subComponents={subComponents} mergeSubComponents={mergeSubComponents} />
<TableOfContent />
<Description />
{isChart && (
Expand Down
22 changes: 11 additions & 11 deletions .storybook/components/DomRefTable.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { DocsContext, Heading } from '@storybook/blocks';
import type { Controls } from '@storybook/addon-docs/blocks';
import { Heading, useOf } from '@storybook/addon-docs/blocks';
import TagDesign from '@ui5/webcomponents/dist/types/TagDesign.js';
import { Tag, Link, MessageStrip, Popover } from '@ui5/webcomponents-react';
import type * as CEM from '@ui5/webcomponents-tools/lib/cem/types';
import type { ReactNode } from 'react';
import { Fragment, useContext, useRef } from 'react';
import type { ComponentProps, ReactNode } from 'react';
import { Fragment, useRef } from 'react';
import { createPortal } from 'react-dom';
import { useGetCem } from '../utils';
import { useGetCem } from '../utils.js';
import classes from './DomRefTable.module.css';

export function CodeBlock(props: { children: ReactNode }) {
Expand Down Expand Up @@ -47,16 +48,15 @@ function Name(props: CEM.ClassMember) {
);
}

export function DomRefTable() {
const docsContext = useContext(DocsContext);
const storyTags: string[] = docsContext.attachedCSFFile?.meta?.tags;
export function DomRefTable({ of }: { of: ComponentProps<typeof Controls>['of'] }) {
const { story: storyContext } = useOf<'story'>(of);
const storyTags: string[] = storyContext.tags;
const cemModuleName = storyTags?.find((tag) => tag.startsWith('cem-module:'));
const componentName = docsContext.componentStories().at(0)?.component?.displayName;
const componentName = storyContext.component.displayName;
const popoverRef = useRef(null);

const knownAttributes = new Set(Object.keys(docsContext.primaryStory?.argTypes ?? {}));
const cem = useGetCem();

const knownAttributes = new Set(Object.keys(storyContext.argTypes));
const cem = useGetCem(storyTags);
const moduleName = cemModuleName ? cemModuleName.split(':')[1] : componentName;

const componentMembers =
Expand Down
15 changes: 6 additions & 9 deletions .storybook/components/Import.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DocsContext } from '@storybook/addon-docs';
import { Fragment, useContext } from 'react';
import { Fragment } from 'react';

interface ImportStatementPropTypes {
/**
Expand Down Expand Up @@ -119,16 +118,14 @@ interface ImportProps {
/**
* Names of module/component (e.g. "Button")
*/
moduleNames?: ImportStatementPropTypes['moduleNames'];
moduleNames: ImportStatementPropTypes['moduleNames'];
componentId: string;
}

export const Import = (props: ImportProps) => {
const context = useContext(DocsContext);
const isChart = context.componentStories().at(0).id.startsWith('charts-');
const isCompat = context.componentStories().at(0).id.startsWith('legacy-');
const groups = context.componentStories().at(0).kind.split('/');
const module = groups[groups.length - 1].replace('(experimental)', '').trim();
const moduleNames = props.moduleNames ?? [module];
const { componentId, moduleNames } = props;
const isChart = componentId.startsWith('charts-');
const isCompat = componentId.startsWith('legacy-');

return (
<ImportStatement
Expand Down
23 changes: 20 additions & 3 deletions .storybook/components/TableOfContent.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import * as tocbot from 'tocbot';
import classes from './ToC.module.css';

export function TableOfContent({
headingSelector = 'h2:not(.noAnchor), h3:not(.noAnchor), h4:not(.noAnchor)',
onlyDisplaySideNav = false,
tocOverflowHeight = 1080,
}: {
headingSelector?: string;
onlyDisplaySideNav?: boolean;
tocOverflowHeight?: number;
}) {
const refCallback = useCallback(
(node: HTMLDivElement) => {
if (node) {
if (
window.matchMedia('(min-width:1330px)').matches &&
window.matchMedia(`(min-height:${tocOverflowHeight}px)`).matches
) {
node.style.overflowY = 'hidden';
} else {
node.style.overflowY = undefined;
}
}
},
[tocOverflowHeight],
);

useEffect(() => {
tocbot.init({
tocSelector: '.js-toc',
Expand All @@ -25,13 +43,12 @@ export function TableOfContent({
tocbot.destroy();
};
}, [headingSelector]);

return (
<>
<h3 className={`${classes.header} noAnchor`} data-show-small={!onlyDisplaySideNav}>
Contents
</h3>
<div className={classes.fixedContainer} data-show-small={!onlyDisplaySideNav}>
<div ref={refCallback} className={classes.fixedContainer} data-show-small={!onlyDisplaySideNav}>
<div className={`js-toc ${classes.toc}`} id="toc-container" />
</div>
</>
Expand Down
16 changes: 8 additions & 8 deletions .storybook/components/ToC.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
.toc :global(.toc-link) {
font-family: var(--sapFontFamily) !important;
font-size: var(--sapFontSize) !important;
-webkit-font-smoothing: auto;
line-height: normal;
}

Expand Down Expand Up @@ -75,13 +74,14 @@
.fixedContainer {
width: 200px;
position: fixed;
top: 75px;
top: 38px;
right: 6px;
overflow-x: hidden;
overflow-y: auto;
}

.toc :global(.toc-list) {
margin-block-start: 0;
padding-inline-start: 1rem;
}

Expand All @@ -93,13 +93,13 @@
width: 4px;
}

.toc :global(.toc-list-item) {
/*overflow: hidden;*/
/*text-overflow: ellipsis;*/
/*white-space: nowrap;*/
}

.toc :global(.is-active-link)::before {
background-color: #0a6ed1;
}
}

@media (min-width: 1650px) {
.fixedContainer {
width: 250px;
}
}
4 changes: 2 additions & 2 deletions .storybook/components/VersionSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable react-hooks/rules-of-hooks */

import { IconButton } from '@storybook/components';
import { addons, types } from '@storybook/manager-api';
import '@ui5/webcomponents/dist/Menu.js';
import '@ui5/webcomponents/dist/MenuItem.js';
import * as React from 'react';
import { IconButton } from 'storybook/internal/components';
import { addons, types } from 'storybook/manager-api';

const ADDON_ID = 'version-switch';
const TOOL_ID = `${ADDON_ID}/toolbar`;
Expand Down
Loading