So I have a page that describes the amenities in the hotel, how I can create a hidden block of amenities that can be expanded by pressing the "Show more amenities" button and collapsed by pressing "Show less amenities" in NextJS with tailwind-css?
Here’s a part of the code:
'use client'
import Image from 'next/image'
import { useState, useEffect } from "react";
export default function Lodge() {
return (
<div className="w-full h-[8964px] bg-white shadow-sm">
<div className="grid grid-cols-2 gap-4">
...
<div className="text-[28px] font-medium leading-normal text-[#313131] underline flex flex-row gap-5 py-16 cursor-pointer">
Show more amenties
<Image
src="/images/icon down.svg"
alt="icon-down"
width={26}
height={26}
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="">
<div className="text-[32px] font-bold leading-normal text-[#313131]">
Heating & Cooling
<div className="text-[24px] font-medium leading-normal text-[#313131] flex flex-row items-end gap-5">
<Image
src="/images/lodge/fan icon.svg"
alt="fan-icon"
width={40}
height={40}
/>
Portable fans
</div>
</div>
<div className="text-[32px] font-bold leading-normal text-[#313131]">
Internet & Working
<div className="text-[24px] font-medium leading-normal text-[#313131] flex flex-row items-end gap-5">
<Image
src="/images/lodge/wifi icon.svg"
alt="wifi-icon"
width={40}
height={40}
/>
WI-FI
</div>
<div className="text-[24px] font-medium leading-normal text-[#313131] flex flex-row items-end gap-5">
<Image
src="/images/lodge/desk icon.svg"
alt="desk-icon"
width={40}
height={40}
/>
Working zone
</div>
</div>
... etc
2
Answers
Add/remove the Tailwind CSS hidden class on click to hide/show the specific content. This changes the display to none.
If you want to change the visibility to hidden instead, use the invisible class.
You can use the following example in order to show and hide the element in react js. In here all you need is the usestate hook to do it at once.