Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
storybook-multilevel-sort
Advanced tools
Applies specific sort order to more than two levels of chapters and stories in a storybook.
Applies specific sort order to more than two levels of chapters and stories in a storybook.
Attention: Versions 2.x
of this package support Storybook 7 and newer. If you use Storybook 6 or older, look for the versions 1.x
of this package. If you upgrade Storybook to the version 7 or newer, you will need a version 2.x
of this package too. See the documentation about how to migrate from a version 1.x
to a version 2.x
of this package.
See also an example of a Storybook project using this package.
The following directory structure:
.
├── Articles
│ ├── Getting Started.mdx Articles/Getting Started
│ └── Versioning.mdx Articles/Versioning
├── Components
│ ├── Header
│ │ ├── Collapsed.mdx Components/Header/Collapsed
│ │ ├── Default.mdx Components/Header/Default
│ │ ├── Expanded.mdx Components/Header/Expanded
│ │ └── WithSearch.mdx Components/Header/With Search
│ └── List
│ ├── Collapsed.mdx Components/List/Collapsed
│ ├── Default.mdx Components/List/Default
│ └── Expanded.mdx Components/List/Expanded
└── Elements
├── Button
│ ├── Active.mdx Elements/Button/Active
│ └── Default.mdx Elements/Button/Default
└── Link
├── Active.mdx Elements/Link/Active
└── Default.mdx Elements/Link/Default
Can be sorted according to this request:
Elements
before Components
Default
stories before the othersWith Search
right after Default
and before the othersResulting in a TOC like this. The "Docs" chapters are inserted by Storybook 7 or newer instead of the "Docs" tab. If you want to change their order, see Type Sort Order and Grouping below:
Articles
Getting Started
Versioning
Elements
Button
Docs
Default
Active
Link
Docs
Default
Active
Components
Header
Docs
Default
With Search
Collapsed
Expanded
List
Docs
Default
Collapsed
Expanded
When using the following code in .storybook/main.js
:
import { configureSort } from 'storybook-multilevel-sort'
configureSort({
storyOrder: {
articles: null,
elements: {
'*': { default: null }
},
components: {
navigation: {
header: {
default: null,
'with search': null
}
}
},
'**': { default: null }
}
})
And the following code in .storybook/preview.js
:
export default {
parameters: {
options: {
storySort: (story1, story2) =>
globalThis['storybook-multilevel-sort:storySort'](story1, story2)
}
}
}
A simpler configuration using nested wildcards:
{
articles: null,
elements: null,
components: {
header: {
default: null,
'with search': null
},
},
'**': { default: null }
}
This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 16 or newer.
npm i -D storybook-multilevel-sort
pnpm i -D storybook-multilevel-sort
yarn add storybook-multilevel-sort
This package exports a function to configure the custom sorting:
import { configureSort } from 'storybook-multilevel-sort'
The function is supposed to be executed in .storybook/main.js
and expects an object with the sorting configuration:
configureSort({
typeOrder: ...
storyOrder: ...
compareNames: ...
})
It prepares a global function, which will be called in the storySort
callback with the two stories to compare, implemented in.storybook/preview.js
:
export default {
parameters: {
options: {
storySort: (story1, story2) =>
globalThis['storybook-multilevel-sort:storySort'](story1, story2)
}
}
}
This package can be imported to CJS projects too:
const { configureSort } = require('storybook-multilevel-sort')
The object expected by the configureSort
function may include the following properties:
storyOrder
: configuration of the sort order based on names of groups and stories (object
, optional)compareNames
: custom name comparison function (function
, optional)typeOrder
: configuration of the page grouping and sort order based on types of the pages (array
, optional)The sorting configuration is an object set by the sortOrder
property. Keys are titles of groups and stories. Values are objects with the next level of groups or stories. Nesting of the objects follows the slash-delimited story paths set to the title
attribute:
configureSort({
storyOrder: {
elements: {
link: null, // Elements/Link/...
button: null // Elements/Button/...
},
components: null // Components/Card/...
// Components/Header/...
}
})
Keys in the sorting objects have to be lower-case. If a value is null
or an empty object, that level will be sorted alphabetically. Names of groups or stories missing among the object keys will be sorted alphabetically, behind the names that are listed as keys.
Names of groups and stories may include spaces. They are usually declared using pascal-case or camel-case and Storybook will separate the words by spaces:
// The name will be "With Search"
export const WithSearch = Template.bind({})
They can be also assigned the displayable name using the storyName
property:
// The name will be "With Search" too
export const story1 = Template.bind({})
story1.storyName = 'With Search'
When you refer to such groups or stories on the ordering configuration, use the displayable name (with spaces) lower-case, for example:
{
storyOrder: {
'*': {
default: null,
'with search': null
}
}
}
Generally, names of groups and stories are expected in the ordering configuration as Storybook displays them. Not as the exported variables are named. You need to be aware of the algorithm how Storybook generates the names of stories.
If you want to skip explicit sorting at one level and specify the next level, use *
instead of names, for which you want to specify the next level. The *
matches any name, which is not listed explicitly at the same level:
{
storyOrder: {
'*': {
default: null // Link/Default
} // Link/Active
} // Link/Visited
}
If you want to enable implicit sorting at multiple levels, you would have to repeat the *
selector on each level:
{
storyOrder: {
elements: {
'*': {
default: null // Link/Default
} // Link/Active
}, // Link/Visited
components: {
'*': {
default: null // Header/Default
} // Header/Collapsed
} // Header/Expanded
}
}
you can use a nested wildcard **
to specify default for the current and deeper levels. The **
matches any name, which is not listed explicitly at the same level and if there is no *
wildcard selector at that level:
{
storyOrder: {
elements: null,
components: null,
'**': {
default: null // Link/Default
} // Link/Active
} // Link/Visited
// Header/Default
// Header/Collapsed
// Header/Expanded
}
The precedence of the selectors at a particular level:
*
matching any name of a group or story**
frm the same or from an outer level matching any name of a group or storyNames of groups and stories on one level are compared alphabetically according to the current locale by default. If you need a different comparison, you can specify it by using the optional compareNames
parameter:
{
storyOrder: ...
compareNames: (name1, name2, context) {
// name1 - the string with the name on the left side of the comparison
// name2 - the string with the name on the right side of the comparison
// context - additional information
// context.path1 - an array of strings with the path of groups
// down to the left compared group or story name (name1)
// context.path2 - an array of strings with the path of groups
// down to the right compared group or story name (name2)
return name1.localeCompare(name2, { numeric: true })
}
}
Mind that the strings with names of groups and stories are converted to lower-case, before they are passed to the comparator.
Storybook 7 introduced a new type of pages, which can appear among the stories - docs
. The documentation page earlier accessible on the "Docs" tab was moved to the tree of groups and stories. It means that there is a new type of the node in the navigation tree, which you may want to sort independently of the pages of the previous type - story
.
Storybook inserts the links to the "Docs" pages before the first story of a particular component. This custom sorting will retain it by default, because the "Docs" page usually contains an overview of the component's usage. But you can change it by the typeOrder
property. This is the default value, which groups all pages of the docs
type before all pages of the story
type:
{
storyOrder: ...
typeOrder: ['docs', 'story']
}
The order of types in the array will be the order of the page groups. If you specify just one type, ['docs']
or ['story']
, pages of this type will be grouped together at the beginning and all other pages will follow behind them, regardless of their type, sorted only by their names.
If you want to handle the docs
pages like any other stories and sort all the pages only by their names, you can pass an empty array to typeOrder
to disable the grouping by type:
{
storyOrder: ...
typeOrder: []
}
Unfortunately, the sorting configuration supported by Storybook works only for two-level storybooks:
The order array can accept a nested array in order to sort 2nd-level story kinds.
If you group your components by one more level, the stories will move to the third level and you won't be able to sort them. For example:
.
├── Articles
│ ├── Getting Started.mdx
│ └── Versioning.mdx
└── Elements
├── Button
│ ├── Active.mdx
│ └── Default.mdx
└── Link
├── Active.mdx
└── Default.mdx
Let's say, that you want to sort the stories alphabetically, but put the Default
story before the other stories. It's impossible using the declarative configuration, because stories are on the third level. The following configuration:
storySort: {
order: ['Articles', '*', ['*', ['Default', '*']]]
}
Will generate the following TOC:
Articles
Getting Started
Versioning
Elements
Button
Active
Default
Link
Active
Default
This package will help generating the proper TOC:
Articles
Getting Started
Versioning
Elements
Button
Default
Active
Link
Default
Active
Using the following order configuration:
const order = {
articles: null,
elements: {
'*': { default: null }
}
}
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
Copyright (c) 2022-2024 Ferdinand Prantl
Licensed under the MIT license.
Sort icons created by Freepik - Flaticon
Licensed under the Icon Free License (with attribution)
FAQs
Applies specific sort order to more than two levels of chapters and stories in a storybook.
We found that storybook-multilevel-sort demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.