2021-07-13 17:30:41 +08:00
|
|
|
import {
|
|
|
|
cloneVNode,
|
|
|
|
computed,
|
|
|
|
Fragment,
|
|
|
|
getCurrentInstance,
|
|
|
|
h,
|
|
|
|
nextTick,
|
|
|
|
toDisplayString,
|
|
|
|
toRef,
|
|
|
|
Transition,
|
|
|
|
ref,
|
|
|
|
renderSlot,
|
|
|
|
withDirectives,
|
|
|
|
} from 'vue'
|
|
|
|
import { NOOP } from '@vue/shared'
|
|
|
|
import { createPopper } from '@popperjs/core'
|
|
|
|
import { ClickOutside } from '@element-plus/directives'
|
|
|
|
import {
|
|
|
|
generateId,
|
|
|
|
isHTMLElement,
|
|
|
|
isString,
|
|
|
|
refAttacher,
|
|
|
|
} from '@element-plus/utils/util'
|
|
|
|
import { getFirstValidNode } from '@element-plus/utils/vnode'
|
|
|
|
import { stop } from '@element-plus/utils/dom'
|
|
|
|
import PopupManager from '@element-plus/utils/popup-manager'
|
2021-09-10 10:01:17 +08:00
|
|
|
import { throwError } from '@element-plus/utils/error'
|
2021-07-13 17:30:41 +08:00
|
|
|
|
|
|
|
import useTeleport from '../use-teleport'
|
|
|
|
import useTimeout from '../use-timeout'
|
|
|
|
import { useModelToggle } from '../use-model-toggle'
|
|
|
|
import { useTransitionFallthrough } from '../use-transition-fallthrough'
|
2021-07-15 14:08:13 +08:00
|
|
|
import { defaultPopperOptions, defaultModifiers } from './use-popper-options'
|
2021-07-13 17:30:41 +08:00
|
|
|
import { useTargetEvents, DEFAULT_TRIGGER } from './use-target-events'
|
|
|
|
|
|
|
|
import type {
|
|
|
|
CSSProperties,
|
|
|
|
ComponentPublicInstance,
|
|
|
|
ExtractPropTypes,
|
|
|
|
PropType,
|
|
|
|
} from 'vue'
|
|
|
|
import type {
|
|
|
|
Instance as PopperInstance,
|
2021-07-15 14:08:13 +08:00
|
|
|
StrictModifiers,
|
2021-07-13 17:30:41 +08:00
|
|
|
} from '@popperjs/core'
|
2021-08-24 13:36:48 +08:00
|
|
|
import type { RefElement, Nullable } from '@element-plus/utils/types'
|
2021-07-13 17:30:41 +08:00
|
|
|
import type { Trigger } from './use-target-events'
|
|
|
|
|
2021-08-24 13:36:48 +08:00
|
|
|
export type PopperEffect = 'light' | 'dark'
|
2021-07-13 17:30:41 +08:00
|
|
|
export type Offset = [number, number] | number
|
2021-08-24 13:36:48 +08:00
|
|
|
|
2021-07-13 17:30:41 +08:00
|
|
|
type ElementType = ComponentPublicInstance | HTMLElement
|
|
|
|
|
|
|
|
export const DARK_EFFECT = 'dark'
|
|
|
|
export const LIGHT_EFFECT = 'light'
|
|
|
|
|
2021-07-15 14:08:13 +08:00
|
|
|
export const usePopperControlProps = {
|
2021-07-14 14:59:39 +08:00
|
|
|
appendToBody: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2021-07-13 17:30:41 +08:00
|
|
|
arrowOffset: {
|
|
|
|
type: Number,
|
2021-07-14 14:59:39 +08:00
|
|
|
},
|
2021-07-15 14:08:13 +08:00
|
|
|
popperOptions: defaultPopperOptions,
|
|
|
|
popperClass: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
2021-07-13 17:30:41 +08:00
|
|
|
},
|
2021-07-14 14:59:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const usePopperProps = {
|
2021-07-15 14:08:13 +08:00
|
|
|
...usePopperControlProps,
|
2021-07-14 14:59:39 +08:00
|
|
|
// the arrow size is an equailateral triangle with 10px side length, the 3rd side length ~ 14.1px
|
|
|
|
// adding a offset to the ceil of 4.1 should be 5 this resolves the problem of arrow overflowing out of popper.
|
|
|
|
autoClose: {
|
2021-07-13 17:30:41 +08:00
|
|
|
type: Number,
|
|
|
|
default: 0,
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
2021-07-15 14:08:13 +08:00
|
|
|
class: String,
|
2021-07-13 17:30:41 +08:00
|
|
|
style: Object,
|
|
|
|
hideAfter: {
|
|
|
|
type: Number,
|
|
|
|
default: 200,
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
effect: {
|
2021-08-24 13:36:48 +08:00
|
|
|
type: String as PropType<PopperEffect>,
|
2021-07-13 17:30:41 +08:00
|
|
|
default: DARK_EFFECT,
|
|
|
|
},
|
|
|
|
enterable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
manualMode: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
showAfter: {
|
|
|
|
type: Number,
|
|
|
|
default: 0,
|
|
|
|
},
|
|
|
|
pure: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
showArrow: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
transition: {
|
|
|
|
type: String,
|
|
|
|
default: 'el-fade-in-linear',
|
|
|
|
},
|
|
|
|
trigger: {
|
|
|
|
type: [String, Array] as PropType<Trigger>,
|
|
|
|
default: DEFAULT_TRIGGER,
|
|
|
|
},
|
|
|
|
visible: {
|
|
|
|
type: Boolean,
|
|
|
|
default: undefined,
|
|
|
|
},
|
|
|
|
stopPopperMouseEvent: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:36:48 +08:00
|
|
|
export const usePopperHook = () => {
|
2021-07-13 17:30:41 +08:00
|
|
|
const vm = getCurrentInstance()
|
2021-08-24 13:36:48 +08:00
|
|
|
const props: ExtractPropTypes<typeof usePopperProps> = vm.props as any
|
2021-07-13 17:30:41 +08:00
|
|
|
const { slots } = vm
|
|
|
|
|
|
|
|
const arrowRef = ref<RefElement>(null)
|
|
|
|
const triggerRef = ref<ElementType>(null)
|
|
|
|
const popperRef = ref<RefElement>(null)
|
|
|
|
|
|
|
|
const popperStyle = ref<CSSProperties>({ zIndex: PopupManager.nextZIndex() })
|
|
|
|
const visible = ref(false)
|
2021-09-04 19:29:28 +08:00
|
|
|
const isManual = computed(
|
|
|
|
() => props.manualMode || props.trigger === 'manual'
|
|
|
|
)
|
2021-07-13 17:30:41 +08:00
|
|
|
|
|
|
|
const popperId = `el-popper-${generateId()}`
|
|
|
|
let popperInstance: Nullable<PopperInstance> = null
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const { renderTeleport, showTeleport, hideTeleport } = useTeleport(
|
|
|
|
popupRenderer,
|
|
|
|
toRef(props, 'appendToBody')
|
|
|
|
)
|
2021-07-13 17:30:41 +08:00
|
|
|
|
|
|
|
const { show, hide } = useModelToggle({
|
|
|
|
indicator: visible,
|
|
|
|
onShow,
|
|
|
|
onHide,
|
|
|
|
})
|
|
|
|
|
|
|
|
const { registerTimeout, cancelTimeout } = useTimeout()
|
|
|
|
|
|
|
|
// event handlers
|
|
|
|
|
|
|
|
function onShow() {
|
|
|
|
popperStyle.value.zIndex = PopupManager.nextZIndex()
|
|
|
|
nextTick(initializePopper)
|
|
|
|
}
|
|
|
|
|
|
|
|
function onHide() {
|
|
|
|
hideTeleport()
|
|
|
|
nextTick(detachPopper)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The calling mechanism here is:
|
|
|
|
* when the visibility gets changed, let's say we change it to true
|
|
|
|
* the delayShow gets called which initializes a global root node for the popper content
|
|
|
|
* to insert in, then it register a timer for calling the show method, which changes the flag to
|
|
|
|
* true, then calls onShow method.
|
|
|
|
* So the order of invocation is: delayedShow -> timer(show) -> set the indicator to true -> onShow
|
|
|
|
*/
|
|
|
|
|
|
|
|
function delayShow() {
|
|
|
|
if (isManual.value || props.disabled) return
|
|
|
|
// renders out the teleport element root.
|
|
|
|
showTeleport()
|
|
|
|
registerTimeout(show, props.showAfter)
|
|
|
|
}
|
|
|
|
|
|
|
|
function delayHide() {
|
|
|
|
if (isManual.value) return
|
|
|
|
registerTimeout(hide, props.hideAfter)
|
|
|
|
}
|
|
|
|
|
|
|
|
function onToggle() {
|
|
|
|
if (visible.value) {
|
|
|
|
delayShow()
|
|
|
|
} else {
|
|
|
|
delayHide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function detachPopper() {
|
|
|
|
popperInstance?.destroy?.()
|
|
|
|
popperInstance = null
|
|
|
|
}
|
|
|
|
|
|
|
|
function onPopperMouseEnter() {
|
|
|
|
// if trigger is click, user won't be able to close popper when
|
|
|
|
// user tries to move the mouse over popper contents
|
|
|
|
if (props.enterable && props.trigger !== 'click') {
|
|
|
|
cancelTimeout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onPopperMouseLeave() {
|
|
|
|
const { trigger } = props
|
|
|
|
const shouldPrevent =
|
|
|
|
(isString(trigger) && (trigger === 'click' || trigger === 'focus')) ||
|
|
|
|
// we'd like to test array type trigger here, but the only case we need to cover is trigger === 'click' or
|
|
|
|
// trigger === 'focus', because that when trigger is string
|
|
|
|
// trigger.length === 1 and trigger[0] === 5 chars string is mutually exclusive.
|
|
|
|
// so there will be no need to test if trigger is array type.
|
|
|
|
(trigger.length === 1 &&
|
|
|
|
(trigger[0] === 'click' || trigger[0] === 'focus'))
|
|
|
|
if (shouldPrevent) return
|
|
|
|
delayHide()
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializePopper() {
|
|
|
|
if (!visible.value || popperInstance !== null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const unwrappedTrigger = triggerRef.value
|
|
|
|
const $el = isHTMLElement(unwrappedTrigger)
|
|
|
|
? unwrappedTrigger
|
|
|
|
: (unwrappedTrigger as ComponentPublicInstance).$el
|
2021-07-15 14:08:13 +08:00
|
|
|
|
|
|
|
popperInstance = createPopper($el, popperRef.value, buildPopperOptions())
|
2021-07-13 17:30:41 +08:00
|
|
|
popperInstance.update()
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:08:13 +08:00
|
|
|
function buildPopperOptions() {
|
2021-09-04 19:29:28 +08:00
|
|
|
const modifiers = [...defaultModifiers, ...props.popperOptions.modifiers]
|
2021-07-15 14:08:13 +08:00
|
|
|
|
|
|
|
if (props.showArrow) {
|
|
|
|
modifiers.push({
|
|
|
|
name: 'arrow',
|
|
|
|
options: {
|
|
|
|
padding: props.arrowOffset || 5,
|
|
|
|
element: arrowRef.value,
|
|
|
|
},
|
|
|
|
} as StrictModifiers)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...props.popperOptions,
|
|
|
|
modifiers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const { onAfterEnter, onAfterLeave, onBeforeEnter, onBeforeLeave } =
|
|
|
|
useTransitionFallthrough()
|
2021-07-13 17:30:41 +08:00
|
|
|
|
|
|
|
const events = useTargetEvents(delayShow, delayHide, onToggle)
|
|
|
|
|
|
|
|
const arrowRefAttacher = refAttacher(arrowRef)
|
|
|
|
const popperRefAttacher = refAttacher(popperRef)
|
|
|
|
const triggerRefAttacher = refAttacher(triggerRef)
|
|
|
|
|
|
|
|
// renderers
|
|
|
|
function popupRenderer() {
|
|
|
|
const mouseUpAndDown = props.stopPopperMouseEvent ? stop : NOOP
|
|
|
|
return h(
|
2021-08-24 13:36:48 +08:00
|
|
|
Transition as any,
|
2021-07-13 17:30:41 +08:00
|
|
|
{
|
|
|
|
name: props.transition,
|
|
|
|
onAfterEnter,
|
|
|
|
onAfterLeave,
|
|
|
|
onBeforeEnter,
|
|
|
|
onBeforeLeave,
|
|
|
|
},
|
|
|
|
{
|
2021-09-04 19:29:28 +08:00
|
|
|
default: () => () =>
|
|
|
|
visible.value
|
|
|
|
? h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
'aria-hidden': false,
|
|
|
|
class: [
|
|
|
|
props.popperClass,
|
|
|
|
'el-popper',
|
|
|
|
`is-${props.effect}`,
|
|
|
|
props.pure ? 'is-pure' : '',
|
|
|
|
],
|
|
|
|
style: popperStyle.value,
|
|
|
|
id: popperId,
|
|
|
|
ref: popperRefAttacher,
|
|
|
|
role: 'tooltip',
|
|
|
|
onMouseenter: onPopperMouseEnter,
|
|
|
|
onMouseleave: onPopperMouseLeave,
|
|
|
|
onClick: stop,
|
|
|
|
onMousedown: mouseUpAndDown,
|
|
|
|
onMouseup: mouseUpAndDown,
|
|
|
|
},
|
|
|
|
[
|
|
|
|
renderSlot(slots, 'default', {}, () => [
|
|
|
|
toDisplayString(props.content),
|
|
|
|
]),
|
|
|
|
arrowRenderer(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
}
|
2021-07-13 17:30:41 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrowRenderer() {
|
|
|
|
return props.showArrow
|
|
|
|
? h(
|
2021-09-04 19:29:28 +08:00
|
|
|
'div',
|
|
|
|
{
|
|
|
|
ref: arrowRefAttacher,
|
|
|
|
class: 'el-popper__arrow',
|
|
|
|
'data-popper-arrow': '',
|
|
|
|
},
|
|
|
|
null
|
|
|
|
)
|
2021-07-13 17:30:41 +08:00
|
|
|
: null
|
|
|
|
}
|
|
|
|
|
|
|
|
function triggerRenderer(triggerProps) {
|
|
|
|
const trigger = slots.trigger?.()
|
|
|
|
const firstElement = getFirstValidNode(trigger, 1)
|
2021-09-04 19:29:28 +08:00
|
|
|
if (!firstElement)
|
|
|
|
throwError('renderTrigger', 'trigger expects single rooted node')
|
2021-07-13 17:30:41 +08:00
|
|
|
return cloneVNode(firstElement, triggerProps, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
const trigger = triggerRenderer({
|
|
|
|
'aria-describedby': popperId,
|
|
|
|
class: props.class,
|
|
|
|
style: props.style,
|
|
|
|
ref: triggerRefAttacher,
|
|
|
|
...events,
|
|
|
|
})
|
|
|
|
return h(Fragment, null, [
|
|
|
|
isManual.value
|
|
|
|
? trigger
|
|
|
|
: withDirectives(trigger, [[ClickOutside, delayHide]]),
|
|
|
|
renderTeleport(),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
render,
|
|
|
|
}
|
|
|
|
}
|