Skip to content

Commit

Permalink
expand-all-once, not filter-event-forever
Browse files Browse the repository at this point in the history
  • Loading branch information
llaenowyd committed May 22, 2022
1 parent f9dfd69 commit ce11b12
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"description": "Expand all sidebar items at Storybook start",
"main": "dist/preset.js",
"scripts": {
"clean": "rimraf dist node_modules",
"clean": "rimraf dist node_modules/.cache",
"build": "babel ./src --out-dir ./dist",
"test": "echo \"Error: no test specified\" && exit 1",
"storybook": "start-storybook -p 6006"
Expand Down
44 changes: 26 additions & 18 deletions src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ import { addons } from '@storybook/addons'

const ADDON_ID = '@a110/s-e-a'

addons.register(
ADDON_ID,
(() => {
let hasExpanded = false

return api => {
const emitter = addons.getChannel()

emitter.on(CURRENT_STORY_WAS_SET, () => {
if (!hasExpanded) {
// "Calling on the next tick after storyRendered
// seems to work reliably."
setTimeout(api.expandAll)
hasExpanded = true
}
})
}
})()
const subscribeEvent = (emitter, event, callback) => (
emitter.on(event, callback), () => emitter.off(event, callback)
)

class ExpandAllOnce {
_emitter = addons.getChannel()
_api
_unsub

expandAll = () => {
this._api.expandAll()
this._unsub()
}

constructor(api) {
this._api = api
this._unsub = subscribeEvent(
this._emitter,
CURRENT_STORY_WAS_SET,
this.expandAll
)
}
}

addons.register(ADDON_ID, api => {
new ExpandAllOnce(api)
})

0 comments on commit ce11b12

Please sign in to comment.