This is not a duplicate question. I have gone through existing answers and have applied the same solution but it hasn't quite worked.
I have a list of Cards and I have addEventListener set up to track ArrowRight and ArrowLeft. I am not quite sure how I can get it to focus and move through the cards using arrow keys.
I tried the following but I get an error ".card:focus".next is not a function
useEffect(() => {
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowRight") {
".card:focus".next().focus();
}
if (e.key === "ArrowLeft") {
".card:focus".prev().focus();
}
});
});
This is what the DOM looks like:
Parent component:
import React, { useEffect, useState } from "react";
import { getPokemons } from "../graphql/fetch/getPokemons.js";
import styled from "styled-components";
import "../components/pokemon-card";
const Cards = styled.div`
display: flex;
`;
export default function Pokemon() {
const [isLoading, setIsLoading] = useState(true);
const [pokemons, setPokemons] = useState([]);
useEffect(() => {
getPokemons().then((response) => {
console.log(response.data.pokemons);
setPokemons(response.data.pokemons);
setIsLoading(false);
});
}, []);
useEffect(() => {
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowRight") {
".card:focus".next().focus();
}
if (e.key === "ArrowLeft") {
".card:focus".prev().focus();
}
});
});
if (isLoading) {
return <p>is loading</p>;
}
return (
<Cards>
{pokemons.map((pokemon) => (
<pokemon-card name={pokemon.name} image={pokemon.image}></pokemon-card>
))}
</Cards>
);
}
Child component:
import { LitElement, html, css } from "lit-element";
class PokemonCard extends LitElement {
static styles = css`
.card {
background: white;
border-radius: 1rem;
padding: 2rem;
box-shadow: 4px 4px 12px 2px rgba(0, 0, 0, 0.75);
height: 500px;
transition: 0.2s;
}
.card:hover,
.card:focus-within {
transform: translateY(-5rem);
}
`;
static get properties() {
return {
name: { type: String },
image: { type: String },
};
}
render() {
const { name, image } = this;
return html`
<div class="card">
<p>${name}</p>
<img src=${image} />
</div>
`;
}
}
customElements.get("pokemon-card") ||
customElements.define("pokemon-card", PokemonCard);