Tailwind transition max-height-fit won't work as intended - reactjs

I found a great accordion solution on the internet and wanted to try it. The difference between the example and my code is I used max-height-fit instead of max-height-20.
Here is my code:
<div className="relative overflow-hidden">
<input
type="checkbox"
className="peer absolute top-0 inset-x-0 w-full h-12 opacity-0 z-10 cursor-pointer"
/>
<div className="bg-gray-500 h-12 w-full pl-5 flex items-center">
<h1 className="text-lg font-semibold text-white">Offline</h1>
</div>
<div className="absolute top-3 right-3 text-white transition-trasnform duration-500 rotate-0 peer-checked:rotate-180">
<FontAwesomeIcon icon={faChevronDown} />
</div>
<div className="overflow-auto transition-all duration-500 max-h-0 peer-checked:max-h-fit">
{players
.filter((p: any) => p.name.toLowerCase().includes(search))
.map((player: any) => {
return (
<div className="flex py-4 border-b-2 border-gray-700 hover:bg-gray-800/90 last:border-b-0">
<div className="ml-5 -mt-1 bg-gray-700 w-8 h-8 text-center leading-8 rounded-full">
{player.ID}
</div>
<div className="ml-5 flex grow">
<div className="w-full">
{player.name} ({player.level})
</div>
<div>
{player.isAdmin && (
<FontAwesomeIcon className="mr-5" icon={faCrown} />
)}
</div>
</div>
</div>
);
})}
</div>
</div>

Related

TypeError: items.map is not a function

My code problem is that I want to create a token from the backend and add it to my item. Now the page is crashing due to mapping the data.
<div className='bg-gray-100'>
<SimpleHeader/>
<div className='flex justify-center'>
<h2 className='text-center text-4xl py-5 inline-block border-b-4 border-blue-400 font-bold mb-4'>My Items</h2>
</div>
<Link to='/addItems' className='flex justify-center'>
<button className='px-2 py-1 bg-transparent rounded border-2 border-blue-500 hover:bg-blue-500 hover:text-white duration-200 hover:scale-105'>Add New Item</button>
</Link>
<div className='container md:grid grid-cols-3 gap-4 w-full mx-auto p-12'>
{
items?.map(item=><MyItem key={item._id} item={item} handleDelete={handleDelete}></MyItem>)
}
</div>
</div>
try to change it this way
<div className='bg-gray-100'>
<SimpleHeader/>
<div className='flex justify-center'>
<h2 className='text-center text-4xl py-5 inline-block border-b-4 border-blue-400 font-bold mb-4'>My Items</h2>
</div>
<Link to='/addItems' className='flex justify-center'>
<button className='px-2 py-1 bg-transparent rounded border-2 border-blue-500 hover:bg-blue-500 hover:text-white duration-200 hover:scale-105'>Add New Item</button>
</Link>
<div className='container md:grid grid-cols-3 gap-4 w-full mx-auto p-12'>
{
items && items.map(item=><MyItem key={item._id} item={item} handleDelete={handleDelete}></MyItem>)
}
</div>
</div>

Make a smoothly opening menu effect using Tailwind CSS and React JS

I want to transform the menu to have a smooth transition when it opens, but it's not working for me.
I've also tried using hidden class, but ran into the same problem.
Is there a way to make it open smoothly?
My attempt:
import React from "react";
export default function ModerSideMenu({}) {
const [menu, setMenu] = React.useState({
home: true,
});
return (
<div className="w-64 bg-blue-900 h-full p-4 text-white">
<div className="relative">
<div className="flex flex-row items-center justify-between uppercase font-semibold w-full">
<div className="ml-2 flex-row flex items-center space-x-2">
<i className="fas fa-home" />
<p className="">Home</p>
</div>
<i
className={`transform transition-all ease-out duration-300 select-none fas ${
menu.home ? "fa-angle-up" : "fa-angle-down"
} mr-2`}
onClick={() => setMenu({ ...menu, home: !menu.home })}
/>
</div>
<div
className={`ml-4 mt-2 space-y-1 text-sm transform transition-all ease-out duration-300 z-20 select-none ${
!menu.home ? "-translate-y-full" : "translate-y-0"
}`}
hidden={!menu.home}
>
<p>- Home 1</p>
<p>- Home 2</p>
</div>
</div>
<div className="relative mt-2">
<div className="flex flex-row items-center justify-between uppercase font-semibold w-full">
<div className="ml-2 flex-row flex items-center space-x-2">
<i className="fas fa-home" />
<p className="">Home 2</p>
</div>
<i className="justify-self-end fas fa-angle-down mr-2" />
</div>
<div className="ml-4 mt-2 space-y-1 text-sm">
<p>- Home 3</p>
<p>- Home 4</p>
</div>
</div>
</div>
);
}

Single Product View Modal Not Working on React Typescript Project

I am trying to display single product data by fetching it from the server, but there's an issue while opening the modal. If I map, I get a double modal and a blank black screen. How do I get a single product view?
import React, { useState } from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";
import { FreeMode, Navigation, Thumbs } from "swiper";
// Import Swiper styles
import "swiper/css";
import "swiper/css/free-mode";
import "swiper/css/navigation";
import "swiper/css/thumbs";
import { Link } from 'react-router-dom';
import { Rating } from "#mui/material";
const ProductView = ({product}) => {
const { title, hoverImg, img, price, rating,vendorName, salePrice } = product;
const [thumbsSwiper, setThumbsSwiper] = useState(null);
return (
<div style={{backgroundColor:'white',width:'800px',height:'600px',overflow:'scroll'}} className='mx-auto container place-content-center px-12 py-8 justify-center items-center grid lg:grid-cols-2 md:grid-cols-2 grid-cols-1 gap-6 sm:flex-1'>
<div>
<Swiper
style={{
"--swiper-navigation-color": "#fff",
"--swiper-pagination-color": "#fff",
}}
spaceBetween={10}
// navigation={true}
thumbs={{ swiper: thumbsSwiper }}
modules={[FreeMode, Navigation, Thumbs]}
className="mySwiper2"
>
<SwiperSlide>
<img alt="" src={img} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={hoverImg} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={img} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={hoverImg} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={img} />
</SwiperSlide>
</Swiper>
<Swiper
onSwiper={setThumbsSwiper}
spaceBetween={10}
slidesPerView={4}
freeMode={true}
watchSlidesProgress={true}
modules={[FreeMode, Navigation, Thumbs]}
className="mySwiper"
>
<SwiperSlide>
<img alt="" src={img} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={hoverImg} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={img} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={hoverImg} />
</SwiperSlide>
<SwiperSlide>
<img alt="" src={img} />
</SwiperSlide>
</Swiper>
</div>
<div>
<h2 className='text-3xl mb-2 font-medium'>{title}</h2>
<div className='flex items-center mb-4'>
<div>
<Rating name="half-rating-read" defaultValue={rating} precision={0.5} readOnly />
</div>
<div className="text-xs text-gray-500 ml-3">
(1 Reviews)
</div>
</div>
<div className='space-y-2'>
<p className="p text-gray-800 font-semibold space-x-2">
<span>Availability:</span>
<span className='text-green-600'>In stock</span>
</p>
<p className="space-x-2">
<span className='text-gray-800 font-semibold'>Vendor:</span>
<span className='text-gray-600'>{vendorName}</span>
</p>
<p className="space-x-2">
<span className='text-gray-800 font-semibold'>Category:</span>
<span className='text-gray-600'>Men</span>
</p>
<p className="space-x-2">
<span className='text-gray-800 font-semibold'>SKU:</span>
{/* <span className='text-gray-600 uppercase'>{_id.slice(4,12)}</span> */}
</p>
</div>
<div className="flex items-baseline mb-1 mt-2 space-x-2">
<p className="text-xl text-indigo-500 font-semibold">{salePrice}</p>
<p className="text-sm text-gray-400 line-through">{price}</p>
</div>
<div className="">
<h3 className="text-xl text-gray-800 mb-3 uppercase font-medium ">Color</h3>
<div className="flex gap-2">
{/* Single Color Starts */}
<div className="color-selctor">
<input type="radio" name='color' className='hidden' color='red' id='color-red' />
<label htmlFor="color" className='border border-gray-200 rounded-sm h-5 w-5 cursor-pointer shadow-sm block' style={{ backgroundColor: "red" }}></label>
</div>
<div className="color-selctor">
<input type="radio" name='color' className='hidden' color='red' id='color-red' />
<label htmlFor="color" className='border border-gray-200 rounded-sm h-5 w-5 cursor-pointer shadow-sm block' style={{ backgroundColor: "green" }}></label>
</div>
<div className="color-selctor">
<input type="radio" name='color' className='hidden' color='red' id='color-red' />
<label htmlFor="color" className='border border-gray-200 rounded-sm h-5 w-5 cursor-pointer shadow-sm block' style={{ backgroundColor: "blue" }}></label>
</div>
</div>
</div>
<div className="pt-4 block">
<h3 className="text-xl text-gray-800 mb-3 uppercase font-medium">Size</h3>
<div className="flex item-center gap-2">
{/* single size selector starts */}
<div className="size-selector">
<input type="radio" name='size' className='hidden' id='xs' />
<label htmlFor="size-xs" className='text-xs border border-gray-200 rounded-sm h-6 w-6 flex items-center justify-center cursor-pointer shadow-sm text-gray-600'>
S
</label>
</div>
<div className="size-selector">
<input type="radio" name='size' className='hidden' id='xs' />
<label htmlFor="size-xs" className='text-xs border border-gray-200 rounded-sm h-6 w-6 flex items-center justify-center cursor-pointer shadow-sm text-gray-600'>
M
</label>
</div>
<div className="size-selector">
<input type="radio" name='size' className='hidden' id='xs' />
<label htmlFor="size-xs" className='text-xs border border-gray-200 rounded-sm h-6 w-6 flex items-center justify-center cursor-pointer shadow-sm text-gray-600'>
L
</label>
</div>
<div className="size-selector">
<input type="radio" name='size' className='hidden' id='xs' />
<label htmlFor="size-xs" className='text-xs border border-gray-200 rounded-sm h-6 w-6 flex items-center justify-center cursor-pointer shadow-sm text-gray-600'>
XS
</label>
</div>
</div>
</div>
<div>
<h3 className='text-xl text-gray-800 mb-1'>Quantity</h3>
<div className="flex border border-gray-500 divide-gray-500 text-gray-600 divide-x w-max">
<div className='h-8 w-8 flex items-center justify-center cursor-pointer select-none'>
-
</div>
<div className='h-8 w-8 flex items-center justify-center'>
<input style={{width:'34px',height:'34px'}} type="text" />
</div>
<div className='h-8 w-8 flex items-center justify-center cursor-pointer select-none'>
+
</div>
</div>
</div>
<div className="flex gap-3 border-b border-gray-200 pb-5 mt-6">
<Link to = "/">
<button className='text-center top-5 text-white p-2 bg-indigo-500 border border-indigo-500 hover:bg-transparent hover:text-indigo-500 transition'>
<i className="fa-regular fa-bag-shopping"></i> Add to cart
</button>
</Link>
<Link to = "/">
<button className='text-center top-5 hover:text-white p-2 hover:bg-indigo-500 border border-indigo-500 bg-transparent text-indigo-500 transition'>
<i className="fa-regular fa-heart"></i> Add to Wishlist
</button>
</Link>
</div>
</div>
</div>
);
};
export default ProductView;

Can't find missing key in 'Each child in a list should have a unique "key" prop'

I've been starring at my code, but I can't see where the key is missing. That is my error:
Warning: Each child in a list should have a unique "key" prop.
Check the render method of Card. It was passed a child from ContractInfo.
Here is Card:
export const Card = ({ selected, onChange, title, price, frequency, description, feature, cta }) => {
const mode = selected ? 'border-yellow-500 border-4' : 'border-gray-200'
return (
<div
className={["relative p-8 bg-white border-gray-200 rounded-2xl shadow-sm flex flex-col border-2", mode].join(' ')}
style={{cursor:'pointer'}}
selected={selected}
onClick={onChange}
>
<div className="flex-1 ">
<h3 className="text-xl font-semibold text-gray-900">{title}</h3>
<p className="mt-4 flex items-baseline text-gray-900">
<span className="text-5xl font-extrabold tracking-tight">€{price}</span>
<span className="ml-1 text-xl font-semibold">{frequency}</span>
</p>
<p className="mt-6 text-gray-500">{description}</p>
{/* Feature list */}
{feature}
</div>
<div className= 'bg-yellow-500 text-white hover:bg-yellow-600 mt-8 block w-full py-3 px-6 border border-transparent rounded-md text-center font-medium'>
{cta}
</div>
</div>
);
}
And here is the bit in ContractInfo:
return (
<div className={formStep === 0 ? 'block' : 'hidden'}>
<div className="bg-white py-16 px-4 overflow-hidden sm:px-6 lg:px-8">
<div className="relative max-w-xl mx-auto">
<div className="text-center">
<h2 className="text-3xl font-extrabold tracking-tight text-gray-600 sm:text-4xl">Welcher Vertrag passt zu dir?</h2>
</div>
</div>
<div className="max-w-7xl mx-auto py-4 px-4 bg-white sm:px-6 lg:px-8">
<div className="mt space-y-12 lg:space-y-0 lg:grid lg:grid-cols-3 lg:gap-x-8">
{contracts.map((contract, index) => (
<Card
key={contract.id}
title={contract.title}
price={contract.price}
frequency={contract.frequency}
description={contract.description}
feature={ contract.features.map((feature, index) =>
<ul role="list" className="mt-6 space-y-6">
<li className="flex" key={feature.id} >
{console.log(feature.id)}
<CheckIcon className="flex-shrink-0 w-6 h-6 text-yellow-500" aria-hidden="true" />
{feature.name}
</li>
</ul> )}
cta={contract.cta}
selected={isSelected === index}
onChange={() => setisSelected(index)}
/>
))}
{/* {console.log(contracts?.[isSelected])} */}
</div>
</div>
<div className="mt-5 sm:mt-8 sm:flex sm:justify-center lg:justify-center">
<div className="rounded-md shadow">
<a role="button" tabIndex={0}
className="w-full flex items-center justify-center px-8 py-3 border border-transparent text-base font-medium rounded-md text-white bg-gray-200 hover:bg-gray-200 focus:outline-none md:py-4 md:text-lg md:px-10 cursor-not-allowed"
>
Zurück
</a>
</div>
<div className="mt-3 sm:mt-0 sm:ml-3">
<a
//onClick={nextQuizStep}
onClick={() => nextFormStep(contracts?.[isSelected]) }
className="w-full flex items-center justify-center px-8 py-3 border border-transparent text-base font-medium rounded-md text-white bg-yellow-500 hover:bg-yallow-600 md:py-4 md:text-lg md:px-10"
>
Weiter
</a>
</div>
</div>
</div>
</div>
);
}
Keys are both present for features list and for cards. Where is the problem?
Key Error is caused by feature rendering in Card component
This is cause the keys are not present among sibling elements of the for loop in feature creation, so to correct this You set key for parent ul in
feature={ contract.features.map((feature, index) =>
<ul role="list" className="mt-6 space-y-6">
<li className="flex" key={feature.id} >
{console.log(feature.id)}
<CheckIcon className="flex-shrink-0 w-6 h-6 text-yellow-500" aria-hidden="true" />
{feature.name}
</li>
</ul> )}
so it becomes
feature={ contract.features.map((feature, index) =>
<ul role="list" className="mt-6 space-y-6" key={'feature-'+feature.id}>
<li className="flex" >
{console.log(feature.id)}
<CheckIcon className="flex-shrink-0 w-6 h-6 text-yellow-500" aria-hidden="true" />
{feature.name}
</li>
</ul> )}

Trying to change the inner element by using get element and it throws error TypeError: Cannot set property 'innerHTML' of null

I am trying to change the element through get element by id and it is throwing:
TypeError: Cannot set property 'innerHTML' of null.
Is this how we pass parameters or we have other approaches?
My code:
const [isOpen, setIsOpen] = useState(false);
const togglePopup = (name='shraweg') => {
setIsOpen(!isOpen);
let inner = document.getElementById(name)
inner.innerHTML=(
<>
{
isOpen &&
<div className="popup-box">
<div className="flex flex-col box gap-1">
<div>
<p className="font-meduim text-lg">Are you sure you want to kick {" "+player.username+" "} ?</p>
</div>
<div className="flex flex-row gap-14 items-center justify-center">
<button type="submit" onClick={handleKick} value={player._id} className="border font-semibold text-lg border-red-300 rounded-xl hover:bg-red-600 px-6">
Yes
</button>
<button onClick={togglePopup} className="border font-semibold text-lg border-yellow-300 rounded-xl hover:bg-yellow-500 px-6">
No
</button>
</div>
</div>
</div>
}
</>
)
}
Here I've written a function to change the elements inside my html tag:
return (
<legend className="text-xl mb-5 border-yellow-500 border-l-4 pl-2 mt-10">Team Members</legend>
<div className="flex gap-5 flex-col w-full mb-4">
{props.team.players.map((player, key) => (
<div key={key} className="">
<label htmlFor="csgoign" className="flex bg-gray-50 hover:bg-gray-200 p-3 rounded-xl w-full items-center justify-between">
<div className="flex font-medium gap-3">
<img src={"https://robohash.org/"+player.username+"?set=set5"} className="w-12 h-12 rounded-3xl bg-blue-100"/>
<div className="ml-3">
<a href={"http://localhost:3000/userprofile/"+player._id} className="text-2xl w-auto font-semibold hover:text-yellow-600">
{player.username}
</a>
<p className="text-sm font-normal text-gray-700 font-paragraph">
Member since : {player.join_date}
</p>
</div>
</div>
{props.user._id == props.team.team_cap && (
<>
{!(player.username == props.user.username) && (
<div id={player.username}>
{
!isOpen &&
<button type="button" onClick={togglePopup(player.username)} className="border font-semibold text-xl border-red-300 rounded-xl hover:bg-red-600 px-7 py-2">
Kick
</button>
}
</div>
)}
</>
)}
</label>
</div>
))}
</div>
)
onClick={togglePopup(player.username)}
change to
onClick={()=>togglePopup(player.username)}
onClick property should be a function

Resources