From 86289a78f6a73e1e635ef4f9a641ecf6a647ce42 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sun, 22 Sep 2024 08:59:04 +0800 Subject: [PATCH] =?UTF-8?q?gitbook=20=E5=AF=BC=E8=88=AA=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9hover-expand=E9=80=82=E9=85=8D=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/gitbook/components/NavPostItem.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/themes/gitbook/components/NavPostItem.js b/themes/gitbook/components/NavPostItem.js index 0110be77..46664a56 100644 --- a/themes/gitbook/components/NavPostItem.js +++ b/themes/gitbook/components/NavPostItem.js @@ -1,6 +1,7 @@ import Badge from '@/components/Badge' import Collapse from '@/components/Collapse' import { siteConfig } from '@/lib/config' +import { useEffect, useState } from 'react' import CONFIG from '../config' import BlogPostCard from './BlogPostCard' @@ -13,17 +14,32 @@ import BlogPostCard from './BlogPostCard' */ const NavPostItem = props => { const { group, expanded, toggleItem } = props // 接收传递的展开状态和切换函数 - // const [isOpen, setIsOpen] = useState(expanded) // 使用展开状态作为组件内部状态 const hoverExpand = siteConfig('GITBOOK_FOLDER_HOVER_EXPAND', false, CONFIG) + const [isTouchDevice, setIsTouchDevice] = useState(false) + + // 检测是否为触摸设备 + useEffect(() => { + const checkTouchDevice = () => { + if (window.matchMedia('(pointer: coarse)').matches) { + setIsTouchDevice(true) + } + } + checkTouchDevice() + + // 可选:监听窗口大小变化时重新检测 + window.addEventListener('resize', checkTouchDevice) + return () => { + window.removeEventListener('resize', checkTouchDevice) + } + }, []) // 当展开状态改变时触发切换函数,并根据传入的展开状态更新内部状态 const toggleOpenSubMenu = () => { toggleItem() // 调用父组件传递的切换函数 - // setIsOpen(!expanded) // 更新内部状态为传入的展开状态的相反值 } const onHoverToggle = () => { // 允许鼠标悬停时自动展开,而非点击展开 - if (!hoverExpand) { + if (!hoverExpand || isTouchDevice) { return } toggleOpenSubMenu()