Getting extra unwanted whitespace in tailwindcss using in react - reactjs

I am trying to learn react js with tailwindcss but I am getting extra whitespace which is not detecting when I inspect it also in mobile view it occupies half of the screen
import React, { useEffect, useState } from "react";
const Hero = () => {
const [animated, setanimated] = useState(false);
useEffect(() => {
setanimated(true);
}, []);
return (
<div
className="min-h-screen w-screen flex justify-center items-center flex-col-reverse md:flex-row md:justify-around "
style={{
background: "linear-gradient(to bottom, #aaacc0, #F0F4FD)",
}}
>
<div
className={`${
animated ? "scale-y-100" : "scale-y-0"
} mt-4 text-center font-nunito transform transition duration-1000 ease-in-out`}
>
<h1 className="font-bold text-3xl md:text-6xl md:flex-col">
Fresh Delicious
<span className="font-extrabold text-3xl md:text-6xl"> Noodles</span>
</h1>
<p className="text-base mt-5 ">We cook most tasty Noodles in Mumbai</p>
<p className="text-base">
We care for you! We maintain{" "}
<span className="md:text-lg md:font-semibold"> Hygein</span> and{" "}
<span className="md:text-lg md:font-semibold"> Contactless</span>{" "}
delivery!
</p>
<button className="inline-block bg-yellow-400 px-10 py-5 rounded-full mt-10 shadow-lg border-2 text-lg font-semibold text-black uppercase tracking-wide duration-1000 animate-bounce">
Order Now!
</button>
</div>
<img
src={process.env.PUBLIC_URL + "/assets/bowl.svg"}
alt="BOWL"
className={`${
animated ? "scale-90" : "translate-x-full translate-y-full"
} mx-5 my-5 w-auto h-auto md:max-w-xl transform transition duration-1000 ease-in-out `}
/>
</div>
);
};
export default Hero;
you can see website here
my website where I am getting bug

Try adding a class on #root div tag
overflow-x-hidden
or
#root {
#apply overflow-x-hidden;
}
The reason this is happening are the animation's starting point.

Related

Portfolio react js mapping error for project cards

import React from 'react'
import net from '../assets/netflix.png'
const Porfolio = () => {
const Projects = [
{
id:1,
title:"Netflix Clone",
desc:"Netflix Landing Page Clone using React JS and Tailwind CSS with Authentication Feature using Firebase",
imgLink:net,
codeLink:"https://github.com/codegoggins/netflix-clone",
projectLink:"https://netflix-clone-13127.firebaseapp.com/"
}
]
return (
<div
className='px-20 py-4'>
<p
className='text-white text-4xl'
>Projects</p>
{/* PROJECT CARDS */}
<div
className='mt-8 flex flex-col justify-center items-center md:flex md:items-start'
>
{
Projects.map(({id,title,desc,imgLink,codeLink,projectLink})=>(
<>
{/* SINGLE CARD */}
<div className='w-[340px] h-auto relative group md:w-[500px]'>
{/* OVERLAY */}
<div
className='bg-black/80 text-sm md:text-lg absolute flex top-0 bottom-0 h-full w-full rounded-lg overflow-hidden opacity-0 group-hover:opacity-100 transition-all duration-300'>
{/* PROJECT DETAILS */}
<div
className='text-white text-sm md:text-lg flex flex-col p-4 gap-3 md:gap-2 items-center justify-center'>
{/* TITLE */}
<p>{title}</p>
{/* DETAIL */}
<p>{desc}</p>
{/* BUTTONS */}
<div
className='flex gap-3 text-sm md:text-lg'>
<button
className='cursor-pointer font-semibold p-1.5 px-4 rounded-md bg-white text-black transition ease-linear duration-300'
>
Code
</button>
<button
className='cursor-pointer font-semibold p-1.5 px-2 rounded-md bg-white text-black transition ease-linear duration-300'>
See Project
</button>
</div>
</div>
</div>
{/* Image */}
<div className='rounded-lg overflow-hidden'>
<img src={imgLink} alt="" className='h-full w-full object-cover'/>
</div>
</div>
</>
))
}
</div>
</div>
)
}
export default Porfolio
I am getting error to add key , for that instead of react fragments, I
replace it with a div and give key to it but I am getting error. With
react fragments I don't get any error but I can't add key to it for
mapping.
I want the card details to be mapped on all the cards.
enter image description here
You should return inside mapping and provide a key for the root element.
for example,
{
products?.map(({ id, title, desc, imgLink, codeLink, projectLink }) => {
return (
<div key={id} className='w-[340px] h-auto relative group md:w-[500px]'>
//Your code here
</div>
)
})}
You can add:
<React.Fragment key={id}>
some code
</React.Fragment>
or remove the fragment and add the key to the first div inside the map

Getting over the Hydration failed because the initial UI does not match what was rendered on the server error

The problem
I'm new to NextJS and TypeScript from React.JS and JavaScript background and discovering SSR and it's constraints, implementing some components to reuse them in multiple pages like this one:
export default function Slider({ className, title }: Props): JSX.Element {
const swiperRef = useRef<SwiperCore>();
return (
<div className={className}>
<h3 className="pb-8 text-center font-primary text-xl md:text-left">
{title}
</h3>
<div className="flex flex-row items-center">
<button onClick={() => swiperRef.current?.slidePrev()}>
<Image
src="/img/homepage/slider-skip.svg"
alt="Previous button"
height="48"
width="48"
/>
</button>
<Swiper
// install Swiper modules
modules={[Navigation, A11y]}
loop={true}
spaceBetween={20}
slidesPerView={3}
breakpoints={{
320: {
slidesPerView: 1,
},
540: {
slidesPerView: 2,
},
768: {
slidesPerView: 3,
},
1100: {
slidesPerView: 4,
},
}}
onBeforeInit={(swiper) => {
swiperRef.current = swiper;
}}
style={{ marginLeft: "1.3rem", marginRight: "1.3rem" }}
>
<SwiperSlide>
<div className="relative h-44 w-full rounded-md bg-primary-400">
<p className="absolute bottom-4 left-4 font-primary text-base font-semibold text-white">
Let's make up
</p>
</div>
</SwiperSlide>
<SwiperSlide>
<div className="relative h-44 w-full rounded-md bg-primary-400">
<p className="absolute bottom-4 left-4 font-primary text-base font-semibold text-white">
Let's make up
</p>
</div>
</SwiperSlide>
<SwiperSlide>
<div className="relative h-44 w-full rounded-md bg-primary-400">
<p className="absolute bottom-4 left-4 font-primary text-base font-semibold text-white">
Let's make up
</p>
</div>
</SwiperSlide>
<SwiperSlide>
<div className="relative h-44 w-full rounded-md bg-primary-400">
<p className="absolute bottom-4 left-4 font-primary text-base font-semibold text-white">
Let's make up
</p>
</div>
</SwiperSlide>
</Swiper>
<button onClick={() => swiperRef.current?.slideNext()}>
<Image
src="/img/homepage/slider-skip.svg"
alt="Previous button"
height="48"
width="48"
className="scale-x-[-1]"
/>
</button>
</div>
</div>
);
}
And start to get these kind of errors:
Possible solution
I know that by doing SSR, the pages are rendered in HTML, which is not the case for my components that run on the client side, but after some research, I discovered that it was possible to avoid these errors by using the next/dynamic package, and that now I will have to import these kind of dynamic packages with this method:
const Slider = dynamic(() => import("../components/homepage/slider"), {
ssr: false,
});
My concern
Everything seems to work perfectly but takes time to load (delay), per example, my navbar is using dynamic data and can potentially be affected by these errors and using the next/dynamic package, it will take a while to display and this makes me embarrassed as the navigation should be the first thing to display, does anyone have a solution to avoid this delay and its constraints?

How to Show or Hide a div using useState in NextJS?

I’m new to React but trying to build a responsive website with a “desktop header” and “mobile header” that is shown when the user clicks on a menu-icon-toggle and closes when the user clicks on Close-icon.
I’m obviously doing it wrong but can’t seem to figure out what the problem is, I believe that NextJS doesn’t know what to open or close.
**Note: I´m using TailwindCSS and this is a component that will be rendered on the index page
My code looks something like this (simplified, without all the content):
import React, { useState } from 'react'
import Image from 'next/Image'
function header() {
const \[mobile__Header, setMobile__Header\] = useState(false)
const showMobile__Header = () =\> setMobile__Header(!mobile__Header)
return (\<div\>
{/* mobile header */}
<div className='absolute flex flex-col w-screen h-screen place-content-between bg-white text-black p-5 z-50'>
<div className='flex items-center justify-between'>
{/* Left Logo */}
<div className='cursor-pointer'>
</div>
{/* close icon */}
<div className='cursor-pointer' onClick={showMobile__Header}>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</div>
</div>
{/* nav links */}
<div className='flex'>
<div className='flex flex-col text-xl space-y-3'>
</div>
</div>
{/* Social links and languaje changer */}
<div className='flex justify-between font-light'>
<div>
<a className="link" href="">EN</a>
</div>
<div className='flex flex-col'>
</div>
</div>
</div>
{/* desktop header */}
<header className="flex w- px-10 py-1 justify-between">
<div className="flex">
{/* Left Logos */}
<div className="flex md:hidden cursor-pointer">
</div>
<div className="hidden md:flex cursor-pointer">
</div>
</div>
<div className="flex items-center">
{/* Menu icon toggle */}
<div className='flex md:hidden cursor-pointer' onClick={showMobile__Header}>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</div>
{/* Right Nav Links and language changer */}
<div className="space-x-4 px-5 py-5 hidden md:flex ">
</div>
</div>
</header>
</div>
)}
export default header
You have many HTML/CSS issues like positioning and element structure.
The free tailwindui example is a solid example to reference. It has nice transitions and accessibility baked in, which I removed for the example. It also uses headlessui and heroicons, both were built by the TW team. The TW menu components handle the state internally, so you will not be able to see the logic in their example.
The below responsive example is based on the above-referenced version but without external dependencies.
import { useState } from "react";
const Navbar = () => {
const [isOpen, setOpen] = useState(false);
const toggleMenu = () => setOpen(!isOpen);
return (
<header className="relative bg-white">
<div className="max-w-7xl mx-auto px-4 sm:px-6">
<div className="flex justify-between items-center border-b-2 border-gray-100 py-6 md:justify-start md:space-x-10">
<div className="flex justify-start lg:w-0 lg:flex-1">
<a href="#">
<span className="h-8 w-auto sm:h-10">LOGO</span>
</a>
</div>
<div className="-mr-2 -my-2 md:hidden">
<button
onClick={toggleMenu}
className="bg-white rounded-md p-2 inline-flex items-center justify-center text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-500"
>
Open
</button>
</div>
<nav className="hidden md:flex space-x-10">
<a href="#" className="text-base font-medium text-gray-500 hover:text-gray-900">
About
</a>
</nav>
</div>
</div>
{isOpen && (
<div className="absolute top-0 inset-x-0 p-2 transition transform origin-top-right md:hidden">
<div className="rounded-lg shadow-lg ring-1 ring-black ring-opacity-5 bg-white divide-y-2 divide-gray-50">
<div className="pt-5 pb-6 px-5">
<div className="flex items-center justify-between">
<div>
<span className="h-8 w-auto">LOGO</span>
</div>
<div className="-mr-2">
<button
onClick={toggleMenu}
className="bg-white rounded-md p-2 inline-flex items-center justify-center text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-500"
>
X
</button>
</div>
</div>
<div className="mt-6">
<nav className="grid gap-y-8">
<a href="#" className="p-3 flex items-center rounded-md hover:bg-gray-50">
About
</a>
</nav>
</div>
</div>
</div>
</div>
)}
</header>
);
};
You will also likely need to handle the closing of the menu on route change.
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
const Navbar = () => {
const [isOpen, setOpen] = useState(false);
const toggleMenu = () => setOpen(!isOpen);
const router = useRouter();
useEffect(() => {
const closeMenu = () => isOpen && setOpen(false);
router.events.on("routeChangeStart", closeMenu);
return () => {
router.events.off("routeChangeStart", closeMenu);
};
}, [isOpen, router]);
return (
...see above example
Without knowing exactly what you are asking, this should set you down the right path, at least from a logic standpoint.
import React, { useState } from 'react'
import Image from 'next/Image'
function header() {
const [mobile__Header, setMobile__Header] = useState(false)
const showMobile__Header = (e) => {
if (e.target.className.includes('mobile')) {
setMobile__Header(true)
} else if (e.target.className.includes('desktop')){
setMobile__Header(false)
}
}
return (
<div>
<div className='absolute flex flex-col w-screen h-screen place-content-between bg-white text-black p-5 z-50'>
<div className='flex items-center justify-between'>
<div className='cursor-pointer'>
</div>
<div className={mobile__Header === true ? 'cursor-pointer-mobile' : 'remove-display'} onClick={showMobile__Header}>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</div>
</div>
<div className='flex'>
<div className='flex flex-col text-xl space-y-3'>
</div>
</div>
<div className='flex justify-between font-light'>
<div>
<a className="link" href="">EN</a>
</div>
<div className='flex flex-col'>
</div>
</div>
</div>
<header className="flex w- px-10 py-1 justify-between">
<div className="flex">
<div className="flex md:hidden cursor-pointer">
</div>
<div className="hidden md:flex cursor-pointer">
</div>
</div>
<div className="flex items-center">
<div className={mobile__Header === false ? 'cursor-pointer-desktop' : 'remove-display'} onClick={showMobile__Header}>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</div>
<div className="space-x-4 px-5 py-5 hidden md:flex ">
</div>
</div>
</header>
</div>
)
}
export default header
Basically, make sure to differentiate between your mobile button and your desktop button using your classNames. Then, you set a bolean indicator depending on what is in the className. From there, you either display the correct container, or use a CSS class that simply puts display: none (in this case, I used the name remove-display. All that is done with an inline ternary operator.
Whether you are toggling a button, or toggling a whole container (a parent div that when display: none removes all the content inside), this approach works in both scenarios.
I am still confused as to what you are asking but my solution should get you going. You have an onClick function on two divs and the comments above each of those says are misleading. One says close icon and the other says menu icon toggle. I am not sure what we are toggling.
EDIT: Here is a much cleaner way to do it and it involved no className toggling. This will teach you what you are doing so you can actually toggle what you are trying to toggle:
import React, { useState } from 'react'
function header() {
const [isActivate, setActive] = useState(false)
const handleActivate = (e) => {
!isActivate ? setActivate(true) : setActivate(false)
}
return (
<div>
<div>
<h1 style={{cursor: 'pointer'}} onClick={handleActivate}>
{isActivate === true ? 'OPEN' : false}
</h1>
<h1 style={{cursor: 'pointer'}} onClick={handleActivate}>
{!isActivate ? 'CLOSE' : false}
</h1>
</div>
</div>
)
}
export default header

TailwindCSS fade in Element on click

So I'm making this app and I need to fade in the menu when I click the button. I have it rendering on click using state, but I can't get it to fade in / fade out on click. When I edit the opacity value inside Chrome Dev Console the transition works fine, but when I want to change it using state it doesn't.
Any help? Thanks in advance!
import React, { useState } from "react";
import { useRouter } from "next/router";
import { MenuIcon, XIcon } from "#heroicons/react/outline";
function Header() {
const router = useRouter();
const [popCard, setPopCard] = useState("hidden");
const [fade, setFade] = useState(true);
const handleMenuClick = () => {
setPopCard("inline-block");
setFade(true);
};
const handleXClick = () => {
setPopCard("hidden");
setFade(false);
};
return (
<div className="text-center">
<header className="sticky z-50 top-0 shadow-md bg-white border-b p-5">
<div className="flex justify-between items-center">
<h1
className="text-6xl text-red-500 cursor-pointer"
onClick={() => router.push("/")}
>
Velvet
</h1>
<MenuIcon
className="h-8 text-red-500 cursor-pointer"
onClick={handleMenuClick}
/>
</div>
</header>
<div
className={
popCard +
" w-[60%] flex-col border my-10 pb-3 rounded-3xl shadow-lg transition duration-300 ease-in-out " +
`${fade === true ? "opacity-100" : "opacity-0"}`
}
>
<div className="flex justify-end">
<XIcon
className="h-6 text-red-500 cursor-pointer mt-2 mr-2 opacity-70"
onClick={handleXClick}
/>
</div>
<div className="space-y-8 text-3xl text-center mt-5 mb-10 text-red-500">
<h1>Contac</h1>
<h1>About Us</h1>
</div>
</div>
</div>
);
}
export default Header;
codesandbox: Sandbox
Just to be clear, I want the menu card to fade in when I click the menu button, and I want the menu card to fade out when I click the close button.
The solution is, you need to add duration, like this:
`transition-all duration-200 ${fade ? "opacity-100" : "opacity-0"}`
Here is my forked sandbox you had given, I've removed extra inline CSS, so it may become evident.
Here is the complete code:
function Header() {
const [popCard, setPopCard] = useState("hidden");
const [fade, setFade] = useState(false);
const handleMenuClick = () => {
setPopCard("inline-block");
setFade(true);
};
const handleXClick = () => {
setPopCard("hidden");
setFade(false);
};
console.log(fade, "fade");
return (
<div className="text-center">
<header className="sticky z-50 top-0 shadow-md bg-white border-b p-5">
<div className="flex justify-between items-center">
<h1 className="text-6xl text-red-500 cursor-pointer">Velvet</h1>
<button
className="text-3xl border rounded-lg px-5"
onClick={handleMenuClick}
>
Menu
</button>
</div>
</header>
<div className="p-10">
<div
className={`transition-all duration-200 ${
fade ? "opacity-100" : "opacity-0"
}`}
>
<div className="flex justify-end">
<button className="mt-2 mr-2 border p-2" onClick={handleXClick}>
Close
</button>
</div>
<div className="space-y-2 text-3xl text-center mt-5 mb-10 mx-5 text-red-500">
<h1>Kontakt</h1>
<h1>O Velvetu</h1>
</div>
</div>
</div>
</div>
);
}
export default Header;
Sandbox: https://codesandbox.io/s/sweet-swartz-mr3nru?file=/pages/index.js:41-1396

Building a blog with React & Sanity.io - (import/no-anonymous-default-export)

This is the code for my blog page.
import React, {useState, useEffect} from "react";
import { Link } from "react-router-dom";
import sanityClient from "../client.js"
export default function Post(){
const [postData, setPost] = useState(null);
useEffect(() => {
sanityClient
.fetch(`*[_type == "post"]{
title,
slug,
mainImage{
asset->{
_id,
url
},
alt
}
}
}`)
.then((data) => setPost(data))
.catch(console.error);
}, []);
return(
<main className="bg-blue-100 min-h-screen p-12">
<section className="container mx-auto">
<h1 className="text-5xl flex justify-center cursive">
Updates Page
</h1>
<h2 className="text-lg text-gray-600 flex justify-center mb-12">
School Updates
</h2>
<div className="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
{postData && postData.map((post, index) => (
<article>
<Link to={"/post/" + post.slug.current} key={post.slug.current}>
<span className="block h-64 relative rounded shadow leading-snug bg-white border-l-8 border-blue-400" key={index}>
<img src={post.mainImage.asset.url}
alt={post.mainImage.alt}
className="w-full h-full rounded-r object-cover absolute"
/>
<span className="block relative h-full flex justify-end items-end pr-4 pb-4">
<h3 className="text-grey-800 text-lg font-bold px-3 py-4 bg-blue-700 text-blue-100 bg-opacity-75 rounded">
{post.title}
</h3>
</span>
</span>
</Link>
</article>
))}
</div>
</section>
</main>
)
}
It compiles just fine but doesn't load in the blog posts from the sanity client.
because in the sanity schema "post.js" I get green underline on everything: "Assign object to a variable before exporting as module default eslint(import/no-anonymous-default-export)"
I've looked at the docs it points me at but I'm just not getting wha the issue is. It seems fine to me. Someone please tell me where I am going wrong.
Thanks in advance!
Fixed, had an extra } in the fetch which wasn't flagging because it was inside the backticks.

Resources