2021-07-13 17:30:41 +08:00
|
|
|
import { computed, getCurrentInstance } from 'vue'
|
|
|
|
|
|
|
|
import type { Ref } from 'vue'
|
2021-07-14 14:59:39 +08:00
|
|
|
import type {
|
|
|
|
Options,
|
|
|
|
Placement,
|
|
|
|
StrictModifiers,
|
|
|
|
PositioningStrategy,
|
|
|
|
} from '@popperjs/core'
|
2021-07-13 17:30:41 +08:00
|
|
|
|
|
|
|
interface IUsePopperProps {
|
|
|
|
arrowOffset: number
|
2021-07-14 14:59:39 +08:00
|
|
|
fallbackPlacements: Array<Placement>
|
|
|
|
gpuAcceleration: boolean
|
2021-07-13 17:30:41 +08:00
|
|
|
offset: number
|
2021-07-14 14:59:39 +08:00
|
|
|
popperOptions: Options
|
2021-07-13 17:30:41 +08:00
|
|
|
placement: Placement
|
2021-07-14 14:59:39 +08:00
|
|
|
strategy: PositioningStrategy
|
2021-07-13 17:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const usePopperOptions = (arrowRef: Ref<HTMLElement>) => {
|
|
|
|
const vm = getCurrentInstance()
|
|
|
|
|
|
|
|
const props = vm.props as unknown as IUsePopperProps
|
|
|
|
|
|
|
|
return computed(() => {
|
|
|
|
return {
|
|
|
|
placement: props.placement,
|
2021-07-14 14:59:39 +08:00
|
|
|
strategy: props.strategy,
|
2021-07-13 17:30:41 +08:00
|
|
|
...props.popperOptions,
|
|
|
|
// Avoiding overriding modifiers.
|
|
|
|
modifiers: buildModifiers({
|
|
|
|
arrow: arrowRef.value,
|
|
|
|
arrowOffset: props.arrowOffset,
|
|
|
|
offset: props.offset,
|
|
|
|
gpuAcceleration: props.gpuAcceleration,
|
|
|
|
fallbackPlacements: props.fallbackPlacements,
|
|
|
|
}, props.popperOptions?.modifiers),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ModifierProps {
|
|
|
|
offset?: number
|
|
|
|
arrow?: HTMLElement
|
|
|
|
arrowOffset?: number
|
|
|
|
gpuAcceleration?: boolean
|
|
|
|
fallbackPlacements?: Array<Placement>
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildModifiers(props: ModifierProps, externalModifiers: StrictModifiers[] = []) {
|
|
|
|
|
|
|
|
const {
|
|
|
|
arrow,
|
|
|
|
arrowOffset,
|
|
|
|
offset,
|
|
|
|
gpuAcceleration,
|
|
|
|
fallbackPlacements,
|
|
|
|
} = props
|
|
|
|
|
|
|
|
const modifiers: Array<StrictModifiers> = [
|
|
|
|
{
|
|
|
|
name: 'offset',
|
|
|
|
options: {
|
|
|
|
offset: [0, offset ?? 12],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'preventOverflow',
|
|
|
|
options: {
|
|
|
|
padding: {
|
|
|
|
top: 2,
|
|
|
|
bottom: 2,
|
|
|
|
left: 5,
|
|
|
|
right: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'flip',
|
|
|
|
options: {
|
|
|
|
padding: 5,
|
|
|
|
fallbackPlacements: fallbackPlacements ?? [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'computeStyles',
|
|
|
|
options: {
|
|
|
|
gpuAcceleration,
|
|
|
|
adaptive: gpuAcceleration,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// tippyModifier,
|
|
|
|
]
|
|
|
|
|
|
|
|
if (arrow) {
|
|
|
|
modifiers.push({
|
|
|
|
name: 'arrow',
|
|
|
|
options: {
|
|
|
|
element: arrow,
|
|
|
|
padding: arrowOffset ?? 5,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiers.push(...(externalModifiers))
|
|
|
|
return modifiers
|
|
|
|
}
|