Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions src/components/markdown/pokelist.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
---
---

<div id="pokeapi-explorer" class="pc-only">
<div class="toolbar not-content">
<div class="sl-search-wrap">
<svg
aria-hidden="true"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
<input
type="search"
id="name-filter"
placeholder="Filter by prefix..."
aria-label="Filter items starting with"
autocomplete="off"
/>
</div>
</div>

<div class="columns not-content">
<section id="section-species">
<h2>pokemon-species (<span id="count-species">0</span>)</h2>
<ul id="list-species"></ul>
</section>

<section id="section-pokemon">
<h2>pokemon (<span id="count-pokemon">0</span>)</h2>
<ul id="list-pokemon"></ul>
</section>

<section id="section-forms">
<h2>pokemon-form (<span id="count-forms">0</span>)</h2>
<ul id="list-forms"></ul>
</section>
</div>
</div>

<script>
const BASE = "https://pokeapi.co/api/v2";
const filterInput = document.getElementById("name-filter") as HTMLInputElement | null;

const sections = {
species: {
endpoint: "pokemon-species",
list: document.getElementById("list-species"),
count: document.getElementById("count-species"),
items: [] as string[],
},
pokemon: {
endpoint: "pokemon",
list: document.getElementById("list-pokemon"),
count: document.getElementById("count-pokemon"),
items: [] as string[],
},
forms: {
endpoint: "pokemon-form",
list: document.getElementById("list-forms"),
count: document.getElementById("count-forms"),
items: [] as string[],
},
};

async function fetchAllNames(endpoint: string): Promise<string[]> {
const res = await fetch(`${BASE}/${endpoint}/?limit=100000&offset=0`);
if (!res.ok) throw new Error(`${endpoint}: HTTP ${res.status}`);
const data = await res.json();
return (data.results || []).map((item: { name: string }) => item.name);
}

function renderFilteredColumn(
listEl: HTMLElement | null,
countEl: HTMLElement | null,
allNames: string[],
prefix: string
) {
const normalizedPrefix = prefix.trim().toLowerCase();
const filtered = normalizedPrefix
? allNames.filter((name) => name.toLowerCase().startsWith(normalizedPrefix))
: allNames;

if (countEl) countEl.textContent = String(filtered.length);
if (!listEl) return;

const frag = document.createDocumentFragment();
for (const name of filtered) {
const li = document.createElement("li");
li.textContent = name;
frag.appendChild(li);
}
listEl.replaceChildren(frag);
}

function applyFilter() {
const query = filterInput ? filterInput.value : "";
renderFilteredColumn(
sections.species.list,
sections.species.count,
sections.species.items,
query
);
renderFilteredColumn(
sections.pokemon.list,
sections.pokemon.count,
sections.pokemon.items,
query
);
renderFilteredColumn(
sections.forms.list,
sections.forms.count,
sections.forms.items,
query
);
}

async function loadAll() {
try {
const [speciesNames, pokemonNames, formNames] = await Promise.all([
fetchAllNames(sections.species.endpoint),
fetchAllNames(sections.pokemon.endpoint),
fetchAllNames(sections.forms.endpoint),
]);

sections.species.items = speciesNames;
sections.pokemon.items = pokemonNames;
sections.forms.items = formNames;

applyFilter();
} catch (err) {
console.error(err);
}
}

filterInput?.addEventListener("input", applyFilter);

if (document.readyState === "complete") {
loadAll();
} else {
window.addEventListener("load", loadAll, { once: true });
}
</script>

<style>
.pc-only {
display: none;
}

@media (min-width: 1024px) {
.pc-only {
display: block;
}

.toolbar {
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 0.75rem;
}

/* Matches Starlight's site search bar */
.sl-search-wrap {
position: relative;
display: flex;
align-items: center;
flex: 1;
min-width: 180px;
height: 2.35rem;
border: 1px solid var(--sl-color-gray-5);
border-radius: 0.5rem;
padding-inline: 0.75rem;
background-color: var(--sl-color-black);
color: var(--sl-color-gray-2);
transition: border-color 0.15s ease;
}

.sl-search-wrap:focus-within {
border-color: var(--sl-color-accent-high);
}

.sl-search-wrap svg {
flex-shrink: 0;
margin-right: 0.5rem;
color: var(--sl-color-gray-3);
}

#name-filter {
width: 100%;
border: 0;
outline: none;
background: transparent;
color: var(--sl-color-white);
font-size: var(--sl-text-sm, 0.875rem);
}

#name-filter::placeholder {
color: var(--sl-color-gray-3);
}

.columns {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 0.75rem;
align-items: start;
}

.columns > section {
min-width: 0;
border: 1px solid var(--sl-color-hairline);
border-radius: 0.5rem;
padding: 0.65rem 0.75rem;
background-color: var(--sl-color-bg-nav, transparent);
}

.columns h2 {
font-size: 0.9rem;
margin: 0 0 0.5rem 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

ul {
list-style: none;
height: 360px;
overflow-y: auto;
margin: 0;
padding: 0;
font-size: 0.85rem;
}

li {
overflow-wrap: anywhere;
}
}
</style>
8 changes: 8 additions & 0 deletions src/content/docs/how-tos/pokemon.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ Roughly 85–90% of Pokémon have one variety and one form, yet building a full
3. **CSV Data Pipeline**: Data is maintained in cross-referenced CSV tables across every generation; changing core foreign keys requires migrating thousands of rows.
4. **Backwards Compatibility**: Flattening or renaming `/api/v2` endpoints would break existing apps, bots, and tutorials unless a separate `/api/v3` were maintained alongside it (see [PokeAPI Issue #1086](https://github.com/PokeAPI/pokeapi/issues/1086)).

## Interactive search

To better understand how pokemons are mapped into the three categories the following search functionality can be used to query simoultaneously all the three resources. Good search examples are: _arceus_, _deoxys_, _minior_, _wooper_, _floette_.

import PokeLists from "../../../components/markdown/pokelist.astro";

<PokeLists />

---

## Next Steps
Expand Down