From 6069b7b63012b410fbf231bc44e1e1f5099d0a09 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Wed, 10 May 2023 09:50:43 +0200 Subject: [PATCH] Merge pull request #21619 from gitstart/ui/show-current-search-shortcut UI: Show current search shortcut in search box sidebar --- .../src/components/sidebar/Search.stories.tsx | 34 +++++++++++++++---- .../manager/src/components/sidebar/Search.tsx | 7 ++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/code/ui/manager/src/components/sidebar/Search.stories.tsx b/code/ui/manager/src/components/sidebar/Search.stories.tsx index 00f8ba61d3c5..a85b50147407 100644 --- a/code/ui/manager/src/components/sidebar/Search.stories.tsx +++ b/code/ui/manager/src/components/sidebar/Search.stories.tsx @@ -1,4 +1,7 @@ import React from 'react'; +import type { StoryFn, Meta } from '@storybook/react'; +import type { API } from '@storybook/manager-api'; +import { ManagerContext } from '@storybook/manager-api'; import { action } from '@storybook/addon-actions'; import { index } from './mockdata.large'; @@ -16,12 +19,13 @@ const getLastViewed = () => .filter((item, i) => item.type === 'component' && item.parent && i % 20 === 0) .map((component) => ({ storyId: component.id, refId })); -export default { +const meta = { component: Search, title: 'Sidebar/Search', parameters: { layout: 'fullscreen', withSymbols: true }, decorators: [(storyFn: any) =>
{storyFn()}
], -}; +} satisfies Meta; +export default meta; const baseProps = { dataset, @@ -29,15 +33,15 @@ const baseProps = { getLastViewed: () => [] as Selection[], }; -export const Simple = () => {() => null}; +export const Simple: StoryFn = () => {() => null}; -export const FilledIn = () => ( +export const FilledIn: StoryFn = () => ( {() => } ); -export const LastViewed = () => ( +export const LastViewed: StoryFn = () => ( {({ query, results, closeMenu, getMenuProps, getItemProps, highlightedIndex }) => ( ( ); -export const ShortcutsDisabled = () => ( +export const ShortcutsDisabled: StoryFn = () => ( {() => null} ); + +export const CustomShortcuts: StoryFn = () => {() => null}; + +CustomShortcuts.decorators = [ + (storyFn) => ( + ({ search: ['control', 'shift', 's'] }), + } as API, + } as any + } + > + {storyFn()} + + ), +]; diff --git a/code/ui/manager/src/components/sidebar/Search.tsx b/code/ui/manager/src/components/sidebar/Search.tsx index 4e3958d5449e..9bd4b6839954 100644 --- a/code/ui/manager/src/components/sidebar/Search.tsx +++ b/code/ui/manager/src/components/sidebar/Search.tsx @@ -1,5 +1,5 @@ /* eslint-disable import/no-cycle */ -import { useStorybookApi } from '@storybook/manager-api'; +import { useStorybookApi, shortcutToHumanString } from '@storybook/manager-api'; import { styled } from '@storybook/theming'; import { Icons } from '@storybook/components'; import type { DownshiftState, StateChangeOptions } from 'downshift'; @@ -115,7 +115,7 @@ const FocusKey = styled.code(({ theme }) => ({ position: 'absolute', top: 8, right: 16, - width: 16, + minWidth: 16, height: 16, zIndex: 1, lineHeight: '16px', @@ -165,6 +165,7 @@ export const Search = React.memo<{ const inputRef = useRef(null); const [inputPlaceholder, setPlaceholder] = useState('Find components'); const [allComponents, showAllComponents] = useState(false); + const searchShortcut = api ? shortcutToHumanString(api.getShortcutKeys().search) : '/'; const selectStory = useCallback( (id: string, refId: string) => { @@ -354,7 +355,7 @@ export const Search = React.memo<{ {/* @ts-expect-error (TODO) */} - {enableShortcuts && /} + {enableShortcuts && {searchShortcut}} clearSelection()} />