|
| 1 | +import { computePosition } from '@floating-ui/dom' |
| 2 | +import { |
| 3 | + type CSSProperties, |
| 4 | + type MaybeRefOrGetter, |
| 5 | + computed, |
| 6 | + shallowRef, |
| 7 | + toValue, |
| 8 | + watch, |
| 9 | + watchEffect, |
| 10 | + watchSyncEffect, |
| 11 | +} from 'vue' |
| 12 | + |
| 13 | +import { useRef } from '../hooks/useRef.ts' |
| 14 | +import type { |
| 15 | + ComputePositionConfig, |
| 16 | + MiddlewareData, |
| 17 | + ReferenceType, |
| 18 | + UseFloatingCofnig, |
| 19 | + UseFloatingOptions, |
| 20 | + UseFloatingReturn, |
| 21 | +} from './types.ts' |
| 22 | +import { roundByDPR } from './utils/roundByDPR.ts' |
| 23 | +import { getDPR } from './utils/getDPR.ts' |
| 24 | + |
| 25 | +/** |
| 26 | + * Computes the `x` and `y` coordinates that will place the floating element next to a reference element when it is given a certain CSS positioning strategy. |
| 27 | + * @param options The floating options. |
| 28 | + * @param config The floating configuration. |
| 29 | + * @see https://floating-ui.com/docs/vue |
| 30 | + */ |
| 31 | +export function useFloating<RT extends ReferenceType = ReferenceType>( |
| 32 | + options: UseFloatingOptions<RT> = {}, |
| 33 | + config: MaybeRefOrGetter<UseFloatingCofnig> = {}, |
| 34 | +): UseFloatingReturn<RT> { |
| 35 | + let configValue: UseFloatingCofnig |
| 36 | + |
| 37 | + watchEffect(() => { |
| 38 | + const shouldUpdate = configValue !== undefined |
| 39 | + configValue = toValue(config) |
| 40 | + |
| 41 | + if (shouldUpdate) { |
| 42 | + update() |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + const { |
| 47 | + transform = true, |
| 48 | + whileElementsMounted, |
| 49 | + open, |
| 50 | + elements: { |
| 51 | + reference: externalReference, |
| 52 | + floating: externalFloating, |
| 53 | + } = {}, |
| 54 | + } = options |
| 55 | + |
| 56 | + const x = shallowRef(0) |
| 57 | + const y = shallowRef(0) |
| 58 | + const strategy = shallowRef(configValue!.strategy ?? 'absolute') |
| 59 | + const placement = shallowRef(configValue!.placement ?? 'bottom') |
| 60 | + const middlewareData = shallowRef<MiddlewareData>({}) |
| 61 | + const isPositioned = shallowRef(false) |
| 62 | + |
| 63 | + const referenceRef = useRef<ReferenceType>() |
| 64 | + const floatingRef = useRef<HTMLElement>() |
| 65 | + |
| 66 | + const _reference = shallowRef<RT>() |
| 67 | + const _floating = shallowRef<HTMLElement>() |
| 68 | + |
| 69 | + function setReference(node: RT | undefined) { |
| 70 | + if (node !== referenceRef.current) { |
| 71 | + referenceRef.current = node |
| 72 | + _reference.value = node |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function setFloating(node: HTMLElement | undefined) { |
| 77 | + if (node !== floatingRef.current) { |
| 78 | + floatingRef.current = node |
| 79 | + _floating.value = node |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const referenceEl = computed(() => externalReference?.value || _reference.value) |
| 84 | + const floatingEl = computed(() => externalFloating?.value || _floating.value) |
| 85 | + |
| 86 | + function update() { |
| 87 | + if (!referenceRef.current || !floatingRef.current) |
| 88 | + return |
| 89 | + |
| 90 | + const config: ComputePositionConfig = { |
| 91 | + placement: configValue.placement, |
| 92 | + strategy: configValue.strategy, |
| 93 | + middleware: configValue.middleware, |
| 94 | + } |
| 95 | + |
| 96 | + if (configValue.platform) |
| 97 | + config.platform = configValue.platform |
| 98 | + |
| 99 | + computePosition(referenceRef.current, floatingRef.current, config).then( |
| 100 | + (data) => { |
| 101 | + x.value = data.x |
| 102 | + y.value = data.y |
| 103 | + strategy.value = data.strategy |
| 104 | + placement.value = data.placement |
| 105 | + middlewareData.value = data.middlewareData |
| 106 | + isPositioned.value = true |
| 107 | + }, |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + watch(() => toValue(open), (openVal) => { |
| 112 | + if (openVal === false && isPositioned.value) |
| 113 | + isPositioned.value = false |
| 114 | + }) |
| 115 | + |
| 116 | + watch([referenceEl, floatingEl], ([referenceEl, floatingEl], __, onCleanup) => { |
| 117 | + if (referenceEl) |
| 118 | + referenceRef.current = referenceEl |
| 119 | + |
| 120 | + if (floatingEl) |
| 121 | + floatingRef.current = floatingEl |
| 122 | + |
| 123 | + if (!referenceEl || !floatingEl) |
| 124 | + return |
| 125 | + |
| 126 | + if (!whileElementsMounted) { |
| 127 | + update() |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + onCleanup(whileElementsMounted(referenceEl, floatingEl, update)) |
| 132 | + }) |
| 133 | + |
| 134 | + const floatingStyles = computed<CSSProperties>(() => { |
| 135 | + const initialStyles = { |
| 136 | + position: strategy.value, |
| 137 | + left: 0, |
| 138 | + top: 0, |
| 139 | + } |
| 140 | + |
| 141 | + const floating = floatingEl.value |
| 142 | + if (!floating) |
| 143 | + return initialStyles |
| 144 | + |
| 145 | + const xVal = roundByDPR(floating, x.value) |
| 146 | + const yVal = roundByDPR(floating, y.value) |
| 147 | + |
| 148 | + if (transform) { |
| 149 | + return { |
| 150 | + ...initialStyles, |
| 151 | + transform: `translate(${xVal}px, ${yVal}px)`, |
| 152 | + ...(getDPR(floating) >= 1.5 && { willChange: 'transform' }), |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return { |
| 157 | + position: strategy.value, |
| 158 | + left: `${xVal}px`, |
| 159 | + top: `${yVal}px`, |
| 160 | + } |
| 161 | + }) |
| 162 | + |
| 163 | + return { |
| 164 | + x, |
| 165 | + y, |
| 166 | + strategy, |
| 167 | + placement, |
| 168 | + middlewareData, |
| 169 | + isPositioned, |
| 170 | + floatingStyles, |
| 171 | + refs: { |
| 172 | + reference: referenceRef, |
| 173 | + floating: floatingRef, |
| 174 | + setReference, |
| 175 | + setFloating, |
| 176 | + }, |
| 177 | + elements: { |
| 178 | + reference: referenceEl, |
| 179 | + floating: floatingEl, |
| 180 | + }, |
| 181 | + update, |
| 182 | + } |
| 183 | +} |
0 commit comments