Skip to content

Commit

Permalink
feat: add expandStoryTree config flag (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesleyzxc authored Nov 12, 2024
1 parent cf54d02 commit 1178c37
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/gold-students-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"website": minor
"@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/expand-story-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",
);
});
1 change: 1 addition & 0 deletions packages/example/.ladle/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export default {
enabled: true,
},
},
expandStoryTree: true,
};
2 changes: 1 addition & 1 deletion packages/ladle/lib/app/src/sidebar/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const Main = ({
story={story}
hotkeys={hotkeys}
updateStory={updateStory}
searchActive={search !== ""}
allExpanded={search !== "" || config.expandStoryTree}
setTreeRootRef={(root: HTMLUListElement | null) =>
(treeRoot.current = root)
}
Expand Down
10 changes: 5 additions & 5 deletions packages/ladle/lib/app/src/sidebar/tree-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const TreeView = ({
stories,
story,
updateStory,
searchActive,
allExpanded,
searchRef,
setTreeRootRef,
hotkeys,
Expand All @@ -39,15 +39,15 @@ const TreeView = ({
searchRef: React.Ref<HTMLInputElement>;
updateStory: UpdateStory;
setTreeRootRef: (root: HTMLUListElement | null) => void;
searchActive?: boolean;
allExpanded?: boolean;
hotkeys: 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 All @@ -64,7 +64,7 @@ const TreeView = ({
const hotkeyStoryTransition = (story?: string) => {
if (story) {
updateStory(story);
setTree(getStoryTree(stories, story, searchActive));
setTree(getStoryTree(stories, story, allExpanded));
setTimeout(() => focusSelectedItem(story), 1);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const ladleConfigToClientConfig = (config) => {
: config.storyOrder.toString(),
}
: {}),
...(config.expandStoryTree
? { expandStoryTree: config.expandStoryTree }
: {}),
};
};

Expand All @@ -37,12 +40,11 @@ const getConfigImport = async (configFolder, config) => {
let clientConfig = {};
if (fs.existsSync(configPath)) {
const fileConfig = (await import(pathToFileURL(configPath).href)).default;
// @ts-ignore
// @ts-expect-error: exclude hotkeys
clientConfig = ladleConfigToClientConfig(fileConfig);
}
merge(clientConfig, ladleConfigToClientConfig(config));
// don't merge hotkeys
// @ts-ignore
// @ts-expect-error: don't merge hotkeys
clientConfig.hotkeys = {
...clientConfig.hotkeys,
...config.hotkeys,
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 @@ -15,6 +15,7 @@ export default {
hmrPort: undefined,
outDir: "build",
base: undefined,
expandStoryTree: false,
hotkeys: {
search: ["/", "meta+p"],
nextStory: ["alt+arrowright"],
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 @@ -189,6 +189,7 @@ export type Config = {
outDir: string;
base?: string;
mode?: string;
expandStoryTree?: boolean;
noWatch: boolean;
hotkeys: {
fullscreen: string[];
Expand Down
11 changes: 11 additions & 0 deletions packages/website/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,14 @@ export default {
noWatch: true,
};
```

### expandStoryTree

You can expand the story tree by default.

```js
/** @type {import('@ladle/react').UserConfig} */
export default {
expandStoryTree: true,
};
```

0 comments on commit 1178c37

Please sign in to comment.