Tutorial
Components

Components

Creating a stand-alone component

Let’s then create the button needed for displaying the list of Pokémon.

Create the following file src/components/PokemonLink.tsx and fill the content with:

// src/components/PokemonLink.tsx
import type { JSX } from 'react'
import { Link } from 'tuono'

interface PokemonLinkProps {
  id: number
  name: string
}

export default function PokemonLink({
  id,
  name,
}: PokemonLinkProps): JSX.Element {
  return (
    <Link href={`/pokemons/${name}`} className={styles.link} id={id.toString()}>
      {name}
      <img
        src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png`}
        alt=""
      />
    </Link>
  )
}

Now that the link is done, let’s import it into the index.tsx file

// src/routes/index.tsx

++  import PokemonLink from '../components/PokemonLink'

// ...
      <ul style={{ flexWrap: "wrap", display: "flex", gap: 10 }}>
        {pokemons.map((pokemon, i) => (
--        <li key={i + 1}>{pokemon.name}</li>
++        <PokemonLink key={i + 1} name={pokemon.name} id={i + 1} />
        })}
      </ul>
// ...

Now the links work. Clicking on any of them, we get redirected to the 404 page because we haven’t yet implemented the pokemons/[pokemon] page. As previously said, CSS modules are enabled out of the box, so let’s make those links a little bit nicer.

Styling the component

Create alongside the PokemonLink.tsx component the CSS module PokemonLink.module.css and copy the following content into it:

/* src/components/PokemonLink.module.css */

.link {
  width: 100%;
  max-width: 216px;
  position: relative;
  background: white;
  margin-bottom: 10px;
  border: solid #f0f0f0 1px;
  text-decoration: none;
  color: black;
  padding: 5px 5px 5px 15px;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
  transition: 0.2s;
  align-items: center;
}

.link:hover {
  box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}

.link img {
  width: 70px;
  background: white;
  border-radius: 50%;
}

💡 SASS is supported out of the box. Just install the processor in the devDependencies npm i -D sass and run again tuono dev

Then import the styles into the PokemonLink component as following:

// src/components/PokemonLink.tsx
import type { JSX } from 'react'
import { Link } from 'tuono'

++ import styles from './PokemonLink.module.css'

interface PokemonLinkProps {
  id: number
  name: string
}

export default function PokemonLink({
  id,
  name,
}: PokemonLinkProps): JSX.Element {
  return (
--  <Link href={`/pokemons/${name}`} id={id.toString()}>
++  <Link href={`/pokemons/${name}`} className={styles.link} id={id.toString()}>
      {name}
      <img
        src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png`}
        alt=""
      />
    </Link>
  )
}
Edit page