update(PlayerCard): 更改路由时隐藏 PlayerCard

mack-mac
mackt 7 months ago
parent 7cea671430
commit e7e0480462

@ -2,6 +2,7 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
import { usePathname } from 'next/navigation';
import { useShallow } from 'zustand/react/shallow'; import { useShallow } from 'zustand/react/shallow';
import Player from './Player'; import Player from './Player';
@ -10,6 +11,8 @@ import PlayerCard from './PlayerCard';
import useAudioStore from '@/store/audio'; import useAudioStore from '@/store/audio';
const PlayerBar = ({ className }: { className?: string }) => { const PlayerBar = ({ className }: { className?: string }) => {
const pathname = usePathname();
const { show, audioId, showCard, setShow, setShowCard } = useAudioStore( const { show, audioId, showCard, setShow, setShowCard } = useAudioStore(
useShallow((state) => { useShallow((state) => {
return { return {
@ -22,10 +25,6 @@ const PlayerBar = ({ className }: { className?: string }) => {
}), }),
); );
const handleSwitchShowCard = () => {
setShowCard(!showCard);
};
let timer: NodeJS.Timeout; let timer: NodeJS.Timeout;
function handleScroll() { function handleScroll() {
@ -36,8 +35,13 @@ const PlayerBar = ({ className }: { className?: string }) => {
}, 800); }, 800);
} }
useEffect(() => {
if (showCard) setShowCard(false);
}, [pathname]);
useEffect(() => { useEffect(() => {
window.addEventListener('scroll', handleScroll); window.addEventListener('scroll', handleScroll);
return () => { return () => {
window.removeEventListener('scroll', handleScroll); window.removeEventListener('scroll', handleScroll);
clearInterval(timer); clearInterval(timer);
@ -56,7 +60,7 @@ const PlayerBar = ({ className }: { className?: string }) => {
className="fixed bottom-0 w-[100vw] h-[130px] m-auto bg-[#fff] shadow-lg shadow-black-[0_0_10px] z-10 transition-bottom duration-600" className="fixed bottom-0 w-[100vw] h-[130px] m-auto bg-[#fff] shadow-lg shadow-black-[0_0_10px] z-10 transition-bottom duration-600"
style={{ bottom: (show && audioId) || showCard ? 0 : -130 }} style={{ bottom: (show && audioId) || showCard ? 0 : -130 }}
> >
<Player className="m-auto" onSwitchShowCard={handleSwitchShowCard} /> <Player className="m-auto" onSwitchShowCard={() => setShowCard(!showCard)} />
</div> </div>
{/* 单曲卡片 */} {/* 单曲卡片 */}
<PlayerCard show={showCard} /> <PlayerCard show={showCard} />

Loading…
Cancel
Save