I don't have much experience with CSS, as I rarely write it these days due to the popularity of libraries like MUI
. However, I recently found myself in a tricky situation where I needed to rewrite this example Emabla carousel in typescript and NextJS.
The issue I'm facing is related to CSS. When I import it globally, everything works fine, but I prefer to import it at the component level. Here is how my code structure looks:
https://i.sstatic.net/roY0v.png
The ProductSlider.tsx
corresponds to EmblaCarousel.js
in the sample project, and the ProductSlider.module.css
file is similar to the embla.css
.
This is what my ProductSlider component looks like:
import s from './ProductSlider.module.css'
import React, { useState, useEffect, useCallback, memo } from 'react'
import useEmblaCarousel from 'embla-carousel-react'
import Thumbnail from './Thumbnail'
import Image from 'next/image'
import { Box } from '@mui/material'
// Component definition here...
I have successfully implemented basic CSS styling, but my challenge lies in how to handle classes like
embla__container embla__container--thumb
using named imports.
For instance, how do I deal with elements like this:
<div
className={`embla__slide embla__slide--thumb ${
selected ? 'is-selected' : ''
}`}
In cases where there are two identical classes, I understand that I can use clsx
like this:
cn(s.embla__container, s.embla__container)
. But when dealing with modifiers like --thumb
, some_name--thumb
, some_name-is-selected
, or simply is-selected
, I'm unsure of the best approach.