Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move the actions decorator into it's own entrypoint #19650

Merged
merged 5 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [From version 6.5.x to 7.0.0](#from-version-65x-to-700)
- [Alpha release notes](#alpha-release-notes)
- [7.0 breaking changes](#70-breaking-changes)
- [removed auto injection of @storybook/addon-actions decorator](#removed-auto-injection-of-storybookaddon-actions-decorator)
- [register.js removed](#registerjs-removed)
- [Change of root html IDs](#change-of-root-html-ids)
- [No more default export from `@storybook/addons`](#no-more-default-export-from-storybookaddons)
Expand Down Expand Up @@ -39,6 +40,7 @@
- [Dropped addon-docs manual configuration](#dropped-addon-docs-manual-configuration)
- [7.0 Deprecations](#70-deprecations)
- [`Story` type deprecated](#story-type-deprecated)
- [`ComponentStory`, `ComponentStoryObj`, `ComponentStoryFn` and `ComponentMeta` types are deprecated](#componentstory-componentstoryobj-componentstoryfn-and-componentmeta-types-are-deprecated)
- [From version 6.4.x to 6.5.0](#from-version-64x-to-650)
- [Vue 3 upgrade](#vue-3-upgrade)
- [React18 new root API](#react18-new-root-api)
Expand Down Expand Up @@ -251,6 +253,32 @@ In the meantime, these migration notes are the best available documentation on t

### 7.0 breaking changes

#### removed auto injection of @storybook/addon-actions decorator

The `withActions` decorator is no longer automatically added to stories. This is because it is really only used in the html renderer, for all other renderers it's redundant.
If you are using the html renderer and use the `handles` parameter, you'll need to manually add the `withActions` decorator:

```diff
import globalThis from 'global';
+import { withActions } from '@storybook/addon-actions/decorator';

export default {
component: globalThis.Components.Button,
args: {
label: 'Click Me!',
},
parameters: {
chromatic: { disable: true },
},
};
export const Basic = {
parameters: {
handles: [{ click: 'clicked', contextmenu: 'right clicked' }],
},
+ decorators: [withActions],
};
```

#### register.js removed

In SB 6.x and earlier, addons exported a `register.js` entry point by convention, and users would import this in `.storybook/manager.js`. This was [deprecated in SB 6.5](#deprecated-registerjs)
Expand Down
7 changes: 6 additions & 1 deletion code/addons/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./decorator": {
"require": "./dist/decorator.js",
"import": "./dist/decorator.mjs",
"types": "./dist/decorator.d.ts"
},
"./manager": {
"require": "./dist/manager.js",
"import": "./dist/manager.mjs",
Expand Down Expand Up @@ -73,7 +78,6 @@
"react-inspector": "^6.0.0",
"telejson": "^6.0.8",
"ts-dedent": "^2.0.0",
"util-deprecate": "^1.0.2",
"uuid-browser": "^3.1.0"
},
"devDependencies": {
Expand All @@ -98,6 +102,7 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/decorator.ts",
"./src/manager.tsx",
"./src/preview.ts"
]
Expand Down
6 changes: 5 additions & 1 deletion code/addons/actions/src/addArgs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { ArgsEnhancer } from '@storybook/types';
import { addActionsFromArgTypes, inferActionsFromArgTypesRegex } from './addArgsHelpers';

export const argsEnhancers = [addActionsFromArgTypes, inferActionsFromArgTypesRegex];
export const argsEnhancers: ArgsEnhancer[] = [
addActionsFromArgTypes,
inferActionsFromArgTypesRegex,
];
3 changes: 0 additions & 3 deletions code/addons/actions/src/addDecorator.ts

This file was deleted.

65 changes: 65 additions & 0 deletions code/addons/actions/src/decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import global from 'global';
import { useEffect, makeDecorator } from '@storybook/addons';
import { actions } from './runtime/actions';

import { PARAM_KEY } from './constants';

const { document, Element } = global;

const delegateEventSplitter = /^(\S+)\s*(.*)$/;

const isIE = Element != null && !Element.prototype.matches;
const matchesMethod = isIE ? 'msMatchesSelector' : 'matches';

const hasMatchInAncestry = (element: any, selector: any): boolean => {
if (element[matchesMethod](selector)) {
return true;
}
const parent = element.parentElement;
if (!parent) {
return false;
}
return hasMatchInAncestry(parent, selector);
};

const createHandlers = (actionsFn: (...arg: any[]) => object, ...handles: any[]) => {
const actionsObject = actionsFn(...handles);
return Object.entries(actionsObject).map(([key, action]) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const [_, eventName, selector] = key.match(delegateEventSplitter) || [];
return {
eventName,
handler: (e: { target: any }) => {
if (!selector || hasMatchInAncestry(e.target, selector)) {
action(e);
}
},
};
});
};

const applyEventHandlers = (actionsFn: any, ...handles: any[]) => {
const root = document && document.getElementById('storybook-root');
useEffect(() => {
if (root != null) {
const handlers = createHandlers(actionsFn, ...handles);
handlers.forEach(({ eventName, handler }) => root.addEventListener(eventName, handler));
return () =>
handlers.forEach(({ eventName, handler }) => root.removeEventListener(eventName, handler));
}
return undefined;
}, [root, actionsFn, handles]);
};

export const withActions = makeDecorator({
name: 'withActions',
parameterName: PARAM_KEY,
skipIfNoParametersOrOptions: true,
wrapper: (getStory, context, { parameters }) => {
if (parameters?.handles) {
applyEventHandlers(actions, ...parameters.handles);
}

return getStory(context);
},
});
1 change: 0 additions & 1 deletion code/addons/actions/src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './addDecorator';
export * from './addArgs';
37 changes: 0 additions & 37 deletions code/addons/actions/src/runtime/decorateAction.ts

This file was deleted.

2 changes: 0 additions & 2 deletions code/addons/actions/src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './action';
export * from './actions';
export * from './configureActions';
export * from './decorateAction';
export * from './withActions';
88 changes: 0 additions & 88 deletions code/addons/actions/src/runtime/withActions.ts

This file was deleted.

2 changes: 2 additions & 0 deletions code/addons/actions/template/stories/parameters.stories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import globalThis from 'global';
import { withActions } from '@storybook/addon-actions/decorator';

export default {
component: globalThis.Components.Button,
Expand All @@ -14,4 +15,5 @@ export const Basic = {
parameters: {
handles: [{ click: 'clicked', contextmenu: 'right clicked' }],
},
decorators: [withActions],
};
Loading