I want to make $
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ,
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 if NOVA hits Direction: hits $
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 +21% by Nov 27 .
Fictional assets and illustrative values. Tap a highlighted word to edit the sentence.
This installs the Nova style for the UI SDK base, which is the only style published for it so far. Keep the style-nova class on <html> so the editors inherit the theme. The registry includes the shared stylesheet and motion dependency. Pills default to the neutral tone and toggles to peach; pass tone to choose. Pass on-step, in-range values to the number editor: the input and slider show the snapped value. Fill Blank has Radix and UI SDK versions; there is no Base UI or React Aria version yet.
Remind me to run
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 Â k m every Tuesday .
Each editor can also stand alone. Lists close their containing pill after a selection; standalone lists keep showing the selected value.
Toggle word before Timing: before
Copy import {
FillBlank, FillBlankPill, FillBlankNumberEditor,
FillBlankSelectEditor, FillBlankDateEditor, FillBlankToggle,
} from "@/components/ui/fill-blank"
< FillBlank >
Run { " " }
< FillBlankPill label = "Distance" value ={ `${ distance } km` } tone = "green" animate >
< FillBlankNumberEditor
label = "How far?" value ={ distance } onValueChange ={ setDistance }
min ={ 1 } max ={ 30 } suffix = " km"
/>
</ FillBlankPill > { " " } today.
</ FillBlank >
Keep literal spaces between words and pills. FillBlank renders a paragraph; its children stay in sentence order. Every pill is a named button with a dialog popup, expanded state, visible focus and at least a 44 px target. Tab visits the pills, Enter or Space opens an editor, and Escape closes it and returns focus. Arrow keys change a slider or move through a list; Enter chooses a row. Toggle words are buttons that cycle between two named options and do not open a popup.
animate uses the existing transitions.dev spinning counter. It respects reduced motion. The caret rotates while its editor is open. The sentence wraps naturally. A very long value can wrap inside its pill rather than overflow on a narrow screen.
FillBlankPill takes label, value, an editor as children, optional tone (yellow, lavender, peach, green, neutral), delta, animate, disabled, open and onOpenChange. Values are controlled by the parent. The delta is decorative; include meaningful delta information in the label when assistive technology needs it.
FillBlankNumberEditor takes a finite value, onValueChange, label, min, max, positive step, optional prefix, suffix, caption and disabled. The input keeps incomplete drafts while typing, then clamps and snaps on blur or Enter. Clearing the input restores the last value on blur. Invalid ranges disable the editor. Number formatting is supplied to the pill separately, so units and currency stay in the caller's control.
FillBlankSelectEditor and FillBlankDateEditor take label, value, onValueChange and options. Each option has a unique value, a plain-text label, optional content slot (including a sparkline), badge, keywords and disabled. Keep interactive elements out of the row slot. searchable shows the search field. Date labels are provided by the caller; the component does not parse dates or assume a timezone.
FillBlankToggle takes a controlled value, onValueChange, a label, and exactly two options with value, label and optional icon. It also accepts tone and disabled.
components/ui/fill-blank.tsxCopy "use client"
import * as React from "react"
import { CaretDownIcon, CheckIcon } from "@phosphor-icons/react"
import { cn } from "@/lib/utils"
import { IconSwap, SpinningCounter } from "@/components/ui/motion"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
Command,
CommandEmpty,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command"
import {
InputGroup,
InputGroupAddon,
InputGroupInput,
} from "@/components/ui/input-group"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import { Slider } from "@/components/ui/slider"
export type FillBlankTone =
| "yellow"
| "lavender"
| "peach"
| "green"
| "neutral"
const EditorContext = React. createContext <() => void >(() => {})
/** Keep literal spaces between words and pills so the sentence reads naturally. */
export function FillBlank ({ className , ... props } : React . ComponentProps < "p" >) {
return (
< p
data-slot = "fill-blank"
className = { cn (className)}
{ ... props}
/>
)
}
type PillProps = {
value : string | number
label : string
children : React . ReactNode
tone ?: FillBlankTone
delta ?: React . ReactNode
animate ?: boolean
disabled ?: boolean
className ?: string
open ?: boolean
onOpenChange ?: ( open : boolean ) => void
}
/** An inline button with a labelled editor dialog. Escape restores focus to it. */
export function FillBlankPill ({
value ,
label ,
children ,
tone = "neutral" ,
delta ,
animate = false ,
disabled ,
className ,
open : controlled ,
onOpenChange ,
} : PillProps ) {
const [ internal , setInternal ] = React. useState ( false )
const open = controlled ?? internal
const titleId = React. useId ()
function change ( next : boolean ) {
if (controlled === undefined ) setInternal (next)
onOpenChange ?.(next)
}
return (
< Popover open = {open} onOpenChange = {change}>
< PopoverTrigger asChild >
< Button
type = "button"
variant = "ghost"
disabled = {disabled}
data-slot = "fill-blank-pill"
data-tone = {tone}
aria-label = { `${ label }: ${ value }` }
className = { cn (className)}
>
< span aria-hidden = "true" >
{animate ? < SpinningCounter value = { String (value)} /> : value}
</ span >
{delta != null && (
< Badge
data-slot = "fill-blank-delta"
variant = "secondary"
aria-hidden = "true"
>
{delta}
</ Badge >
)}
< CaretDownIcon
weight = "bold"
aria-hidden = "true"
className = ""
/>
</ Button >
</ PopoverTrigger >
< PopoverContent
align = "start"
sideOffset = { 10 }
aria-labelledby = {titleId}
data-tone = {tone}
className = ""
onOpenAutoFocus = {( event ) => {
// A list editor with no search field takes focus on its listbox, so arrow keys work at once.
const list = (
event.currentTarget as HTMLElement
). querySelector < HTMLElement >(
"[data-slot=command]:not(:has([data-slot=command-input])) [data-slot=command-list]"
)
if (list) {
event. preventDefault ()
list. focus ()
}
}}
>
< span id = {titleId} className = "sr-only" >
{label}
</ span >
< EditorContext.Provider value = {() => change ( false )}>
{children}
</ EditorContext.Provider >
</ PopoverContent >
</ Popover >
)
}
export type FillBlankNumberEditorProps = {
label : string
value : number
onValueChange : ( value : number ) => void
min ?: number
max ?: number
step ?: number
prefix ?: string
suffix ?: string
caption ?: React . ReactNode
disabled ?: boolean
}
/** Input and slider share a controlled value; empty drafts never emit NaN. */
export function FillBlankNumberEditor ({
label ,
value ,
onValueChange ,
min = 0 ,
max = 100 ,
step = 1 ,
prefix ,
suffix ,
caption ,
disabled ,
} : FillBlankNumberEditorProps ) {
const id = React. useId ()
const [ draft , setDraft ] = React. useState < string | null >( null )
const validRange =
Number. isFinite (min) &&
Number. isFinite (max) &&
max > min &&
Number. isFinite (step) &&
step > 0
const blocked = disabled || ! validRange
const normalize = ( n : number ) =>
Math. min (
max,
Math. max (
min,
Number ((min + Math. round ((n - min) / step) * step). toFixed ( 10 ))
)
)
const safeValue = validRange
? normalize (Number. isFinite (value) ? value : min)
: 0
// A pill closing on an outside click or Escape unmounts the input without a reliable blur,
// so the draft also lives in a ref that the unmount cleanup reads. Committing clears it first.
const draftRef = React. useRef < string | null >( null )
const commitRef = React. useRef <( text : string ) => void >(() => {})
commitRef. current = ( text ) => {
if (text. trim () && Number. isFinite ( Number (text)) && ! blocked)
onValueChange ( normalize ( Number (text)))
}
React. useEffect (
() => () => {
if (draftRef.current !== null ) commitRef. current (draftRef.current)
},
[]
)
function commit () {
const text = draftRef.current
draftRef.current = null
if (text !== null ) commitRef. current (text)
setDraft ( null )
}
return (
< div
data-slot = "fill-blank-number-editor"
className = ""
>
< label htmlFor = {id}>{label}</ label >
< InputGroup size = "lg" >
{prefix && < InputGroupAddon >{prefix}</ InputGroupAddon >}
< InputGroupInput
id = {id}
type = "number"
inputMode = "decimal"
min = {validRange ? min : 0 }
max = {validRange ? max : 100 }
step = {validRange ? step : 1 }
disabled = {blocked}
value = {draft ?? safeValue}
aria-describedby = { `${ id }-hint` }
onBlur = {commit}
onKeyDown = {( event ) => {
if (event.key === "Escape" ) draftRef.current = null
if (event.key === "Enter" ) {
event. preventDefault ()
commit ()
}
}}
onChange = {( event ) => {
// The value is sent on blur, Enter or close; the slider is the live control.
draftRef.current = event.target.value
setDraft (event.target.value)
}}
/>
{suffix && (
< InputGroupAddon align = "inline-end" >{suffix}</ InputGroupAddon >
)}
</ InputGroup >
< span id = { `${ id }-hint` } className = "" >
Type or drag
</ span >
< Slider
aria-label = { `${ label }, slider` }
min = {validRange ? min : 0 }
max = {validRange ? max : 100 }
step = {validRange ? step : 1 }
disabled = {blocked}
value = {[validRange ? safeValue : 0 ]}
onValueChange = {([ next ]) => {
draftRef.current = null
setDraft ( null )
onValueChange (next)
}}
/>
{validRange && (
< div className = "" aria-hidden = "true" >
< span >
{prefix}
{min. toLocaleString ( "en-US" )}
{suffix}
</ span >
< span >
{prefix}
{max. toLocaleString ( "en-US" )}
{suffix}
</ span >
</ div >
)}
{caption != null && (
< div data-slot = "fill-blank-caption" className = "" >
{caption}
</ div >
)}
</ div >
)
}
export type FillBlankOption = {
value : string
label : string
/** Plain searchable text stays available when the visual row is a slot. */
keywords ?: string []
content ?: React . ReactNode
badge ?: React . ReactNode
disabled ?: boolean
}
export type FillBlankListEditorProps = {
label : string
value : string
onValueChange : ( value : string ) => void
options : FillBlankOption []
searchable ?: boolean
}
export function FillBlankSelectEditor ({
label ,
value ,
onValueChange ,
options ,
searchable = false ,
} : FillBlankListEditorProps ) {
const close = React. useContext (EditorContext)
// Without a search field the listbox holds focus, so it names the highlighted row itself.
const [ active , setActive ] = React. useState (value)
React. useEffect (() => setActive (value), [value])
const listRef = React. useRef < HTMLDivElement >( null )
// cmdk writes its own aria-label and tabindex on the list after our props, so these are set here.
React. useEffect (() => {
const list = listRef.current
if ( ! list) return
list. setAttribute ( "aria-label" , label)
if (searchable) return
list.tabIndex = 0
// Items carry data-slot on both bases and, below, their own data-option-value (cmdk's data-value is its business),
// so the list can name its active descendant.
const item = list. querySelector < HTMLElement >(
`[data-slot=command-item][data-option-value="${ CSS . escape ( active ) }"]`
)
if (item?.id) list. setAttribute ( "aria-activedescendant" , item.id)
else list. removeAttribute ( "aria-activedescendant" )
})
// The list drives its own highlight (below), so it also keeps the active row in view.
React. useEffect (() => {
if (searchable) return
listRef.current
?. querySelector < HTMLElement >(
`[data-slot=command-item][data-option-value="${ CSS . escape ( active ) }"]`
)
?. scrollIntoView ({ block: "nearest" })
}, [active, searchable])
// With no search field there is no input for the command to listen on (the UI SDK's Command only handles keys on
// its input, cmdk on its root), so the list drives the highlight itself and both bases behave the same.
const enabled = options. filter (( option ) => ! option.disabled)
const choose = ( next : string ) => {
onValueChange (next)
close ()
}
const onListKeyDown = ( event : React . KeyboardEvent ) => {
if (searchable || enabled. length === 0 ) return
const at = enabled. findIndex (( option ) => option.value === active)
const last = enabled. length - 1
let next : number | null = null
if (event.key === "ArrowDown" ) next = at < 0 ? 0 : at === last ? 0 : at + 1
else if (event.key === "ArrowUp" ) next = at <= 0 ? last : at - 1
else if (event.key === "Home" ) next = 0
else if (event.key === "End" ) next = last
else if (event.key === "Enter" && at >= 0 ) {
event. preventDefault ()
event. stopPropagation ()
choose (enabled[at].value)
return
}
if (next === null ) return
event. preventDefault ()
event. stopPropagation ()
setActive (enabled[next].value)
}
return (
< Command
label = {label}
data-slot = "fill-blank-select-editor"
className = ""
value = {active}
onValueChange = {setActive}
loop
>
{searchable && (
< CommandInput
aria-label = {label}
placeholder = { `Search ${ label . toLowerCase () }` }
/>
)}
< CommandList ref = {listRef} aria-label = {label} onKeyDown = {onListKeyDown}>
< CommandEmpty >
{options. length ? "No options found." : "No options available." }
</ CommandEmpty >
{options. map (( option ) => (
< CommandItem
key = {option.value}
value = {option.value}
keywords = {[option.label, ... (option.keywords ?? [])]}
disabled = {option.disabled}
data-current = {option.value === value || undefined }
data-option-value = {option.value}
onSelect = {() => choose (option.value)}
className = ""
>
< span className = "" >
{option.content ?? option.label}
</ span >
{option.badge != null && (
< Badge variant = "secondary" >{option.badge}</ Badge >
)}
{option.value === value && (
<>
< CheckIcon
weight = "duotone"
aria-hidden = "true"
className = "size-4 shrink-0"
/>
< span className = "sr-only" >(current)</ span >
</>
)}
</ CommandItem >
))}
</ CommandList >
</ Command >
)
}
/** Date labels and badges are supplied by the caller, avoiding timezone guesses. */
export function FillBlankDateEditor ( props : FillBlankListEditorProps ) {
return < FillBlankSelectEditor { ... props} />
}
type ToggleOption = { value : string ; label : string ; icon ?: React . ReactNode }
export function FillBlankToggle ({
value ,
onValueChange ,
options ,
label ,
tone = "peach" ,
disabled ,
className ,
} : {
value : string
onValueChange : ( value : string ) => void
options : readonly [ ToggleOption , ToggleOption ]
label : string
tone ?: FillBlankTone
disabled ?: boolean
className ?: string
}) {
const index = options[ 1 ].value === value ? 1 : 0
if (
process.env. NODE_ENV !== "production" &&
options[ 0 ].value !== value &&
options[ 1 ].value !== value
)
console. warn ( `FillBlankToggle: "${ value }" matches neither option` )
const current = options[index]
return (
<>
< Button
type = "button"
variant = "ghost"
data-slot = "fill-blank-pill"
data-tone = {tone}
className = { cn (className)}
disabled = {disabled}
aria-label = { `${ label }: ${ current . label }. Change to ${ options [ 1 - index ]. label }` }
onClick = {() => onValueChange (options[ 1 - index].value)}
>
< span >{current.label}</ span >
{(options[ 0 ].icon || options[ 1 ].icon) && (
< IconSwap
state = {index ? "b" : "a" }
a = {options[ 0 ].icon}
b = {options[ 1 ].icon}
/>
)}
</ Button >
< span className = "sr-only" aria-live = "polite" >
{ `${ label }: ${ current . label }` }
</ span >
</>
)
}
Expand