Skip to content

Commit afa28de

Browse files
authored
docs(VersionSwitch): enable addon again (#6843)
1 parent c46d4f1 commit afa28de

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

.storybook/components/VersionSwitch.tsx

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
1+
/* eslint-disable react-hooks/rules-of-hooks */
2+
13
import { IconButton } from '@storybook/components';
24
import { addons, types } from '@storybook/manager-api';
3-
import { ActionSheet, Button } from '@ui5/webcomponents-react';
5+
import '@ui5/webcomponents/dist/Menu.js';
6+
import '@ui5/webcomponents/dist/MenuItem.js';
47
import * as React from 'react';
58

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

9-
addons.register(ADDON_ID, (api) => {
12+
const handleVersionSelect = (e) => {
13+
const { slug } = e.detail.item.dataset;
14+
window.location.href = `https://sap.github.io/ui5-webcomponents-react/${slug}/`;
15+
};
16+
17+
addons.register(ADDON_ID, () => {
1018
addons.add(TOOL_ID, {
1119
type: types.TOOLEXTRA,
1220
title: 'Version Switch',
1321
render: () => {
1422
const [open, setOpen] = React.useState(false);
15-
16-
const handleVersionSelect = (e) => {
17-
const { slug } = e.target.dataset;
18-
window.location.href = `https://sap.github.io/ui5-webcomponents-react/${slug}/`;
19-
};
23+
const menuRef = React.useRef(null);
2024

2125
const activeVersionSlug =
2226
window.location.pathname.replace('/ui5-webcomponents-react/', '').replaceAll('/', '') || 'nightly';
2327
const activeVersion = activeVersionSlug.at(0).toUpperCase() + activeVersionSlug.slice(1);
28+
29+
React.useEffect(() => {
30+
const menu = menuRef.current;
31+
if (menu) {
32+
menuRef.current.open = open;
33+
}
34+
}, [open]);
35+
36+
React.useEffect(() => {
37+
const menu = menuRef.current;
38+
const handleClose = () => {
39+
setOpen(false);
40+
};
41+
42+
if (menu) {
43+
menu.addEventListener('item-click', handleVersionSelect);
44+
menu.addEventListener('close', handleClose);
45+
}
46+
47+
return () => {
48+
if (menu) {
49+
menu.removeEventListener('item-click', handleVersionSelect);
50+
menu.removeEventListener('close', handleClose);
51+
}
52+
};
53+
}, []);
54+
2455
return (
2556
<>
2657
<IconButton
@@ -35,24 +66,12 @@ addons.register(ADDON_ID, (api) => {
3566
>
3667
Version: {activeVersion}
3768
</IconButton>
38-
<ActionSheet
39-
placement="Bottom"
40-
open={open}
41-
opener={TOOL_ID}
42-
onClose={() => {
43-
setOpen(false);
44-
}}
45-
>
46-
<Button onClick={handleVersionSelect} data-slug={'v2'}>
47-
Version 2
48-
</Button>
49-
<Button onClick={handleVersionSelect} data-slug={'v1'}>
50-
Version 1
51-
</Button>
52-
<Button onClick={handleVersionSelect} data-slug={'nightly'}>
53-
🚧 Nightly
54-
</Button>
55-
</ActionSheet>
69+
{/*todo: use wcr components once supported*/}
70+
<ui5-menu opener={TOOL_ID} ref={menuRef}>
71+
<ui5-menu-item text="Version 2" data-slug={'v2'}></ui5-menu-item>
72+
<ui5-menu-item text="Version 1" data-slug={'v1'}></ui5-menu-item>
73+
<ui5-menu-item text="🚧 Nightly" data-slug={'nightly'}></ui5-menu-item>
74+
</ui5-menu>
5675
</>
5776
);
5877
}

.storybook/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { isChromatic } from './utils';
77
const isDevMode = process.env.NODE_ENV === 'development';
88

99
const addons = [
10-
// todo: enable again once https://github.com/storybookjs/storybook/pull/30003 is published
11-
// './addons/version-switch',
10+
'./addons/version-switch',
1211
{
1312
name: '@storybook/addon-essentials',
1413
options: {

0 commit comments

Comments
 (0)