Product
Search bar
The ad's search bar: a Black field with the brand corner, the prompt in White DM Sans and a White Search button inside it. Enter or the button runs the search.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add search-bar<script type="module" src="/spyglass-ds/elements/search-bar.js"></script>Usage
The one search of a page or a product view: searching creatives, ads, brands and creators by asking in words. Usually the first thing on the page, often over the brand light.
Never
- Two search bars on one page
- A colored search button: it is the White primary button
- The search bar inside a card
Adds with it: button
Props
The web component takes the same as attributes or properties (<spyglass-search-bar>); title is heading there.
<SearchBar>
| Prop | Type | Default |
|---|---|---|
value | string | |
defaultValue | string | '' |
onValueChange | (value: string) => void | |
onSearch | (query: string) => void The query, when Enter is pressed or the button clicked. | |
placeholder | string | 'Search creatives' |
buttonLabel | string The button's label. | 'Search' |
label | string The field's accessible name (the placeholder is not one). | 'Search' |
name | string | 'q' |
disabled | boolean | |
className | string |
Examples
The same example in React and as web components; both render identically.
'use client'
import { useState } from 'react'
import { SearchBar } from '@/react/components/product/SearchBar'
// A search with the ad's prompt; the last query shows under it.
export default function Default() {
const [last, setLast] = useState('')
return (
<div className="mx-auto flex w-full max-w-238 flex-col gap-3">
<SearchBar defaultValue="Show me UGC ads for skincare brands using before/after hooks" onSearch={setLast} label="Search creatives" />
<p className="type-caption text-fg-muted" aria-live="polite">{last ? `Searching: ${last}` : 'Ask in words. Enter searches.'}</p>
</div>
)
}<!-- A search with the ad's prompt; the last query shows under it. -->
<div class="mx-auto flex w-full max-w-238 flex-col gap-3">
<spyglass-search-bar id="search-default" value="Show me UGC ads for skincare brands using before/after hooks" aria-label="Search creatives"></spyglass-search-bar>
<p id="search-default-last" class="type-caption text-fg-muted" aria-live="polite">Ask in words. Enter searches.</p>
</div>
<script type="module">
document.getElementById('search-default').addEventListener('search', (e) => {
document.getElementById('search-default-last').textContent = e.detail.query ? `Searching: ${e.detail.query}` : 'Ask in words. Enter searches.'
})
</script>import { BrandLight } from '@/react/components/visuals/BrandLight'
import { SearchBar } from '@/react/components/product/SearchBar'
// As the ad sets it: the search bar across a band of the light.
export default function OnLight() {
return (
<div className="relative overflow-hidden rounded-container">
<BrandLight variant="band" seed={2} cover className="absolute inset-0 size-full" />
<div className="relative px-6 py-14 max-sm:px-4 max-sm:py-10">
<SearchBar placeholder="Show me UGC ads for skincare brands using before/after hooks" label="Search creatives" />
</div>
</div>
)
}<!-- As the ad sets it: the search bar across a band of the light. -->
<div class="relative overflow-hidden rounded-container">
<spyglass-brand-light variant="band" seed="2" cover class="absolute inset-0 size-full"></spyglass-brand-light>
<div class="relative px-6 py-14 max-sm:px-4 max-sm:py-10">
<spyglass-search-bar placeholder="Show me UGC ads for skincare brands using before/after hooks" aria-label="Search creatives"></spyglass-search-bar>
</div>
</div>Source
'use client'
import type { FormEvent, Ref } from 'react'
import { Button } from '@/react/components/ui/Button'
import { useControllable } from '@/react/hooks/useControllable'
import { cn } from '@/react/lib/cn'
// The ad's search bar: a Black field with the brand corner (88px tall on the canvas, 56px here), the prompt in White
// DM Sans and a White "Search" button inside it, 8px in (16px on the canvas). A #666666 hairline lets it read on Black
// and turns White while the field has focus. Enter or the button calls onSearch with the query; the value is
// controlled (value + onValueChange) or kept inside (defaultValue).
export interface SearchBarProps {
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
/** The query, when Enter is pressed or the button clicked. */
onSearch?: (query: string) => void
placeholder?: string
/** The button's label. */
buttonLabel?: string
/** The field's accessible name (the placeholder is not one). */
label?: string
name?: string
disabled?: boolean
className?: string
ref?: Ref<HTMLFormElement>
}
export function SearchBar({ value, defaultValue = '', onValueChange, onSearch, placeholder = 'Search creatives', buttonLabel = 'Search', label = 'Search', name = 'q', disabled, className, ref }: SearchBarProps) {
const [query, setQuery] = useControllable(value, defaultValue, onValueChange)
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault()
if (!disabled) onSearch?.(query)
}
return (
<form data-ds="search-bar" ref={ref} role="search" onSubmit={submit} className={cn('flex h-search w-full items-center gap-2 rounded-control border border-edge-strong bg-surface pr-2 pl-4 transition-[border-color] duration-160 ease-standard has-[input:focus-visible]:border-edge-focus', className)}>
<input
type="search"
name={name}
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder={placeholder}
aria-label={label}
disabled={disabled}
autoComplete="off"
className="h-full min-w-0 flex-1 bg-transparent type-body text-fg outline-none placeholder:text-fg-muted disabled:cursor-not-allowed [&::-webkit-search-cancel-button]:hidden"
/>
<Button type="submit" disabled={disabled}>{buttonLabel}</Button>
</form>
)
}
export default SearchBar'use client'
import { useCallback, useRef, useState } from 'react'
// Vue's defineModel in React: a value that is controlled when the caller passes it (value + onValueChange, as Radix
// does) and kept inside otherwise (defaultValue). setValue calls onChange either way.
export function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (value: T) => void) {
const [inner, setInner] = useState<T>(defaultValue)
const controlled = value !== undefined
const current = controlled ? value : inner
const ref = useRef(onChange)
ref.current = onChange
const set = useCallback((next: T) => {
if (!controlled) setInner(next)
ref.current?.(next)
}, [controlled])
return [current, set] as const
}// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html } from 'lit'
import { live } from 'lit/directives/live.js'
import { DsFormElement, define, HostLayoutController } from '@/elements/lib/element'
import '@/elements/components/ui/button'
import styles from 'virtual:tw'
// SearchBar.tsx as <spyglass-search-bar>: the ad's search bar, a Black field with the brand corner (56px), the prompt
// in White DM Sans and a White Search button inside it, 8px in. A #666666 hairline turns White while the field has
// focus. Enter or the button fires `search` with the query in event.detail.query; the value is `value` with the native
// `input` and `change` events, and it goes with a surrounding form under `name`.
/**
* @summary The search bar: a Black field with a White Search button inside.
* @fires search - Enter or the button: detail.query is the text.
* @fires input - On every keystroke (read `value`).
*/
export class SearchBar extends DsFormElement {
static styles = [DsFormElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('form'))
static properties = {
value: {},
placeholder: {},
buttonLabel: { attribute: 'button-label' },
label: { attribute: 'aria-label' },
name: {},
disabled: { type: Boolean, reflect: true },
}
value = ''
placeholder = 'Search creatives'
/** The button's label. */
buttonLabel = 'Search'
/** The field's accessible name (aria-label on the element). */
label = 'Search'
name = 'q'
disabled = false
connectedCallback() {
super.connectedCallback()
if (!this.value && this.getAttribute('value')) this.value = this.getAttribute('value')!
}
protected updated(changed: Map<PropertyKey, unknown>) {
super.updated(changed)
if (changed.has('value')) this.internals.setFormValue(this.value)
}
formResetCallback() {
this.value = this.getAttribute('value') ?? ''
}
private onInput(event: Event) {
this.value = (event.target as HTMLInputElement).value
}
private onSubmit(event: Event) {
event.preventDefault()
if (!this.inactive) this.emit('search', { query: this.value })
}
render() {
return html`<form data-ds="search-bar" role="search" class="flex h-search w-full items-center gap-2 rounded-control border border-edge-strong bg-surface pr-2 pl-4 transition-[border-color] duration-160 ease-standard has-[input:focus-visible]:border-edge-focus" @submit=${this.onSubmit}><input type="search" name=${this.name} .value=${live(this.value)} placeholder=${this.placeholder} aria-label=${this.label} ?disabled=${this.inactive} autocomplete="off" class="h-full min-w-0 flex-1 bg-transparent type-body text-fg outline-none placeholder:text-fg-muted disabled:cursor-not-allowed [&::-webkit-search-cancel-button]:hidden" @input=${this.onInput}><spyglass-button type="submit" ?disabled=${this.inactive}>${this.buttonLabel}</spyglass-button></form>`
}
}
define('search-bar', SearchBar)Reference: Brand assets (Figma page 8): Ad, The only creative search engine: Search bar (952x88, radius 10, Black), Button, Search (136x56, 16px in), prompt in White DM Sans 24px.