Skip to content

docs(VersionSwitch): enable addon again #6843

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

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions .storybook/components/VersionSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
/* eslint-disable react-hooks/rules-of-hooks */

import { IconButton } from '@storybook/components';
import { addons, types } from '@storybook/manager-api';
import { ActionSheet, Button } from '@ui5/webcomponents-react';
import '@ui5/webcomponents/dist/Menu.js';
import '@ui5/webcomponents/dist/MenuItem.js';
import * as React from 'react';

const ADDON_ID = 'version-switch';
const TOOL_ID = `${ADDON_ID}/toolbar`;

addons.register(ADDON_ID, (api) => {
const handleVersionSelect = (e) => {
const { slug } = e.detail.item.dataset;
window.location.href = `https://sap.github.io/ui5-webcomponents-react/${slug}/`;
};

addons.register(ADDON_ID, () => {
addons.add(TOOL_ID, {
type: types.TOOLEXTRA,
title: 'Version Switch',
render: () => {
const [open, setOpen] = React.useState(false);

const handleVersionSelect = (e) => {
const { slug } = e.target.dataset;
window.location.href = `https://sap.github.io/ui5-webcomponents-react/${slug}/`;
};
const menuRef = React.useRef(null);

const activeVersionSlug =
window.location.pathname.replace('/ui5-webcomponents-react/', '').replaceAll('/', '') || 'nightly';
const activeVersion = activeVersionSlug.at(0).toUpperCase() + activeVersionSlug.slice(1);

React.useEffect(() => {
const menu = menuRef.current;
if (menu) {
menuRef.current.open = open;
}
}, [open]);

React.useEffect(() => {
const menu = menuRef.current;
const handleClose = () => {
setOpen(false);
};

if (menu) {
menu.addEventListener('item-click', handleVersionSelect);
menu.addEventListener('close', handleClose);
}

return () => {
if (menu) {
menu.removeEventListener('item-click', handleVersionSelect);
menu.removeEventListener('close', handleClose);
}
};
}, []);

return (
<>
<IconButton
Expand All @@ -35,24 +66,12 @@ addons.register(ADDON_ID, (api) => {
>
Version: {activeVersion}
</IconButton>
<ActionSheet
placement="Bottom"
open={open}
opener={TOOL_ID}
onClose={() => {
setOpen(false);
}}
>
<Button onClick={handleVersionSelect} data-slug={'v2'}>
Version 2
</Button>
<Button onClick={handleVersionSelect} data-slug={'v1'}>
Version 1
</Button>
<Button onClick={handleVersionSelect} data-slug={'nightly'}>
🚧 Nightly
</Button>
</ActionSheet>
{/*todo: use wcr components once supported*/}
<ui5-menu opener={TOOL_ID} ref={menuRef}>
<ui5-menu-item text="Version 2" data-slug={'v2'}></ui5-menu-item>
<ui5-menu-item text="Version 1" data-slug={'v1'}></ui5-menu-item>
<ui5-menu-item text="🚧 Nightly" data-slug={'nightly'}></ui5-menu-item>
</ui5-menu>
</>
);
}
Expand Down
3 changes: 1 addition & 2 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { isChromatic } from './utils';
const isDevMode = process.env.NODE_ENV === 'development';

const addons = [
// todo: enable again once https://github.com/storybookjs/storybook/pull/30003 is published
// './addons/version-switch',
'./addons/version-switch',
{
name: '@storybook/addon-essentials',
options: {
Expand Down
Loading