-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Bug]: DocsPage does not respect story sort #19605
Comments
It seems that the stories returned by
I patched around it by changing
Unfortunately I don't know the codebase well enough to understand whether |
I had same problem.. well it is not random.. actually is sort by alphabetical |
Just sharing my workaround. This is pretty hacky and relies on Storybook internals which may break, but it allows me to customize which story becomes the primary one. I do this by defining a custom docs page component: import React, {useContext} from 'react'
import {Title, Subtitle, Description, Primary, ArgsTable, Stories, DocsContext, Heading} from '@storybook/addon-docs'
/** For each component, we'll extract the story with this name and use it as the primary. */
const PRIMARY_STORY_NAME = "Default"
export const DocsPage = () => {
const context = useContext(DocsContext)
// The 'primary' story is rendered at the top of the page. The default implementation picks
// the first story in the alphabet; we look for one with a standard name instead and then
// override the private field on the context object to update it.
// This will probably break with updates to Storybook, but ideally Storybook will simultaneously
// add configuration for the primary story so we can just set this properly.
let primaryStoryId
try {
primaryStoryId = context.storyIdByName(PRIMARY_STORY_NAME) // Throws if missing
// Even though we pass the primary story name to the ArgsTable, it still gets confused
// and renders the wrong controls unlesss we also set the primaryStory private value
context.primaryStory = context.storyById(primaryStoryId)
// The Stories block is wierdly doesn't read primary story from any field or props,
// instead just assuming the first story is primary. So even though we set that field
// we still have to sort the stories to put the primary one first
// see: https://github.com/storybookjs/storybook/blob/65957e6789bc57d506ca63e6f3be3927f110565e/code/ui/blocks/src/blocks/Stories.tsx
context.componentStoriesValue = context
.componentStories()
// This leaves the rest of the stories in alpha order. We could instead define a custom sort here
.sort((a, b) => (a.name === PRIMARY_STORY_NAME ? -1 : b.name === PRIMARY_STORY_NAME ? 1 : 0))
} catch {}
const primaryStoryFound = primaryStoryId !== undefined
return (
<>
<Title />
<Subtitle />
<Description />
{primaryStoryFound && <Primary name={PRIMARY_STORY_NAME} />}
<Heading>Props</Heading>
<ArgsTable story={primaryStoryFound ? PRIMARY_STORY_NAME : undefined} />
<Stories includePrimary={!primaryStoryFound} />
</>
)
} Which I then set in my export const parameters = {
docs: {
page: DocsPage
}
} |
closing as dupe to #19163 |
Describe the bug
The DocsPage feature seems to be ignoring the sort order of my stories. For example, I have this
Avatar
component which exports five stories:This looks correct in the story navigation sidebar:
However, on the automatically generated Docs page, the stories seem to be rendered in random order:
As you can see from the screenshot, the default (top) story is not "Default" as I would expect. Instead it's "Custom Alt Text" and the rest of the stories follow in apparently random order.
To Reproduce
This repository has no changes at all from the
npx sb@next repro
output but it does still reproduce the problem. For example, see theButton
stories - the first story should bePrimary
but instead it'sLarge
.System
Additional context
There is no
storySort
configured - I am using the default sort order. But the issue still happens even with a custom sort order.The text was updated successfully, but these errors were encountered: