Skip to content

Commit 1178c37

Browse files
authored
feat: add expandStoryTree config flag (#587)
1 parent cf54d02 commit 1178c37

File tree

11 files changed

+43
-10
lines changed

11 files changed

+43
-10
lines changed

.changeset/gold-students-doubt.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"website": minor
3+
"@ladle/react": minor
4+
"test-config": minor
5+
---
6+
7+
add expandStoryTree config option

e2e/config/.ladle/config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export default {
33
viteConfig: "ladle-vite.config.js",
44
appendToHead: `<style>.append {color: green}</style>`,
55
storyOrder: () => ["specific*", "Hello*"],
6+
expandStoryTree: true,
67
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test("story tree is expanded", async ({ page }) => {
4+
await page.goto("/");
5+
6+
await expect(page.getByRole("treeitem")).toHaveCount(5);
7+
});

e2e/config/tests/hello.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ test("navigation respects storyOrder from the .ladle/config.mjs", async ({
99
page,
1010
}) => {
1111
await page.goto("/");
12-
await expect(page.locator("nav")).toHaveText("Specific fileCustomHello");
12+
await expect(page.locator("nav")).toHaveText(
13+
"Specific fileCustomHelloStylesWorld",
14+
);
1315
});

packages/example/.ladle/config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export default {
1818
enabled: true,
1919
},
2020
},
21+
expandStoryTree: true,
2122
};

packages/ladle/lib/app/src/sidebar/main.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const Main = ({
146146
story={story}
147147
hotkeys={hotkeys}
148148
updateStory={updateStory}
149-
searchActive={search !== ""}
149+
allExpanded={search !== "" || config.expandStoryTree}
150150
setTreeRootRef={(root: HTMLUListElement | null) =>
151151
(treeRoot.current = root)
152152
}

packages/ladle/lib/app/src/sidebar/tree-view.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const TreeView = ({
2929
stories,
3030
story,
3131
updateStory,
32-
searchActive,
32+
allExpanded,
3333
searchRef,
3434
setTreeRootRef,
3535
hotkeys,
@@ -39,15 +39,15 @@ const TreeView = ({
3939
searchRef: React.Ref<HTMLInputElement>;
4040
updateStory: UpdateStory;
4141
setTreeRootRef: (root: HTMLUListElement | null) => void;
42-
searchActive?: boolean;
42+
allExpanded?: boolean;
4343
hotkeys: boolean;
4444
}) => {
4545
const treeItemRefs: TreeItemRefs = React.useRef({});
4646
const [tree, setTree] = React.useState(
47-
getStoryTree(stories, story, searchActive),
47+
getStoryTree(stories, story, allExpanded),
4848
);
4949
React.useEffect(() => {
50-
setTree(getStoryTree(stories, story, searchActive));
50+
setTree(getStoryTree(stories, story, allExpanded));
5151
}, [stories.join(",")]);
5252

5353
const [selectedItemId, setSelectedItemId] = React.useState<string | null>(
@@ -64,7 +64,7 @@ const TreeView = ({
6464
const hotkeyStoryTransition = (story?: string) => {
6565
if (story) {
6666
updateStory(story);
67-
setTree(getStoryTree(stories, story, searchActive));
67+
setTree(getStoryTree(stories, story, allExpanded));
6868
setTimeout(() => focusSelectedItem(story), 1);
6969
}
7070
};

packages/ladle/lib/cli/vite-plugin/generate/get-config-import.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const ladleConfigToClientConfig = (config) => {
2222
: config.storyOrder.toString(),
2323
}
2424
: {}),
25+
...(config.expandStoryTree
26+
? { expandStoryTree: config.expandStoryTree }
27+
: {}),
2528
};
2629
};
2730

@@ -37,12 +40,11 @@ const getConfigImport = async (configFolder, config) => {
3740
let clientConfig = {};
3841
if (fs.existsSync(configPath)) {
3942
const fileConfig = (await import(pathToFileURL(configPath).href)).default;
40-
// @ts-ignore
43+
// @ts-expect-error: exclude hotkeys
4144
clientConfig = ladleConfigToClientConfig(fileConfig);
4245
}
4346
merge(clientConfig, ladleConfigToClientConfig(config));
44-
// don't merge hotkeys
45-
// @ts-ignore
47+
// @ts-expect-error: don't merge hotkeys
4648
clientConfig.hotkeys = {
4749
...clientConfig.hotkeys,
4850
...config.hotkeys,

packages/ladle/lib/shared/default-config.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default {
1515
hmrPort: undefined,
1616
outDir: "build",
1717
base: undefined,
18+
expandStoryTree: false,
1819
hotkeys: {
1920
search: ["/", "meta+p"],
2021
nextStory: ["alt+arrowright"],

packages/ladle/lib/shared/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export type Config = {
189189
outDir: string;
190190
base?: string;
191191
mode?: string;
192+
expandStoryTree?: boolean;
192193
noWatch: boolean;
193194
hotkeys: {
194195
fullscreen: string[];

packages/website/docs/config.md

+11
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,14 @@ export default {
331331
noWatch: true,
332332
};
333333
```
334+
335+
### expandStoryTree
336+
337+
You can expand the story tree by default.
338+
339+
```js
340+
/** @type {import('@ladle/react').UserConfig} */
341+
export default {
342+
expandStoryTree: true,
343+
};
344+
```

0 commit comments

Comments
 (0)