Created
March 20, 2023 19:27
-
-
Save DanilAgafonov/749be1ac35b1f34b1ab7c9a4c3870a19 to your computer and use it in GitHub Desktop.
Storybook v7 URL params
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
<script> | |
function slugify(str) { | |
return str | |
.toLowerCase() | |
.replace(/[^a-z0-9]+/g, '-') | |
.replace(/(^-|-$)+/g, ''); | |
} | |
// screener-storybook is not compatible with storybook 7. | |
// It uses `selectedKind` and `selectedStory` query params to determine which story to render. | |
// Storybook 7 uses `id` query param instead. | |
// https://github.com/storybookjs/storybook/blob/e01d876314e7fc1f02d065a93a690f4787f58cec/MIGRATION.md#new-url-structure | |
const url = new URL(window.location.href); | |
const params = new URLSearchParams(window.location.search); | |
if (params.has('selectedKind') && params.has('selectedStory')) { | |
url.search = (new URLSearchParams({ | |
id: `${slugify(params.get('selectedKind'))}--${slugify(params.get('selectedStory'))}`, | |
})).toString(); | |
window.location.replace(url.toString()) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment