|
| 1 | +--- |
| 2 | +title: GlobalFindBar |
| 3 | +description: Floating search bar for notebook-wide find functionality with keyboard navigation |
| 4 | +icon: Search |
| 5 | +--- |
| 6 | + |
| 7 | +import { Tab, Tabs } from 'fumadocs-ui/components/tabs'; |
| 8 | +import { RegistrySetup } from '@/components/docs/registry-setup'; |
| 9 | +import { GlobalFindBarDemo } from '@/app/components/GlobalFindBarDemo'; |
| 10 | + |
| 11 | +<div className="my-8"> |
| 12 | + <GlobalFindBarDemo /> |
| 13 | +</div> |
| 14 | + |
| 15 | +A floating search bar component for notebook-wide find functionality. Supports keyboard navigation for efficient searching through cells. |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +- **Keyboard navigation**: Enter for next match, Shift+Enter for previous, Escape to close |
| 20 | +- **Match counter**: Shows current position and total matches |
| 21 | +- **Auto-focus**: Input field is focused and selected on mount |
| 22 | +- **Accessible**: Full ARIA labels and keyboard support |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +<Tabs items={['CLI', 'Manual']}> |
| 27 | + <Tab value="CLI"> |
| 28 | + ```bash |
| 29 | + npx shadcn@latest add @nteract/global-find-bar |
| 30 | + ``` |
| 31 | + <RegistrySetup /> |
| 32 | + </Tab> |
| 33 | + <Tab value="Manual"> |
| 34 | + Copy from [nteract/elements](https://github.com/nteract/elements/blob/main/registry/cell/GlobalFindBar.tsx). |
| 35 | + </Tab> |
| 36 | +</Tabs> |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +```tsx |
| 41 | +import { GlobalFindBar } from "@/components/cell/GlobalFindBar" |
| 42 | + |
| 43 | +function NotebookHeader() { |
| 44 | + const [query, setQuery] = useState("") |
| 45 | + const [matches, setMatches] = useState<Match[]>([]) |
| 46 | + const [currentMatch, setCurrentMatch] = useState(0) |
| 47 | + |
| 48 | + return ( |
| 49 | + <GlobalFindBar |
| 50 | + query={query} |
| 51 | + matchCount={matches.length} |
| 52 | + currentMatchIndex={currentMatch} |
| 53 | + onQueryChange={setQuery} |
| 54 | + onNextMatch={() => setCurrentMatch((i) => (i + 1) % matches.length)} |
| 55 | + onPrevMatch={() => setCurrentMatch((i) => (i - 1 + matches.length) % matches.length)} |
| 56 | + onClose={() => setShowFindBar(false)} |
| 57 | + /> |
| 58 | + ) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Integration with CodeMirror |
| 63 | + |
| 64 | +GlobalFindBar pairs well with the `searchHighlight` CodeMirror extension for highlighting matches within cells: |
| 65 | + |
| 66 | +```tsx |
| 67 | +import { GlobalFindBar } from "@/components/cell/GlobalFindBar" |
| 68 | +import { searchHighlight } from "@/components/editor/search-highlight" |
| 69 | +import { CodeMirrorEditor } from "@/components/editor/codemirror-editor" |
| 70 | + |
| 71 | +function SearchableNotebook() { |
| 72 | + const [query, setQuery] = useState("") |
| 73 | + const [activeOffset, setActiveOffset] = useState(-1) |
| 74 | + |
| 75 | + return ( |
| 76 | + <> |
| 77 | + <GlobalFindBar |
| 78 | + query={query} |
| 79 | + matchCount={matchCount} |
| 80 | + currentMatchIndex={currentMatch} |
| 81 | + onQueryChange={setQuery} |
| 82 | + onNextMatch={handleNextMatch} |
| 83 | + onPrevMatch={handlePrevMatch} |
| 84 | + onClose={() => setQuery("")} |
| 85 | + /> |
| 86 | + |
| 87 | + {cells.map((cell) => ( |
| 88 | + <CodeMirrorEditor |
| 89 | + key={cell.id} |
| 90 | + value={cell.source} |
| 91 | + extensions={[searchHighlight(query, activeOffset)]} |
| 92 | + /> |
| 93 | + ))} |
| 94 | + </> |
| 95 | + ) |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Props |
| 100 | + |
| 101 | +| Prop | Type | Default | Description | |
| 102 | +| --- | --- | --- | --- | |
| 103 | +| `query` | `string` | — | Current search query | |
| 104 | +| `matchCount` | `number` | — | Total number of matches found | |
| 105 | +| `currentMatchIndex` | `number` | — | Index of the currently active match (0-based) | |
| 106 | +| `onQueryChange` | `(query: string) => void` | — | Called when the search query changes | |
| 107 | +| `onNextMatch` | `() => void` | — | Called when user navigates to next match | |
| 108 | +| `onPrevMatch` | `() => void` | — | Called when user navigates to previous match | |
| 109 | +| `onClose` | `() => void` | — | Called when the find bar is closed | |
| 110 | + |
| 111 | +## Keyboard Shortcuts |
| 112 | + |
| 113 | +| Key | Action | |
| 114 | +| --- | --- | |
| 115 | +| `Enter` | Go to next match | |
| 116 | +| `Shift + Enter` | Go to previous match | |
| 117 | +| `Escape` | Close the find bar | |
0 commit comments