-
-
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.
- Loading branch information
Showing
12 changed files
with
153 additions
and
20 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,8 +1,15 @@ | ||
import { action, actions, decorate, configureActions, decorateAction } from './preview'; | ||
import { | ||
action, | ||
actions, | ||
decorate, | ||
configureActions, | ||
decorateAction, | ||
withActions, | ||
} from './preview'; | ||
|
||
// addons, panels and events get unique names using a prefix | ||
export const ADDON_ID = 'storybook/actions'; | ||
export const PANEL_ID = `${ADDON_ID}/actions-panel`; | ||
export const EVENT_ID = `${ADDON_ID}/action-event`; | ||
|
||
export { action, actions, decorate, configureActions, decorateAction }; | ||
export { action, actions, decorate, configureActions, decorateAction, withActions }; |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Based on http://backbonejs.org/docs/backbone.html#section-164 | ||
import { document, Element } from 'global'; | ||
import isEqual from 'lodash.isequal'; | ||
import addons from '@storybook/addons'; | ||
import Events from '@storybook/core-events'; | ||
|
||
import actions from './actions'; | ||
|
||
let lastSubscription; | ||
let lastArgs; | ||
|
||
const delegateEventSplitter = /^(\S+)\s*(.*)$/; | ||
|
||
const isIE = !Element.prototype.matches; | ||
const matchesMethod = isIE ? 'msMatchesSelector' : 'matches'; | ||
|
||
const root = document.getElementById('root'); | ||
|
||
const hasMatchInAncestry = (element, selector) => { | ||
if (element[matchesMethod](selector)) { | ||
return true; | ||
} | ||
const parent = element.parentElement; | ||
if (!parent) { | ||
return false; | ||
} | ||
return hasMatchInAncestry(parent, selector); | ||
}; | ||
|
||
const createHandlers = (actionsFn, ...args) => { | ||
const actionsObject = actionsFn(...args); | ||
return Object.entries(actionsObject).map(([key, action]) => { | ||
// eslint-disable-next-line no-unused-vars | ||
const [_, eventName, selector] = key.match(delegateEventSplitter); | ||
return { | ||
eventName, | ||
handler: e => { | ||
if (!selector || hasMatchInAncestry(e.target, selector)) { | ||
action(e); | ||
} | ||
}, | ||
}; | ||
}); | ||
}; | ||
|
||
const actionsSubscription = (...args) => { | ||
if (!isEqual(args, lastArgs)) { | ||
lastArgs = args; | ||
const handlers = createHandlers(...args); | ||
lastSubscription = () => { | ||
handlers.forEach(({ eventName, handler }) => root.addEventListener(eventName, handler)); | ||
return () => | ||
handlers.forEach(({ eventName, handler }) => root.removeEventListener(eventName, handler)); | ||
}; | ||
} | ||
return lastSubscription; | ||
}; | ||
|
||
export const createDecorator = actionsFn => (...args) => story => { | ||
addons.getChannel().emit(Events.REGISTER_SUBSCRIPTION, actionsSubscription(actionsFn, ...args)); | ||
return story(); | ||
}; | ||
|
||
export default createDecorator(actions); |
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
34 changes: 34 additions & 0 deletions
34
examples/html-kitchen-sink/stories/addon-actions.stories.js
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,34 @@ | ||
import { storiesOf } from '@storybook/html'; | ||
import { withActions, decorate } from '@storybook/addon-actions'; | ||
|
||
const pickTarget = decorate([args => [args[0].target]]); | ||
|
||
const button = () => `<button type="button">Hello World</button>`; | ||
|
||
storiesOf('Addons|Actions', module) | ||
.add('Hello World', () => withActions('click')(button)) | ||
.add('Multiple actions', () => withActions('click', 'contextmenu')(button)) | ||
.add('Multiple actions + config', () => | ||
withActions('click', 'contextmenu', { clearOnStoryChange: false })(button) | ||
) | ||
.add('Multiple actions, object', () => | ||
withActions({ click: 'clicked', contextmenu: 'right clicked' })(button) | ||
) | ||
.add('Multiple actions, selector', () => | ||
withActions({ 'click .btn': 'clicked', contextmenu: 'right clicked' })( | ||
() => ` | ||
<div> | ||
Clicks on this button will be logged: <button class="btn" type="button">Button</button> | ||
</div> | ||
` | ||
) | ||
) | ||
.add('Multiple actions, object + config', () => | ||
withActions({ click: 'clicked', contextmenu: 'right clicked' }, { clearOnStoryChange: false })( | ||
button | ||
) | ||
) | ||
.add('Decorated actions', () => pickTarget.withActions('click', 'contextmenu')(button)) | ||
.add('Decorated actions + config', () => | ||
pickTarget.withActions('click', 'contextmenu', { clearOnStoryChange: false })(button) | ||
); |
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