-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jaskang
committed
Jul 24, 2023
1 parent
29151eb
commit 3c29642
Showing
5 changed files
with
141 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,45 +1,63 @@ | ||
import { cloneVNode, type ComponentPublicInstance, defineComponent, onMounted, onUnmounted, ref, watch } from 'vue' | ||
import { on } from 'events' | ||
import { | ||
cloneVNode, | ||
type ComponentPublicInstance, | ||
defineComponent, | ||
onMounted, | ||
onUnmounted, | ||
onUpdated, | ||
type PropType, | ||
type Ref, | ||
ref, | ||
type VNode, | ||
watch, | ||
watchEffect, | ||
} from 'vue' | ||
|
||
import { extractSingleChild } from '@/utils/vnode' | ||
import { getFirstElementFromChildren, mergeFirstChild } from '@/utils/vnode' | ||
|
||
export function useFirstElement(): [Ref<HTMLElement | null>, (v: VNode[]) => VNode[]] { | ||
let vnode: VNode[] = [] | ||
// only save VNodes reference, not use ref | ||
const el = ref<HTMLElement | null>(null) | ||
const hookVNode = (v: VNode[]) => { | ||
vnode = v | ||
return v | ||
} | ||
const getFirstElement = () => { | ||
const element = getFirstElementFromChildren(vnode) | ||
if (element !== el.value) { | ||
el.value = element | ||
} | ||
} | ||
|
||
onMounted(() => getFirstElement()) | ||
onUpdated(() => getFirstElement()) | ||
onUnmounted(() => { | ||
el.value = null | ||
}) | ||
return [el, hookVNode] | ||
} | ||
|
||
export const RefSlot = defineComponent({ | ||
name: 'TRefSlot', | ||
props: { | ||
extraProps: Object as PropType<Record<string, unknown>>, | ||
}, | ||
emits: { | ||
updateEl: (el: HTMLElement | null) => true, | ||
}, | ||
setup(props, { slots, emit }) { | ||
const childRef = ref<HTMLElement | ComponentPublicInstance | null>(null) | ||
watch(childRef, child => { | ||
if (child) { | ||
if ('$el' in child) { | ||
emit('updateEl', child.$el || null) | ||
} else { | ||
emit('updateEl', child) | ||
} | ||
} else { | ||
emit('updateEl', null) | ||
} | ||
}) | ||
onUnmounted(() => { | ||
childRef.value = null | ||
// trigger相关变量 | ||
const [el, hookVNode] = useFirstElement() | ||
|
||
watch(el, () => { | ||
emit('updateEl', el.value) | ||
}) | ||
return () => { | ||
const slotVNodes = slots.default?.() ?? [] | ||
const child = extractSingleChild(slotVNodes) | ||
|
||
const childWithRef = | ||
child && | ||
cloneVNode( | ||
child, | ||
{ | ||
ref: el => { | ||
// @ts-expect-error | ||
childRef.value = el | ||
}, | ||
}, | ||
true | ||
) | ||
return childWithRef | ||
const children = hookVNode(slots.default?.() ?? []) | ||
mergeFirstChild(children, props.extraProps || {}) | ||
return <>{children}</> | ||
} | ||
}, | ||
}) |
This file contains 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,3 +1,5 @@ | ||
export type Fn = (...args: any[]) => any | ||
|
||
export type Data = Record<string, any> | ||
|
||
export type Flat<T> = T extends Fn ? T : T extends object ? { [K in keyof T]: T[K] } : T |
This file contains 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