diff --git a/.changeset/tidy-boxes-sell.md b/.changeset/tidy-boxes-sell.md index 52d95fd8f8..82ee16af57 100644 --- a/.changeset/tidy-boxes-sell.md +++ b/.changeset/tidy-boxes-sell.md @@ -1,5 +1,18 @@ --- -'@backstage/plugin-search': patch +'@backstage/plugin-search': minor --- -SearchModal invoke search only when visible +**BREAKING**: `useSearch` doesn't return anymore `open` and `toggleModal`. +The two properties have been moved to the `useSearchModal` hook. + +``` +import { SearchModal, useSearchModal } from '@backstage/plugin-search'; + +const Foo = () => { + const { state, setOpen, toggleModal } = useSearchModal(); + + return ( + + ); +}; +``` diff --git a/docs/features/search/getting-started.md b/docs/features/search/getting-started.md index 285e399cb4..7c3717e22e 100644 --- a/docs/features/search/getting-started.md +++ b/docs/features/search/getting-started.md @@ -114,15 +114,13 @@ const routes = ( In `Root.tsx`, add the `SidebarSearchModal` component: ```bash -import { SidebarSearchModal, SearchContextProvider } from '@backstage/plugin-search'; +import { SidebarSearchModal } from '@backstage/plugin-search'; export const Root = ({ children }: PropsWithChildren<{}>) => ( - - - + ... ``` diff --git a/plugins/search/api-report.md b/plugins/search/api-report.md index 7533085fce..10ce787a6c 100644 --- a/plugins/search/api-report.md +++ b/plugins/search/api-report.md @@ -327,4 +327,14 @@ export type SidebarSearchProps = { // // @public (undocumented) export const useSearch: () => SearchContextValue; + +// @public +export function useSearchModal(initialState?: boolean): { + state: { + hidden: boolean; + open: boolean; + }; + toggleModal: () => void; + setOpen: (open: boolean) => void; +}; ``` diff --git a/plugins/search/src/components/SearchModal/SearchModal.stories.tsx b/plugins/search/src/components/SearchModal/SearchModal.stories.tsx index 1cf0506acb..aa0ad0479a 100644 --- a/plugins/search/src/components/SearchModal/SearchModal.stories.tsx +++ b/plugins/search/src/components/SearchModal/SearchModal.stories.tsx @@ -16,10 +16,11 @@ import { wrapInTestApp } from '@backstage/test-utils'; import { Button } from '@material-ui/core'; -import React, { ComponentType, useState } from 'react'; +import React, { ComponentType } from 'react'; import { rootRouteRef } from '../../plugin'; import { SearchApiProvider } from '../SearchContext/SearchContextForStorybook.stories'; import { SearchModal } from './SearchModal'; +import { useSearchModal } from './useSearchModal'; const mockResults = { results: [ @@ -65,23 +66,14 @@ export default { }; export const Default = () => { - const [state, setState] = useState({ hidden: true, opened: false }); - const handleToggleModal = () => - setState(previousState => ({ - opened: true, - hidden: !previousState.hidden, - })); + const { state, toggleModal } = useSearchModal(); return ( <> - -