Home loads under navabr - css

import Home from './components/Home/Home.jsx'
import Navbar from './components/Navbar/Navbar'
import {BrowserRouter, Routes, Route} from "react-router-dom"
export default function App() {
return (
<BrowserRouter>
<Navbar />
<div className="container">
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</div>
</BrowserRouter>
);
}
App.css:
.container {
flex: 1;
display: flex;
flex-direction: column;
}
Navbar CSS
import React from "react";
import cart from "../assets/images/cart.png"
import { TfiMenu } from "react-icons/tfi";
import { AiOutlineCloseCircle} from "react-icons/ai";
export default function Navbar() {
const [open, setopen] = React.useState(false);
let Links = [
{name:"Home" ,link:"/"},
{name:"Headphone" ,link:"/"},
{name:"Speaker" ,link:"earphones"},
{name:"Earphone" ,link:"/"},
];
const change= ()=> {
setopen(!open);
}
return (
<>
<div className=" fixed shadow-md w-full top-0 left-0 ">
<div className=" lg:flex items-center bg-black py-4 md:justify-around">
<div className=" flex justify-between px-3 pt-2 text-white cursor-pointer font-[Poppins]">
audiophile
<div onClick={change} className=" lg:hidden w-9 inline-block">
{
open
? (<span className=""><AiOutlineCloseCircle/></span>)
: (<span className=""><TfiMenu/></span>)
}
</div>
</div>
<ul className={`lg:flex md:items-center ${open ?"" :"hidden"} `}>
{ Links.map((link) => (
<li className=' md:ml-8 text-xl md:my-0 my-7'>
<a href={link.link} className='text-white hover:text-yellow-500 duration-500'>{link.name}</a>
</li>
))}
</ul>
<div className>
<img className="absolute right-20 md:inline-block w-7 cursor- top-5 " src={cart} alt="cart" />
</div>
</div>
</div>
</>
);
}
this is my code and whenever I try to load the home top portion is always under the navbar. .my navbar is positioned sticky. also can't make my home width to 100. maybe because it is under another component. to render it properly i am using margin-top (mt-44) or else its under navabr Any help will be really appreciated.

you should move your container one level up and move your navbar out of it like this:
import Home from './components/Home/Home.jsx'
import Earphones from './components/Earphones/Earphones'
import Navbar from './components/Navbar/Navbar'
import {BrowserRouter,Routes,Route} from "react-router-dom"
export default function App()
{
return(
<div className="container">
<Navbar/>
<BrowserRouter>
<div className="anothercontainer">
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
</div>
</div>
)
}

Related

How to upload icon to GraphCms / GraphQl from react-icon and then render to nextJS?

I want to have the option to upload icon to graphcms and then use the icon in Next.js.
I tried to add the "Multi line text" function in graphcms and inside provide the name of the icon like this "AiFillHome".
However unfortunately it looks like this:
Bad result
this is what I need:
Required result
import React, { useState, useEffect } from "react";
import Link from "next/link";
import { getCategories } from "../services";
import { AiFillHome } from "react-icons/ai";
import { MdTravelExplore } from "react-icons/md";
import { MdFastfood } from "react-icons/md";
import { HiTrendingUp } from "react-icons/hi";
import { SiAnydesk } from "react-icons/si";
const Sidebar = () => {
const [categories, setCategories] = useState([]);
useEffect(() => {
getCategories().then((newCategories) => {
setCategories(newCategories);
});
}, []);
console.log(categories);
return (
<div className="px-8 pt-2 -mt-10">
<div className="py-10">
<ul>
<span className="tracking-widest text-gray-500 uppercase text-xxs">
category
</span>
<div className="flex flex-col pt-5 space-y-8">
<div className="flex items-center space-x-4 group">
<span className="p-2 text-gray-500 bg-gray-800 rounded-xl group-hover:text-white group-hover:bg-gray-500">
<AiFillHome />
</span>
<Link
href="/"
className="text-sm text-gray-500 group-hover:text-white group-hover:font-semibold"
>
Discover
</Link>
</div>
</div>
{categories?.map((category, index) => (
<li>
<ul className="flex flex-col pt-5 space-y-8">
<li className="flex items-center space-x-4 group">
<span className="p-2 text-gray-500 bg-gray-800 rounded-xl group-hover:text-white group-hover:bg-gray-500">
{`<${category.icon} />`}
{/*<img src={category?.icon?.url} alt="" className="" /> */}
</span>
<Link
key={index}
href={`/category/${category.slug}`}
className="text-sm text-gray-500 group-hover:text-white group-hover:font-semibold"
>
{category.name}
</Link>
</li>
</ul>
</li>
))}
</ul>
</div>
</div>
);
};
export default Sidebar;
The graphql:
export const getCategories = async () => {
const query = gql`
query GetGategories {
categories {
icon
name
slug
}
}
`;
const result = await request(graphqlAPI, query);
return result.categories;
};

My swipper next button is working in localhost but not in netlify deployed site

The refs changes the slides. It is working fine in my localhost but when it is deployed in netlify this buttons does not change the slide. Below is my code.
import React, { useRef } from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import "swiper/css";
import "swiper/css/navigation";
import { Navigation } from "swiper";
import { FaLessThan, FaGreaterThan } from 'react-icons/fa'
Here are the refs for the changing of the slides.
const ServiceSlider = () => {
const navigationPrevRef = useRef(null);
const navigationNextRef = useRef(null);
return (
<div className='mt-24'>
<div className='relative z-10 flex items-baseline space-x-3 h-[180rem] lg:h-auto'>
This is the previous button.
<div ref={navigationPrevRef}>
<button className='btn text-xs lg:text-base rounded-full bg-black'><FaLessThan /></button>
</div>
<Swiper
spaceBetween={10}
modules={[Navigation]}
onBeforeInit={(swiper) => {
swiper.params.navigation.prevEl = navigationPrevRef.current;
swiper.params.navigation.nextEl = navigationNextRef.current;
}}
navigation={{
prevEl: navigationPrevRef.current,
nextEl: navigationNextRef.current,
}}
slidesPerView={1}
className="mySwiper"
>
<SwiperSlide>
<Quality></Quality>
</SwiperSlide>
<SwiperSlide>
<Art2D></Art2D>
</SwiperSlide>
<SwiperSlide>
<Art3D></Art3D>
</SwiperSlide>
</Swiper>
Here is the next button.
<div ref={navigationNextRef}>
<button className='btn text-xs lg:text-base rounded-full bg-black'><FaGreaterThan /></button>
</div>
</div>
</div>
);
};
export default ServiceSlider;

CSS padding and width issue

So I'm trying to give conditional render div a width of full, but bcz I've given padding to its parent it's not taking the full width of the container
here is the code:
import Image from "next/image";
import DoubleTickIcon from "../PersonalChatAssets/DoubleTick.png";
import SingleTickIcon from "../PersonalChatAssets/SingleTick.png";
import { useRef, useEffect } from "react";
interface textbody {
content: string;
sent: boolean;
time: string;
replyReference: any;
}
const TextMessage = (props: textbody) => {
console.log("Referece::::", props.replyReference);
return (
<div className="flex flex-col items-end w-[332px] ml-[52px] mr-[20px] mb-[18px]">
<div className="flex justify-center items-center">
<Image src={props.sent ? DoubleTickIcon : SingleTickIcon} alt="" />
<h1 className="font-normal text-[12px] mb-0 font-[#787580]">
{props.time}
</h1>
</div>
//this is its parent
<div className="flex flex-col justify-center items-center bg-[#F7CA16] rounded-l-[16px] pr-[14px] pl-[14px] pt-[8px] pb-[8px] font-inter font-[14px] rounded-b-[16px] min-h-[40px] max-w-[340px] min-w-[60px] break-words">
//This is Conditional rendring part
{props.replyReference?.message !== undefined && (
<div className="h-[52px] reply-gradient p-2 ">
<div className="-space-y-3 overflow-ellipsis truncate border-l-[4px] border-solid border-[#1F1D25] pl-2">
<h1 className="">to {props.replyReference?.author}</h1>
<p className="max-w-[230px] truncate ">
{props.replyReference?.message}
</p>
</div>
</div>
)}
<h1 className="flex justify-center item-center w-full h-full mb-0">{props.content}</h1>
</div>
</div>
);
};
export default TextMessage;
This is the output I'm getting now:
This is the output I want:
You should move pr-[14px] pl-[14px] pt-[8px] pb-[8px] to reply-gradient and h1 elements.
Similarly, you also need to add rounded-l-[16px] rounded-b-[0] to reply-gradient to have the same border styles with the parent component.
import Image from "next/image";
import DoubleTickIcon from "../PersonalChatAssets/DoubleTick.png";
import SingleTickIcon from "../PersonalChatAssets/SingleTick.png";
import { useRef, useEffect } from "react";
interface textbody {
content: string;
sent: boolean;
time: string;
replyReference: any;
}
const TextMessage = (props: textbody) => {
console.log("Referece::::", props.replyReference);
return (
<div className="flex flex-col items-end w-[332px] ml-[52px] mr-[20px] mb-[18px]">
<div className="flex justify-center items-center">
<Image src={props.sent ? DoubleTickIcon : SingleTickIcon} alt="" />
<h1 className="font-normal text-[12px] mb-0 font-[#787580]">
{props.time}
</h1>
</div>
//this is its parent
<div className="flex flex-col justify-center items-center bg-[#F7CA16] rounded-l-[16px] font-inter font-[14px] rounded-b-[16px] min-h-[40px] max-w-[340px] min-w-[60px] break-words">
//This is Conditional rendring part
{props.replyReference?.message !== undefined && (
<div className="h-[52px] reply-gradient p-2 pr-[14px] pl-[14px] pt-[8px] pb-[8px] rounded-l-[16px] rounded-b-[0]">
<div className="-space-y-3 overflow-ellipsis truncate border-l-[4px] border-solid border-[#1F1D25] pl-2">
<h1 className="">to {props.replyReference?.author}</h1>
<p className="max-w-[230px] truncate ">
{props.replyReference?.message}
</p>
</div>
</div>
)}
<h1 className="flex justify-center item-center w-full h-full mb-0 pr-[14px] pl-[14px] pt-[8px] pb-[8px]">{props.content}</h1>
</div>
</div>
);
};
export default TextMessage;
You can check this playground

Justify-center in FlexBox causes items in scrollable div to get clipped

const { useState } = React;
const App = () => {
const [items, setItems] = useState([]);
const [numItems, setNumItems] = useState(0);
return (
<div className="w-full">
<div className="flex p-5 align-center w-full flex-col">
<div className="flex justify-center">
<button
className="border-red-500 border-2 m-2 p-2"
onClick={() => {
addItems("Item " + (numItems + 1));
setNumItems(numItems + 1);
}}
>
Add Item
</button>
</div>
<div className="flex flex-nowrap justify-center align-center p-2 overflow-x-auto border-2 border-blue-700">
{items.map((item) => (
<div className="m-2 p-2 border-red-800 border-2 flex-shrink-0" key={item}>
<h1>{item}</h1>
</div>
))}
</div>
</div>
</div>
);
function addItems(item) {
setItems([...items, item]);
}
};
ReactDOM.render(<App />, document.getElementById("app"));
I am using Tailwind css with React.js
In the component above, I have a button that simply adds divs horizontally to a flexbox that automatically scrolls. The issue is, I am not able to add the divs from the center i.e if I add the justify-center or justify-content: center; in normall css, the items in the beginning get clipped off. How can I keep adding items indefinitely, without losing the items and maintaining the ability to scroll?
Codepen link : https://codepen.io/vineet192/pen/jOGZQZE
I've changed your snippet a little bit, to make it work.
Basically the clipping was because of the overflow + center.
const { useState } = React;
const App = () => {
const [items, setItems] = useState([]);
const [numItems, setNumItems] = useState(0);
return (
<div className="w-full">
<div className="flex p-5 align-center w-full flex-col">
<div className="flex justify-center">
<button
className="border-red-500 border-2 m-2 p-2"
onClick={() => {
addItems("Item " + (numItems + 1));
setNumItems(numItems + 1);
}}
>
Add Item
</button>
</div>
<div className="flex flex-nowrap justify-center align-center p-2 overflow-x-hidden border-2 border-blue-700">
<div className='flex overflow-x-auto'>
{items.map((item) => (
<div
className="m-2 p-2 border-red-800 border-2 flex-shrink-0"
key={item}
>
<h1>{item}</h1>
</div>
))}
</div>
</div>
</div>
</div>
);
function addItems(item) {
setItems([...items, item]);
}
};
ReactDOM.render(<App />, document.getElementById("app"));

Display Block not moving to next line

I'm trying to make a responsive nav bar with tailwind. I want to list my nav links with a single item in each row when I open the navigation menu. Right now, they are all on one row. I'm using tailwind:
function Navbar() {
const [isOpen, setIsOpen] = useState(false);
return (
<header>
<div className="flex flex-col max-w-screen-xl px-4 mx-auto md:items-center md:justify-between md:flex-row md:px-6 lg:px-8">
<div className="p-4 flex flex-row items-center justify-between">
<Link href="/">
<a className="text-lg font-semibold tracking-widest text-gray-900 uppercase rounded-lg dark-mode:text-white focus:outline-none focus:shadow-outline">
Header
</a>
</Link>
<button
className="md:hidden rounded-lg focus:outline-none focus:shadow-outline"
onClick={() => {
setIsOpen(!isOpen);
}}
>
Menu
</button>
</div>
<nav className={isOpen ? "flex" : "md:flex hidden"}>
<NavLink pathTo="/" displayText="Home" />
<NavLink pathTo="/" displayText="Page 1" />
<NavLink pathTo="/" displayText="Page 2" />
</nav>
</div>
</header>
);
}
export default Navbar;
My NavLink component is as follows:
import React from "react";
import Link from "next/link";
interface NavItem {
pathTo: string;
displayText: string;
}
function NavLink(item: NavItem) {
return (
<Link href={item.pathTo}>
<a className="block lg:p-4 px-4 py-2 mt-2 border-b-2 border-white text-sm bg-transparent dark-mode:bg-transparent dark-mode:hover:border-red-400 dark-mode:focus:border-red-400 dark-mode:focus:text-white dark-mode:text-gray-200 md:mt-0 md:ml-4 hover:text-gray-900 focus:text-gray-900 hover:border-red-400 focus:border-red-400">
{item.displayText}
</a>
</Link>
);
}
export default NavLink;
What am I missing? I want the NavLinks to have one item on each row when opened from the menu. I thought display:block would do it, but it seems not to. Why's that?
You need to add a flex direction to your nav links. Adding flex-col should do the trick.

Resources