Skip to content

Commit 2a3d265

Browse files
ghengeveldstorybook-bot
authored andcommitted
Merge pull request #23411 from Spielboerg/hide-shortcut-page-when-disabled
UI: Hide keyboard shortcuts entry from menu when shortcuts are disabled (cherry picked from commit a3274ac)
1 parent 650fabd commit 2a3d265

File tree

4 files changed

+79
-29
lines changed

4 files changed

+79
-29
lines changed

code/core/src/manager/components/sidebar/Menu.stories.tsx

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ export const Expanded: Story = {
5656
globals: { sb_theme: 'light' },
5757
render: () => {
5858
const menu = useMenu(
59-
{ whatsNewData: { status: 'SUCCESS', disableWhatsNewNotifications: false } } as State,
59+
{ whatsNewData: undefined } as State,
6060
{
6161
// @ts-expect-error (Converted from ts-ignore)
6262
getShortcutKeys: () => ({}),
6363
getAddonsShortcuts: () => ({}),
6464
versionUpdateAvailable: () => false,
65-
isWhatsNewUnread: () => true,
65+
isWhatsNewUnread: () => false,
6666
getDocsUrl: () => 'https://storybook.js.org/docs/',
6767
},
6868
false,
@@ -73,7 +73,7 @@ export const Expanded: Story = {
7373
);
7474
return (
7575
<DoubleThemeRenderingHack>
76-
<SidebarMenu menu={menu} isHighlighted />
76+
<SidebarMenu menu={menu} />
7777
</DoubleThemeRenderingHack>
7878
);
7979
},
@@ -96,14 +96,27 @@ export const Expanded: Story = {
9696
],
9797
};
9898

99-
export const ExpandedWithoutWhatsNew: Story = {
99+
export const ExpandedWithShortcuts: Story = {
100100
...Expanded,
101101
render: () => {
102102
const menu = useMenu(
103103
{ whatsNewData: undefined } as State,
104104
{
105105
// @ts-expect-error (invalid)
106-
getShortcutKeys: () => ({}),
106+
getShortcutKeys: () => ({
107+
shortcutsPage: ['⌘', '⇧​', ','],
108+
toggleNav: ['⌥', 'S'],
109+
togglePanel: ['⌥', 'A'],
110+
toolbar: ['⌥', 'T'],
111+
panelPosition: ['⌥', 'D'],
112+
fullScreen: ['⌥', 'F'],
113+
search: ['⌥', 'K'],
114+
prevComponent: ['⌥', '↑'],
115+
nextComponent: ['⌥', '↓'],
116+
prevStory: ['⌥', '←'],
117+
nextStory: ['⌥', '→'],
118+
collapseAll: ['⌥', '⇧', '↑'],
119+
}),
107120
getAddonsShortcuts: () => ({}),
108121
versionUpdateAvailable: () => false,
109122
isWhatsNewUnread: () => false,
@@ -113,7 +126,7 @@ export const ExpandedWithoutWhatsNew: Story = {
113126
false,
114127
false,
115128
false,
116-
false
129+
true
117130
);
118131

119132
return (
@@ -133,3 +146,41 @@ export const ExpandedWithoutWhatsNew: Story = {
133146
await expect(releaseNotes).not.toBeInTheDocument();
134147
},
135148
};
149+
150+
export const ExpandedWithWhatsNew: Story = {
151+
...Expanded,
152+
render: () => {
153+
const menu = useMenu(
154+
{ whatsNewData: { status: 'SUCCESS', disableWhatsNewNotifications: false } } as State,
155+
{
156+
// @ts-expect-error (invalid)
157+
getShortcutKeys: () => ({}),
158+
getAddonsShortcuts: () => ({}),
159+
versionUpdateAvailable: () => false,
160+
isWhatsNewUnread: () => true,
161+
getDocsUrl: () => 'https://storybook.js.org/docs/',
162+
},
163+
false,
164+
false,
165+
false,
166+
false,
167+
false
168+
);
169+
170+
return (
171+
<DoubleThemeRenderingHack>
172+
<SidebarMenu menu={menu} isHighlighted />
173+
</DoubleThemeRenderingHack>
174+
);
175+
},
176+
play: async (context) => {
177+
const canvas = within(context.canvasElement);
178+
await new Promise((res) => {
179+
setTimeout(res, 500);
180+
});
181+
// @ts-expect-error (non strict)
182+
await Expanded.play(context);
183+
const releaseNotes = await canvas.queryByText(/What's new/);
184+
await expect(releaseNotes).not.toBeInTheDocument();
185+
},
186+
};

code/core/src/manager/container/Menu.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import React, { useCallback, useMemo } from 'react';
44
import { Badge } from 'storybook/internal/components';
55
import { STORIES_COLLAPSE_ALL } from 'storybook/internal/core-events';
66

7-
import { CheckIcon, InfoIcon, ShareAltIcon, WandIcon } from '@storybook/icons';
7+
import { CheckIcon, CommandIcon, InfoIcon, ShareAltIcon, WandIcon } from '@storybook/icons';
88

99
import type { API, State } from 'storybook/manager-api';
1010
import { shortcutToHumanString } from 'storybook/manager-api';
11-
import { styled, useTheme } from 'storybook/theming';
11+
import { styled } from 'storybook/theming';
1212

1313
import type { Link } from '../../components/components/tooltip/TooltipLinkList';
1414

@@ -32,25 +32,20 @@ const Key = styled.span(({ theme }) => ({
3232
padding: '0 6px',
3333
}));
3434

35-
const KeyChild = styled.code(
36-
({ theme }) => `
37-
padding: 0;
38-
vertical-align: middle;
39-
40-
& + & {
41-
margin-left: 6px;
42-
}
43-
`
44-
);
35+
const KeyChild = styled.code({
36+
padding: 0,
37+
verticalAlign: 'middle',
38+
'& + &': {
39+
marginLeft: 6,
40+
},
41+
});
4542

4643
export const Shortcut: FC<{ keys: string[] }> = ({ keys }) => (
47-
<>
48-
<Key>
49-
{keys.map((key, index) => (
50-
<KeyChild key={key}>{shortcutToHumanString([key])}</KeyChild>
51-
))}
52-
</Key>
53-
</>
44+
<Key>
45+
{keys.map((key) => (
46+
<KeyChild key={key}>{shortcutToHumanString([key])}</KeyChild>
47+
))}
48+
</Key>
5449
);
5550

5651
export const useMenu = (
@@ -88,6 +83,7 @@ export const useMenu = (
8883
const whatsNewNotificationsEnabled =
8984
state.whatsNewData?.status === 'SUCCESS' && !state.disableWhatsNewNotifications;
9085
const isWhatsNewUnread = api.isWhatsNewUnread();
86+
9187
const whatsNew = useMemo(
9288
() => ({
9389
id: 'whats-new',
@@ -107,6 +103,7 @@ export const useMenu = (
107103
title: 'Keyboard shortcuts',
108104
onClick: () => api.changeSettingsTab('shortcuts'),
109105
right: enableShortcuts ? <Shortcut keys={shortcutKeys.shortcutsPage} /> : null,
106+
icon: <CommandIcon />,
110107
}),
111108
[api, enableShortcuts, shortcutKeys.shortcutsPage]
112109
);
@@ -138,7 +135,7 @@ export const useMenu = (
138135
const addonsToggle = useMemo(
139136
() => ({
140137
id: 'A',
141-
title: 'Show addons',
138+
title: 'Show addons panel',
142139
onClick: () => api.togglePanel(),
143140
active: isPanelShown,
144141
right: enableShortcuts ? <Shortcut keys={shortcutKeys.togglePanel} /> : null,
@@ -249,7 +246,7 @@ export const useMenu = (
249246
about,
250247
...(state.whatsNewData?.status === 'SUCCESS' ? [whatsNew] : []),
251248
documentation,
252-
shortcuts,
249+
...(enableShortcuts ? [shortcuts] : []),
253250
],
254251
[
255252
sidebarToggle,
@@ -284,6 +281,7 @@ export const useMenu = (
284281
next,
285282
collapse,
286283
getAddonsShortcuts,
284+
enableShortcuts,
287285
]
288286
);
289287
};

scripts/bench/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export async function getPreviewPage(page: Page) {
4343
* This loop will keep trying to access the iframe until it's available.
4444
*/
4545
for (let i = 0; i < 10; i++) {
46-
// eslint-disable-next-line @typescript-eslint/no-loop-func
4746
await page.waitForFunction(() => {
4847
return document.querySelector('iframe')?.contentDocument.readyState === 'complete';
4948
});

scripts/event-log-checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ async function run() {
7171
});
7272
}
7373

74-
if (events.length === 0) throw new Error('No events were logged');
74+
if (events.length === 0) {
75+
throw new Error('No events were logged');
76+
}
7577
const [bootEvent, mainEvent] = definition.noBoot ? [null, events[0]] : events;
7678

7779
const storybookVersion = versions.storybook;

0 commit comments

Comments
 (0)