# Floating-ui

通过JS原生方式使用

pnpm add @floating-ui/dom
1
import { autoPlacement, autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/react'
import { useEffect, useRef } from 'react'
import styles from './index.module.css'

const FloatingDom = () => {
    const referenceEl = useRef(null)
    const floatingEl = useRef(null)

    useEffect(() => {
        if (referenceEl.current && floatingEl.current) {
            const updatePosition = () => {
                computePosition(referenceEl.current!, floatingEl.current!, {
                    middleware: [
                        offset(20),
                        // flip({ padding: 8 }),
                        shift({ padding: 8 })
                        // autoPlacement({ alignment: 'start' })
                    ],
                    placement: 'right'
                    // strategy: 'absolute'
                }).then(({ x, y }) => {
                    floatingEl.current &&
                        Object.assign((floatingEl.current as HTMLElement).style, {
                            left: `${x}px`,
                            top: `${y}px`
                        })
                })
            }
            window.onscroll = () => {
                updatePosition()
            }
            updatePosition()
        }
    }, [])

    return (
        <div className='flex h-[800px] flex-col'>
            <div className='h-[100px]'>111</div>
            <div className={styles.target} ref={referenceEl}>
                referenceEl
            </div>
            <div className={styles.floating} ref={floatingEl}>
                floatingEl
            </div>
        </div>
    )
}

export default FloatingDom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

当页面滚动时,通过updatePosition来更新弹出层的状态。

也可以不使用window.onscroll。而是使用floating-ui提供的api autoUpdate

useEffect(() => {
   const cleanup = autoUpdate(referenceEl.current, floatingEl.current, updatePosition)
   return () => {
       cleanup()
   }
}, [])
1
2
3
4
5
6