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

Allow storySort to sort nested stories below generic parent #16573

Closed
Dangoo opened this issue Nov 3, 2021 · 11 comments
Closed

Allow storySort to sort nested stories below generic parent #16573

Dangoo opened this issue Nov 3, 2021 · 11 comments

Comments

@Dangoo
Copy link

Dangoo commented Nov 3, 2021

Is your feature request related to a problem? Please describe
We maintain a component library where we want to sort all nested stories of Components (e.g. Components/Button) in the same order. Here's an example structure:

📄 Introduction
📄 About
📁 Components
  📁 Accordion
    📄 Overview
    📄 Accessibility
    📁 Variations
  📁 Button
    📄 Overview
    📄 Accessibility
    📁 Variations
  📁 ...
📄 FAQ

And here's what you would pass to options.storySort:

"options": {
  "storySort": {
    "method": "alphabetical",
    "order": [
      "Introduction",
      "About",
      "Components",
      ["Accordion", ["Overview", "Accessibility", "Variations"]],
      ["Button", ["Overview", "Accessibility", "Variations"]],
      "FAQ"
    ]
  }
}

Here the order for each section of Components/* must be defined separately.

Describe the solution you'd like
Given there already is a special * operator we thought we might declare the order as follows:

"options": {
  "storySort": {
    "method": "alphabetical",
    "order": [
      "Introduction",
      "About",
      "Components",
      ["*", ["Overview", "Accessibility", "Variations"]],
      "FAQ"
    ]
  }
}

Which would basically sort all children of Components with whats specified by method and apply the custom order to the next level. But as you might imagine from this issue this does not work out of the box.

Describe alternatives you've considered
For now we copied contents of storySort.ts and adjusted line 82 by adding special treatment for *:

82| // If a nested array is provided for a name or a '*' , use it for ordering.
83| const indexA = order.indexOf(nameA)
84| const indexWildcard = order.indexOf('*')
85| const index = indexA !== -1 ? indexA : indexWildcard
86| order = index !== -1 && Array.isArray(order[index + 1]) ? order[index + 1] : []

Are you able to assist to bring the feature to reality?
yes, I'd be glad to help

Additional context
A similar problem was described before here by @brandondurham:
https://stackoverflow.com/questions/68838338/sorting-in-storybook-possible-to-force-an-order-for-nested-stories

I've seen there's some shuffling around on the next branch, so I'm not able to tell whether sorting will work the same as right now in v6. But let me know what you think and maybe this valuable input for future versions as well :)

@Dangoo Dangoo changed the title Allow storySort to sort nested stories Allow storySort to sort nested stories below generic parent Nov 3, 2021
@shilman shilman added core and removed needs triage labels Nov 3, 2021
@mikaoelitiana
Copy link
Contributor

Hello, I am also interested to help on this issue

@PedersenM-ELS
Copy link

Hey, has there been any progress on this issue? Would be nice to have it :)

@ahnxsj
Copy link

ahnxsj commented Mar 21, 2022

I'd like this to happen too! One workaround that works for me is when you export stories, name the one you'd like to show first in capital letter first, whereas the rest in lowercase first.

const Template: ComponentStory<typeof Button> = (args) => (
  <Button {...args} />
);

export const Examples = () => <Button />;
...
export const primary = Template.bind({});
...
export const secondary = Template.bind({});

would make it show in this order: Examples, Primary, Secondary

@kyrstenkelly
Copy link

+1 Would really like this feature

@suchali93
Copy link

suchali93 commented Apr 1, 2022

I got nested sorting to work by setting method: 'configure' and adding includeNames: true to the storySort object.

options: {
  storySort: {
    method: 'configure',
    includeNames: true,
    order: [
         'Other',
        ['*', 'Typography', ['Usage']],
        'Components',
        'Customization',
        ['Styling', 'Theming', ['Overview', '*']],
    ]
  }
}

@Dangoo
Copy link
Author

Dangoo commented Apr 3, 2022

@suchali93 can you confirm that this approach works as well if you e.g. swapped Styling and Theming?
Since method: configure is using the default import order algorithm I have doubts that this works as it should described above where "every sub path below Components should be sorted like the given array" 🤔
Besides that, which "parent" would * in ['*', 'Typography', ['Usage']] refer to?

@suchali93
Copy link

It does. I've edited the example above to include the parents. I originally removed more than I should've, for the sake of the example. My bad.

['Styling', 'Theming', ['Overview', '*']] ['Theming', ['Overview', '*'], 'Styling']
Styling first Theming first

@EloB
Copy link
Contributor

EloB commented May 17, 2022

I've added a PR to add that wildcard. Anyone have feedback on it? :)

#18243

@toljaEmbroker
Copy link

toljaEmbroker commented Jun 11, 2022

I've added a PR to add that wildcard. Anyone have feedback on it? :)

#18243

I tested this fix on my project and it's working for me.

I have this order

{
    storySort: {
            includeNames: true,
            order: [
                'Phoenix-UI-Layouts',
                'Phoenix-UI-Atoms',
                'Phoenix-UI-Components',
                ['*', ['Overview', 'Parameters']],
                '*',
                'Legacy',
            ],
        },
},

@shilman
Copy link
Member

shilman commented Jun 29, 2022

Olé!! I just released https://github.com/storybookjs/storybook/releases/tag/v7.0.0-alpha.7 containing PR #18243 that references this issue. Upgrade today to the @future NPM tag to try it out!

npx sb upgrade --prerelease

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Jun 29, 2022
@adarsh-thakur
Copy link

adarsh-thakur commented Dec 5, 2022

I got nested sorting to work by setting method: 'configure' and adding includeNames: true to the storySort object.

options: {
  storySort: {
    method: 'configure',
    includeNames: true,
    order: [
         'Other',
        ['*', 'Typography', ['Usage']],
        'Components',
        'Customization',
        ['Styling', 'Theming', ['Overview', '*']],
    ]
  }
}

It worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants