Infinity Scroll includes a built-in keyword search that queries the Unsplash search API. When a search is submitted, the existing gallery is cleared and replaced with results matching the term. Infinite scroll then continues to load additional matching results as the user scrolls β the search context is preserved until explicitly cleared.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ClaytonSeager/InifinityScroll/llms.txt
Use this file to discover all available pages before exploring further.
Using the search bar
The search UI is a pill-shaped bar containing a plain text input and a button:- Click the π button β fires the
clickevent on#search-button - Press Enter β the
keypresslistener on#search-inputdetectse.key === 'Enter'
handleSearch() function:
What handleSearch() does
| Line | Purpose |
|---|---|
query = searchInput.value | Stores the search term in the module-level query variable, which getApiUrl() reads to choose the correct API endpoint |
imageContainer.innerHTML = '' | Wipes the #Image__Container DOM node so the new results start from a clean slate |
totalLoadedImages = 0 | Resets the running total so the counter reflects only the current searchβs results |
imageCounter.textContent = '0' | Immediately syncs the visible counter badge to the reset value |
loader.hidden = false | Makes the full-screen loading overlay visible before the fetch begins |
getPhotos() | Initiates the API call; getApiUrl() will now build a search URL because query is non-empty |
How search affects the API URL
ThegetApiUrl() function inspects query to choose between the Unsplash random-photos endpoint and the search endpoint:
| State | URL used | Example |
|---|---|---|
query is a non-empty string | https://api.unsplash.com/search/photos/?query=<term>&client_id=<KEY>&count=<count> | β¦?query=mountains&client_id=β¦&count=10 |
query is '' (empty string) | https://api.unsplash.com/photos/random/?client_id=<KEY>&count=<count> | β¦/random/?client_id=β¦&count=10 |
Search results parsing
The two Unsplash endpoints return data in different shapes. The random endpoint returns a top-level array, while the search endpoint wraps results inside aresults property. A single ternary in getPhotos() normalises both:
- Search response (
queryis set):data.resultsβ an array of photo objects nested inside the search response wrapper. - Random response (
queryis''):dataβ the response body is itself the array of photo objects.
photosArray always holds a plain array of photo objects regardless of which endpoint was called, so displayPhotos() can iterate it without any further branching.
Clearing a search
To return to the random photo feed, the user empties the search input and submits (either by clicking the button or pressing Enter). WhensearchInput.value is an empty string, handleSearch() sets query = ''. getApiUrl() then evaluates query as falsy and returns the random endpoint URL β random photos resume from the next fetch onward.
This implementation does not use cursor-based pagination for search results. Each scroll event beyond the threshold triggers a fresh call to the search endpoint with the same query term, returning a new set of up to
count matching images. The Unsplash API may return a different selection of results on each call.