Skip to content

Commit

Permalink
Update titles for addons, controls, and panel tabs to support render …
Browse files Browse the repository at this point in the history
…functions and JSX elements
  • Loading branch information
valentinpalkovic committed Mar 17, 2023
1 parent 49b53d7 commit ef0a7fb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
33 changes: 19 additions & 14 deletions code/addons/actions/src/manager.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import React, { useState, useEffect } from 'react';
import type { API } from '@storybook/manager-api';
import { addons, types } from '@storybook/manager-api';
import { STORY_CHANGED } from '@storybook/core-events';
import ActionLogger from './containers/ActionLogger';
import { ADDON_ID, EVENT_ID, PANEL_ID, PARAM_KEY } from './constants';

function Title({ api }: { api: API }) {
const [actionsCount, setActionsCount] = useState(0);
const onEvent = () => setActionsCount((previous) => previous + 1);
const onChange = () => setActionsCount(0);

useEffect(() => {
api.on(EVENT_ID, onEvent);
api.on(STORY_CHANGED, onChange);
return () => {
api.off(EVENT_ID, onEvent);
api.off(STORY_CHANGED, onChange);
};
});
const suffix = actionsCount === 0 ? '' : ` (${actionsCount})`;
return <>Actions{suffix}</>;
}

addons.register(ADDON_ID, (api) => {
addons.addPanel(PANEL_ID, {
title() {
const [actionsCount, setActionsCount] = useState(0);
const onEvent = () => setActionsCount((previous) => previous + 1);
const onChange = () => setActionsCount(0);

useEffect(() => {
api.on(EVENT_ID, onEvent);
api.on(STORY_CHANGED, onChange);
return () => {
api.off(EVENT_ID, onEvent);
api.off(STORY_CHANGED, onChange);
};
});
const suffix = actionsCount === 0 ? '' : ` (${actionsCount})`;
return `Actions${suffix}`;
return <Title api={api} />;
},
type: types.PANEL,
render: ({ active, key }) => <ActionLogger key={key} api={api} active={!!active} />,
Expand Down
17 changes: 11 additions & 6 deletions code/addons/controls/src/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { AddonPanel } from '@storybook/components';
import { ControlsPanel } from './ControlsPanel';
import { ADDON_ID, PARAM_KEY } from './constants';

function Title() {
const rows = useArgTypes();
const controlsCount = Object.values(rows).filter(
(argType) => argType?.control && !argType?.table?.disable
).length;
const suffix = controlsCount === 0 ? '' : ` (${controlsCount})`;

return <>Controls{suffix}</>;
}

addons.register(ADDON_ID, (api: API) => {
addons.addPanel(ADDON_ID, {
title() {
const rows = useArgTypes();
const controlsCount = Object.values(rows).filter(
(argType) => argType?.control && !argType?.table?.disable
).length;
const suffix = controlsCount === 0 ? '' : ` (${controlsCount})`;
return `Controls${suffix}`;
return <Title />;
},
type: types.PANEL,
paramKey: PARAM_KEY,
Expand Down
8 changes: 7 additions & 1 deletion code/lib/types/src/modules/addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,14 @@ export interface Addon_RenderOptions {
key?: string;
}

export type ReactJSXElement = {
type: any;
props: any;
key: any;
};

export interface Addon_Type {
title: (() => string) | string;
title: (() => string | ReactJSXElement) | string;
type?: Addon_Types;
id?: string;
route?: (routeOptions: RouterData) => string;
Expand Down
10 changes: 9 additions & 1 deletion code/ui/components/src/tabs/tabs.helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ export const VisuallyHidden = styled.div<VisuallyHiddenProps>(({ active }) =>
active ? { display: 'block' } : { display: 'none' }
);

type TChild = {
active: boolean;
title: (() => string | ReactElement) | string;
id: any;
color: any;
render: any;
};

export const childrenToList = (children: any, selected: string) =>
Children.toArray(children).map(
({ props: { title, id, color, children: childrenOfChild } }: ReactElement, index) => {
({ props: { title, id, color, children: childrenOfChild } }: ReactElement, index): TChild => {
const content = Array.isArray(childrenOfChild) ? childrenOfChild[0] : childrenOfChild;
return {
active: selected ? id === selected : index === 0,
Expand Down
8 changes: 5 additions & 3 deletions code/ui/components/src/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ export const Tabs: FC<TabsProps> = memo(
<Wrapper absolute={absolute} bordered={bordered} id={htmlId}>
<FlexBar scrollable={false} border backgroundColor={backgroundColor}>
<TabBar style={{ whiteSpace: 'normal' }} ref={tabBarRef} role="tablist">
{visibleList.map(({ title, id, active, color }) => {
{visibleList.map(({ title, id, active, color }, index) => {
const indexId = `index-${index}`;

return (
<TabButton
id={`tabbutton-${sanitize(title)}`}
id={`tabbutton-${sanitize(id) ?? indexId}`}
ref={(ref: HTMLButtonElement) => {
tabRefs.current.set(id, ref);
}}
Expand All @@ -164,7 +166,7 @@ export const Tabs: FC<TabsProps> = memo(
}}
role="tab"
>
{title}
{typeof title === 'string' ? title : title()}
</TabButton>
);
})}
Expand Down
4 changes: 2 additions & 2 deletions code/ui/manager/src/components/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { shortcutToHumanString } from '@storybook/manager-api';
import useMediaQuery from '../hooks/useMedia';

export interface SafeTabProps {
title: (() => string) | string;
title: (() => string | ReactElement) | string;
id: string;
children: ReactElement;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ const AddonPanel = React.memo<{
id="storybook-panel-root"
>
{Object.entries(panels).map(([k, v]) => (
<SafeTab key={k} id={k} title={typeof v.title === 'function' ? v.title() : v.title}>
<SafeTab key={k} id={k} title={v.title}>
{v.render}
</SafeTab>
))}
Expand Down

0 comments on commit ef0a7fb

Please sign in to comment.