Skip to content

Commit

Permalink
Fallback to use corresponding new packages for storybook version 8
Browse files Browse the repository at this point in the history
  • Loading branch information
indigolain committed Mar 15, 2024
1 parent f104e77 commit 365331c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/v8-react18/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"@storybook/addon-actions": "^8.0.0",
"@storybook/addon-essentials": "^8.0.0",
"@storybook/addon-links": "^8.0.0",
"@storybook/addons": "^7.6.17",
"@storybook/cli": "^8.0.0",
"@storybook/manager-api": "^8.0.0",
"@storybook/preset-create-react-app": "^8.0.0",
"@storybook/preview-api": "^8.0.0",
"@storybook/react": "^8.0.0",
"@storybook/react-webpack5": "^8.0.0",
"@testing-library/react": "^14.0.0",
Expand Down
36 changes: 35 additions & 1 deletion packages/storycap/decl/storybook.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
declare module '@storybook/addons' {
declare module '@storybook/manager-api' {
import { EventEmitter } from 'events';
export interface API {
raw?: () => { id: string; kind: string; name: string }[];
getStorybook(): { kind: string; stories: { name: string }[] }[];
}
export interface StoryKind {
kind: string;
stories: string[];
}
export class Channel extends EventEmitter {
on(name: 'setStories', listener: (event: { stories: StoryKind[] }) => void): this;
once(name: 'setStories', listener: (event: { stories: StoryKind[] }) => void): this;
}
export interface Addons {
register(name: string, callback: (api: API) => void): void;
getChannel(): Channel;
}

export const addons: Addons;
}
declare module '@storybook/preview-api' {
interface MakeDecorator {
(options: {
name: string;
parameterName: string;
skipIfNoParametersOrOptions: boolean;
allowDeprecatedUsage: boolean;
wrapper: (getStory: any, context: any, args: { parameters: any; options: any }) => any;
}): Function;
}

export const makeDecorator: MakeDecorator;
}
declare module '@storybook/addons' {
// eslint-disable-next-line no-duplicate-imports
import { EventEmitter } from 'events';
export interface API {
raw?: () => { id: string; kind: string; name: string }[];
getStorybook(): { kind: string; stories: { name: string }[] }[];
Expand Down
17 changes: 13 additions & 4 deletions packages/storycap/src/client/register.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { addons } from '@storybook/addons';
import type { Addons } from '@storybook/addons';

let addons: Addons | undefined;
try {
addons = require('@storybook/addons').addons;
} catch {}
try {
addons = require('@storybook/manager-api').addons;
} catch {}

(window as any).__STORYCAP_MANAGED_MODE_REGISTERED__ = true;

addons.register('storycap', () => {
// nothing to do
});
addons &&
addons.register('storycap', () => {
// nothing to do
});
9 changes: 8 additions & 1 deletion packages/storycap/src/client/with-screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { StoryKind, makeDecorator } from '@storybook/addons';
import type { StoryKind, MakeDecorator } from '@storybook/addons';
let makeDecorator: MakeDecorator | undefined;
try {
makeDecorator = require('@storybook/addons').makeDecorator;
} catch {}
try {
makeDecorator = require('@storybook/preview-api').makeDecorator;
} catch {}

import { ScreenshotOptions } from '../shared/types';
import { triggerScreenshot } from './trigger-screenshot';
Expand Down

0 comments on commit 365331c

Please sign in to comment.