Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

navbarcomponent react

import Link from 'next/link';
import React, { useContext, useState } from 'react';
//componentsHomePageNavbarNavbar.tsx
import NavbarComponent from './NavbarComponent';
import { image } from '../../../utils/data/websiteData';

const Navbar = () => {
  return <NavbarComponent navItem={['home', 'roadmap', 'utilities', 'team']} logo={image.logo} />;
};

export default Navbar;

//componentsHomePageNavbarNavbarComponent.tsx
import Hamburger from 'hamburger-react';
import React, { FC, useContext } from 'react';
import { NavbarContext } from '../../../utils/context/NavbarContext';
import useWindowSize from '../../../utils/Hook/useWindowSize';
import { Flex } from '../../../utils/styledComponents';
import { Nav } from './SingleNav';
import { AnimatePresence, motion } from 'framer-motion';

const NavbarComponent = ({ navItem, logo }) => {
  const { toggleIsOpen, isOpen } = useContext(NavbarContext);
  const { width } = useWindowSize();

  return (
    <nav className="bg-black smallSection  min-h-[10vh] py-4">
      <Flex className="flex justify-between section-wrapper flex-center">
        {/*TODO: 1)Left image */}
        <div className="w-[150px] h-auto hover:scale-105 cursor-pointer transition">
          <img className="object-cover w-full h-full" src={logo} alt="" />
        </div>

        {/*TODO: 2) Right section */}
        <div className="non-mobile">
          <ul className="h-full flex-center navbar">
            {navItem.map(item => (
              <Nav title={item} key={item} />
            ))}
          </ul>
        </div>

        <div className=" only-mobile text-secondary">
          <Hamburger toggled={isOpen} toggle={toggleIsOpen} />
        </div>

        {/*TODO: 3)Mobile section */}
      </Flex>

      <AnimatePresence>
        {isOpen && width! < 1024 && (
          <motion.ul
            layout
            initial={{ opacity: 0, height: '0vh' }}
            animate={{ opacity: 1, height: '60vh' }}
            exit={{ opacity: 0, height: '0vh' }}
            transition={{ type: 'tween' }}
            className="flex flex-col space-y-[5%]  bg-black  w-full  justify-center h-[60vh] lg:h-[0vh] "
          >
            {navItem.map(item => (
              <Nav title={item} key={item} />
            ))}
          </motion.ul>
        )}
      </AnimatePresence>
    </nav>
  );
};

export default NavbarComponent;

//componentsHomePageNavbarSingleNav.tsx
import Link from 'next/link';
import React, { FC, useContext } from 'react';
import { NavbarContext } from '../../../utils/context/NavbarContext';

export const Nav: FC<{ title: 'home' | 'roadmap' | 'utilities' | 'team' | 'faq' }> = ({ title }) => {
  const { setCurrentSection, currentSection } = useContext(NavbarContext);
  return (
    <li onClick={() => setCurrentSection(title)}>
      <Link href={`#${title}`}>
        <a className={`${currentSection === title ? 'text-secondary' : 'text-white'} px-4 font-neuro uppercase hover:text-secondary`}>{title}</a>
      </Link>
    </li>
  );
};

Comment

Reactjs Navbar component

import React, { useEffect, useState } from "react";

const NavBar = ({ menuItems = [{route: "/", label: "Home"}], brand = "Tailwind" }) => {

    const [toggle, setToggle] = useState(false)
    const [matches, setMatches] = useState(
        window.matchMedia("(min-width: 600px)").matches
    )

    useEffect(() => {
        window
            .matchMedia("(min-width: 600px)")
            .addEventListener('change', e => {
                setMatches(e.matches);
                setToggle(false);
            });
    }, []);

    const toggleHandler = () => {
        setToggle(!toggle);
    }


    const desktopMenu = (
        <ul className="flex last:pr-3">
            {menuItems.map(item => {
                return <li key={item.route}><a href={item.route} className="p-2 hover:bg-gray-100 mx-1 rounded">{item.label}</a></li>
            })}
        </ul>
    );

    const mobileMenu = (
        <ul className="flex flex-col text-center bg-gray-700 text-gray-50">
            {menuItems.map(item => {
                return <li key={item.route}><a href={item.route} className="block p-2 hover:bg-gray-600">{item.label}</a></li>
            })}
        </ul>
    );

    return (
        <nav>
            <div className="bg-gray-800 text-slate-200 flex justify-between items-center w-full">
                <div className={`flex justify-between ${!matches && "w-full"}`}>
                    <h1 className="h-14 text-2xl font-bold px-7 flex items-center">{brand}</h1>
                    {!matches && <button onClick={toggleHandler}><i className="pr-4">fx</i></button>}
                </div>
                {matches && desktopMenu}
            </div>
            {(!matches && toggle) && mobileMenu}
        </nav>
    )
}

export default NavBar;
Comment

PREVIOUS NEXT
Code Example
Javascript :: object values template literal js 
Javascript :: splash screen react native 
Javascript :: js import export 
Javascript :: how to read files in node 
Javascript :: ajax get request parameters 
Javascript :: catch errors async await javascript 
Javascript :: Use parseInt() in the convertToInteger function so it converts the input string str into an integer, and returns it. 
Javascript :: mui date picker remove underline 
Javascript :: how to add multiple elements to A new array javascript 
Javascript :: js remove all child elements from html 
Javascript :: react hook form reset only one field 
Javascript :: how to send the mail using node with template 
Javascript :: vue nuxt vuex store watch 
Javascript :: como eliminar un elemento del dom con javascript 
Javascript :: js validation library 
Javascript :: convert datetime to timestamp javascript 
Javascript :: javascript replace ios apostrophe 
Javascript :: Make Floating label TextInput in react native 
Javascript :: dummy data json 
Javascript :: how to print an array javascript 
Javascript :: vue electron read file 
Javascript :: queryselector in javascript 
Javascript :: MONGOOSE update on by body 
Javascript :: nestjs custom error class validator 
Javascript :: ./node_modules/bootstrap/dist/js/bootstrap.bundle.js 
Javascript :: what is node js 
Javascript :: jwt strategy 
Javascript :: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string. 
Javascript :: scroll out js threshold 
Javascript :: how to remove last element from array in javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =