Skip to content

Commit 047dcab

Browse files
authored
Reset control state when navigating between stories (#340)
1 parent 38dc2fd commit 047dcab

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

.changeset/cuddly-buttons-cheer.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ladle/react": patch
3+
---
4+
5+
Reset control state when navigating between stories.

packages/ladle/lib/app/src/addons/control.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const getQuery = (
5050
return controlState;
5151
}
5252
Object.keys(params).forEach((paramKey) => {
53-
if (paramKey.startsWith("arg-")) {
53+
if (paramKey.startsWith("arg-") && controlState[paramKey.split("-")[1]]) {
5454
const keyParts = paramKey.split("-");
5555
const argKey = keyParts[1];
5656
const argValue = params[paramKey] as string;

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,16 @@ const App = () => {
143143
<Navigation
144144
stories={stories}
145145
story={globalState.story}
146-
updateStory={(story) =>
147-
dispatch({ type: ActionType.UpdateStory, value: story })
148-
}
146+
updateStory={(story) => {
147+
// we need to strip the control state from the URL first
148+
// so it doesn't leak into other stories with the same named controls
149+
modifyParams({
150+
...globalState,
151+
story,
152+
control: {},
153+
});
154+
dispatch({ type: ActionType.UpdateStory, value: story });
155+
}}
149156
/>
150157
<AddonPanel globalState={globalState} dispatch={dispatch} />
151158
</Context.Provider>

packages/ladle/lib/app/src/args-provider.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ const ArgsProvider = ({
108108
value: controls,
109109
});
110110
} else {
111-
dispatch({
112-
type: ActionType.UpdateControlIntialized,
113-
value: true,
114-
});
111+
if (!globalState.controlInitialized) {
112+
dispatch({
113+
type: ActionType.UpdateControlIntialized,
114+
value: true,
115+
});
116+
}
115117
}
116118
}, []);
117119

0 commit comments

Comments
 (0)