menu-animation

This commit is contained in:
tangly1024
2023-03-24 22:34:36 +08:00
parent ae886fac0c
commit 83584ad06a
10 changed files with 130 additions and 59 deletions

View File

@@ -7,7 +7,8 @@ import { useState } from 'react'
* @param {*} param0
* @returns
*/
export const MenuItemCollapse = ({ link }) => {
export const MenuItemCollapse = (props) => {
const { link } = props
const [show, changeShow] = useState(false)
const hasSubMenu = link?.subMenus?.length > 0
@@ -29,19 +30,19 @@ export const MenuItemCollapse = ({ link }) => {
<div className='w-full px-8 py-3 text-left border-b dark:bg-hexo-black-gray dark:border-black' onClick={toggleShow} >
{!hasSubMenu && <Link
href={link?.to}
className="font-extralight flex justify-between pl-2 pr-4 dark:text-gray-200 no-underline tracking-widest pb-1">
className="font-extralight items-center flex justify-between pl-2 pr-4 dark:text-gray-200 no-underline tracking-widest pb-1">
<span className='text-blue-400 hover:text-red-400 transition-all items-center duration-200'>{link?.name}</span>
</Link>}
{hasSubMenu && <div
onClick={hasSubMenu ? toggleOpenSubMenu : null}
className="font-extralight flex justify-between pl-2 pr-4 cursor-pointer dark:text-gray-200 no-underline tracking-widest pb-1">
className="font-extralight items-center flex justify-between pl-2 pr-4 cursor-pointer dark:text-gray-200 no-underline tracking-widest pb-1">
<span className='text-blue-400 hover:text-red-400 transition-all items-center duration-200'>{link?.name}</span>
<i className='px-2 fa fa-plus text-gray-400'></i>
<i className={`px-2 fa fa-plus transition-all duration-200 ${isOpen && 'rotate-45'} text-gray-400`}></i>
</div>}
</div>
{/* 折叠子菜单 */}
{hasSubMenu && <Collapse isOpen={isOpen}>
{hasSubMenu && <Collapse isOpen={isOpen} onHeightChange={props.onHeightChange}>
{link.subMenus.map(sLink => {
return <div key={sLink.id} className='font-extralight dark:bg-black text-left px-10 justify-start text-blue-400 bg-gray-50 hover:bg-gray-50 dark:hover:bg-gray-900 tracking-widest transition-all duration-200 border-b dark:border-gray-800 py-3 pr-6'>
<Link href={sLink.to}>

View File

@@ -2,7 +2,7 @@ import BLOG from '@/blog.config'
import Collapse from '@/components/Collapse'
import { useGlobal } from '@/lib/global'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import CONFIG_SIMPLE from '../config_simple'
import { MenuItemCollapse } from './MenuItemCollapse'
import { MenuItemDrop } from './MenuItemDrop'
@@ -22,6 +22,8 @@ export const MenuList = ({ customNav, customMenu }) => {
changeIsOpen(false)
}
const router = useRouter()
const collapseRef = useRef(null)
useEffect(() => {
router.events.on('routeChangeStart', closeMenu)
})
@@ -54,13 +56,13 @@ export const MenuList = ({ customNav, customMenu }) => {
{/* 移动端小屏菜单 */}
<div id='nav-menu-mobile' className='flex md:hidden my-auto justify-start'>
<div onClick={toggleIsOpen} className='cursor-pointer hover:text-red-400 transition-all duration-200'>
<i className='fa fa-bars mr-3' />
<i className={`${isOpen && 'rotate-90'} transition-all duration-200 fa fa-bars mr-3`} />
<span>{!isOpen ? 'MENU' : 'CLOSE'}</span>
</div>
<Collapse className='absolute w-full top-12 left-0' isOpen={isOpen}>
<Collapse collapseRef={collapseRef} className='absolute w-full top-12 left-0' isOpen={isOpen}>
<div id='menu-wrap' className='bg-white dark:border-hexo-black-gray border'>
{links?.map(link => <MenuItemCollapse key={link.id} link={link} />)}
{links?.map(link => <MenuItemCollapse key={link.id} link={link} onHeightChange={(param) => collapseRef.current?.updateCollapseHeight(param)}/>)}
</div>
</Collapse>
</div>