Skip to content

Commit

Permalink
fix: 修改tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
zmm-fe committed Sep 10, 2020
1 parent f11fce8 commit fe9d895
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 11 deletions.
16 changes: 15 additions & 1 deletion examples/docs/zh-CN/tooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="box">
<div class="top">
<el-tooltip class="item" effect="dark" content="Top Left 提示文字" placement="top-start">
<el-button>上左</el-button>
<el-button class="zmm" @focus="focusHandler">上左</el-button>
</el-tooltip>
<el-tooltip class="item" effect="dark" content="Top Center 提示文字" placement="top">
<el-button>上边</el-button>
Expand Down Expand Up @@ -56,6 +56,20 @@
</el-tooltip>
</div>
</div>
<script>
export default {
data() {
return {
};
},
methods: {
focusHandler() {
console.log('focusHandler');
}
}
};
</script>

<style>
.box {
Expand Down
2 changes: 1 addition & 1 deletion examples/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","0.0.8":"2.13"}
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","0.0.9":"2.13"}
1 change: 1 addition & 0 deletions packages/popover/vue-popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function useVuePopper(props, { emit, slots, referenceEl }) {
}

function updatePopper() {
debugger
const popperJSRef = popperJS.value
if (popperJSRef) {
popperJSRef.update()
Expand Down
3 changes: 2 additions & 1 deletion packages/tooltip/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Tooltip from './src/main.vue'
// import Tooltip from './src/main.vue'
import Tooltip from './src/main-test'

/* istanbul ignore next */
Tooltip.install = function (app) {
Expand Down
248 changes: 248 additions & 0 deletions packages/tooltip/src/main-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import { vuePopperProps, useVuePopper } from 'packages/popover/vue-popper'
import debounce from 'throttle-debounce/debounce'
import { addClass, removeClass, on, off } from 'element-ui/src/utils/dom'
import { generateId } from 'element-ui/src/utils/util'
// eslint-disable-next-line
import { h, createApp, ref, watch, watchEffect, onMounted, getCurrentInstance, Transition } from 'vue'

export default {
name: 'ElTooltip',
emits: ['input', 'update:modelValue'],

props: {
...vuePopperProps,
openDelay: {
type: Number,
default: 0
},
disabled: Boolean,
manual: Boolean,
effect: {
type: String,
default: 'dark'
},
arrowOffset: {
type: Number,
default: 0
},
popperClass: String,
content: String,
visibleArrow: {
default: true
},
transition: {
type: String,
default: 'el-fade-in-linear'
},
popperOptions: {
default() {
return {
boundariesPadding: 10,
gpuAcceleration: false
}
}
},
enterable: {
type: Boolean,
default: true
},
hideAfter: {
type: Number,
default: 0
},
tabindex: {
type: Number,
default: 0
}
},
setup(props, context) {
let timeoutPending = ref(null)
let timeout = ref(null)
let focusing = ref(false)
let expectedState = ref(false)
let referenceElm = ref(null)
const { emit, slots } = context
const {
openDelay,
disabled,
manual,
effect,
popperClass,
content,
transition,
enterable,
hideAfter,
tabindex,
} = props
let {
showPopper,
currentPlacement,
popperElm,
popperJS,
createPopper,
updatePopper,
doDestroy,
destroyPopper,
resetTransformOrigin,
appendArrow
} = useVuePopper(props, { emit, slots, referenceEl: referenceElm})

const instance = getCurrentInstance()
const tooltipId = `el-tooltip-${generateId()}`
const debounceClose = debounce(200, () => handleClosePopper())

const show = () => {
setExpectedState(true)
handleShowPopper()
}

const hide = () => {
setExpectedState(false)
debounceClose()
}
const handleFocus = () => {
focusing.value = true
show()
}
const handleBlur = () => {
focusing.value = false
hide()
}
const removeFocusing = () => {
focusing.value = false
}

const addTooltipClass = (prev) => {
if (!prev) {
return 'el-tooltip'
} else {
return 'el-tooltip ' + prev.replace('el-tooltip', '')
}
}

const handleShowPopper = () => {
if (!expectedState.value || manual) return
clearTimeout(timeout.value)
timeout.value = setTimeout(() => {
showPopper.value = true
}, openDelay)

if (hideAfter > 0) {
timeoutPending.value = setTimeout(() => {
showPopper.value = false
}, hideAfter)
}
}

const handleClosePopper = () => {
if ((enterable && expectedState.value) || manual) return
clearTimeout(timeout.value)

if (timeoutPending.value) {
clearTimeout(timeoutPending.value)
}
showPopper.value = false

if (disabled) {
doDestroy()
}
}

const setExpectedState = (state) => {
if (state === false) {
clearTimeout(timeoutPending.value)
}
expectedState.value = state
}

const getFirstElement = () => {
const slotsDefault = slots.default()
if (!Array.isArray(slotsDefault)) return null
let element = null
for (let index = 0; index < slotsDefault.length; index++) {
if (slotsDefault[index] && slotsDefault[index].type) {
element = slotsDefault[index]
}
}
return element
}

watch(focusing, (val, old) => {
if (val) {
addClass(referenceElm.value, 'focusing')
} else {
removeClass(referenceElm.value, 'focusing')
}
})

watchEffect(() => {
debugger
const popperVM = createApp({
setup() {
debugger
return () => (<Transition name={transition} onAfterLeave={doDestroy}>
<div
onMouseleave={() => {
setExpectedState(false)
debounceClose()
}}
onMouseenter={() => {
setExpectedState(true)
}}
ref="popper"
role="tooltip"
id={tooltipId}
aria-hidden={disabled || !showPopper.value ? 'true' : 'false'}
v-show={!disabled && showPopper.value}
class={[
'el-tooltip__popper',
'is-' + effect,
popperClass
]}
>
{slots.content ? slots.content() : content}
</div>
</Transition>)
}
}).mount()

debugger

})

onMounted(() => {
referenceElm.value = instance.ctx.$el
if (referenceElm.value.nodeType === 1) {
referenceElm.value.setAttribute('aria-describedby', tooltipId)
referenceElm.value.setAttribute('tabindex', tabindex)
on(referenceElm.value, 'mouseenter', show)
on(referenceElm.value, 'mouseleave', hide)
on(referenceElm.value, 'focus', () => {
if (!slots.default || !slots.default().length) {
handleFocus()
return
}
const slotsProps = slots.default()[0].props

if (slotsProps && slotsProps.onFocus) {
slotsProps.onFocus()
} else {
handleFocus()
}
})
on(referenceElm.value, 'blur', handleBlur)
on(referenceElm.value, 'click', removeFocusing)
}
})

return () => {
const firstElement = getFirstElement()
if (!firstElement) return null

const firstElementProps = (firstElement.props = firstElement.props || {})
firstElementProps.class = addTooltipClass(firstElementProps.class)

return firstElement
}
}
}
16 changes: 8 additions & 8 deletions packages/tooltip/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ export default {
} = props
const { slots } = context
if (Array.isArray(slots.default())) {
slots.default().forEach((slot) => {
console.log( h(slot) )
})
}
if (slots.content) {
console.log(slots.content(), 'slots.content')
}
// if (Array.isArray(slots.default())) {
// slots.default().forEach((slot) => {
// console.log( h(slot) )
// })
// }
// if (slots.content) {
// console.log(slots.content(), 'slots.content')
// }
const {
showPopper,
currentPlacement,
Expand Down

0 comments on commit fe9d895

Please sign in to comment.