Skip to content
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

Do not destroy rendered contents on addon tab changes #157

Merged
merged 1 commit into from
Jun 18, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @jsx jsx */
import { FC, ReactNode, useEffect, useState } from "react";
import { DependencyList, FC, ReactNode, useEffect, useState } from "react";
import { jsx } from "@storybook/theming";

import { Tabs as SbTabs } from "@storybook/components";
Expand All @@ -13,14 +13,20 @@ export interface Tab {

export interface TabsProps {
tabs: readonly Tab[];

/**
* Effect trigger for tab initialization. Everytime this dependency list is
* updated, the selection goes to the first tab.
*/
deps?: DependencyList;
}

export const Tabs: FC<TabsProps> = ({ tabs }) => {
export const Tabs: FC<TabsProps> = ({ tabs, deps = [] }) => {
const [selected, setSelected] = useState(tabs[0].id);

useEffect(() => {
setSelected(tabs[0].id);
}, [tabs]);
}, deps);

return (
<SbTabs absolute selected={selected} actions={{ onSelect: setSelected }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const Wrapper: FC<Props> = ({ config }) => {
return <div>{tabs[0].content}</div>;
}

return <Tabs tabs={tabs} />;
return <Tabs tabs={tabs} deps={[config]} />;
};

export default Wrapper;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @jsx jsx */
import { FC } from "react";
import { FC, useEffect, useState } from "react";
import { jsx } from "@storybook/theming";
import { useParameter, useStorybookState } from "@storybook/api";

Expand All @@ -13,12 +13,33 @@ interface Props {
}

export const Wrapper: FC<Props> = ({ active }) => {
if (!active) {
const state = useStorybookState();
const config = useParameter(ParameterName) as Config;

// Whether the addon panel has been visible to a user with the config.
const [onceRevealed, setOnceRevealed] = useState(active);

// When a user navigates to another story or updates a story parameter,
// sync `onceRevealed` to `active`.
useEffect(() => {
setOnceRevealed(active);
}, [config]);

// When a user enters the "Design" tab, mark the addon panel "once revealed".
useEffect(() => {
if (active) {
setOnceRevealed(true);
}
}, [active]);

// If a user has not entered the "Design" tab for the current story yet,
// do not render panel contents. This makes loading embeds explicit and
// prevents embedded sites from obtaining viewport (iframe) size behind,
// which often leads to incorrect measurements.
if (!onceRevealed) {
return null;
}

const state = useStorybookState();
const config = useParameter(ParameterName) as Config;
return <Pure key={state.storyId} config={config} />;
};

Expand Down
29 changes: 22 additions & 7 deletions packages/storybook-addon-designs/src/register/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jsx jsx */
import addons, { types, Addon } from "@storybook/addons";
import addons, { types } from "@storybook/addons";
import { useParameter } from "@storybook/api";
import { AddonPanel } from "@storybook/components";
import { jsx } from "@storybook/theming";

import { AddonName, PanelName, ParameterName } from "../addon";
Expand Down Expand Up @@ -30,21 +31,35 @@ export default function register(renderTarget: "panel" | "tab") {
return (param.name || DEFAULT_TAB_NAME) + " (1)";
};

const render: Addon["render"] = ({ active, key }) => (
<Wrapper key={key} active={!!active} />
);

if (renderTarget === "tab") {
addons.add(PanelName, {
title: DEFAULT_TAB_NAME,
render,
render({ active, key }) {
if (!active) {
// NOTE: Return type of render is `ReactElement`, hence returning `null` causes
// type error. I'm using `<noscript>` in place of `null`.
return <noscript key={key} />;
}

return <Wrapper key={key} active />;
},
type: types.TAB,
paramKey: ParameterName,
route: ({ storyId }) => `/design/${storyId}`,
match: ({ viewMode }) => viewMode === "design",
});
} else {
addons.addPanel(PanelName, { title, paramKey: ParameterName, render });
addons.addPanel(PanelName, {
title,
paramKey: ParameterName,
render({ active, key }) {
return (
<AddonPanel key={key} active={!!active}>
<Wrapper active={!!active} />
</AddonPanel>
);
},
});
}
});
}