Restore args param when switching stories or viewMode

This commit is contained in:
Gert Hengeveld 2021-04-07 11:53:49 +02:00
parent 0b4c01cff8
commit 6d54805127

View File

@ -8,7 +8,6 @@ import { window } from 'global';
import { ModuleArgs, ModuleFn } from '../index';
import { PanelPositions } from './layout';
import { isStory } from '../lib/stories';
import type { Story } from '../lib/stories';
interface Additions {
isFullscreen?: boolean;
@ -150,11 +149,14 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r
const initModule = () => {
// Sets `args` parameter in URL, omitting any args that have their initial value or cannot be unserialized safely.
const updateArgsParam = (args?: Story['args']) => {
const updateArgsParam = () => {
const { path, viewMode } = fullAPI.getUrlState();
if (viewMode !== 'story') return;
const currentStory = fullAPI.getCurrentStoryData();
const initialArgs = (isStory(currentStory) && currentStory.initialArgs) || {};
if (!isStory(currentStory)) return;
const { args, initialArgs } = currentStory;
const argsString = buildArgsParam(initialArgs, args);
const argsParam = argsString.length ? `&args=${argsString}` : '';
queryNavigate(`${path}${argsParam}`, { replace: true });
@ -164,13 +166,13 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r
fullAPI.on(SET_CURRENT_STORY, () => updateArgsParam());
let handleOrId: any;
fullAPI.on(STORY_ARGS_UPDATED, ({ args }) => {
fullAPI.on(STORY_ARGS_UPDATED, () => {
if ('requestIdleCallback' in window) {
if (handleOrId) window.cancelIdleCallback(handleOrId);
handleOrId = window.requestIdleCallback(() => updateArgsParam(args), { timeout: 1000 });
handleOrId = window.requestIdleCallback(updateArgsParam, { timeout: 1000 });
} else {
if (handleOrId) clearTimeout(handleOrId);
setTimeout(updateArgsParam, 100, args);
setTimeout(updateArgsParam, 100);
}
});