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

Add expandStoryTree config option #432

Closed
Closed
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
6 changes: 6 additions & 0 deletions .changeset/rare-walls-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ladle/react": minor
"test-config": minor
---

add expandStoryTree config option
1 change: 1 addition & 0 deletions e2e/config/.ladle/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export default {
viteConfig: "ladle-vite.config.js",
appendToHead: `<style>.append {color: green}</style>`,
storyOrder: () => ["specific*", "Hello*"],
expandStoryTree: true,
};
7 changes: 7 additions & 0 deletions e2e/config/tests/expanded-tree.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test, expect } from "@playwright/test";

test("story tree is expanded", async ({ page }) => {
await page.goto("/");

await expect(page.getByRole("treeitem")).toHaveCount(5);
});
4 changes: 3 additions & 1 deletion e2e/config/tests/hello.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ test("navigation respects storyOrder from the .ladle/config.mjs", async ({
page,
}) => {
await page.goto("/");
await expect(page.locator("nav")).toHaveText("Specific fileCustomHello");
await expect(page.locator("nav")).toHaveText(
"Specific fileCustomHelloStylesWorld",
);
});
5 changes: 4 additions & 1 deletion packages/ladle/lib/app/src/sidebar/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from "react";
import TreeView from "./tree-view";
import config from "../get-config";
import type { UpdateStory } from "../../../shared/types";

const Main = ({
Expand Down Expand Up @@ -41,6 +42,8 @@ const Main = ({
story.includes(canonicalSearch),
);

console.log("config", config);

return (
<nav role="navigation" className="ladle-aside">
<input
Expand All @@ -62,7 +65,7 @@ const Main = ({
stories={filteredStories}
story={story}
updateStory={updateStory}
searchActive={search !== ""}
allExpanded={search !== "" || config.expandStoryTree}
setTreeRootRef={(root: HTMLUListElement | null) =>
(treeRoot.current = root)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/ladle/lib/app/src/sidebar/tree-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const TreeView = ({
stories,
story,
updateStory,
searchActive,
allExpanded,
searchRef,
setTreeRootRef,
}: {
Expand All @@ -34,14 +34,14 @@ const TreeView = ({
searchRef: React.Ref<HTMLLinkElement>;
updateStory: UpdateStory;
setTreeRootRef: (root: HTMLUListElement | null) => void;
searchActive?: boolean;
allExpanded?: boolean;
}) => {
const treeItemRefs: TreeItemRefs = React.useRef({});
const [tree, setTree] = React.useState(
getStoryTree(stories, story, searchActive),
getStoryTree(stories, story, allExpanded),
);
React.useEffect(() => {
setTree(getStoryTree(stories, story, searchActive));
setTree(getStoryTree(stories, story, allExpanded));
}, [stories.join(",")]);

const [selectedItemId, setSelectedItemId] = React.useState<string | null>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const ladleConfigToClientConfig = (config) => {
: config.storyOrder.toString(),
}
: {}),
...(config.expandStoryTree
? { expandStoryTree: config.expandStoryTree }
: {}),
};
};

Expand Down
1 change: 1 addition & 0 deletions packages/ladle/lib/shared/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
previewPort: 8080,
outDir: "build",
base: undefined,
expandStoryTree: false,
onDevServerStart: () => {
return;
},
Expand Down
1 change: 1 addition & 0 deletions packages/ladle/lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export type Config = {
outDir: string;
base?: string;
mode?: string;
expandStoryTree?: boolean;
onDevServerStart: (serverUrl: string) => void;
i18n: { [key: string]: string };
addons: {
Expand Down
Loading