AI搜索栏生成器。

描述用户会搜索什么,MeDo即生成完整组件 — 防抖查询、联想下拉、方向键导航、最近搜索、加载与无结果状态,以及用于聚焦的 cmd+K 快捷键。

搜索栏
搜索栏

搜索栏是唯一一个视觉焦点与 DOM 焦点不一致的输入控件

页面上其他所有元素的焦点和高亮都停留在同一处。联想搜索做不到:光标必须留在文本框里,用户才能继续输入,而高亮要沿着建议列表向下移动。正确的模式是 aria-activedescendant,输入框保留 DOM 焦点,并通过 id 指向当前激活的选项。那些把真实焦点移入列表的实现会立刻破坏输入,这也是为什么许多手写搜索栏用鼠标能用、用键盘就崩。把输入框和列表作为一个组件一起生成,意味着这层关系从一开始就接好了,而不是等收到缺陷报告后再打补丁。

6 个模板

可以生成的 搜索栏 模板

每个模板都是真实的提示词,而不是截图。复制到任意 AI 编辑器,或在 MeDo 中运行,直接拿到 React + Tailwind 代码。

BasicInput + Clear

Plain Search Field With Submit

No dropdown, no fetch — just a field that submits to a results page. Start here when the corpus is large enough that suggestions would be guesses rather than shortcuts.

Create a search field: a 40px-tall rounded input inside a real form with role="search", type="search" on the input, enterkeyhint="search" for mobile keyboards, and a visible label or aria-label since a magnifier icon is not an accessible name. Put a 16px magnifier on the left with aria-hidden, and a clear button on the right that appears only once there is text, is a real button with an accessible label, and returns focus to the input after clearing. Enter submits the raw query through the form so search still works with JavaScript disabled. Escape clears the field when it has text. Show a focus-visible ring on the wrapper, not the bare input, so the icon and field read as one control.

在 MeDo 中尝试
ComboboxTypeahead

Debounced Suggestions Listbox

The full combobox: debounce, listbox, aria-activedescendant. The critical rule is that DOM focus never leaves the input, or typing stops working the moment someone presses Arrow Down.

Create a typeahead search bar using the ARIA combobox pattern: aria-expanded, aria-controls and aria-activedescendant on the input, role="listbox" on the panel, role="option" with a unique id on each row. DOM focus must stay in the text field at all times — move the visual highlight by updating aria-activedescendant, never by focusing an option, or typing breaks. Debounce queries at 250ms and cancel in-flight requests so a slow earlier response cannot overwrite a newer one. Arrow Down and Up move the active option and wrap at the ends, Home and End jump to the first and last, Enter selects the active option or submits the raw query when none is active, Escape closes the panel and restores the typed text, and Tab closes and moves on rather than walking the options.

在 MeDo 中尝试
cmd+KCommand Palette

Modal Search Overlay

Search as primary navigation, opened from anywhere with cmd+K. It is a modal dialog, which means a focus trap and focus restoration — the part most palette clones skip.

Create a command-palette search overlay opened by cmd+K and ctrl+K: a centered dialog with a backdrop, using the native dialog element or role="dialog" with aria-modal="true". Trap focus inside it, place initial focus in the input, close on Escape and backdrop click, and return focus to the element that was focused before opening. Ignore the shortcut while the user is typing in another input or a contenteditable region. Inside, run the ARIA combobox pattern with aria-activedescendant, group results under headed sections such as Pages, Docs and Actions, and let Arrow Down and Up move across group boundaries while skipping the group headings. Show a visible cmd+K hint in the trigger and keyboard hints in the dialog footer.

在 MeDo 中尝试
StatesRecent + Empty

Search With Recent Queries and No-Results

Three panels, not one: recent searches on an empty query, skeleton rows in flight, a no-results row with a fallback. Leave any out and the dropdown flickers blank between keystrokes.

Create a search bar that renders three distinct dropdown states rather than one panel that empties. On an empty query, show up to five recent searches from localStorage under a Recent heading, each with a small remove button and a Clear all action. While a request is in flight, keep the previous results visible and overlay three skeleton rows, and swap the magnifier for an inline spinner rather than blanking the panel. On zero matches, show a no-results row naming the query plus a "Search everything" fallback action that submits the raw text. Announce the outcome in an aria-live="polite" region — "8 results available" or "No results" — debounced so intermediate counts are not read out. Arrow keys, Enter and Escape behave identically in all three states.

在 MeDo 中尝试
FiltersScoped Search

Search With Scope Chips and Filters

A scope chip inside the field narrows the query before it runs. Make Backspace on an empty query remove the chip — anything else and people are hunting for a tiny × with the mouse.

Create a scoped search bar: removable scope chips render inside the input wrapper to the left of the text caret, each a button with an accessible label such as "Remove filter: Invoices". Pressing Backspace with an empty query removes the last chip rather than doing nothing, and typing a known prefix followed by a colon converts it into a chip. Keep the chips out of the input value itself and send them as separate query parameters. Below the field, render filter pills that are toggle buttons with aria-pressed, and re-run the debounced query on every change. Preserve the caret position when a chip is added, keep the ARIA combobox wiring for the suggestion listbox, and reflect the active scope in the aria-live result-count announcement.

在 MeDo 中尝试
Media rowsRich Results

Suggestions With Thumbnails and Prices

Product and media rows where each suggestion carries an image, title and price. Reserve the thumbnail box explicitly, or every keystroke reflows the panel as images resolve.

Create a search bar with rich suggestion rows: each row is a role="option" containing a 40px thumbnail with explicit width and height attributes so the panel does not reflow as images load, a title with the matched substring bolded, and a secondary line with price or category. Because the row holds several elements, give each option an aria-label describing the whole row so a screen reader hears one coherent option rather than fragments. Cap the panel at 320px with internal scroll, and scroll the active option into view with block nearest as aria-activedescendant moves. Keep the last row a "See all results for <query>" action that submits the raw text. Highlight matches by wrapping the substring, never by injecting HTML from the response.

在 MeDo 中尝试

如何自定义你的 搜索栏

明确说出防抖间隔

说"防抖250ms",而不是"给输入框加防抖"。低于约150ms,每敲一个键就会发一次请求;超过约400ms,下拉会让人觉得坏了。同时配上请求取消,避免较慢的旧响应覆盖更新的结果。

分别描述三种空状态

查询为空、请求进行中、结果为零是三个不同的界面。第一种要最近搜索,第二种要加载指示器或骨架行,第三种要无结果行加兜底操作。少写任何一种,你就会得到一个闪成空白的面板。

说明 Enter 是提交还是选中

有建议处于高亮时,Enter 应选中它;没有任何高亮时,Enter 应把原始查询提交到结果页。把两条分支都写清楚,并要求使用带 role="search" 的真实 form,让提交路径在没有 JavaScript 时也能工作。

要求播报结果数量

视力正常的用户能看到列表变长。读屏器用户需要一个 aria-live="polite" 区域播报类似"有8条结果"的信息。明确要求这个实时区域,并要求它同样做防抖,以免把每个中间数量都念出来。

谁在使用 搜索栏 组件

文档站点

一个 cmd+K 搜索栏,覆盖在页面之上打开,按章节归组命中结果,在每个标题下显示匹配的那一行,并保留最近五次查询,方便用户跳回只记得个大概的页面。

电商店铺

每行含缩略图、名称和价格的商品建议,上方是分类快捷入口,底部有一行"查看…的全部结果",把原始查询提交到完整结果页。

仪表盘或后台工具

一个输入框同时搜索客户、发票和设置,结果按类型归组,输入框内带范围标签,让用户在输入前先收窄到单一实体。

内容或媒体库

一条宽搜索栏,旁边是筛选胶囊,查询运行时显示骨架行,无结果状态给出拼写纠正建议,而不是摆一个空面板。

不同网站的 搜索栏 方案

同一个 搜索栏 组件,针对它所在网站的类型做调整。

01 / 06

Documentation Search

A cmd+K palette that groups hits by section and shows the matching line under each title, since a page title alone rarely tells a reader whether the answer is on it. Keep the last five queries — docs search is mostly people returning to a page they half remember.

搜索栏 的风格与变体

挑选与页面其余部分匹配的变体,然后生成它。

01 / 06

Inline Field Search

A permanent bordered input in the header or page body. The most discoverable option and the only one that works without JavaScript, since a real form with role="search" submits on Enter regardless. Give the wrapper the focus ring so the icon and field read as one control.

在 React、Next.js、Vue 与 Svelte 中使用 搜索栏

React、Next.js、Vue 3、SvelteKit、Astro 或纯 HTML —— 同一个 搜索栏,六种接入方式。

01 / 06

React Search Bar

Track the active index as a number and derive aria-activedescendant from it — that is the entire highlight mechanism, with no focus moves. Abort the previous fetch in the effect cleanup so a slow response cannot land after a newer one.

src/components/SearchBar.tsx

const [active, setActive] = useState(-1)
const debounced = useDebounce(query, 250)

<input
  type="search"
  role="combobox"
  aria-expanded={open}
  aria-controls="search-listbox"
  aria-activedescendant={active >= 0 ? `opt-${active}` : undefined}
  onKeyDown={onArrowKeys}
/>

如何把 搜索栏 加入项目

四个步骤,从挑选 搜索栏 模板到上线生产环境。

01~30s

Pick a Search Pattern

Decide whether you need suggestions at all. A plain field posting to a results page is often the honest answer; reach for the typeahead or the cmd+K palette only when search is a primary navigation path.

02~10s

Copy the Prompt

Take the prompt into MeDo, Lovable, Bolt, v0 or Cursor, then set two numbers: the debounce interval and how many suggestions the panel shows. Name what is being searched too, since that decides how a result row is shaped.

03~1min

Generate and Refine

You get React + Tailwind back with a live preview. Follow up in plain English — "add recent searches", "group results by type", "announce the result count politely" — rather than rewriting the keydown handler.

04~2min

Connect Your Index and Ship

Point the query at your real endpoint, then test with the keyboard only: type, Arrow Down, Enter, Escape. If typing stops working after Arrow Down, focus was moved into the list and the highlight needs to go back to aria-activedescendant.

关于 搜索栏 的常见问题

如何用AI生成搜索栏?

描述搜索的对象、建议数据来自哪里,以及你需要哪些状态 — 最近搜索、加载中、无结果。MeDo会把输入框、建议下拉、防抖查询处理和键盘绑定作为一个组件一起生成,而不是给你一个还得自己接线的纯文本框。

搜索栏和自动补全有什么区别?

搜索栏提交自由文本查询,把建议当作快捷方式,因此输入没有匹配项的内容仍然有效。自动补全会把取值限制在一个列表内,只有选择某个选项时输入才算确定。这个区别会改变标记结构:搜索栏应放在带 role="search" 的 form 中,而受限的自动补全是一个绑定到表单值的 combobox。

AI能生成无障碍的搜索栏吗?

可以,只要你点名要这个模式。MeDo实现 ARIA combobox 模式:输入框上使用 aria-expanded、aria-controls 和 aria-activedescendant,面板上使用 role="listbox" 与 role="option",并为结果数量提供 polite 实时区域。焦点始终留在文本框内,这正是让输入和方向键同时可用的关键。

搜索下拉的键盘导航应该怎么设计?

向下方向键移动到第一条建议并继续向下走,向上方向键回退并可以把焦点交还给原始查询,Enter 选中高亮项,Escape 关闭面板并恢复用户输入的内容。长列表还可以加上 Home 和 End。Tab 应关闭面板并移到下一个控件,而不是逐个遍历选项。

输入框应该用 type="search" 吗?

应该。它在部分平台上提供浏览器自带的清除功能,向辅助技术传达意图,并可与移动键盘上的 enterkeyhint="search" 搭配。把它包在带 role="search" 的真实 form 中,这样脚本失效时查询仍能提交;同时给输入框加上 label 或 aria-label,因为单独的放大镜图标不构成无障碍名称。

搜索栏需要 cmd+K 快捷键吗?

在搜索属于主要导航路径的场景中很有帮助,比如文档、仪表盘和开发者工具。同时绑定 cmd+K 和 ctrl+K,用户正在其他输入框中打字时忽略该快捷键,并在输入框内显示提示以便被发现。在给 MeDo 的提示词中提到它,监听器和可见提示会一起生成。