Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
Dynamically create object literals based on prop title and color

I am working on my Next.js website. Using Tailwind CSS I have managed to change the color dynamically before returning a component with static names & colors.

Now I am fetching data from the API and the title + color needs to be dynamically created with the values from the API.

the title prop equals the hardcoded project (1,2,3) name. The color should be color prop.

Is there an intelligent way to create an object literal with the dynamic data?

Hardcoded values work just perfectly fine.

import { motion } from 'framer-motion';

import Image from 'next/image';

import Link from 'next/link';

import { projectsArrayProps } from '@typings/propTypes';

const Projects = ({ allProjects }) => {
  console.log(allProjects);

  return (
    <div className="card--grid grid grid-cols-3 lg:grid-cols-4 gap-4 auto-rows-[100px] sm:auto-rows-[120px] md:auto-rows-[200px]">
      {allProjects.map(
        ({ id, logo, title, color }: projectsArrayProps, index) => {
          // const projectColor: { [key: string]: string } = {
          //   project1: 'bg-[#fbc340]/[0.07]',
          //   project2: 'bg-[#70d1db]/[0.07]',
          //   project3: 'bg-[#ea5b52]/[0.07]',
          //   project4: 'bg-[#ff1e00]/[0.07]',
          //   project5: 'bg-[#ff99f3]/[0.07]',
          // };

          const projectColor: { [key: string]: string } = {};

          return (
            <motion.div
              key={id}
              className={`rounded-md bg-[${projectColor['title']}]`}
              initial="hidden"
              whileInView="visible"
              viewport={{ once: true }}
              transition={{
                duration: 0.1,
                stiffness: 200,
                delay: index * 0.085,
                type: 'spring'
              }}
              variants={{
                hidden: { opacity: 0, scale: 0.6 },
                visible: { opacity: 1, scale: 1 }
              }}
            >
              <Link href="">
                <a
                  target="_blank"
                  className="flex flex-col items-center justify-center w-full h-full"
                >
                  <div className="flex flex-col items-center justify-center relative w-full h-full max-w-[64px] md:max-w-[100px] lg:max-w-[120px]">
                    <img
                      className="object-contain"
                      src={logo.url}
                      alt={logo.alt}
                    />
                  </div>
                </a>
              </Link>
            </motion.div>
          );
        }
      )}
    </div>
  );
};

export default Projects;
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Tailwind extracts classes at build time, so you cannot create dynamic classes at runtime. You could safelist some classes to make sure they are created even when they are not present in the code at build time (https://tailwindcss.com/docs/content-configuration#safelisting-classes). Unfortunately this only helps you if you know all possible colors that can be returned from the API.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda