Skip to content

Commit

Permalink
fix: Use context to pass API to Layout and mock it in Layout stories
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidnioulz committed Feb 13, 2025
1 parent 4cd8b7f commit 6838695
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 1 addition & 5 deletions code/core/src/manager/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@ import React from 'react';
import { Global, createGlobal } from '@storybook/core/theming';
import type { Addon_PageType } from '@storybook/core/types';

import type { API } from '@storybook/core/manager-api';

import { Layout } from './components/layout/Layout';
import { useLayout } from './components/layout/LayoutProvider';
import Panel from './container/Panel';
import Preview from './container/Preview';
import Sidebar from './container/Sidebar';

type Props = {
api: API;
managerLayoutState: ComponentProps<typeof Layout>['managerLayoutState'];
setManagerLayoutState: ComponentProps<typeof Layout>['setManagerLayoutState'];
pages: Addon_PageType[];
hasTab: boolean;
};

export const App = ({ api, managerLayoutState, setManagerLayoutState, pages, hasTab }: Props) => {
export const App = ({ managerLayoutState, setManagerLayoutState, pages, hasTab }: Props) => {
const { setMobileAboutOpen } = useLayout();

return (
<>
<Global styles={createGlobal} />
<Layout
api={api}
hasTab={hasTab}
managerLayoutState={managerLayoutState}
setManagerLayoutState={setManagerLayoutState}
Expand Down
14 changes: 14 additions & 0 deletions code/core/src/manager/components/layout/Layout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { styled } from '@storybook/core/theming';
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';

import { ManagerContext } from '@storybook/core/manager-api';

import { action } from '@storybook/addon-actions';

import MobileNavigationStoriesMeta from '../mobile/navigation/MobileNavigation.stories';
Expand Down Expand Up @@ -53,6 +55,15 @@ const defaultState = {
viewMode: 'story',
} as const;

const managerContext: any = {
state: {},
api: {
getNavSizeWithCustomisations: fn()
.mockName('api::getNavSizeWithCustomisations')
.mockImplementation((size: number) => size),
},
};

const meta = {
title: 'Layout',
component: Layout,
Expand All @@ -68,6 +79,9 @@ const meta = {
globals: { sb_theme: 'light' },
parameters: { layout: 'fullscreen' },
decorators: [
(storyFn) => (
<ManagerContext.Provider value={managerContext}>{storyFn()}</ManagerContext.Provider>
),
MobileNavigationStoriesMeta.decorators[0] as any,
(storyFn) => (
<LocationProvider>
Expand Down
12 changes: 3 additions & 9 deletions code/core/src/manager/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Match } from '@storybook/core/router';
import { styled } from '@storybook/core/theming';
import type { API_Layout, API_ViewMode } from '@storybook/core/types';

import type { API } from '@storybook/core/manager-api';
import { type API, useStorybookApi } from '@storybook/core/manager-api';

import { MEDIA_DESKTOP_BREAKPOINT } from '../../constants';
import { Notifications } from '../../container/Notifications';
Expand All @@ -24,7 +24,6 @@ interface ManagerLayoutState
export type LayoutState = InternalLayoutState & ManagerLayoutState;

interface Props {
api: API;
managerLayoutState: ManagerLayoutState;
setManagerLayoutState: (state: Partial<Omit<ManagerLayoutState, 'viewMode'>>) => void;
slotMain?: React.ReactNode;
Expand Down Expand Up @@ -128,14 +127,9 @@ const useLayoutSyncingState = ({
};
};

export const Layout = ({
api,
managerLayoutState,
setManagerLayoutState,
hasTab,
...slots
}: Props) => {
export const Layout = ({ managerLayoutState, setManagerLayoutState, hasTab, ...slots }: Props) => {
const { isDesktop, isMobile } = useLayout();
const api = useStorybookApi();

const {
navSize,
Expand Down

0 comments on commit 6838695

Please sign in to comment.