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

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>
);
}

Related

Responsive Media React Tailwind

So i have component that i try to make it responsive, but when it comes to media below md: it not working at all and i have some problem on turning the view into what i want
Here is the look on the web view:
and here is the problem :
is that posible to make the button into this when it comes to phone view?
Here is my try:
const CardCalculation = () => {
return (
<div className="ml-2">
<div className="mx-auto lg:flex lg:items-center lg:justify-between">
<h2 className="tracking-tight text-gray-900 sm:text-xl">
<div className="gap-2">
<div className="w-[120px] h-[110px] absolute bg-[#d9d9d9] lg:mb-10"></div>
<div className="text-xl ml-32 text-left text-[#e44]">
Mie Goleng
</div>
<div className="ml-32 text-left text-[#b4b4b4]">Plant K103.</div>
<div className="ml-32 text-left text-[#b4b4b4]">500.02 UOG.</div>
</div>
</h2>
<div className="mt-4 flex lg:mt-0 lg:flex-shrink-0 md:mt-4 sm:mt-8">
<div>
<div className="rounded-md mb-2 shadow">
<a
href="#"
className="inline-flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-5 py-3 text-base font-medium text-white hover:bg-indigo-700"
>
Get started
</a>
</div>
<div className="rounded-md shadow">
<a
href="#"
className="inline-flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-5 py-3 text-base font-medium text-indigo-600 hover:bg-indigo-50"
>
Learn more
</a>
</div>
</div>
</div>
</div>
</div>
);
};
export default CardCalculation;
can someone explained to me why it not responsive to the screen and does it possible to turn the button into horizontal when it comes to phone view as below?
Use the following code:
<div class="p-2">
<div class="flex flex-col md:flex-row md:justify-between">
<div class="flex gap-4">
<div name="grey window" class="h-[110px] w-[120px] bg-[#d9d9d9]"></div>
<div name="text" class="pt-2 md:flex md:flex-col md:justify-evenly md:pt-4">
<div class="text-xl text-[#e44] md:text-3xl">Mie Goleng</div>
<div class="mt-2 text-[#b4b4b4] md:text-xl">Plant K103.</div>
<div class="mt-2 text-[#b4b4b4] md:text-xl">500.02 UOG.</div>
</div>
</div>
<div name="Buttons">
<div class="flex gap-4 md:flex-col p-4 md:p-0">
<div class="rounded-md shadow">
Get started
</div>
<div class="rounded-md shadow">
Learn more
</div>
</div>
</div>
</div>
</div>
Output on large devices:
Output on small devices:
Tailwind play

How to fix the mobile menu so the logo and "x" button wont disappear & remove space that isn't needed so there won't be a need for a scroll bar

When I scroll down after I've opened the menu bar in mobile and then scroll up, the logo and "x" button disappears and other areas of the page are showing instead. I know that the solution to it is make it so there won't be anything to scrol down since it's a menu bar but I can't find a way to it. I know that one thing that causes a scroll bar to appear is when you have margin and I've checked and no margin.
I've taken a video of the screen but apparentley can't upload a video here...
I've tried using h-screen and h-full, changed fixed property to sticky
Anyway here's the code
const Navbar = () => {
const [nav, setNav] = useState(false);
const { user, logout } = UserAuth();
const navigate = useNavigate();
const handleNav = () => {
setNav(!nav);
};
const handleSignOut = async () => {
try {
await logout();
navigate("/");
} catch (e) {
console.log(e.message);
}
};
return (
<div className=" md:rounded-div flex items-center justify-between h-20 font-bold">
<Link to="/">
<h1 className="text-2xl">Cryptoboss</h1>
</Link>
<div className="hidden md:block">
<ThemeToggle />
</div>
{user?.email ? (
<div>
<Link to="/account" className="hidden md:block p-4">
Account
</Link>
</div>
) : (
<div className="hidden md:block">
<Link to="/signin" className="p-4 hover:text-accent">
Sign In
</Link>
<Link
to="/signup"
className="bg-button text-btnText px-5 py-2 ml-2 rounded-2xl shadow-lg hover:shadow-2xl"
>
Sign Up
</Link>
</div>
)}
{/* Menu Icon */}
<div onClick={handleNav} className="block md:hidden cursor-pointer z-10">
{nav ? <AiOutlineClose size={20} /> : <AiOutlineMenu size={20} />}
</div>
{/* Mobile Menu */}
<div
className={
nav
? "md:hidden fixed left-0 top-20 flex flex-col items-center justify-between w-full h-full bg-primary ease-in duration-300 z-10"
: "fixed left-[-100%] top-20 h-[90%] flex flex-col items-center justify-between ease-in duration-300"
}
>
<ul className="w-full p-4">
<li onClick={handleNav} className="border-b py-6">
<Link to="/">Home</Link>
</li>
<li onClick={handleNav} className="border-b py-6">
<Link to="/account">Account</Link>
</li>
<li className=" py-6">
<ThemeToggle />
</li>
<div className="flex flex-row ">
<li>
<Link to="/signin">
<button
onClick={handleNav}
className="flex my-2 py-3 px-10 mr-11 ml-5 bg-primary text-primary border border-secondary rounded-2xl shadow-xl"
>
Sign In
</button>
</Link>
</li>
<li>
<Link onClick={handleNav} to="/signup">
<button className="flex my-2 py-3 px-10 bg-button text-btnText rounded-2xl shadow-xl">
Sign Up
</button>
</Link>
</li>
</div>
</ul>
{/* <div className="flex flex-col w-full h-screen inline-block p-4"></div> */}
</div>
</div>
);
};
export default Navbar;
I believe that the change that needs to be done is in the div below:
<div
className={
nav
? "md:hidden fixed left-0 top-20 flex flex-col items-center justify-between w-full h-full bg-primary ease-in duration-300 z-10"
: "fixed left-[-100%] top-20 h-[90%] flex flex-col items-center justify-between ease-in duration-300"
}

How can I do to put the edit input into my react project?

Hello I have that edit input :
<link rel="stylesheet" href="https://unpkg.com/boxicons#2.0.7/css/boxicons.min.css" />
<script defer src="https://unpkg.com/alpinejs#3.9.1/dist/cdn.min.js"></script>
<main class="flex min-h-screen w-full items-center justify-center bg-gray-100">
<div x-data="{ open : false }" class="p-2 bg-white border shadow rounded w-96">
<div x-show="!open" class="flex justify-between items-center">
<div class="ml-2">Hafiz Haziq</div>
<button type="button" class="btn bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 font-medium rounded "
#click="open = !open">Edit</button>
</div>
<!-- container after clicked "EDIT" -->
<div x-show="open" class="flex justify-between items-center">
<input type="text" class="w-full bg-gray-100 rounded p-2 mr-4" value="Hafiz Haziq">
<div class="flex justify-center items-center space-x-2">
<button type="button" class="rounded bg-emerald-500 hover:bg-emerald-600 w-10 h-10 text-white text-3xl">
<i class='bx bx-check'></i>
</button>
<button type="button" class="rounded bg-red-500 hover:bg-red-600 text-white w-10 h-10 text-3xl"
#click="open = false">
<i class='bx bx-x'></i>
</button>
</div>
</div>
</div>
</main>
that I found here : The edit input
And I would like to put it into my react project :
I tried that :
import "./styles.css";
const App = ()=> {
return (
<main class="flex min-h-screen w-full items-center justify-center bg-gray-100">
<div x-data="{ open : false }" class="p-2 bg-white border shadow rounded w-96">
<div x-show="!open" class="flex justify-between items-center">
<div class="ml-2">Hafiz Haziq</div>
<button type="button" class="btn bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 font-medium rounded "
#click="open = !open">Edit</button>
</div>
{/*<!-- container after clicked "EDIT" --> */}
<div x-show="open" class="flex justify-between items-center">
<input type="text" class="w-full bg-gray-100 rounded p-2 mr-4" value="Hafiz Haziq" />
<div class="flex justify-center items-center space-x-2">
<button type="button" class="rounded bg-emerald-500 hover:bg-emerald-600 w-10 h-10 text-white text-3xl">
<i class='bx bx-check'></i>
</button>
<button type="button" class="rounded bg-red-500 hover:bg-red-600 text-white w-10 h-10 text-3xl"
#click="open = false">
<i class='bx bx-x'></i>
</button>
</div>
</div>
</div>
</main>
);
}
export default App;
The code of the full project is there : My project
But it does not work ...
Could you help me please ?
Thank you very much !
It is not working, because this is not a React code. You would need to change few things in there.
You need use useState to keep track of open state
const [open, setOpen] = useState(false);
To handle click events on button you should use onClick instead of #click
<button onClick={() => setOpen(true)}>
Edit
</button>
Change places where you are defining classes for HTML elements. In react you should use className keyword for that.
<div className="ml-2">Hafiz Haziq</div>
You can also remove x-data and x-show attributes
<div x-show="!open" class="flex justify-between items-center">

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

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>

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> )}

Resources