-
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
Showing
6 changed files
with
189 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ module.exports = { | |
parserOptions: { | ||
ecmaVersion: 'latest', | ||
}, | ||
rules: { | ||
'sort-imports': 'error', | ||
}, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
Fragment, | ||
type InjectionKey, | ||
type Ref, | ||
type VNode, | ||
cloneVNode, | ||
defineComponent, | ||
h, | ||
inject, | ||
withDirectives, | ||
} from 'vue' | ||
import { isObject } from 'kotl' | ||
|
||
export const POPPER_TRIGGER_TOKEN: InjectionKey<Ref> = Symbol('popper-trigger') | ||
|
||
export function getFirstValidChild(nodes: VNode[]): VNode | null { | ||
for (const child of nodes) { | ||
if (isObject(child)) { | ||
if (child.type === Comment) { | ||
continue | ||
} | ||
if (child.type === 'svg' || child.type === Text) { | ||
return h('span', child) | ||
} | ||
if (child.type === Fragment) { | ||
return getFirstValidChild(child.children as VNode[]) | ||
} | ||
return child | ||
} | ||
return h('span', child) | ||
} | ||
|
||
return null | ||
} | ||
|
||
// https://github.com/DevCloudFE/vue-devui/blob/dev/packages/devui-vue/devui/shared/components/popper-trigger/src/use-popper-trigger.ts | ||
export default defineComponent({ | ||
name: 'TPopperTrigger', | ||
setup(_, { slots, attrs }) { | ||
return () => { | ||
const defaultSlot = slots.default?.(attrs) | ||
const triggerRef = inject(POPPER_TRIGGER_TOKEN) as Ref<HTMLElement | null> | ||
|
||
if (!defaultSlot) { | ||
return null | ||
} | ||
|
||
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 | ||
}, | ||
}, | ||
], | ||
]) | ||
} | ||
}, | ||
}) |
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
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