-
Notifications
You must be signed in to change notification settings - Fork 287
refactor: tooltip #1678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ttju
wants to merge
1
commit into
dev
Choose a base branch
from
jutiantian/tooltip_refact
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
refactor: tooltip #1678
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/devui-vue/devui/overlay/src/flexible-overlay/flexible-overlay.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { defineComponent, toRefs, withModifiers } from 'vue'; | ||
import { flexibleOverlayProps, FlexibleOverlayProps } from './flexible-overlay-types'; | ||
import { useOverlay } from './use-flexible-overlay'; | ||
import { useNamespace } from '../../../shared/hooks/use-namespace'; | ||
import { useNamespace } from '@devui/shared/utils'; | ||
import './flexible-overlay.scss'; | ||
|
||
export const FlexibleOverlay = defineComponent({ | ||
|
@@ -12,14 +12,15 @@ export const FlexibleOverlay = defineComponent({ | |
setup(props: FlexibleOverlayProps, { slots, attrs, emit, expose }) { | ||
const ns = useNamespace('flexible-overlay'); | ||
const { clickEventBubble } = toRefs(props); | ||
const { arrowRef, overlayRef, updatePosition } = useOverlay(props, emit); | ||
const { arrowRef, overlayRef, styles,showOverlay, updatePosition } = useOverlay(props, emit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 格式化看一下 |
||
expose({ updatePosition }); | ||
|
||
return () => | ||
props.modelValue && ( | ||
showOverlay.value && ( | ||
<div | ||
ref={overlayRef} | ||
class={ns.b()} | ||
style={styles.value} | ||
{...attrs} | ||
onClick={withModifiers(() => ({}), [clickEventBubble.value ? '' : 'stop'])} | ||
onPointerup={withModifiers(() => ({}), ['stop'])}> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { ref, unref, watch, nextTick, onUnmounted } from 'vue'; | ||
import { arrow, autoPlacement, computePosition, offset, shift } from '@floating-ui/dom'; | ||
import { FlexibleOverlayProps, Placement, Point, UseOverlayFn, EmitEventFn, Rect } from './flexible-overlay-types'; | ||
import { ref, unref, watch, nextTick, onUnmounted, computed, toRefs } from 'vue'; | ||
import { arrow, autoPlacement, computePosition, offset, shift, flip } from '@floating-ui/dom'; | ||
import { FlexibleOverlayProps, Placement, Point, EmitEventFn, Rect } from './flexible-overlay-types'; | ||
import { getScrollParent } from './utils'; | ||
|
||
function adjustArrowPosition(isArrowCenter: boolean, point: Point, placement: Placement, originRect: Rect): Point { | ||
|
@@ -24,10 +24,58 @@ function adjustArrowPosition(isArrowCenter: boolean, point: Point, placement: Pl | |
return { x, y }; | ||
} | ||
|
||
export function useOverlay(props: FlexibleOverlayProps, emit: EmitEventFn): UseOverlayFn { | ||
export function useOverlay(props: FlexibleOverlayProps, emit: EmitEventFn) { | ||
const { fitOriginWidth, autoUpdatePosition, align, position, showArrow, shiftOffset, placeStrategy } = toRefs(props); | ||
const overlayRef = ref<HTMLElement | undefined>(); | ||
const arrowRef = ref<HTMLElement | undefined>(); | ||
let originParent = null; | ||
const showOverlay = ref(false); | ||
const overlayWidth = ref(0) | ||
const baseOption = { strategy: 'fixed' } | ||
const baseMiddleware = [offset(props.offset)] | ||
let originParent: HTMLElement; | ||
let rect: DOMRect | ||
let originObserver: ResizeObserver | ||
let overlayObserver: ResizeObserver | ||
const styles = computed(() => { | ||
if (fitOriginWidth.value) { | ||
return { width: overlayWidth.value + 'px' } | ||
} else { | ||
return {} | ||
} | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 格式化看一下,没有分号 |
||
|
||
const generateMostSpaceOptions = () => { | ||
const middleware = [ | ||
...baseMiddleware, | ||
autoPlacement({ | ||
alignment: align.value, | ||
allowedPlacements: position.value, | ||
}) | ||
]; | ||
if (showArrow.value) { | ||
middleware.push(arrow({ element: arrowRef.value! })) | ||
} | ||
if (shiftOffset?.value !== undefined) { | ||
middleware.push(shift()) | ||
} | ||
return { ...baseOption, middleware } | ||
}; | ||
|
||
const generateNoSpaceOptions = () => { | ||
const [mainPostion, ...fallbackPostion] = position.value | ||
const middleware = [...baseMiddleware]; | ||
if (showArrow.value) { | ||
middleware.push(arrow({ element: arrowRef.value! })) | ||
} | ||
middleware.push(fallbackPostion.length ? flip({ fallbackPlacements: fallbackPostion }) : flip()); | ||
return { ...baseOption, placement: mainPostion, middleware } | ||
} | ||
|
||
const optionMap = { | ||
'most-space': generateMostSpaceOptions, | ||
'no-space': generateNoSpaceOptions, | ||
} | ||
|
||
const updateArrowPosition = (arrowEl: HTMLElement, placement: Placement, point: Point, overlayEl: HTMLElement) => { | ||
const { x, y } = adjustArrowPosition(props.isArrowCenter, point, placement, overlayEl.getBoundingClientRect()); | ||
const staticSide = { | ||
|
@@ -45,25 +93,16 @@ export function useOverlay(props: FlexibleOverlayProps, emit: EmitEventFn): UseO | |
}); | ||
}; | ||
const updatePosition = async () => { | ||
const hostEl = <HTMLElement>props.origin; | ||
const hostEl = <HTMLElement>(props.origin?.$el ?? props.origin); | ||
const overlayEl = <HTMLElement>unref(overlayRef.value); | ||
const arrowEl = <HTMLElement>unref(arrowRef.value); | ||
const middleware = [ | ||
offset(props.offset), | ||
autoPlacement({ | ||
alignment: props.align, | ||
allowedPlacements: props.position, | ||
}), | ||
]; | ||
props.showArrow && middleware.push(arrow({ element: arrowEl })); | ||
props.shiftOffset !== undefined && middleware.push(shift()); | ||
const { x, y, placement, middlewareData } = await computePosition(hostEl, overlayEl, { | ||
strategy: 'fixed', | ||
middleware, | ||
}); | ||
if (!hostEl || !overlayEl) { | ||
return; | ||
} | ||
const { x, y, placement, middlewareData } = await computePosition(hostEl, overlayEl, optionMap[placeStrategy.value]); | ||
let applyX = x; | ||
let applyY = y; | ||
if (props.shiftOffset !== undefined) { | ||
if (placeStrategy.value === 'most-space' && props.shiftOffset !== undefined) { | ||
const { x: shiftX, y: shiftY } = middlewareData.shift; | ||
shiftX < 0 && (applyX -= props.shiftOffset); | ||
shiftX > 0 && (applyX += props.shiftOffset); | ||
|
@@ -74,27 +113,107 @@ export function useOverlay(props: FlexibleOverlayProps, emit: EmitEventFn): UseO | |
Object.assign(overlayEl.style, { top: `${applyY}px`, left: `${applyX}px` }); | ||
props.showArrow && updateArrowPosition(arrowEl, placement, middlewareData.arrow, overlayEl); | ||
}; | ||
watch( | ||
() => props.modelValue, | ||
() => { | ||
if (props.modelValue && props.origin) { | ||
originParent = getScrollParent(props.origin); | ||
nextTick(updatePosition); | ||
originParent?.addEventListener('scroll', updatePosition); | ||
originParent !== window && window.addEventListener('scroll', updatePosition); | ||
window.addEventListener('resize', updatePosition); | ||
} else { | ||
originParent?.removeEventListener('scroll', updatePosition); | ||
originParent !== window && window.removeEventListener('scroll', updatePosition); | ||
window.removeEventListener('resize', updatePosition); | ||
|
||
const scrollCallBack = (e: Event) => { | ||
const scrollElement = e.target as HTMLElement; | ||
if (scrollElement?.contains(props.origin?.$el ?? props.origin)) { | ||
if (props.appendToBodyScrollStrategy === 'repostion') { | ||
updatePosition() | ||
} | ||
if (props.appendToBodyScrollStrategy === 'close') { | ||
showOverlay.value = false; | ||
emit('update:modelValue', false) | ||
} | ||
} | ||
} | ||
|
||
const updateWidth = (originEl: Element) => { | ||
overlayWidth.value = originEl.getBoundingClientRect().width; | ||
updatePosition() | ||
}; | ||
|
||
const observeOrigin = () => { | ||
if (fitOriginWidth.value && typeof window !== 'undefined') { | ||
const originEl = props.origin?.$el ?? props.origin | ||
if (originEl) { | ||
originObserver = new window.ResizeObserver(() => updateWidth(originEl)) | ||
originObserver.observe(originEl) | ||
} | ||
} | ||
} | ||
|
||
const unobserveOrigin = () => { | ||
const originEl = props.origin?.$el ?? props.origin | ||
originEl && originObserver?.unobserve(originEl) | ||
} | ||
|
||
const observeOverlay = () => { | ||
if (autoUpdatePosition.value && typeof window !== 'undefined') { | ||
overlayObserver = new window.ResizeObserver(updatePosition) | ||
originObserver.observe(overlayRef.value) | ||
} | ||
} | ||
const unobserveOverlay = () => { | ||
overlayRef.value && overlayObserver?.unobserve(overlayRef.value) | ||
} | ||
|
||
|
||
const checkBounds = (rect: DOMRect, scrollElement: HTMLElement) => { | ||
if (!scrollElement.getBoundingClientRect) { | ||
return false; | ||
} | ||
if (props.scrollElement) { | ||
const containerRect = scrollElement.getBoundingClientRect() | ||
const positionFixArr = [rect.height, 0, 0, 0]; | ||
const bounds = [ | ||
Math.round(rect.top + positionFixArr[0]) >= Math.round(containerRect.top), | ||
Math.round(rect.right + positionFixArr[1]) <= Math.round(containerRect.right), | ||
Math.round(rect.bottom + positionFixArr[2]) <= Math.round(containerRect.bottom), | ||
Math.round(rect.left + positionFixArr[3]) >= Math.round(containerRect.left), | ||
] | ||
if (bounds.includes(false)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
watch([() => props.modelValue, () => props.origin], () => { | ||
if (props.modelValue && props.origin) { | ||
originParent = | ||
!props.scrollElement || props.scrollElement === 'auto' | ||
? (getScrollParent((props.origin.$el ?? props.origin) as HTMLElement) as HTMLElement) | ||
: props.scrollElement; | ||
rect = ((props.origin.$el ?? props.origin) as HTMLElement).getBoundingClientRect() | ||
if (checkBounds(rect, originParent)) { | ||
showOverlay.value = false; | ||
nextTick(() => { | ||
emit('update:modelValue', false) | ||
}) | ||
return | ||
} | ||
showOverlay.value = true | ||
nextTick(updatePosition); | ||
window.addEventListener('scroll', scrollCallBack, true); | ||
window.addEventListener('resize', updatePosition); | ||
observeOrigin() | ||
nextTick(observeOverlay) | ||
} else { | ||
showOverlay.value = false; | ||
originParent?.removeEventListener('scroll', scrollCallBack, true); | ||
originParent?.removeEventListener('resize', updatePosition); | ||
unobserveOrigin() | ||
unobserveOverlay() | ||
} | ||
} | ||
); | ||
onUnmounted(() => { | ||
originParent?.removeEventListener('scroll', updatePosition); | ||
originParent !== window && window.removeEventListener('scroll', updatePosition); | ||
window.removeEventListener('resize', updatePosition); | ||
showOverlay.value = false; | ||
originParent?.removeEventListener('scroll', scrollCallBack, true); | ||
originParent?.removeEventListener('resize', updatePosition); | ||
unobserveOrigin() | ||
unobserveOverlay() | ||
|
||
}); | ||
|
||
return { arrowRef, overlayRef, updatePosition }; | ||
return { arrowRef, overlayRef, styles, showOverlay, updatePosition }; | ||
} |
37 changes: 8 additions & 29 deletions
37
packages/devui-vue/devui/shared/components/popper-trigger/src/popper-trigger.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,20 @@ | ||
import { cloneVNode, defineComponent, withDirectives, inject } from 'vue'; | ||
import type { SetupContext, Ref } from 'vue'; | ||
import { POPPER_TRIGGER_TOKEN } from './popper-trigger-types'; | ||
import { getFirstValidChild } from './use-popper-trigger'; | ||
import { defineComponent } from 'vue'; | ||
import type { SetupContext } from 'vue'; | ||
import { usePopperTrigger } from './use-popper-trigger'; | ||
|
||
export default defineComponent({ | ||
name: 'DPopperTrigger', | ||
setup(_, ctx: SetupContext) { | ||
const { slots, attrs } = ctx; | ||
const { slots } = ctx; | ||
return () => { | ||
const defaultSlot = slots.default?.(attrs); | ||
const triggerRef = inject(POPPER_TRIGGER_TOKEN) as Ref<HTMLElement | null>; | ||
|
||
const defaultSlot = slots.default?.(); | ||
if (!defaultSlot) { | ||
return null; | ||
} | ||
const {addDirectiveToFirstValidChild} = usePopperTrigger(); | ||
addDirectiveToFirstValidChild(defaultSlot) | ||
|
||
const firstValidChild = getFirstValidChild(defaultSlot); | ||
|
||
if (!firstValidChild) { | ||
return null; | ||
} | ||
|
||
return withDirectives(cloneVNode(firstValidChild, attrs), [ | ||
[ | ||
{ | ||
mounted(el) { | ||
triggerRef.value = el; | ||
}, | ||
updated(el) { | ||
triggerRef.value = el; | ||
}, | ||
unmounted() { | ||
triggerRef.value = null; | ||
}, | ||
}, | ||
], | ||
]); | ||
return defaultSlot; | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
格式化看一下