diff --git a/lib/addons/src/hooks.ts b/lib/addons/src/hooks.ts index dd329046ca4e..3cfea7c3f981 100644 --- a/lib/addons/src/hooks.ts +++ b/lib/addons/src/hooks.ts @@ -33,7 +33,7 @@ interface Effect { type AbstractFunction = (...args: any[]) => any; -export class HooksContext { +export class HooksContext { hookListsMap: WeakMap; mountedDecorators: Set; @@ -54,7 +54,7 @@ export class HooksContext { hasUpdates: boolean; - currentContext: StoryContext | null; + currentContext: StoryContext | null; renderListener = (storyId: StoryId) => { if (storyId !== this.currentContext.id) return; @@ -216,12 +216,18 @@ const areDepsEqual = (deps: any[], nextDeps: any[]) => const invalidHooksError = () => new Error('Storybook preview hooks can only be called inside decorators and story functions.'); -function getHooksContextOrNull(): HooksContext | null { +function getHooksContextOrNull(): HooksContext< + TFramework, + TArgs +> | null { return globalWindow.STORYBOOK_HOOKS_CONTEXT || null; } -function getHooksContextOrThrow(): HooksContext { - const hooks = getHooksContextOrNull(); +function getHooksContextOrThrow(): HooksContext< + TFramework, + TArgs +> { + const hooks = getHooksContextOrNull(); if (hooks == null) { throw invalidHooksError(); } @@ -405,8 +411,11 @@ export function useChannel(eventMap: EventMap, deps: any[] = []) { } /* Returns current story context */ -export function useStoryContext(): StoryContext { - const { currentContext } = getHooksContextOrThrow(); +export function useStoryContext(): StoryContext< + TFramework, + TArgs +> { + const { currentContext } = getHooksContextOrThrow(); if (currentContext == null) { throw invalidHooksError(); } @@ -424,17 +433,21 @@ export function useParameter(parameterKey: string, defaultValue?: S): S | und } /* Returns current value of story args */ -export function useArgs(): [Args, (newArgs: Args) => void, (argNames?: [string]) => void] { +export function useArgs(): [ + TArgs, + (newArgs: Partial) => void, + (argNames?: (keyof TArgs)[]) => void +] { const channel = addons.getChannel(); - const { id: storyId, args } = useStoryContext(); + const { id: storyId, args } = useStoryContext(); const updateArgs = useCallback( - (updatedArgs: Args) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }), + (updatedArgs: Partial) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }), [channel, storyId] ); const resetArgs = useCallback( - (argNames?: [string]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }), + (argNames?: (keyof TArgs)[]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }), [channel, storyId] );