-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add trigger component
- Loading branch information
Showing
21 changed files
with
1,616 additions
and
70 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
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,8 @@ | ||
<template> | ||
<slot v-if="mounted" /> | ||
</template> | ||
|
||
<script setup lang='ts'> | ||
const mounted = ref(false) | ||
onMounted(() => { mounted.value = true }) | ||
</script> |
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,46 @@ | ||
import { defineComponent } from 'vue' | ||
import ResizeObserver from 'resize-observer-polyfill' | ||
import { useFirstElement } from '../_hooks' | ||
|
||
export default defineComponent({ | ||
name: 'ResizeObserver', | ||
props: { | ||
watchOnUpdated: Boolean, | ||
}, | ||
emits: ['resize'], | ||
setup(_, { emit, slots }) { | ||
const { children, firstElement } = useFirstElement() | ||
let resizeObserver: ResizeObserver | null | ||
|
||
const createResizeObserver = (target: HTMLElement) => { | ||
if (!target) return | ||
resizeObserver = new ResizeObserver((entries: ResizeObserverEntry[]) => { | ||
const entry = entries[0] | ||
emit('resize', entry) | ||
}) | ||
resizeObserver.observe(target) | ||
} | ||
|
||
const destroyResizeObserver = () => { | ||
if (resizeObserver) { | ||
resizeObserver.disconnect() | ||
resizeObserver = null | ||
} | ||
} | ||
|
||
watch(firstElement, (element) => { | ||
if (resizeObserver) destroyResizeObserver() | ||
if (element) | ||
createResizeObserver(element) | ||
}) | ||
|
||
onBeforeUnmount(() => { | ||
if (resizeObserver) destroyResizeObserver() | ||
}) | ||
|
||
return () => { | ||
children.value = slots.default?.() | ||
return children.value | ||
} | ||
}, | ||
}) |
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,3 @@ | ||
export * from './useFirstElement' | ||
export * from './useTeleport' | ||
export * from './useResizeObserver' |
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,20 @@ | ||
import { getFirstElementFromChildren } from '@onu-ui/utils' | ||
import type{ VNode } from 'vue' | ||
export const useFirstElement = () => { | ||
const children: { value?: VNode[] } = {} | ||
const firstElement = ref<HTMLElement>() | ||
|
||
const getFirstElement = () => { | ||
const element = getFirstElementFromChildren(children.value) | ||
if (element !== firstElement.value) | ||
firstElement.value = element | ||
} | ||
|
||
onMounted(() => getFirstElement()) | ||
onUpdated(() => getFirstElement()) | ||
|
||
return { | ||
children, | ||
firstElement, | ||
} | ||
} |
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,34 @@ | ||
import { isFunction } from '@onu-ui/utils' | ||
import ResizeObserver from 'resize-observer-polyfill' | ||
import type { Ref } from 'vue' | ||
|
||
export const useResizeObserver = ({ | ||
elementRef, | ||
onResize, | ||
}: { | ||
elementRef: Ref<HTMLElement | undefined> | ||
onResize: (entry?: ResizeObserverEntry) => void | ||
}) => { | ||
let resizeObserver: ResizeObserver | null | ||
|
||
const createResizeObserver = () => { | ||
if (!elementRef.value) return | ||
resizeObserver = new ResizeObserver((entries: ResizeObserverEntry[]) => { | ||
const entry = entries[0] | ||
isFunction(onResize) && onResize(entry) | ||
}) | ||
resizeObserver.observe(elementRef.value) | ||
} | ||
|
||
const destroyResizeObserver = () => { | ||
if (resizeObserver) { | ||
resizeObserver.disconnect() | ||
resizeObserver = null | ||
} | ||
} | ||
|
||
return { | ||
createResizeObserver, | ||
destroyResizeObserver, | ||
} | ||
} |
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,52 @@ | ||
import { onMounted, ref, watch } from 'vue' | ||
import { getElement } from '@onu-ui/utils' | ||
import type { Ref } from 'vue' | ||
|
||
/** | ||
* | ||
* @param param0 | ||
* @returns teleportContainer: mounted Teleport container,containerRef: popupContainer element | ||
*/ | ||
export const useTeleport = ({ | ||
popupContainer, | ||
visible, | ||
defaultContainer = 'body', | ||
documentContainer, | ||
}: { | ||
popupContainer: Ref<string | HTMLElement | undefined> | ||
visible: Ref<boolean> | ||
defaultContainer?: string | ||
documentContainer?: boolean | ||
}) => { | ||
const teleportContainer = ref(popupContainer.value) | ||
const containerRef = ref<HTMLElement>() | ||
|
||
const getContainer = () => { | ||
const element = getElement(popupContainer.value) | ||
const _teleportContainer = element | ||
? popupContainer.value | ||
: defaultContainer | ||
const _containerElement | ||
= element | ||
?? (documentContainer | ||
? document.documentElement | ||
: getElement(defaultContainer)) | ||
if (_teleportContainer !== teleportContainer.value) | ||
teleportContainer.value = _teleportContainer | ||
|
||
if (_containerElement !== containerRef.value) | ||
containerRef.value = _containerElement | ||
} | ||
|
||
onMounted(() => getContainer()) | ||
|
||
watch(visible, (visible) => { | ||
if (teleportContainer.value !== popupContainer.value && visible) | ||
getContainer() | ||
}) | ||
|
||
return { | ||
teleportContainer, | ||
containerRef, | ||
} | ||
} |
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
Empty file.
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,12 @@ | ||
import type { InjectionKey } from 'vue' | ||
|
||
export interface TriggerContext { | ||
onMouseenter: (ev: MouseEvent) => void | ||
onMouseleave: (ev: MouseEvent) => void | ||
addChildRef: (ref: any) => void | ||
removeChildRef: (ref: any) => void | ||
} | ||
|
||
/** use for Nesting triggers */ | ||
export const triggerInjectionKey: InjectionKey<TriggerContext> | ||
= Symbol('OnuTrigger') |
Oops, something went wrong.