@imnull/movable
TypeScript icon, indicating that this package has built-in type declarations

0.0.11 • Public • Published

Movable

通过 mousedown mousemove mouseup 事件,封装用户操作,快速创建拖移效果的基础模块。

useMovable

import { useEffect, useState } from 'react'
import { useMovable } from '@imnull/movable'

export default () => {
    const [target, setTarget] = useState<HTMLElement | null>(null)
    const [pos, setPos] = useState({ x: 0, y: 0 })

    useEffect(() => {
        if (target) {
            const m = useMovable({
                update: setPos,
            })
            m.init(target)
            return () => {
                m.dispose()
            }
        }
    }, [target])

    return <>
        <h1>Playgound</h1>
        <div className='block' ref={setTarget} style={{ transform: `translateX(${pos.x}px) translateY(${pos.y}px)` }}>block</div>
    </>
}

Readme

Keywords

none

Package Sidebar

Install

npm i @imnull/movable

Weekly Downloads

3

Version

0.0.11

License

ISC

Unpacked Size

7.76 kB

Total Files

5

Last publish

Collaborators

  • imnull