Display Block not moving to next line - css

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.

Related

Button not clickable with div in negative z index in tailwind css

I'm using Next.js with typescript and Tailwind CSS. (T3 stack)
I'm having trouble making the button clickable here is the code. I susspect that the div page is blocking the div and making all unclickable but I don't know how to do it. I think that the fix may be making the div where I call the component somehow not clickable
import React, { useRef, useContext, useState, useCallback } from "react";
import Image from "next/image";
import { FaChevronDown } from "react-icons/fa";
import { ScrollContext } from "../utils/scroll-observer";
const Masthead: React.FC = () => {
const refContainer = useRef<HTMLDivElement>(null);
const { scrollY } = useContext(ScrollContext);
let progress = 0;
const { current: elContainer } = refContainer;
if (elContainer) {
progress = Math.min(scrollY / elContainer.clientHeight, 1);
}
return (
<div
ref={refContainer}
className="min-h-screen flex flex-col items-center justify-center sticky top-0 -z-10"
style={{
transform: `translateY(-${progress * 20}vh)`,
}}
>
<video
autoPlay
loop
muted
playsInline
className="bg-white absolute w-full h-full object-cover"
>
<source src="/assets/background_video.mp4" />
</video>
<div className={`flex-grow-0 pt-10 transition-opacity duration-1000`}>
<Image src="/logoLiteContract.png" width={70} height={70} alt="logo" />
</div>
<div className="p-12 font-bold z-10 text-white drop-shadow-[0_5px_3px_rgba(0,0,0,0.4)] text-center flex-1 flex items-center justify-center flex-col">
<h1 className="mb-6 text-4xl xl:text-5xl">Lite contract</h1>
<h2 className="mb-2 text-2xl xl:text-3x tracking-tight">
<span>Registrate y empieza subir tus contratos</span>
</h2>
{/* Create a div in z-10 to make the button clickable */}
{/* Create a button for registration with hover effect and animation ensure that the button is on top of the video */}
{/* Space between the buttons*/}
<div className="h-10"></div>
<button className="bg-gray-700 hover:bg-black text-white font-bold py-2 px-4 rounded-full">
Registrate
</button>
{/* Space between the buttons*/}
<div className="h-10"></div>
{/* Create a button for login with hover effect and animation */}
<button className="bg-gray-500 hover:bg-gray-900 text-white font-bold py-2 px-4 rounded-full">
Iniciar sesiĆ³n
</button>
</div>
<div className="flex-grow-0 pb-20 md:pd-10 transition-all duration-1000">
<FaChevronDown className="w-10 h-10 text-white animate-bounce" />
</div>
</div>
);
};
export default Masthead;
Any Ideas?

Modal component is not showing up in Next.JS

I'm currently working on an application using Next.JS where a user can navigate to a page that contains a table with say 'Projects'. Above the table I have a button that, when clicked, should show a modal to a user that will allow him to add a new project.
I defined a Modal component but I can't seem to get it working when the user clicks the 'Add' button in my application.
Here is the Modal component code:
const Modal = ({ show, onClose }) => {
const handleCloseClick = (e) => {
e.preventDefault();
onClose();
};
return ( show ? (
<div className="modal fade fixed top-0 left-0 hidden w-full h-full outline-none overflow-x-hidden overflow-y-auto" id="exampleModalCenteredScrollable" tabIndex="-1" aria-labelledby="exampleModalCenteredScrollable" aria-modal="true" role="dialog">
<div className="modal-dialog modal-dialog-centered modal-dialog-scrollable relative w-auto pointer-events-none">
<div className="modal-content border-none shadow-lg relative flex flex-col w-full pointer-events-auto bg-white bg-clip-padding rounded-md outline-none text-current">
<div className="modal-header flex flex-shrink-0 items-center justify-between p-4 border-b border-gray-200 rounded-t-md">
<h5 className="text-xl font-medium leading-normal text-gray-800" id="exampleModalCenteredScrollableLabel">
Modal title
</h5>
<button type="button"
className="btn-close box-content w-4 h-4 p-1 text-black border-none rounded-none opacity-50 focus:shadow-none focus:outline-none focus:opacity-100 hover:text-black hover:opacity-75 hover:no-underline"
data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body relative p-4">
<p>This is some placeholder content to show a vertically centered modal. We&apos;ve added some extra copy here to show how vertically centering the modal works when combined with scrollable modals. We also use some repeated line breaks to quickly extend the height of the content, thereby triggering the scrolling. When content becomes longer than the predefined max-height of modal, content will be cropped and scrollable within the modal.</p>
<p>Just like that.</p>
</div>
<div
className="modal-footer flex flex-shrink-0 flex-wrap items-center justify-end p-4 border-t border-gray-200 rounded-b-md">
<button type="button"
onClick={handleCloseClick}
className="inline-block px-6 py-2.5 bg-purple-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-purple-700 hover:shadow-lg focus:bg-purple-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-purple-800 active:shadow-lg transition duration-150 ease-in-out"
data-bs-dismiss="modal">
Close
</button>
<button type="button"
className="inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out ml-1">
Save changes
</button>
</div>
</div>
</div>
</div>
) : null
);
};
export default Modal;
Below you can find the code that contains the table and the 'Add' button that should show the Modal.
import ProjectCols from '../data/projectcols';
import DataTable from 'react-data-table-component';
import React, { useState } from 'react';
import Modal from './modal';
const Table = () => {
const [showModal, setShowModal] = useState(false);
const data = [
{
id: 1,
project: 'Test Project A',
client: 'Customer A',
pm: 'John Doe',
active: true
},
{
id: 2,
project: 'Test Project B',
client: 'Customer B',
pm: 'Jane Doe',
active: false
}
];
return (
<div className="bg-slate-100 h-screen w-full overflow-y-auto">
<div className='flex justify-end h-20 w-3/4 m-auto'>
<div className='flex space-x-2 h-full'>
<button type='button' onClick={() => setShowModal(true)} className='self-center inline-block px-6 py-2.5 bg-green-500 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-green-600 hover:shadow-lg focus:bg-green-600 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-green-700 active:shadow-lg transition duration-150 ease-in-out'>Add</button>
<Modal onClose={() => setShowModal(false)} show={showModal}></Modal>
</div>
</div>
<div className='w-3/4 m-auto'>
<DataTable
columns={ProjectCols}
data={data} //This will need to be retrieved from the database
direction="auto"
fixedHeaderScrollHeight="300px"
pagination
responsive
/>
</div>
</div>
);
};
export default Table;
When I click the 'Add' button however, nothing happens. The modal does not get shown. I added a console.log in the Modal component just to check if it got called by the 'Add' button function and it did but I still cannot figure out why the modal doesn't show.
I'm using Tailwind CSS in my project and the table page is part of bigger page that contains a sidebar. You can find that code below:
import { useAuth } from '../../../lib/hooks/auth';
import SideBar from '../../../components/sidebar/sidebar';
import Table from '../../../components/table';
const ProjectOverview = () => {
const { user, loading, error, loggedIn } = useAuth();
return (
<div className='flex'>
{loading && <p>Loading...</p>}
{error && <p>An error occured...</p>}
{loggedIn && (
<SideBar user={user} /> //Pass in the user role as a prop to decide which items to show in the sidebar
)
}
<Table />
</div>
);
};
export default ProjectOverview;
Can anyone explain me what I'm doing wrong? Is it CSS related or is there another thing that is blocking the modal of being displayed on screen?
Thanks!
Found it! I copied pasted the Modal code from a tutorial and didn't notice that the top of the modal component had a 'hidden' class property in it... Removed it and now everything works fine!

Navbar with Tailwind Hover Has Stopped Working

I've been building this navbar this week and seem to have the layout working for the most part. However there are a couple of little functionality issues I have run in to with the hover effect and the links for the menu items on the left and the logo in the middle.
For some reason when the menu is at its full width the hover stops working, but when I collapse it to the hamburger menu it works just fine. I'm not too sure what I've miss here, and would welcome and suggestions on what I've done wrong to stop the hover and the links from working.
The accented-pink color is a custom configured color in my tailwind.config.js file
Navbar.jsx
import Link from "next/link";
import { useState } from "react";
import React from "react";
import Logo from "../Logo";
import headerLogo from "../../assets/images/headerImages/phreaquencyLogoPink.png";
import { FiGithub, FiMail, FiTwitter } from "react-icons/fi";
const NewNavbarLogoCenter = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(!active);
};
return (
<>
<nav className="flex flex-row w-screen bg-yellow-100">
<div className="fixed top-0 flex text-center pl-[5vw] pr-[5vw] md:pl-[1.5vw] md:pr-[1.5vw] lg:pt-[3vw] pb-9 lg:px-[1.5vw] z-50 text-xl w-full align-baseline pt-[5vw]">
<div className="lg:absolute flex lg:left-0 lg:right-0 z-10 lg:mx-auto lg:inline-block md:top-[3vw] md:left-[1.5vw] md:w-[calc(131px+3vw)] md:block">
<Link href="/">
<a>
{" "}
<Logo
logoSrc={headerLogo}
logoAltSrc="phreaquency logo"
logoLayout="intrinsic"
logoObjectFit="contain"
logoWidth="172px"
logoHeight="50px"
className="relative items-center "
/>
</a>
</Link>
</div>
<button
className="inline-flex p-3 ml-auto rounded outline-none hover:text-pink-accented lg:hidden"
onClick={handleClick}
>
<svg
className="w-6 h-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 6h16M4 12h16M4 18h16"
/>
</svg>
</button>
<div
className={`${
active ? "" : "hidden"
} w-full lg:inline-flex lg:flex-grow lg:w-auto`}
>
<div className="">
<div className="flex flex-col lg:flex-row lg:justify-start">
<div className="items-center justify-center w-full mr-3 lg:flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>Agency</a>
</Link>
</div>
<div className="items-center justify-center w-full mr-3 lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>Work</a>
</Link>
</div>
<div className="items-center justify-center w-full mr-3 lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>Services</a>
</Link>
</div>
<div className="items-center justify-center w-full mr-3 lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>Insights</a>
</Link>
</div>
<div className="items-center justify-center w-full mr-3 lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>Contact</a>
</Link>
</div>
</div>
<div className="lg:absolute lg:top-0 lg:right-0 flex flex-row text-center pt-[6vw] lg:pt-[3vw] lg:pb-9 px-[1.5vw] z-50 text-xl lg:items-center lg:justify-end w-full justify-center items-center content-center">
<div className="flex items-center justify-center w-full mr-3 lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>
<FiGithub />
</a>
</Link>
</div>
<div className="flex items-center justify-center w-full mr-3 lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>
<FiTwitter />
</a>
</Link>
</div>
<div className="flex items-center justify-center w-full lg:inline-flex lg:w-auto hover:text-pink-accented">
<Link href="/">
<a>
<FiMail />
</a>
</Link>
</div>
</div>
</div>
</div>
</div>
</nav>
</>
);
};
export default NewNavbarLogoCenter;
tailwind.config.js
const defaultTheme = require("tailwindcss/defaultTheme");
module.exports = {
darkMode: "class", //remove this to have dark mode switch automatically
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
"./layouts/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {
height: {
"50v": "50vh",
"60v": "60vh",
"70v": "70vh",
"80v": "80vh",
"90v": "90vh",
},
fontFamily: {
sans: ["Poppins", ...defaultTheme.fontFamily.sans],
},
colors: {
"off-white": "#f8f8ff",
"off-black": "#2e343b",
"pink-accented": "#ed61a2",
"section-overlay": "rgba(0,0,0, .2)",
},
},
},
plugins: [],
};
The accented colors are used with forms as tailwind docs says
so if you deleted the -accent and specify the degree of color after pink you should be good to go
hover:text-pink-500
Works for me.
wide screen
smaller screen
Maybe u could use the dev tools to see what's going on when u hover on wide screen.
Little advice, try to loop through the data in an array rather than writing the same code multiple times.
Here's how I test the code, hope it helps.
import React,{ useState } from "react";
const Navbar = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(!active);
};
const links = [
{
name:'Agency',
url:'/',
},
{
name:'Work',
url:'/',
},
{
name:'Services',
url:'/',
},
{
name:'Insights',
url:'/',
},
{
name:'Contact',
url:'/',
},
]
return (
<>
<nav className="flex flex-row w-screen bg-yellow-100">
<div className="fixed top-0 flex text-center pl-[5vw] pr-[5vw] md:pl-[1.5vw] md:pr-[1.5vw] lg:pt-[3vw] pb-9 lg:px-[1.5vw] z-50 text-xl w-full align-baseline pt-[5vw]">
<div className="lg:absolute flex lg:left-0 lg:right-0 z-10 lg:mx-auto lg:inline-block md:top-[3vw] md:left-[1.5vw] md:w-[calc(131px+3vw)] md:block">
<a href="/">
logo
</a>
</div>
<button
className="inline-flex p-3 ml-auto rounded outline-none hover:text-pink-accented lg:hidden"
onClick={handleClick}
>
burgerbutton
</button>
<div className={`${
active ? "" : "hidden"
} w-full lg:inline-flex lg:flex-grow lg:w-auto`}>
<div className="">
<div className="flex flex-col lg:flex-row lg:justify-start">
{
links.map((link)=>{
return (
<div className="items-center justify-center w-full mr-3 lg:flex lg:w-auto hover:text-pink-accented">
<a href={link.url}>{link.name}</a>
</div>
)
})
}
</div>
</div>
</div>
</div>
</nav>
</>
);
};
export default Navbar;

Tailwind + Headless UI: Close Mobile Menu after click or clickaway

I am using Tailwind + Headless UI to create a hamburger Menu bar on mobile to show the menu on click. But when I click on the menu it does not close automatically and creates a bad UX.
<Disclosure
as="nav"
className="fixed top-0 left-0 right-0 z-10 w-full bg-white shadow"
>
{({ open }) => (
<>
<div className="px-2 mx-auto max-w-7xl sm:px-4 lg:px-8">
<div className="flex justify-between h-16">
<div className="flex px-2 lg:px-0">
<div className="flex items-center flex-shrink-0">
<Link href="/">
<a className="relative block w-12 h-12">
<Image
src="/img/logo.png"
alt="NFT Volt Logo"
layout="fill"
className="w-auto h-6 lg:block"
/>
</a>
</Link>
</div>
<div className="hidden lg:ml-6 lg:flex lg:space-x-1">
{navLinks.map((link) => (
<NavLink key={link.id} href={link.href}>
{link.name}
</NavLink>
))}
</div>
</div>
<div className="flex items-center justify-center flex-1 px-2 lg:ml-6 lg:justify-end">
<div className="w-full max-w-lg lg:max-w-xs">
<label htmlFor="search" className="sr-only">
Search
</label>
<div className="relative">
<div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<SearchIcon
className="w-5 h-5 text-gray-400"
aria-hidden="true"
/>
</div>
<input
id="search"
name="search"
className="block w-full py-2 pl-10 pr-3 leading-5 placeholder-gray-500 bg-white border border-gray-300 rounded-md focus:outline-none focus:placeholder-gray-400 focus:ring-1 focus:ring-blue-500 focus:border-blue-500 sm:text-sm"
placeholder="Search NFT projects..."
type="search"
/>
</div>
</div>
<Link href="/list-project" passHref>
<a
href="#"
className="items-center hidden px-4 py-2 ml-6 text-sm font-medium text-white bg-blue-600 border border-transparent rounded-md shadow-sm lg:inline-flex hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 whitespace-nowrap"
>
List your Project
</a>
</Link>
</div>
<div className="flex items-center lg:hidden">
{/* Mobile menu button */}
<Disclosure.Button className="inline-flex items-center justify-center p-2 text-gray-400 rounded-md hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-blue-500">
<span className="sr-only">
Open main menu
</span>
{open ? (
<XIcon
className="block w-6 h-6"
aria-hidden="true"
/>
) : (
<MenuIcon
className="block w-6 h-6"
aria-hidden="true"
/>
)}
</Disclosure.Button>
</div>
</div>
</div>
<Disclosure.Panel className="lg:hidden">
<div className="pt-2 pb-3 space-y-1">
{/* {navLinks.map((link) => (
<NavLinkMobile
key={link.id}
href={link.href}
onClick={() => {
console.log('click');
close();
}}
>
{link.name}
</NavLinkMobile>
))} */}
{navLinks.map((link) => (
<Disclosure.Button
as={NavLinkMobile}
key={link.id}
href={link.href}
>
{link.name}
</Disclosure.Button>
))}
<NavLinkMobile href="/list-project">
List your Project
</NavLinkMobile>
</div>
</Disclosure.Panel>
</>
)}
</Disclosure>
Tried to add manually close on click but doesn't seem to work.
The trick is to wrap the links with <Disclosure.Button> </Disclosure.Button> and it will close automatically the panel.
See: https://headlessui.dev/react/disclosure#closing-disclosures-manually
Use can use the close prop from Disclose itself.
import it this way ({ open, close }) and use it
onClick={() => {
close();
}}
Try using Next.js Router's push function to navigate the user when the Disclosure.Button is clicked.
At the top of your component, call the useRouter hook:
const router = useRouter();
With that, you can modify your JSX by adding an onClick property where you then call router.push({path}) , like this:
{
navLinks.map((link) => (
<Disclosure.Button
as="a"
key={link.id}
onClick={() => {
router.push(`${link.href}`);
}}
>
{link.name}
</Disclosure.Button>
));
}
Using the Next.js router will navigate the user to the desired href while still allowing the Disclosure render prop to toggle from open to closed.
For more infomation, check out - https://nextjs.org/docs/api-reference/next/router#routerpush

Tailwind with ternary operator - Active vs Inactive class

I am working on a project with React and using Tailwind as my CSS.
I have got a Sidebar that should really not be present in the landing page when user lands only when clicking burger menu. At the moment this is present when user lands and it's looking slightly funny. I am thinking this might be related with the fact that tailwind doesn't recognise activeclasses in Ternary Operators.
My code:
import React, {useState} from 'react';
import {Link} from 'react-router-dom';
import * as FaIcons from 'react-icons/fa';
import * as AiIcons from 'react-icons/ai';
import {SidebarData} from './SidebarData';
function Navbar() {
const [sidebar,setSidebar] = useState(false)
const showSidebar =() => setSidebar(!sidebar)
return(
<>
<div className="navbar flex bg-white text-black h-12 justify-start items-center m-0 p-0 ">
<Link to ="#" className="menu-bars ml-8 text-4xl bg-none">
<FaIcons.FaBars onClick={showSidebar}/>
</Link>
</div>
<nav className={sidebar ? 'nav-menu active left-0 transition duration-250 ' : 'nav-menu bg-white w-1/4 h-screen flex justify-center fixed top-0 -left-100 transition duration-1000'}>
<ul className="nav-menu-items w-full " onClick={showSidebar}>
<li className="navbar-toggle w-full h-5 flex justify-start items-center">
<Link to='#' className="menu-bars flex justify-start items-center pt-8 pr-0 pb-10 list-none h-1">
<AiIcons.AiOutlineClose/>
</Link>
</li>
{SidebarData.map((item,index) =>{
return (
<li key={index} className="{item.cName}">
<Link to={item.path} className="text w-11/12 h-full flex items-center pt-0 pb-1 border">
{item.icon}
<span className="ml-1">{item.title}</span>
</Link>
</li>
)
})}
</ul>
</nav>
</>
);
}
How it looks like:
How it should really look like when you land:
After clicking the burger menu:
This one is in black but you get the picture. It's quite different.
All help is appreciated,thanks!
Please make sure that your tailwind config files are configured properly for the active class and check out this tutorial here

Resources