132 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-06-08 00:07:04 +08:00
import { player } from '@/web/store'
import { css, cx } from '@emotion/css'
import { useSnapshot } from 'valtio'
import Image from '@/web/components/New/Image'
import Icon from '@/web/components/Icon'
import useCoverColor from '@/web/hooks/useCoverColor'
import { resizeImage } from '@/web/utils/common'
import { motion, PanInfo, useMotionValue } from 'framer-motion'
2022-06-08 11:48:22 +08:00
import { useLockBodyScroll } from 'react-use'
import { useState } from 'react'
2022-06-08 00:07:04 +08:00
const PlayerMobile = () => {
2022-06-11 00:19:07 +08:00
const { track, state } = useSnapshot(player)
const bgColor = useCoverColor(track?.al?.picUrl ?? '')
2022-06-08 11:48:22 +08:00
const [locked, setLocked] = useState(false)
useLockBodyScroll(locked)
2022-06-08 00:07:04 +08:00
const x = useMotionValue(0)
const onDragEnd = (
event: MouseEvent | TouchEvent | PointerEvent,
info: PanInfo
) => {
2022-06-08 11:48:22 +08:00
console.log(JSON.stringify(info))
2022-06-25 13:47:07 +08:00
const { x, y } = info.offset
2022-06-08 00:07:04 +08:00
const offset = 100
2022-06-25 13:47:07 +08:00
if (y > -100) {
if (x > offset) player.prevTrack()
if (x < -offset) player.nextTrack()
}
2022-06-08 11:48:22 +08:00
setLocked(false)
2022-06-08 00:07:04 +08:00
}
2022-06-25 13:47:07 +08:00
const y = useMotionValue(0)
2022-06-08 00:07:04 +08:00
return (
<div
className={cx(
2022-06-25 13:47:07 +08:00
'relative z-20 flex h-16 w-full items-center rounded-20 px-3',
2022-06-08 00:07:04 +08:00
css`
background-color: ${bgColor.to};
`
)}
>
2022-06-25 13:47:07 +08:00
{/* Indictor */}
<motion.div
drag='y'
dragConstraints={{ top: 0, bottom: 0 }}
style={{ y: y.get() * 2 }}
2022-06-08 00:07:04 +08:00
className={cx(
2022-06-25 13:47:07 +08:00
'absolute flex items-center justify-center',
2022-06-08 00:07:04 +08:00
css`
2022-06-25 13:47:07 +08:00
--width: 60px;
--height: 26px;
left: calc((100% - var(--width)) / 2);
top: calc(var(--height) * -1);
height: var(--height);
width: var(--width);
2022-06-08 00:07:04 +08:00
`
)}
2022-06-25 13:47:07 +08:00
>
<div className='h-1.5 w-10 rounded-full bg-brand-700'></div>
</motion.div>
2022-06-08 00:07:04 +08:00
2022-06-25 13:47:07 +08:00
{/* Cover */}
2022-06-08 11:48:22 +08:00
<div className='h-full py-2.5'>
<Image
2022-06-11 00:19:07 +08:00
src={resizeImage(track?.al.picUrl || '', 'sm')}
2022-06-08 11:48:22 +08:00
className='z-10 aspect-square h-full rounded-lg'
/>
</div>
2022-06-08 00:07:04 +08:00
2022-06-25 13:47:07 +08:00
{/* Track info */}
2022-06-08 11:48:22 +08:00
<div className='relative flex h-full flex-grow items-center overflow-hidden px-3'>
2022-06-08 00:07:04 +08:00
<motion.div
drag='x'
style={{ x }}
dragConstraints={{ left: 0, right: 0 }}
2022-06-08 11:48:22 +08:00
onDragStart={() => setLocked(true)}
2022-06-08 00:07:04 +08:00
onDragEnd={onDragEnd}
2022-06-08 11:48:22 +08:00
className=' flex h-full flex-grow items-center '
2022-06-08 00:07:04 +08:00
>
2022-06-08 11:48:22 +08:00
<div className='flex-shrink-0'>
<div className='line-clamp-1 text-14 font-bold text-white'>
2022-06-11 00:19:07 +08:00
{track?.name}
2022-06-08 11:48:22 +08:00
</div>
<div className='line-clamp-1 mt-1 text-12 font-bold text-white/60'>
2022-06-11 00:19:07 +08:00
{track?.ar?.map(a => a.name).join(', ')}
2022-06-08 11:48:22 +08:00
</div>
</div>
<div className='h-full flex-grow'></div>
2022-06-08 00:07:04 +08:00
</motion.div>
<div
className={cx(
'absolute left-0 top-0 bottom-0 w-3 ',
css`
background: linear-gradient(to right, ${bgColor.to}, transparent);
`
)}
></div>
<div
className={cx(
'absolute right-0 top-0 bottom-0 w-3 bg-red-200',
css`
background: linear-gradient(to left, ${bgColor.to}, transparent);
`
)}
></div>
</div>
2022-06-25 13:47:07 +08:00
{/* Like */}
2022-06-08 00:07:04 +08:00
<button>
<Icon name='heart' className='h-7 w-7 text-white/10' />
</button>
2022-06-25 13:47:07 +08:00
{/* Play or pause */}
2022-06-08 00:07:04 +08:00
<button
onClick={() => player.playOrPause()}
className='ml-2.5 flex items-center justify-center rounded-full bg-white/20 p-2.5'
>
<Icon
2022-06-11 00:19:07 +08:00
name={state === 'playing' ? 'pause' : 'play'}
2022-06-08 00:07:04 +08:00
className='h-6 w-6 text-white/80'
/>
</button>
</div>
)
}
export default PlayerMobile