-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19650 from storybookjs/norbert/sb-803-resolve-add…
…on-actions-deprecations move the actions decorator into it's own entrypoint
- Loading branch information
Showing
13 changed files
with
111 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './addDecorator'; | ||
export * from './addArgs'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters