Skip to content

Commit

Permalink
feat: trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
JasKang committed Jun 15, 2023
1 parent baf1ecb commit 120f9d4
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ module.exports = {
parserOptions: {
ecmaVersion: 'latest',
},
rules: {
'sort-imports': 'error',
},
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@vue/tsconfig": "^0.4.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.40.0",
"eslint-plugin-perfectionist": "^1.3.0",
"jsdom": "^22.0.0",
"postcss": "^8.4.23",
"postcss-import": "^15.1.0",
Expand Down
101 changes: 100 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions src/components/Popper/PopperTrigger.tsx
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
},
},
],
])
}
},
})
36 changes: 10 additions & 26 deletions src/components/Popper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
import {
ref,
computed,
defineComponent,
type ExtractPropTypes,
type ExtractPublicPropTypes,
type InjectionKey,
type PropType,
type SlotsType,
type VNode,
type Ref,
Teleport,
computed,
defineComponent,
inject,
onBeforeMount,
onBeforeUnmount,
onMounted,
onUnmounted,
provide,
shallowRef,
toRef,
watchEffect,
toRefs,
provide,
onUnmounted,
inject,
type InjectionKey,
type Ref,
type ShallowRef,
onMounted,
watchEffect,
} from 'vue'
import {
useFloating,
type UseFloatingOptions,
type Placement,
type Strategy,
type Middleware,
autoUpdate,
offset,
shift,
} from '@floating-ui/vue'
import { type Placement, type Strategy, autoUpdate, offset, shift, useFloating } from '@floating-ui/vue'
import { uid } from 'kotl'

const props = {
Expand Down Expand Up @@ -111,7 +96,6 @@ export const Popper = defineComponent({
})
onMounted(() => {
parent?.append({ id, floatingEl, open })
// click outside floatingEl to close
})
const clickOutsideHandler = (e: MouseEvent) => {
console.log('clickOutsideHandler', e.target)
Expand Down
8 changes: 4 additions & 4 deletions src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ref,
computed,
defineComponent,
type ExtractPropTypes,
type ExtractPublicPropTypes,
type PropType,
type SlotsType,
type VNode,
Teleport,
type VNode,
computed,
defineComponent,
ref,
} from 'vue'
import { Popper } from '../Popper'
import { useFloating } from '@floating-ui/vue'
Expand Down

0 comments on commit 120f9d4

Please sign in to comment.