Draw your users’ attention to the search box by mimicking the effect of typing queries.
Animated placeholders mimic the effect of typing queries in a passive search box.
They’re a good way of drawing your users’ attention to the search box and suggesting possible queries.
Such a pattern can help increase the usage of your search box and improve and discovery.
To create an animated placeholder, you want to run some code that animates text character by character with pauses between, and injects it into the search box as placeholder text. The length of a pause should be random so it looks like actual typing. The recommended delay range of 50-90 milliseconds looks the most natural.
Change your PLACEHOLDER constant to an array of strings called PLACEHOLDERS,
containing all the placeholder sentences you want to display.
JavaScript
Report incorrect code
Copy
const searchBar = document.querySelector(".ais-SearchBox-input");const DELAY_AFTER_ANIMATION = 1000;const PLACEHOLDER = "This is an animated placeholder"; const PLACEHOLDERS = [ "This is an animated placeholder", "Search for a green hoodie", "Search for our latest item", "Find your favorite movie", ]; const MIN_ANIMATION_DELAY = 50;const MAX_ANIMATION_DELAY = 90;
2
Select random placeholder
Add an onAnimationEnd callback function that selects a random (but different) placeholder string from your PLACEHOLDERS array and calls the animatePlaceholder function with the new string. This function triggers every time the animation ends.