dou-ui 0.1.0-alpha.21 → 0.1.0-alpha.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/chat-input/chat-input.vue_vue_type_script_setup_true_lang.js +19 -14
- package/dist/components/chat-input/chat-input.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/chat-input/chat-input2.js.map +1 -1
- package/dist/components/chat-list/chat-list.js.map +1 -1
- package/dist/components/chat-list/chat-list.vue_vue_type_script_setup_true_lang.js +51 -19
- package/dist/components/chat-list/chat-list.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/chat-list/chat-list2.js.map +1 -1
- package/dist/components/chat-message/chat-message.js.map +1 -1
- package/dist/components/chat-message/chat-message.vue_vue_type_script_setup_true_lang.js +52 -12
- package/dist/components/chat-message/chat-message.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/chat-message/chat-message2.js.map +1 -1
- package/dist/components/chat-plan/chat-plan.js.map +1 -1
- package/dist/components/chat-plan/chat-plan.vue_vue_type_script_setup_true_lang.js +22 -9
- package/dist/components/chat-plan/chat-plan.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/chat-plan/chat-plan2.js.map +1 -1
- package/dist/components/code-block/code-block.js.map +1 -1
- package/dist/components/code-block/code-block.vue_vue_type_script_setup_true_lang.js +144 -7
- package/dist/components/code-block/code-block.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/dock/dock-group.js.map +1 -1
- package/dist/components/dock/dock-group.vue_vue_type_script_setup_true_lang.js +4 -2
- package/dist/components/dock/dock-group.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/dock/dock.js.map +1 -1
- package/dist/components/dock/dock.vue_vue_type_script_setup_true_lang.js +30 -4
- package/dist/components/dock/dock.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/dock/style.js +1 -0
- package/dist/components/form/form.vue_vue_type_script_setup_true_lang.js +1 -0
- package/dist/components/form/form.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/form/form2.js.map +1 -1
- package/dist/components/image/image-preview.js.map +1 -1
- package/dist/components/image/image-preview.vue_vue_type_script_setup_true_lang.js +63 -95
- package/dist/components/image/image-preview.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/image/use-preview-viewport.js +619 -0
- package/dist/components/image/use-preview-viewport.js.map +1 -0
- package/dist/components/image/viewport.js +142 -0
- package/dist/components/image/viewport.js.map +1 -0
- package/dist/components/link/link.js.map +1 -1
- package/dist/components/link/link.vue_vue_type_script_setup_true_lang.js +59 -14
- package/dist/components/link/link.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/popover/popover.js.map +1 -1
- package/dist/components/popover/popover.vue_vue_type_script_setup_true_lang.js +13 -3
- package/dist/components/popover/popover.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/splitter/splitter.js.map +1 -1
- package/dist/components/splitter/splitter.vue_vue_type_script_setup_true_lang.js +111 -41
- package/dist/components/splitter/splitter.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/tabs/style.js +1 -0
- package/dist/components/tabs/tab-button.js +6 -0
- package/dist/components/tabs/tab-button.js.map +1 -0
- package/dist/components/tabs/tab-button.vue_vue_type_script_setup_true_lang.js +108 -0
- package/dist/components/tabs/tab-button.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/tabs/tab-drag.js +10 -2
- package/dist/components/tabs/tab-drag.js.map +1 -1
- package/dist/components/tabs/tab-panel.js.map +1 -1
- package/dist/components/tabs/tab-panel.vue_vue_type_script_setup_true_lang.js +4 -0
- package/dist/components/tabs/tab-panel.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/tabs/tabs.js.map +1 -1
- package/dist/components/tabs/tabs.vue_vue_type_script_setup_true_lang.js +430 -181
- package/dist/components/tabs/tabs.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/tabs/tabs2.js.map +1 -1
- package/dist/components/virtual-list/virtual-list.vue_vue_type_script_setup_true_lang.js +77 -34
- package/dist/components/virtual-list/virtual-list.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/virtual-list/virtual-list2.js.map +1 -1
- package/dist/composables/use-floating.js +60 -8
- package/dist/composables/use-floating.js.map +1 -1
- package/dist/composables/use-measured-sizes.js +7 -3
- package/dist/composables/use-measured-sizes.js.map +1 -1
- package/dist/composables/use-popup.js +3 -0
- package/dist/composables/use-popup.js.map +1 -1
- package/dist/composables/use-touch-scroll.js +141 -29
- package/dist/composables/use-touch-scroll.js.map +1 -1
- package/dist/locale/en.js +3 -2
- package/dist/locale/en.js.map +1 -1
- package/dist/locale/zh-cn.js +3 -2
- package/dist/locale/zh-cn.js.map +1 -1
- package/dist/locale/zh-tw.js +3 -2
- package/dist/locale/zh-tw.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/types/components/chat-list/chat-list.d.ts +13 -0
- package/dist/types/components/chat-list/index.d.ts +2 -0
- package/dist/types/components/chat-message/chat-message.d.ts +19 -0
- package/dist/types/components/chat-message/chat-message.vue.d.ts +1 -0
- package/dist/types/components/chat-message/index.d.ts +3 -0
- package/dist/types/components/chat-plan/chat-plan.d.ts +3 -1
- package/dist/types/components/chat-plan/chat-plan.vue.d.ts +1 -0
- package/dist/types/components/chat-plan/index.d.ts +3 -0
- package/dist/types/components/code-block/code-block.d.ts +23 -0
- package/dist/types/components/code-block/code-block.vue.d.ts +8 -2
- package/dist/types/components/code-block/index.d.ts +19 -3
- package/dist/types/components/dock/dock.d.ts +6 -0
- package/dist/types/components/image/image.d.ts +1 -1
- package/dist/types/components/image/use-preview-viewport.d.ts +48 -0
- package/dist/types/components/image/viewport.d.ts +58 -0
- package/dist/types/components/link/index.d.ts +3 -0
- package/dist/types/components/link/link.d.ts +12 -3
- package/dist/types/components/link/link.vue.d.ts +1 -0
- package/dist/types/components/popover/index.d.ts +3 -0
- package/dist/types/components/popover/popover.d.ts +7 -0
- package/dist/types/components/popover/popover.vue.d.ts +1 -0
- package/dist/types/components/splitter/index.d.ts +6 -9
- package/dist/types/components/splitter/splitter.d.ts +31 -4
- package/dist/types/components/splitter/splitter.vue.d.ts +3 -4
- package/dist/types/components/tabs/index.d.ts +3 -0
- package/dist/types/components/tabs/tab-button.vue.d.ts +28 -0
- package/dist/types/components/tabs/tab-drag.d.ts +8 -0
- package/dist/types/components/tabs/tab-panel.vue.d.ts +1 -0
- package/dist/types/components/tabs/tabs.d.ts +6 -0
- package/dist/types/composables/use-floating.d.ts +1 -1
- package/dist/types/composables/use-measured-sizes.d.ts +2 -0
- package/dist/types/composables/use-touch-scroll.d.ts +8 -0
- package/dist/types/locale/types.d.ts +6 -3
- package/package.json +3 -3
package/dist/components/virtual-list/virtual-list.vue_vue_type_script_setup_true_lang.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"virtual-list.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createElementBlock","_normalizeClass","_unref","_normalizeStyle","_Fragment","_renderList","_renderSlot"],"sources":["../../../src/components/virtual-list/virtual-list.vue"],"sourcesContent":["<script setup lang=\"ts\" generic=\"T\">\nimport type { HTMLAttributes } from 'vue'\nimport type { ForwardedAttributes } from '../../utils/install'\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n onUpdated,\n ref,\n shallowRef,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useMeasuredSizes } from '../../composables/use-measured-sizes'\nimport { useTouchScroll } from '../../composables/use-touch-scroll'\nimport { scrollPositionOf } from '../../utils/scroll'\nimport { useNamespace } from '../../utils/namespace'\nimport { createOffsets, virtualItemKey } from './virtual-list'\nimport type {\n VirtualListAlign,\n VirtualListEmits,\n VirtualListExpose,\n VirtualListKey,\n VirtualListProps,\n VirtualListSlots,\n} from './virtual-list'\n\ndefineOptions({ name: 'DouVirtualList' })\n\nconst props = withDefaults(\n defineProps<\n VirtualListProps<T> &\n /* @vue-ignore */ ForwardedAttributes<\n HTMLAttributes,\n VirtualListProps<T>,\n keyof VirtualListEmits & string\n >\n >(),\n {\n itemSize: undefined,\n estimateSize: 40,\n overscan: 4,\n endThreshold: 40,\n },\n)\nconst emit = defineEmits<{ scroll: [event: Event]; endReached: [] }>()\ndefineSlots<VirtualListSlots<T>>()\n\n// The list itself takes focus (tabindex 0) so the keyboard can scroll it.\n// A parent that manages focus on its own passes `:tabindex=\"undefined\"`.\nconst ns = useNamespace('virtual-list')\nconst elRef = useTemplateRef<HTMLElement>('el')\n\nconst sizeBy = (size: number | ((item: T, index: number) => number), index: number) =>\n typeof size === 'function' ? size(props.items[index]!, index) : size\n\n// Without `itemSize`, heights come from the rendered items.\nconst measured = computed(() => props.itemSize === undefined)\nconst measuredSizes = useMeasuredSizes({\n count: () => props.items.length,\n keyOf: (index) => keyOf(props.items[index]!, index),\n estimate: (index) => sizeBy(props.estimateSize, index),\n container: () => elRef.value,\n})\nconst fixedOffsets = computed(() =>\n createOffsets(props.items.length, (index) => sizeBy(props.itemSize!, index)),\n)\nconst offsets = computed(() => (measured.value ? measuredSizes.offsets.value : fixedOffsets.value))\nconst sizeOf = (index: number) =>\n measured.value ? undefined : `${sizeBy(props.itemSize!, index)}px`\nonUpdated(measuredSizes.prune)\n\nconst scrollTop = ref(0)\n// The measured viewport, or the `height` prop until it can be measured.\nconst viewportSize = ref(0)\nconst viewport = computed(\n () => viewportSize.value || (typeof props.height === 'number' ? props.height : 0),\n)\n\nconst range = computed(() => {\n const count = props.items.length\n if (!count) return { from: 0, to: 0 }\n const { find } = offsets.value\n const first = find(scrollTop.value)\n const last = find(scrollTop.value + Math.max(viewport.value, 1) - 1)\n return {\n from: Math.max(0, first - props.overscan),\n to: Math.min(count, last + 1 + props.overscan),\n }\n})\n\nconst keyOf = (item: T, index: number) => virtualItemKey(props.itemKey, item, index)\n\nconst visible = computed(() => {\n const { from, to } = range.value\n return props.items.slice(from, to).map((item, offset) => {\n const index = from + offset\n return { item, index, key: keyOf(item, index) }\n })\n})\n\nconst style = computed(() =>\n props.height === undefined\n ? undefined\n : { height: typeof props.height === 'number' ? `${props.height}px` : props.height },\n)\n\nlet reachedEnd = false\nfunction onScroll(event: Event) {\n const el = event.currentTarget as HTMLElement\n // iOS reports positions past either end while the content bounces back\n scrollTop.value = scrollPositionOf(el).top\n remember(el)\n emit('scroll', event)\n const atEnd = el.scrollTop + el.clientHeight >= el.scrollHeight - props.endThreshold\n if (atEnd && !reachedEnd && props.items.length) emit('endReached')\n reachedEnd = atEnd\n}\n// New items push the end away again.\nwatch(\n () => props.items.length,\n () => (reachedEnd = false),\n)\n\n// With measured items, items added or removed above the view keep the\n// first visible item where it was, e.g. earlier messages loaded on top,\n// also when the view is at the very top.\nconst touch = useTouchScroll()\nlet anchor: { key: VirtualListKey; offset: number } | undefined\nfunction remember(el: HTMLElement) {\n if (!measured.value || !props.items.length) return (anchor = undefined)\n const top = scrollPositionOf(el).top\n const index = offsets.value.find(top)\n anchor = {\n key: keyOf(props.items[index]!, index),\n offset: offsets.value.start(index) - top,\n }\n}\nwatch(\n () => props.items,\n (items) => {\n const held = anchor\n const el = elRef.value\n if (!held || !el) return\n const index = items.findIndex((item, at) => keyOf(item, at) === held.key)\n if (index < 0) return\n // a move iOS drops during a touch scroll is made once it is over\n const target = Math.max(0, offsets.value.start(index) - held.offset)\n touch.shiftScroll(el, target - el.scrollTop)\n scrollTop.value = scrollPositionOf(el).top\n remember(el)\n },\n { flush: 'post' },\n)\n\nconst observer = shallowRef<ResizeObserver>()\nonMounted(() => {\n const el = elRef.value\n if (!el) return\n viewportSize.value = el.clientHeight\n if (typeof ResizeObserver === 'undefined') return\n observer.value = new ResizeObserver(() => (viewportSize.value = el.clientHeight))\n observer.value.observe(el)\n})\nonBeforeUnmount(() => observer.value?.disconnect())\n\nfunction scrollTo(offset: number) {\n const el = elRef.value\n if (!el) return\n el.scrollTop = offset\n // Rendering follows right away, without waiting for the scroll event.\n scrollTop.value = el.scrollTop\n}\n\nfunction scrollToIndex(index: number, align: VirtualListAlign = 'nearest', settle = 2) {\n if (index < 0 || index >= props.items.length) return\n // Measured items land where estimated at first; once the items around\n // the target are rendered and measured, aim again.\n if (measured.value && settle > 0 && typeof requestAnimationFrame !== 'undefined') {\n requestAnimationFrame(() => scrollToIndex(index, align, settle - 1))\n }\n const { start } = offsets.value\n const top = start(index)\n const bottom = start(index + 1)\n const height = viewport.value\n const current = scrollTop.value\n let target = current\n if (align === 'start') target = top\n else if (align === 'end') target = bottom - height\n else if (align === 'center') target = top - (height - (bottom - top)) / 2\n else if (top < current) target = top\n else if (bottom > current + height) target = bottom - height\n if (target !== current) scrollTo(Math.max(0, target))\n}\n\ndefineExpose<VirtualListExpose>({\n scrollToIndex: (index, align) => scrollToIndex(index, align),\n scrollTo,\n el: () => elRef.value,\n})\n</script>\n\n<template>\n <div ref=\"el\" tabindex=\"0\" :class=\"ns.b()\" :style=\"style\" @scroll=\"onScroll\">\n <div v-if=\"items.length\" :class=\"ns.e('spacer')\" :style=\"{ height: `${offsets.total}px` }\">\n <div\n v-for=\"entry in visible\"\n :key=\"entry.key\"\n :ref=\"\n measured ? (el) => measuredSizes.track(el as Element, entry.key, entry.index) : undefined\n \"\n :class=\"ns.e('item')\"\n :data-index=\"entry.index\"\n :style=\"{\n transform: `translateY(${offsets.start(entry.index)}px)`,\n height: sizeOf(entry.index),\n }\"\n >\n <slot :item=\"entry.item\" :index=\"entry.index\" />\n </div>\n </div>\n <slot v-else name=\"empty\" />\n </div>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;EA6BA,MAAM,QAAQ;EAgBd,MAAM,OAAO;EAKb,MAAM,KAAK,aAAa,cAAc;EACtC,MAAM,QAAQ,eAA4B,IAAI;EAE9C,MAAM,UAAU,MAAqD,UACnE,OAAO,SAAS,aAAa,KAAK,MAAM,MAAM,QAAS,KAAK,IAAI;EAGlE,MAAM,WAAW,eAAe,MAAM,aAAa,KAAA,CAAS;EAC5D,MAAM,g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A;IAnBD,KAAI;IAAK,UAAS;IAAK,OAAKC,eAAEC,MAAA,EAAA,CAAE,CAAC,EAAC,CAAA;IAAK,OAAKC,eAAE,MAAA,KAAK;IAAW;G,IACtD,QAAA,MAAM,UAAjBJ,UAAA,GAAAC,mBAgBM,OAAA;;IAhBoB,OAAKC,eAAEC,MAAA,EAAA,CAAE,CAAC,EAAC,QAAA,CAAA;IAAa,OAAKC,eAAA,EAAA,QAAA,GAAe,QAAA,MAAQ,MAAK,IAAA,CAAA;G,KACjFJ,UAAA,IAAA,GAAAC,mBAcMI,UAAA,MAAAC,WAbY,QAAA,QAAT,UAAK;IADd,OAAAN,UAAA,GAAAC,mBAcM,OAAA;KAZH,KAAK,MAAM;;KACX,KAAgB,SAAA,SAAY,OAAOE,MAAA,aAAA,CAAa,CAAC,MAAM,IAAe,MAAM,KAAK,MAAM,KAAK,IAAI,KAAA;KAGhG,OAAKD,eAAEC,MAAA,EAAA,CAAE,CAAC,EAAC,MAAA,CAAA;KACX,cAAY,MAAM;KAClB,OAAKC,eAAA;MAAuC,WAAA,cAAA,QAAA,MAAQ,MAAM,MAAM,KAAK,EAAA;MAA0B,QAAA,OAAO,MAAM,KAAK;;I,IAKlHG,WAAgD,KAAA,QAAA,WAAA;KAAzC,MAAM,MAAM;KAAO,OAAO,MAAM;;G,kBAG3CA,WAA4B,KAAA,QAAA,SAAA,CAAA,GAAA,KAAA,GAAA,KAAA,GAAA,CAAA,C"}
|
|
1
|
+
{"version":3,"file":"virtual-list.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createElementBlock","_normalizeClass","_unref","_normalizeStyle","_createCommentVNode","_Fragment","_renderList","_renderSlot"],"sources":["../../../src/components/virtual-list/virtual-list.vue"],"sourcesContent":["<script setup lang=\"ts\" generic=\"T\">\nimport type { HTMLAttributes } from 'vue'\nimport type { ForwardedAttributes } from '../../utils/install'\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n onUpdated,\n ref,\n shallowRef,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useMeasuredSizes } from '../../composables/use-measured-sizes'\nimport { useTouchScroll } from '../../composables/use-touch-scroll'\nimport { scrollPositionOf } from '../../utils/scroll'\nimport { useNamespace } from '../../utils/namespace'\nimport { createOffsets, virtualItemKey } from './virtual-list'\nimport type {\n VirtualListAlign,\n VirtualListEmits,\n VirtualListExpose,\n VirtualListKey,\n VirtualListProps,\n VirtualListSlots,\n} from './virtual-list'\n\ndefineOptions({ name: 'DouVirtualList' })\n\nconst props = withDefaults(\n defineProps<\n VirtualListProps<T> &\n /* @vue-ignore */ ForwardedAttributes<\n HTMLAttributes,\n VirtualListProps<T>,\n keyof VirtualListEmits & string\n >\n >(),\n {\n itemSize: undefined,\n estimateSize: 40,\n overscan: 4,\n endThreshold: 40,\n },\n)\nconst emit = defineEmits<{ scroll: [event: Event]; endReached: [] }>()\ndefineSlots<VirtualListSlots<T>>()\n\n// The list itself takes focus (tabindex 0) so the keyboard can scroll it.\n// A parent that manages focus on its own passes `:tabindex=\"undefined\"`.\nconst ns = useNamespace('virtual-list')\nconst elRef = useTemplateRef<HTMLElement>('el')\n\nconst sizeBy = (size: number | ((item: T, index: number) => number), index: number) =>\n typeof size === 'function' ? size(props.items[index]!, index) : size\n\nconst touch = useTouchScroll()\nconst pendingOffset = computed(() => touch.pendingShift(elRef.value))\nconst spacerStyle = computed(() =>\n pendingOffset.value ? { transform: `translateY(${-pendingOffset.value}px)` } : undefined,\n)\n\nfunction logicalTopOf(el: HTMLElement) {\n return Math.max(0, scrollPositionOf(el).top + touch.pendingShift(el))\n}\n\n// Without `itemSize`, heights come from the rendered items.\nconst measured = computed(() => props.itemSize === undefined)\nconst measuredSizes = useMeasuredSizes({\n count: () => props.items.length,\n keyOf: (index) => keyOf(props.items[index]!, index),\n estimate: (index) => sizeBy(props.estimateSize, index),\n container: () => elRef.value,\n onShift: (_shift, el) => {\n syncScroll(el)\n if (touch.active.value) touch.whenSettled(syncAfterTouch)\n },\n})\nconst fixedOffsets = computed(() =>\n createOffsets(props.items.length, (index) => sizeBy(props.itemSize!, index)),\n)\nconst offsets = computed(() => (measured.value ? measuredSizes.offsets.value : fixedOffsets.value))\nconst sizeOf = (index: number) =>\n measured.value ? undefined : `${sizeBy(props.itemSize!, index)}px`\nonUpdated(measuredSizes.prune)\n\nconst scrollTop = ref(0)\n// The measured viewport, or the `height` prop until it can be measured.\nconst viewportSize = ref(0)\nconst viewport = computed(\n () => viewportSize.value || (typeof props.height === 'number' ? props.height : 0),\n)\n\nconst range = computed(() => {\n const count = props.items.length\n if (!count) return { from: 0, to: 0 }\n const { find } = offsets.value\n const first = find(scrollTop.value)\n const last = find(scrollTop.value + Math.max(viewport.value, 1) - 1)\n return {\n from: Math.max(0, first - props.overscan),\n to: Math.min(count, last + 1 + props.overscan),\n }\n})\n\nconst keyOf = (item: T, index: number) => virtualItemKey(props.itemKey, item, index)\n\nconst visible = computed(() => {\n const { from, to } = range.value\n return props.items.slice(from, to).map((item, offset) => {\n const index = from + offset\n return { item, index, key: keyOf(item, index) }\n })\n})\nconst spacerBefore = computed(() => offsets.value.start(range.value.from))\nconst spacerAfter = computed(() =>\n Math.max(0, offsets.value.total - offsets.value.start(range.value.to)),\n)\n\nconst style = computed(() =>\n props.height === undefined\n ? undefined\n : { height: typeof props.height === 'number' ? `${props.height}px` : props.height },\n)\n\nlet reachedEnd = false\nfunction onScroll(event: Event) {\n const el = event.currentTarget as HTMLElement\n // iOS reports positions past either end while the content bounces back\n scrollTop.value = logicalTopOf(el)\n remember(el)\n emit('scroll', event)\n const atEnd =\n el.scrollTop + touch.pendingShift(el) + el.clientHeight >= el.scrollHeight - props.endThreshold\n if (atEnd && !reachedEnd && props.items.length) emit('endReached')\n reachedEnd = atEnd\n}\n// New items push the end away again.\nwatch(\n () => props.items.length,\n () => (reachedEnd = false),\n)\n\n// With measured items, items added or removed above the view keep the\n// first visible item where it was, e.g. earlier messages loaded on top,\n// also when the view is at the very top.\nlet anchor: { key: VirtualListKey; offset: number } | undefined\nfunction remember(el: HTMLElement) {\n if (!measured.value || !props.items.length) return (anchor = undefined)\n const top = logicalTopOf(el)\n const index = offsets.value.find(top)\n anchor = {\n key: keyOf(props.items[index]!, index),\n offset: offsets.value.start(index) - top,\n }\n}\nfunction syncScroll(el: HTMLElement) {\n scrollTop.value = logicalTopOf(el)\n remember(el)\n}\nfunction syncAfterTouch() {\n const el = elRef.value\n if (el) syncScroll(el)\n}\nfunction targetOf(items: T[]) {\n const held = anchor\n if (!held) return undefined\n const index = items.findIndex((item, at) => keyOf(item, at) === held.key)\n if (index < 0) return undefined\n return Math.max(0, offsets.value.start(index) - held.offset)\n}\n// Keep the rendered window on the anchored item before DOM updates so visible\n// items stay mounted and newly prepended items do not briefly render at top.\nwatch(\n () => props.items,\n (items) => {\n const target = targetOf(items)\n if (target !== undefined) scrollTop.value = target\n },\n)\nwatch(\n () => props.items,\n (items) => {\n const el = elRef.value\n const target = targetOf(items)\n if (!el || target === undefined) return\n // a move iOS drops during a touch scroll is made once it is over\n touch.shiftScroll(el, target - logicalTopOf(el))\n syncScroll(el)\n if (touch.active.value) touch.whenSettled(syncAfterTouch)\n },\n { flush: 'post' },\n)\n\nconst observer = shallowRef<ResizeObserver>()\nonMounted(() => {\n const el = elRef.value\n if (!el) return\n viewportSize.value = el.clientHeight\n if (typeof ResizeObserver === 'undefined') return\n observer.value = new ResizeObserver(() => (viewportSize.value = el.clientHeight))\n observer.value.observe(el)\n})\nonBeforeUnmount(() => {\n observer.value?.disconnect()\n touch.clearShift(elRef.value)\n})\n\nfunction scrollTo(offset: number) {\n const el = elRef.value\n if (!el) return\n touch.clearShift(el)\n el.scrollTop = offset\n // Rendering follows right away, without waiting for the scroll event.\n scrollTop.value = el.scrollTop\n}\n\nfunction scrollToIndex(index: number, align: VirtualListAlign = 'nearest', settle = 2) {\n if (index < 0 || index >= props.items.length) return\n // Measured items land where estimated at first; once the items around\n // the target are rendered and measured, aim again.\n if (measured.value && settle > 0 && typeof requestAnimationFrame !== 'undefined') {\n requestAnimationFrame(() => scrollToIndex(index, align, settle - 1))\n }\n const { start } = offsets.value\n const top = start(index)\n const bottom = start(index + 1)\n const endPadding = elRef.value\n ? Number.parseFloat(getComputedStyle(elRef.value).scrollPaddingBlockEnd) || 0\n : 0\n const height = Math.max(0, viewport.value - endPadding)\n const current = scrollTop.value\n let target = current\n if (align === 'start') target = top\n else if (align === 'end') target = bottom - height\n else if (align === 'center') target = top - (height - (bottom - top)) / 2\n else if (top < current) target = top\n else if (bottom > current + height) target = bottom - height\n if (target !== current) scrollTo(Math.max(0, target))\n}\n\ndefineExpose<VirtualListExpose>({\n scrollToIndex: (index, align) => scrollToIndex(index, align),\n scrollTo,\n el: () => elRef.value,\n})\n</script>\n\n<template>\n <div ref=\"el\" tabindex=\"0\" :class=\"ns.b()\" :style=\"style\" @scroll.passive=\"onScroll\">\n <div v-if=\"items.length\" :class=\"ns.e('spacer')\" :style=\"spacerStyle\">\n <div\n v-if=\"spacerBefore > 0\"\n :class=\"ns.e('pad')\"\n aria-hidden=\"true\"\n :style=\"{ height: `${spacerBefore}px` }\"\n />\n <div\n v-for=\"entry in visible\"\n :key=\"entry.key\"\n :ref=\"\n measured ? (el) => measuredSizes.track(el as Element, entry.key, entry.index) : undefined\n \"\n :class=\"ns.e('item')\"\n :data-index=\"entry.index\"\n :style=\"measured ? undefined : { height: sizeOf(entry.index) }\"\n >\n <slot :item=\"entry.item\" :index=\"entry.index\" />\n </div>\n <div\n v-if=\"spacerAfter > 0\"\n :class=\"ns.e('pad')\"\n aria-hidden=\"true\"\n :style=\"{ height: `${spacerAfter}px` }\"\n />\n </div>\n <slot v-else name=\"empty\" />\n </div>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;EA6BA,MAAM,QAAQ;EAgBd,MAAM,OAAO;EAKb,MAAM,KAAK,aAAa,cAAc;EACtC,MAAM,QAAQ,eAA4B,IAAI;EAE9C,MAAM,UAAU,MAAqD,UACnE,OAAO,SAAS,aAAa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@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"virtual-list2.js","names":["_sfc_main"],"sources":["../../../src/components/virtual-list/virtual-list.vue"],"sourcesContent":["<script setup lang=\"ts\" generic=\"T\">\nimport type { HTMLAttributes } from 'vue'\nimport type { ForwardedAttributes } from '../../utils/install'\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n onUpdated,\n ref,\n shallowRef,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useMeasuredSizes } from '../../composables/use-measured-sizes'\nimport { useTouchScroll } from '../../composables/use-touch-scroll'\nimport { scrollPositionOf } from '../../utils/scroll'\nimport { useNamespace } from '../../utils/namespace'\nimport { createOffsets, virtualItemKey } from './virtual-list'\nimport type {\n VirtualListAlign,\n VirtualListEmits,\n VirtualListExpose,\n VirtualListKey,\n VirtualListProps,\n VirtualListSlots,\n} from './virtual-list'\n\ndefineOptions({ name: 'DouVirtualList' })\n\nconst props = withDefaults(\n defineProps<\n VirtualListProps<T> &\n /* @vue-ignore */ ForwardedAttributes<\n HTMLAttributes,\n VirtualListProps<T>,\n keyof VirtualListEmits & string\n >\n >(),\n {\n itemSize: undefined,\n estimateSize: 40,\n overscan: 4,\n endThreshold: 40,\n },\n)\nconst emit = defineEmits<{ scroll: [event: Event]; endReached: [] }>()\ndefineSlots<VirtualListSlots<T>>()\n\n// The list itself takes focus (tabindex 0) so the keyboard can scroll it.\n// A parent that manages focus on its own passes `:tabindex=\"undefined\"`.\nconst ns = useNamespace('virtual-list')\nconst elRef = useTemplateRef<HTMLElement>('el')\n\nconst sizeBy = (size: number | ((item: T, index: number) => number), index: number) =>\n typeof size === 'function' ? size(props.items[index]!, index) : size\n\n// Without `itemSize`, heights come from the rendered items.\nconst measured = computed(() => props.itemSize === undefined)\nconst measuredSizes = useMeasuredSizes({\n count: () => props.items.length,\n keyOf: (index) => keyOf(props.items[index]!, index),\n estimate: (index) => sizeBy(props.estimateSize, index),\n container: () => elRef.value,\n})\nconst fixedOffsets = computed(() =>\n createOffsets(props.items.length, (index) => sizeBy(props.itemSize!, index)),\n)\nconst offsets = computed(() => (measured.value ? measuredSizes.offsets.value : fixedOffsets.value))\nconst sizeOf = (index: number) =>\n measured.value ? undefined : `${sizeBy(props.itemSize!, index)}px`\nonUpdated(measuredSizes.prune)\n\nconst scrollTop = ref(0)\n// The measured viewport, or the `height` prop until it can be measured.\nconst viewportSize = ref(0)\nconst viewport = computed(\n () => viewportSize.value || (typeof props.height === 'number' ? props.height : 0),\n)\n\nconst range = computed(() => {\n const count = props.items.length\n if (!count) return { from: 0, to: 0 }\n const { find } = offsets.value\n const first = find(scrollTop.value)\n const last = find(scrollTop.value + Math.max(viewport.value, 1) - 1)\n return {\n from: Math.max(0, first - props.overscan),\n to: Math.min(count, last + 1 + props.overscan),\n }\n})\n\nconst keyOf = (item: T, index: number) => virtualItemKey(props.itemKey, item, index)\n\nconst visible = computed(() => {\n const { from, to } = range.value\n return props.items.slice(from, to).map((item, offset) => {\n const index = from + offset\n return { item, index, key: keyOf(item, index) }\n })\n})\n\nconst style = computed(() =>\n props.height === undefined\n ? undefined\n : { height: typeof props.height === 'number' ? `${props.height}px` : props.height },\n)\n\nlet reachedEnd = false\nfunction onScroll(event: Event) {\n const el = event.currentTarget as HTMLElement\n // iOS reports positions past either end while the content bounces back\n scrollTop.value =
|
|
1
|
+
{"version":3,"file":"virtual-list2.js","names":["_sfc_main"],"sources":["../../../src/components/virtual-list/virtual-list.vue"],"sourcesContent":["<script setup lang=\"ts\" generic=\"T\">\nimport type { HTMLAttributes } from 'vue'\nimport type { ForwardedAttributes } from '../../utils/install'\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n onUpdated,\n ref,\n shallowRef,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useMeasuredSizes } from '../../composables/use-measured-sizes'\nimport { useTouchScroll } from '../../composables/use-touch-scroll'\nimport { scrollPositionOf } from '../../utils/scroll'\nimport { useNamespace } from '../../utils/namespace'\nimport { createOffsets, virtualItemKey } from './virtual-list'\nimport type {\n VirtualListAlign,\n VirtualListEmits,\n VirtualListExpose,\n VirtualListKey,\n VirtualListProps,\n VirtualListSlots,\n} from './virtual-list'\n\ndefineOptions({ name: 'DouVirtualList' })\n\nconst props = withDefaults(\n defineProps<\n VirtualListProps<T> &\n /* @vue-ignore */ ForwardedAttributes<\n HTMLAttributes,\n VirtualListProps<T>,\n keyof VirtualListEmits & string\n >\n >(),\n {\n itemSize: undefined,\n estimateSize: 40,\n overscan: 4,\n endThreshold: 40,\n },\n)\nconst emit = defineEmits<{ scroll: [event: Event]; endReached: [] }>()\ndefineSlots<VirtualListSlots<T>>()\n\n// The list itself takes focus (tabindex 0) so the keyboard can scroll it.\n// A parent that manages focus on its own passes `:tabindex=\"undefined\"`.\nconst ns = useNamespace('virtual-list')\nconst elRef = useTemplateRef<HTMLElement>('el')\n\nconst sizeBy = (size: number | ((item: T, index: number) => number), index: number) =>\n typeof size === 'function' ? size(props.items[index]!, index) : size\n\nconst touch = useTouchScroll()\nconst pendingOffset = computed(() => touch.pendingShift(elRef.value))\nconst spacerStyle = computed(() =>\n pendingOffset.value ? { transform: `translateY(${-pendingOffset.value}px)` } : undefined,\n)\n\nfunction logicalTopOf(el: HTMLElement) {\n return Math.max(0, scrollPositionOf(el).top + touch.pendingShift(el))\n}\n\n// Without `itemSize`, heights come from the rendered items.\nconst measured = computed(() => props.itemSize === undefined)\nconst measuredSizes = useMeasuredSizes({\n count: () => props.items.length,\n keyOf: (index) => keyOf(props.items[index]!, index),\n estimate: (index) => sizeBy(props.estimateSize, index),\n container: () => elRef.value,\n onShift: (_shift, el) => {\n syncScroll(el)\n if (touch.active.value) touch.whenSettled(syncAfterTouch)\n },\n})\nconst fixedOffsets = computed(() =>\n createOffsets(props.items.length, (index) => sizeBy(props.itemSize!, index)),\n)\nconst offsets = computed(() => (measured.value ? measuredSizes.offsets.value : fixedOffsets.value))\nconst sizeOf = (index: number) =>\n measured.value ? undefined : `${sizeBy(props.itemSize!, index)}px`\nonUpdated(measuredSizes.prune)\n\nconst scrollTop = ref(0)\n// The measured viewport, or the `height` prop until it can be measured.\nconst viewportSize = ref(0)\nconst viewport = computed(\n () => viewportSize.value || (typeof props.height === 'number' ? props.height : 0),\n)\n\nconst range = computed(() => {\n const count = props.items.length\n if (!count) return { from: 0, to: 0 }\n const { find } = offsets.value\n const first = find(scrollTop.value)\n const last = find(scrollTop.value + Math.max(viewport.value, 1) - 1)\n return {\n from: Math.max(0, first - props.overscan),\n to: Math.min(count, last + 1 + props.overscan),\n }\n})\n\nconst keyOf = (item: T, index: number) => virtualItemKey(props.itemKey, item, index)\n\nconst visible = computed(() => {\n const { from, to } = range.value\n return props.items.slice(from, to).map((item, offset) => {\n const index = from + offset\n return { item, index, key: keyOf(item, index) }\n })\n})\nconst spacerBefore = computed(() => offsets.value.start(range.value.from))\nconst spacerAfter = computed(() =>\n Math.max(0, offsets.value.total - offsets.value.start(range.value.to)),\n)\n\nconst style = computed(() =>\n props.height === undefined\n ? undefined\n : { height: typeof props.height === 'number' ? `${props.height}px` : props.height },\n)\n\nlet reachedEnd = false\nfunction onScroll(event: Event) {\n const el = event.currentTarget as HTMLElement\n // iOS reports positions past either end while the content bounces back\n scrollTop.value = logicalTopOf(el)\n remember(el)\n emit('scroll', event)\n const atEnd =\n el.scrollTop + touch.pendingShift(el) + el.clientHeight >= el.scrollHeight - props.endThreshold\n if (atEnd && !reachedEnd && props.items.length) emit('endReached')\n reachedEnd = atEnd\n}\n// New items push the end away again.\nwatch(\n () => props.items.length,\n () => (reachedEnd = false),\n)\n\n// With measured items, items added or removed above the view keep the\n// first visible item where it was, e.g. earlier messages loaded on top,\n// also when the view is at the very top.\nlet anchor: { key: VirtualListKey; offset: number } | undefined\nfunction remember(el: HTMLElement) {\n if (!measured.value || !props.items.length) return (anchor = undefined)\n const top = logicalTopOf(el)\n const index = offsets.value.find(top)\n anchor = {\n key: keyOf(props.items[index]!, index),\n offset: offsets.value.start(index) - top,\n }\n}\nfunction syncScroll(el: HTMLElement) {\n scrollTop.value = logicalTopOf(el)\n remember(el)\n}\nfunction syncAfterTouch() {\n const el = elRef.value\n if (el) syncScroll(el)\n}\nfunction targetOf(items: T[]) {\n const held = anchor\n if (!held) return undefined\n const index = items.findIndex((item, at) => keyOf(item, at) === held.key)\n if (index < 0) return undefined\n return Math.max(0, offsets.value.start(index) - held.offset)\n}\n// Keep the rendered window on the anchored item before DOM updates so visible\n// items stay mounted and newly prepended items do not briefly render at top.\nwatch(\n () => props.items,\n (items) => {\n const target = targetOf(items)\n if (target !== undefined) scrollTop.value = target\n },\n)\nwatch(\n () => props.items,\n (items) => {\n const el = elRef.value\n const target = targetOf(items)\n if (!el || target === undefined) return\n // a move iOS drops during a touch scroll is made once it is over\n touch.shiftScroll(el, target - logicalTopOf(el))\n syncScroll(el)\n if (touch.active.value) touch.whenSettled(syncAfterTouch)\n },\n { flush: 'post' },\n)\n\nconst observer = shallowRef<ResizeObserver>()\nonMounted(() => {\n const el = elRef.value\n if (!el) return\n viewportSize.value = el.clientHeight\n if (typeof ResizeObserver === 'undefined') return\n observer.value = new ResizeObserver(() => (viewportSize.value = el.clientHeight))\n observer.value.observe(el)\n})\nonBeforeUnmount(() => {\n observer.value?.disconnect()\n touch.clearShift(elRef.value)\n})\n\nfunction scrollTo(offset: number) {\n const el = elRef.value\n if (!el) return\n touch.clearShift(el)\n el.scrollTop = offset\n // Rendering follows right away, without waiting for the scroll event.\n scrollTop.value = el.scrollTop\n}\n\nfunction scrollToIndex(index: number, align: VirtualListAlign = 'nearest', settle = 2) {\n if (index < 0 || index >= props.items.length) return\n // Measured items land where estimated at first; once the items around\n // the target are rendered and measured, aim again.\n if (measured.value && settle > 0 && typeof requestAnimationFrame !== 'undefined') {\n requestAnimationFrame(() => scrollToIndex(index, align, settle - 1))\n }\n const { start } = offsets.value\n const top = start(index)\n const bottom = start(index + 1)\n const endPadding = elRef.value\n ? Number.parseFloat(getComputedStyle(elRef.value).scrollPaddingBlockEnd) || 0\n : 0\n const height = Math.max(0, viewport.value - endPadding)\n const current = scrollTop.value\n let target = current\n if (align === 'start') target = top\n else if (align === 'end') target = bottom - height\n else if (align === 'center') target = top - (height - (bottom - top)) / 2\n else if (top < current) target = top\n else if (bottom > current + height) target = bottom - height\n if (target !== current) scrollTo(Math.max(0, target))\n}\n\ndefineExpose<VirtualListExpose>({\n scrollToIndex: (index, align) => scrollToIndex(index, align),\n scrollTo,\n el: () => elRef.value,\n})\n</script>\n\n<template>\n <div ref=\"el\" tabindex=\"0\" :class=\"ns.b()\" :style=\"style\" @scroll.passive=\"onScroll\">\n <div v-if=\"items.length\" :class=\"ns.e('spacer')\" :style=\"spacerStyle\">\n <div\n v-if=\"spacerBefore > 0\"\n :class=\"ns.e('pad')\"\n aria-hidden=\"true\"\n :style=\"{ height: `${spacerBefore}px` }\"\n />\n <div\n v-for=\"entry in visible\"\n :key=\"entry.key\"\n :ref=\"\n measured ? (el) => measuredSizes.track(el as Element, entry.key, entry.index) : undefined\n \"\n :class=\"ns.e('item')\"\n :data-index=\"entry.index\"\n :style=\"measured ? undefined : { height: sizeOf(entry.index) }\"\n >\n <slot :item=\"entry.item\" :index=\"entry.index\" />\n </div>\n <div\n v-if=\"spacerAfter > 0\"\n :class=\"ns.e('pad')\"\n aria-hidden=\"true\"\n :style=\"{ height: `${spacerAfter}px` }\"\n />\n </div>\n <slot v-else name=\"empty\" />\n </div>\n</template>\n"],"mappings":""}
|
|
@@ -52,13 +52,29 @@ function computePosition(reference, size, placement, { offset, padding, flip, vi
|
|
|
52
52
|
y = clamp(y, padding, viewport.height - padding - height);
|
|
53
53
|
x = clamp(x, padding, Math.max(padding, viewport.width - padding - width));
|
|
54
54
|
}
|
|
55
|
+
const maxAvailable = viewport.height - padding * 2;
|
|
56
|
+
const availableHeight = side === "top" || side === "bottom" ? Math.min(room(side), maxAvailable) : maxAvailable;
|
|
55
57
|
return {
|
|
56
58
|
x: Math.round(x),
|
|
57
59
|
y: Math.round(y),
|
|
58
60
|
placement: align ? `${side}-${align}` : side,
|
|
59
|
-
available: Math.max(0, Math.floor(
|
|
61
|
+
available: Math.max(0, Math.floor(availableHeight))
|
|
60
62
|
};
|
|
61
63
|
}
|
|
64
|
+
function collectScrollOffsets(root) {
|
|
65
|
+
const saved = [];
|
|
66
|
+
if (root.scrollTop || root.scrollLeft) saved.push([
|
|
67
|
+
root,
|
|
68
|
+
root.scrollTop,
|
|
69
|
+
root.scrollLeft
|
|
70
|
+
]);
|
|
71
|
+
for (const el of root.querySelectorAll("*")) if (el.scrollTop || el.scrollLeft) saved.push([
|
|
72
|
+
el,
|
|
73
|
+
el.scrollTop,
|
|
74
|
+
el.scrollLeft
|
|
75
|
+
]);
|
|
76
|
+
return saved;
|
|
77
|
+
}
|
|
62
78
|
/**
|
|
63
79
|
* Keep `floating` next to `reference` while open: positioned `fixed`,
|
|
64
80
|
* recomputed on scroll, resize and when either element changes size.
|
|
@@ -69,12 +85,18 @@ function useFloating(reference, floating, options) {
|
|
|
69
85
|
const anchor = reference();
|
|
70
86
|
const target = floating.value;
|
|
71
87
|
if (!anchor || !target) return;
|
|
88
|
+
if (target.style.display === "none") {
|
|
89
|
+
if (toValue(options.open)) schedule();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
72
92
|
const box = anchor.getBoundingClientRect();
|
|
73
93
|
if (toValue(options.sameWidth)) target.style.minWidth = `${box.width}px`;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
94
|
+
const previousAvailable = target.style.getPropertyValue("--dou-floating-available");
|
|
95
|
+
const savedScroll = previousAvailable ? collectScrollOffsets(target) : null;
|
|
96
|
+
if (previousAvailable) target.style.removeProperty("--dou-floating-available");
|
|
97
|
+
const naturalWidth = target.offsetWidth;
|
|
98
|
+
const naturalHeight = target.offsetHeight;
|
|
99
|
+
const floatingOptions = {
|
|
78
100
|
offset: toValue(options.offset) ?? 8,
|
|
79
101
|
padding: 8,
|
|
80
102
|
flip: true,
|
|
@@ -82,7 +104,28 @@ function useFloating(reference, floating, options) {
|
|
|
82
104
|
width: document.documentElement.clientWidth,
|
|
83
105
|
height: document.documentElement.clientHeight
|
|
84
106
|
}
|
|
85
|
-
}
|
|
107
|
+
};
|
|
108
|
+
const desired = computePosition(box, {
|
|
109
|
+
width: naturalWidth,
|
|
110
|
+
height: naturalHeight
|
|
111
|
+
}, toValue(options.placement), floatingOptions);
|
|
112
|
+
target.style.setProperty("--dou-floating-available", `${desired.available}px`);
|
|
113
|
+
const actualWidth = target.offsetWidth;
|
|
114
|
+
const actualHeight = target.offsetHeight;
|
|
115
|
+
if (savedScroll?.length) for (const [el, top, left] of savedScroll) {
|
|
116
|
+
if (el.scrollTop !== top) el.scrollTop = top;
|
|
117
|
+
if (el.scrollLeft !== left) el.scrollLeft = left;
|
|
118
|
+
}
|
|
119
|
+
position.value = actualWidth === naturalWidth && actualHeight === naturalHeight ? desired : {
|
|
120
|
+
...computePosition(box, {
|
|
121
|
+
width: actualWidth,
|
|
122
|
+
height: actualHeight
|
|
123
|
+
}, desired.placement, {
|
|
124
|
+
...floatingOptions,
|
|
125
|
+
flip: false
|
|
126
|
+
}),
|
|
127
|
+
available: desired.available
|
|
128
|
+
};
|
|
86
129
|
}
|
|
87
130
|
let frame = 0;
|
|
88
131
|
const schedule = () => {
|
|
@@ -92,9 +135,18 @@ function useFloating(reference, floating, options) {
|
|
|
92
135
|
update();
|
|
93
136
|
});
|
|
94
137
|
};
|
|
138
|
+
const onScroll = (event) => {
|
|
139
|
+
const panel = floating.value;
|
|
140
|
+
const scrollTarget = event.target;
|
|
141
|
+
if (panel && scrollTarget instanceof Node && panel.contains(scrollTarget)) {
|
|
142
|
+
const anchor = reference();
|
|
143
|
+
if (!(anchor instanceof Node) || !scrollTarget.contains(anchor)) return;
|
|
144
|
+
}
|
|
145
|
+
schedule();
|
|
146
|
+
};
|
|
95
147
|
let observer;
|
|
96
148
|
function start() {
|
|
97
|
-
window.addEventListener("scroll",
|
|
149
|
+
window.addEventListener("scroll", onScroll, {
|
|
98
150
|
capture: true,
|
|
99
151
|
passive: true
|
|
100
152
|
});
|
|
@@ -107,7 +159,7 @@ function useFloating(reference, floating, options) {
|
|
|
107
159
|
}
|
|
108
160
|
}
|
|
109
161
|
function stop() {
|
|
110
|
-
window.removeEventListener("scroll",
|
|
162
|
+
window.removeEventListener("scroll", onScroll, { capture: true });
|
|
111
163
|
window.removeEventListener("resize", schedule);
|
|
112
164
|
observer?.disconnect();
|
|
113
165
|
observer = void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-floating.js","names":[],"sources":["../../src/composables/use-floating.ts"],"sourcesContent":["import { computed, onScopeDispose, ref, toValue, watch } from 'vue'\nimport type { CSSProperties, MaybeRefOrGetter, Ref } from 'vue'\n\nexport type Side = 'top' | 'right' | 'bottom' | 'left'\nexport type Placement = Side | `${Side}-start` | `${Side}-end`\n\ninterface Box {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport interface PositionOptions {\n /** The gap between the reference and the floating element. */\n offset: number\n /** The least distance kept from the edges of the viewport. */\n padding: number\n /** Move to the opposite side when the preferred one has no room. */\n flip: boolean\n viewport: { width: number; height: number }\n}\n\nexport interface Position {\n x: number\n y: number\n placement: Placement\n /** The room left on the chosen side, for capping the height of long content. */\n available: number\n}\n\nconst clamp = (value: number, min: number, max: number) =>\n Math.max(min, Math.min(value, Math.max(min, max)))\n\nconst opposite: Record<Side, Side> = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' }\n\n/**\n * Place a box of `size` next to `reference`, all in viewport coordinates.\n *\n * The box sits on the preferred side, flips to the opposite one when it\n * would overflow there and the opposite has more room, then slides along\n * the other axis to stay inside the viewport. A box beside the reference\n * that fits on neither side also slides sideways, over the reference.\n */\nexport function computePosition(\n reference: Box,\n size: { width: number; height: number },\n placement: Placement,\n { offset, padding, flip, viewport }: PositionOptions,\n): Position {\n const [preferred, align] = placement.split('-') as [Side, 'start' | 'end' | undefined]\n const { width, height } = size\n\n const place = (side: Side) => {\n const vertical = side === 'top' || side === 'bottom'\n const cross = vertical\n ? align === 'start'\n ? reference.x\n : align === 'end'\n ? reference.x + reference.width - width\n : reference.x + (reference.width - width) / 2\n : align === 'start'\n ? reference.y\n : align === 'end'\n ? reference.y + reference.height - height\n : reference.y + (reference.height - height) / 2\n if (side === 'top') return { x: cross, y: reference.y - height - offset }\n if (side === 'bottom') return { x: cross, y: reference.y + reference.height + offset }\n if (side === 'left') return { x: reference.x - width - offset, y: cross }\n return { x: reference.x + reference.width + offset, y: cross }\n }\n const room = (side: Side) => {\n if (side === 'top') return reference.y - offset - padding\n if (side === 'bottom')\n return viewport.height - reference.y - reference.height - offset - padding\n if (side === 'left') return reference.x - offset - padding\n return viewport.width - reference.x - reference.width - offset - padding\n }\n const needed = (side: Side) => (side === 'top' || side === 'bottom' ? height : width)\n\n let side = preferred\n if (flip && room(side) < needed(side) && room(opposite[side]) > room(side)) side = opposite[side]\n\n let { x, y } = place(side)\n if (side === 'top' || side === 'bottom') x = clamp(x, padding, viewport.width - padding - width)\n else {\n y = clamp(y, padding, viewport.height - padding - height)\n // too wide for either side (a submenu on a phone): it covers part of the\n // reference instead of running off the screen\n x = clamp(x, padding, Math.max(padding, viewport.width - padding - width))\n }\n\n return {\n x: Math.round(x),\n y: Math.round(y),\n placement: (align ? `${side}-${align}` : side) as Placement,\n available: Math.max(0, Math.floor(room(side))),\n }\n}\n\n/** Anything with a box to sit next to, e.g. a point where the pointer was pressed. */\nexport interface VirtualElement {\n getBoundingClientRect: () => DOMRect\n}\n\nexport interface UseFloatingOptions {\n open: MaybeRefOrGetter<boolean>\n placement: MaybeRefOrGetter<Placement>\n offset?: MaybeRefOrGetter<number>\n /** Make the floating element at least as wide as the reference. */\n sameWidth?: MaybeRefOrGetter<boolean>\n}\n\n/**\n * Keep `floating` next to `reference` while open: positioned `fixed`,\n * recomputed on scroll, resize and when either element changes size.\n */\nexport function useFloating(\n reference: () => Element | VirtualElement | null | undefined,\n floating: Ref<HTMLElement | null>,\n options: UseFloatingOptions,\n) {\n const position = ref<Position | null>(null)\n\n function update() {\n const anchor = reference()\n const target = floating.value\n if (!anchor || !target) return\n const box = anchor.getBoundingClientRect()\n if (toValue(options.sameWidth)) target.style.minWidth = `${box.width}px`\n position.value = computePosition(\n box,\n { width: target.offsetWidth, height: target.offsetHeight },\n toValue(options.placement),\n {\n offset: toValue(options.offset) ?? 8,\n padding: 8,\n flip: true,\n viewport: {\n width: document.documentElement.clientWidth,\n height: document.documentElement.clientHeight,\n },\n },\n )\n }\n\n let frame = 0\n const schedule = () => {\n if (frame) return\n frame = requestAnimationFrame(() => {\n frame = 0\n update()\n })\n }\n\n let observer: ResizeObserver | undefined\n function start() {\n window.addEventListener('scroll', schedule, { capture: true, passive: true })\n window.addEventListener('resize', schedule, { passive: true })\n if (typeof ResizeObserver !== 'undefined') {\n // ResizeObserver already runs after layout and before paint. Deferring it\n // to rAF paints the resized panel once at the previous size's position.\n observer = new ResizeObserver(update)\n const anchor = reference()\n if (anchor instanceof Element) observer.observe(anchor)\n if (floating.value) observer.observe(floating.value)\n }\n }\n function stop() {\n window.removeEventListener('scroll', schedule, { capture: true })\n window.removeEventListener('resize', schedule)\n observer?.disconnect()\n observer = undefined\n if (frame) cancelAnimationFrame(frame)\n frame = 0\n }\n\n watch(\n [() => toValue(options.open), floating],\n ([open, target]) => {\n stop()\n if (!open || !target || typeof window === 'undefined') return\n update()\n start()\n },\n { flush: 'post' },\n )\n watch([() => toValue(options.placement), () => toValue(options.offset)], () => update(), {\n flush: 'post',\n })\n onScopeDispose(stop)\n\n const style = computed<CSSProperties>(() => {\n const { x = 0, y = 0, available } = position.value ?? {}\n return {\n position: 'fixed',\n top: `${y}px`,\n left: `${x}px`,\n '--dou-floating-available': available === undefined ? undefined : `${available}px`,\n } as CSSProperties\n })\n const placement = computed(() => position.value?.placement ?? toValue(options.placement))\n\n return { style, placement, update }\n}\n"],"mappings":";;AA+BA,IAAM,SAAS,OAAe,KAAa,QACzC,KAAK,IAAI,KAA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iBAAiB,UAAU,UAAU;GAAE,SAAS;GAAM,SAAS;EAAK,CAAC;EAC5E,OAAO,iBAAiB,UAAU,UAAU,EAAE,SAAS,KAAK,CAAC;EAC7D,IAAI,OAAO,mBAAmB,aAAa;GAGzC,WAAW,IAAI,eAAe,MAAM;GACpC,MAAM,SAAS,UAAU;GACzB,IAAI,kBAAkB,SAAS,SAAS,QAAQ,MAAM;GACtD,IAAI,SAAS,OAAO,SAAS,QAAQ,SAAS,KAAK;EACrD;CACF;CACA,SAAS,OAAO;EACd,OAAO,oBAAoB,UAAU,UAAU,EAAE,SAAS,KAAK,CAAC;EAChE,OAAO,oBAAoB,UAAU,QAAQ;EAC7C,UAAU,WAAW;EACrB,WAAW,KAAA;EACX,IAAI,OAAO,qBAAqB,KAAK;EACrC,QAAQ;CACV;CAEA,MACE,OAAO,QAAQ,QAAQ,IAAI,GAAG,QAAQ,IACrC,CAAC,MAAM,YAAY;EAClB,KAAK;EACL,IAAI,CAAC,QAAQ,CAAC,UAAU,OAAO,WAAW,aAAa;EACvD,OAAO;EACP,MAAM;CACR,GACA,EAAE,OAAO,OAAO,CAClB;CACA,MAAM,OAAO,QAAQ,QAAQ,SAAS,SAAS,QAAQ,QAAQ,MAAM,CAAC,SAAS,OAAO,GAAG,EACvF,OAAO,OACT,CAAC;CACD,eAAe,IAAI;CAanB,OAAO;EAAE,OAXK,eAA8B;GAC1C,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,cAAc,SAAS,SAAS,CAAC;GACvD,OAAO;IACL,UAAU;IACV,KAAK,GAAG,EAAE;IACV,MAAM,GAAG,EAAE;IACX,4BAA4B,cAAc,KAAA,IAAY,KAAA,IAAY,GAAG,UAAU;GACjF;EACF,CAGS;EAAO,WAFE,eAAe,SAAS,OAAO,aAAa,QAAQ,QAAQ,SAAS,CAEvE;EAAW;CAAO;AACpC"}
|
|
1
|
+
{"version":3,"file":"use-floating.js","names":[],"sources":["../../src/composables/use-floating.ts"],"sourcesContent":["import { computed, onScopeDispose, ref, toValue, watch } from 'vue'\nimport type { CSSProperties, MaybeRefOrGetter, Ref } from 'vue'\n\nexport type Side = 'top' | 'right' | 'bottom' | 'left'\nexport type Placement = Side | `${Side}-start` | `${Side}-end`\n\ninterface Box {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport interface PositionOptions {\n /** The gap between the reference and the floating element. */\n offset: number\n /** The least distance kept from the edges of the viewport. */\n padding: number\n /** Move to the opposite side when the preferred one has no room. */\n flip: boolean\n viewport: { width: number; height: number }\n}\n\nexport interface Position {\n x: number\n y: number\n placement: Placement\n /** The vertical room left for the panel, for capping the height of long content. */\n available: number\n}\n\nconst clamp = (value: number, min: number, max: number) =>\n Math.max(min, Math.min(value, Math.max(min, max)))\n\nconst opposite: Record<Side, Side> = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' }\n\n/**\n * Place a box of `size` next to `reference`, all in viewport coordinates.\n *\n * The box sits on the preferred side, flips to the opposite one when it\n * would overflow there and the opposite has more room, then slides along\n * the other axis to stay inside the viewport. A box beside the reference\n * that fits on neither side also slides sideways, over the reference.\n */\nexport function computePosition(\n reference: Box,\n size: { width: number; height: number },\n placement: Placement,\n { offset, padding, flip, viewport }: PositionOptions,\n): Position {\n const [preferred, align] = placement.split('-') as [Side, 'start' | 'end' | undefined]\n const { width, height } = size\n\n const place = (side: Side) => {\n const vertical = side === 'top' || side === 'bottom'\n const cross = vertical\n ? align === 'start'\n ? reference.x\n : align === 'end'\n ? reference.x + reference.width - width\n : reference.x + (reference.width - width) / 2\n : align === 'start'\n ? reference.y\n : align === 'end'\n ? reference.y + reference.height - height\n : reference.y + (reference.height - height) / 2\n if (side === 'top') return { x: cross, y: reference.y - height - offset }\n if (side === 'bottom') return { x: cross, y: reference.y + reference.height + offset }\n if (side === 'left') return { x: reference.x - width - offset, y: cross }\n return { x: reference.x + reference.width + offset, y: cross }\n }\n const room = (side: Side) => {\n if (side === 'top') return reference.y - offset - padding\n if (side === 'bottom')\n return viewport.height - reference.y - reference.height - offset - padding\n if (side === 'left') return reference.x - offset - padding\n return viewport.width - reference.x - reference.width - offset - padding\n }\n const needed = (side: Side) => (side === 'top' || side === 'bottom' ? height : width)\n\n let side = preferred\n if (flip && room(side) < needed(side) && room(opposite[side]) > room(side)) side = opposite[side]\n\n let { x, y } = place(side)\n if (side === 'top' || side === 'bottom') x = clamp(x, padding, viewport.width - padding - width)\n else {\n y = clamp(y, padding, viewport.height - padding - height)\n // too wide for either side (a submenu on a phone): it covers part of the\n // reference instead of running off the screen\n x = clamp(x, padding, Math.max(padding, viewport.width - padding - width))\n }\n\n const maxAvailable = viewport.height - padding * 2\n const availableHeight =\n side === 'top' || side === 'bottom' ? Math.min(room(side), maxAvailable) : maxAvailable\n\n return {\n x: Math.round(x),\n y: Math.round(y),\n placement: (align ? `${side}-${align}` : side) as Placement,\n available: Math.max(0, Math.floor(availableHeight)),\n }\n}\n\n/** Anything with a box to sit next to, e.g. a point where the pointer was pressed. */\nexport interface VirtualElement {\n getBoundingClientRect: () => DOMRect\n}\n\nexport interface UseFloatingOptions {\n open: MaybeRefOrGetter<boolean>\n placement: MaybeRefOrGetter<Placement>\n offset?: MaybeRefOrGetter<number>\n /** Make the floating element at least as wide as the reference. */\n sameWidth?: MaybeRefOrGetter<boolean>\n}\n\nfunction collectScrollOffsets(root: HTMLElement): Array<[Element, number, number]> {\n const saved: Array<[Element, number, number]> = []\n if (root.scrollTop || root.scrollLeft) saved.push([root, root.scrollTop, root.scrollLeft])\n for (const el of root.querySelectorAll('*')) {\n if (el.scrollTop || el.scrollLeft) saved.push([el, el.scrollTop, el.scrollLeft])\n }\n return saved\n}\n\n/**\n * Keep `floating` next to `reference` while open: positioned `fixed`,\n * recomputed on scroll, resize and when either element changes size.\n */\nexport function useFloating(\n reference: () => Element | VirtualElement | null | undefined,\n floating: Ref<HTMLElement | null>,\n options: UseFloatingOptions,\n) {\n const position = ref<Position | null>(null)\n\n function update() {\n const anchor = reference()\n const target = floating.value\n if (!anchor || !target) return\n // Wait until `v-show` / `<Transition>` reveals a re-opened panel: measuring\n // while `display: none` sees 0×0 and would lock `--dou-floating-available`\n // to an unflipped side before the panel is shown.\n if (target.style.display === 'none') {\n if (toValue(options.open)) schedule()\n return\n }\n const box = anchor.getBoundingClientRect()\n if (toValue(options.sameWidth)) target.style.minWidth = `${box.width}px`\n // Measure natural size without our own height cap so flip decisions see the\n // panel's real needed height even when previously clamped to a tighter side.\n // Removing the cap can temporarily expand scrollable descendants and clamp\n // their scroll offset to 0, so save and restore any active scroll positions.\n const previousAvailable = target.style.getPropertyValue('--dou-floating-available')\n const savedScroll = previousAvailable ? collectScrollOffsets(target) : null\n if (previousAvailable) target.style.removeProperty('--dou-floating-available')\n const naturalWidth = target.offsetWidth\n const naturalHeight = target.offsetHeight\n const floatingOptions: PositionOptions = {\n offset: toValue(options.offset) ?? 8,\n padding: 8,\n flip: true,\n viewport: {\n width: document.documentElement.clientWidth,\n height: document.documentElement.clientHeight,\n },\n }\n const desired = computePosition(\n box,\n { width: naturalWidth, height: naturalHeight },\n toValue(options.placement),\n floatingOptions,\n )\n target.style.setProperty('--dou-floating-available', `${desired.available}px`)\n const actualWidth = target.offsetWidth\n const actualHeight = target.offsetHeight\n if (savedScroll?.length) {\n for (const [el, top, left] of savedScroll) {\n if (el.scrollTop !== top) el.scrollTop = top\n if (el.scrollLeft !== left) el.scrollLeft = left\n }\n }\n position.value =\n actualWidth === naturalWidth && actualHeight === naturalHeight\n ? desired\n : {\n ...computePosition(\n box,\n { width: actualWidth, height: actualHeight },\n desired.placement,\n { ...floatingOptions, flip: false },\n ),\n available: desired.available,\n }\n }\n\n let frame = 0\n const schedule = () => {\n if (frame) return\n frame = requestAnimationFrame(() => {\n frame = 0\n update()\n })\n }\n const onScroll = (event: Event) => {\n const panel = floating.value\n const scrollTarget = event.target\n if (panel && scrollTarget instanceof Node && panel.contains(scrollTarget)) {\n const anchor = reference()\n if (!(anchor instanceof Node) || !scrollTarget.contains(anchor)) return\n }\n schedule()\n }\n\n let observer: ResizeObserver | undefined\n function start() {\n window.addEventListener('scroll', onScroll, { capture: true, passive: true })\n window.addEventListener('resize', schedule, { passive: true })\n if (typeof ResizeObserver !== 'undefined') {\n // ResizeObserver already runs after layout and before paint. Deferring it\n // to rAF paints the resized panel once at the previous size's position.\n observer = new ResizeObserver(update)\n const anchor = reference()\n if (anchor instanceof Element) observer.observe(anchor)\n if (floating.value) observer.observe(floating.value)\n }\n }\n function stop() {\n window.removeEventListener('scroll', onScroll, { capture: true })\n window.removeEventListener('resize', schedule)\n observer?.disconnect()\n observer = undefined\n if (frame) cancelAnimationFrame(frame)\n frame = 0\n }\n\n watch(\n [() => toValue(options.open), floating],\n ([open, target]) => {\n stop()\n if (!open || !target || typeof window === 'undefined') return\n update()\n start()\n },\n { flush: 'post' },\n )\n watch([() => toValue(options.placement), () => toValue(options.offset)], () => update(), {\n flush: 'post',\n })\n onScopeDispose(stop)\n\n const style = computed<CSSProperties>(() => {\n const { x = 0, y = 0, available } = position.value ?? {}\n return {\n position: 'fixed',\n top: `${y}px`,\n left: `${x}px`,\n '--dou-floating-available': available === undefined ? undefined : `${available}px`,\n } as CSSProperties\n })\n const placement = computed(() => position.value?.placement ?? toValue(options.placement))\n\n return { style, placement, update }\n}\n"],"mappings":";;AA+BA,IAAM,SAAS,OAAe,KAAa,QACzC,KAAK,IAAI,KAAK,KAAK,IAAI,OAAO,KAAK,IAAI,KAAK,GAAG,CAAC,CAAC;AAEnD,IAAM,WAA+B;CAAE,KAAK;CAAU,QAAQ;CAAO,MAAM;CAAS,OAAO;AAAO;;;;;;;;;AAUlG,SAAgB,gBACd,WACA,MACA,WACA,EAAE,QAAQ,SAAS,MAAM,YACf;CACV,MAAM,CAAC,WAAW,SAAS,UAAU,MAAM,GAAG;CAC9C,MAAM,EAAE,OAAO,WAAW;CAE1B,MAAM,SAAS,SAAe;EAE5B,MAAM,QADW,SAAS,SAAS,SAAS,WAExC,UAAU,UACR,UAAU,IACV,UAAU,QACR,UAAU,IA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S,SAAS,QAAQ,MAAM;GACtD,IAAI,SAAS,OAAO,SAAS,QAAQ,SAAS,KAAK;EACrD;CACF;CACA,SAAS,OAAO;EACd,OAAO,oBAAoB,UAAU,UAAU,EAAE,SAAS,KAAK,CAAC;EAChE,OAAO,oBAAoB,UAAU,QAAQ;EAC7C,UAAU,WAAW;EACrB,WAAW,KAAA;EACX,IAAI,OAAO,qBAAqB,KAAK;EACrC,QAAQ;CACV;CAEA,MACE,OAAO,QAAQ,QAAQ,IAAI,GAAG,QAAQ,IACrC,CAAC,MAAM,YAAY;EAClB,KAAK;EACL,IAAI,CAAC,QAAQ,CAAC,UAAU,OAAO,WAAW,aAAa;EACvD,OAAO;EACP,MAAM;CACR,GACA,EAAE,OAAO,OAAO,CAClB;CACA,MAAM,OAAO,QAAQ,QAAQ,SAAS,SAAS,QAAQ,QAAQ,MAAM,CAAC,SAAS,OAAO,GAAG,EACvF,OAAO,OACT,CAAC;CACD,eAAe,IAAI;CAanB,OAAO;EAAE,OAXK,eAA8B;GAC1C,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,cAAc,SAAS,SAAS,CAAC;GACvD,OAAO;IACL,UAAU;IACV,KAAK,GAAG,EAAE;IACV,MAAM,GAAG,EAAE;IACX,4BAA4B,cAAc,KAAA,IAAY,KAAA,IAAY,GAAG,UAAU;GACjF;EACF,CAGS;EAAO,WAFE,eAAe,SAAS,OAAO,aAAa,QAAQ,QAAQ,SAAS,CAEvE;EAAW;CAAO;AACpC"}
|
|
@@ -33,10 +33,11 @@ function useMeasuredSizes(options) {
|
|
|
33
33
|
let shift = 0;
|
|
34
34
|
let changed = false;
|
|
35
35
|
const el = options.container();
|
|
36
|
-
const top = el ? scrollPositionOf(el).top : 0;
|
|
36
|
+
const top = el ? Math.max(0, scrollPositionOf(el).top + touch.pendingShift(el)) : 0;
|
|
37
|
+
const count = options.count();
|
|
37
38
|
const seen = /* @__PURE__ */ new Set();
|
|
38
39
|
for (const { key, index } of items) {
|
|
39
|
-
if (seen.has(key)) continue;
|
|
40
|
+
if (index < 0 || index >= count || seen.has(key)) continue;
|
|
40
41
|
seen.add(key);
|
|
41
42
|
const size = heightOf(key);
|
|
42
43
|
const before = sizeOf(index);
|
|
@@ -47,7 +48,10 @@ function useMeasuredSizes(options) {
|
|
|
47
48
|
}
|
|
48
49
|
if (!changed) return;
|
|
49
50
|
version.value++;
|
|
50
|
-
if (el && shift)
|
|
51
|
+
if (el && shift) {
|
|
52
|
+
touch.shiftScroll(el, shift);
|
|
53
|
+
options.onShift?.(shift, el);
|
|
54
|
+
}
|
|
51
55
|
}
|
|
52
56
|
const observer = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver((entries) => update(entries.flatMap((entry) => tracked.get(entry.target) ?? [])));
|
|
53
57
|
/** Watch an item's element; call it from a function ref on every render. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-measured-sizes.js","names":[],"sources":["../../src/composables/use-measured-sizes.ts"],"sourcesContent":["import { computed, onBeforeUnmount, shallowRef } from 'vue'\nimport { createOffsets } from '../components/virtual-list/virtual-list'\nimport { scrollPositionOf } from '../utils/scroll'\nimport { useTouchScroll } from './use-touch-scroll'\n\nexport interface UseMeasuredSizesOptions<K> {\n /** How many items there are. */\n count: () => number\n /** A key that follows an item as the list changes. */\n keyOf: (index: number) => K\n /** The height to assume for an item not measured yet. */\n estimate: (index: number) => number\n /** The element that scrolls, kept steady while items above the view change height. */\n container: () => HTMLElement | null | undefined\n /**\n * Where an item starts in the scrolled content, when items don't simply\n * follow each other from the top, e.g. under group labels. Defaults to\n * the sum of the sizes before it.\n */\n start?: (index: number) => number\n}\n\n/**\n * Item offsets for a list whose items are measured once rendered.\n *\n * Unmeasured items count at their estimate. Rendered items are watched for\n * size changes; when one above the top of the view changes, the scroll\n * position moves by the same amount, so what the reader sees stays put;\n * a move iOS drops during a touch scroll is made once the scroll is over.\n */\nexport function useMeasuredSizes<K>(options: UseMeasuredSizesOptions<K>) {\n const known = new Map<K, number>()\n const touch = useTouchScroll()\n // Bumped when a measurement changes; reading it ties offsets to it.\n const version = shallowRef(0)\n\n const sizeOf = (index: number) => {\n void version.value\n return known.get(options.keyOf(index)) ?? options.estimate(index)\n }\n const offsets = computed(() => createOffsets(options.count(), sizeOf))\n const startOf = (index: number) => options.start?.(index) ?? offsets.value.start(index)\n\n // An item can span several elements, e.g. a table row and its detail row.\n const tracked = new Map<Element, { key: K; index: number }>()\n const byKey = new Map<K, Set<Element>>()\n const heightOf = (key: K) => {\n let size = 0\n for (const element of byKey.get(key) ?? []) size += (element as HTMLElement).offsetHeight\n return size\n }\n\n /** Record new heights for `keys`, keeping the view steady above the top. */\n function update(items: Iterable<{ key: K; index: number }>) {\n let shift = 0\n let changed = false\n const el = options.container()\n const top = el ? scrollPositionOf(el).top : 0\n const seen = new Set<K>()\n for (const { key, index } of items) {\n if (seen.has(key)) continue\n seen.add(key)\n const size = heightOf(key)\n const before = sizeOf(index)\n if (size === before && known.has(key)) continue\n known.set(key, size)\n changed = true\n if (startOf(index) < top) shift += size - before\n }\n if (!changed) return\n version.value++\n if (el && shift) touch.shiftScroll(el, shift)\n }\n\n const observer =\n typeof ResizeObserver === 'undefined'\n ? undefined\n : new ResizeObserver((entries) =>\n update(entries.flatMap((entry) => tracked.get(entry.target) ?? [])),\n )\n\n /** Watch an item's element; call it from a function ref on every render. */\n function track(element: Element | null, key: K, index: number) {\n if (!element) return\n const item = tracked.get(element)\n if (item) {\n if (item.key !== key) {\n byKey.get(item.key)?.delete(element)\n item.key = key\n add(element, key)\n }\n item.index = index\n return\n }\n tracked.set(element, { key, index })\n add(element, key)\n observer?.observe(element)\n }\n function add(element: Element, key: K) {\n let set = byKey.get(key)\n if (!set) byKey.set(key, (set = new Set()))\n set.add(element)\n }\n /** Stop watching elements that left the page, and re-measure what they belonged to. */\n function prune() {\n const touched: { key: K; index: number }[] = []\n for (const [element, item] of tracked) {\n if (element.isConnected) continue\n observer?.unobserve(element)\n tracked.delete(element)\n const set = byKey.get(item.key)\n set?.delete(element)\n // Only re-measure an item still on the page, e.g. a row whose detail closed.\n if (set?.size) touched.push(item)\n else byKey.delete(item.key)\n }\n if (touched.length) update(touched)\n }\n onBeforeUnmount(() => observer?.disconnect())\n\n return { offsets, sizeOf, track, prune }\n}\n"],"mappings":";;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"use-measured-sizes.js","names":[],"sources":["../../src/composables/use-measured-sizes.ts"],"sourcesContent":["import { computed, onBeforeUnmount, shallowRef } from 'vue'\nimport { createOffsets } from '../components/virtual-list/virtual-list'\nimport { scrollPositionOf } from '../utils/scroll'\nimport { useTouchScroll } from './use-touch-scroll'\n\nexport interface UseMeasuredSizesOptions<K> {\n /** How many items there are. */\n count: () => number\n /** A key that follows an item as the list changes. */\n keyOf: (index: number) => K\n /** The height to assume for an item not measured yet. */\n estimate: (index: number) => number\n /** The element that scrolls, kept steady while items above the view change height. */\n container: () => HTMLElement | null | undefined\n /**\n * Where an item starts in the scrolled content, when items don't simply\n * follow each other from the top, e.g. under group labels. Defaults to\n * the sum of the sizes before it.\n */\n start?: (index: number) => number\n /** Called right after a height change above the view shifts the scroll position. */\n onShift?: (shift: number, el: HTMLElement) => void\n}\n\n/**\n * Item offsets for a list whose items are measured once rendered.\n *\n * Unmeasured items count at their estimate. Rendered items are watched for\n * size changes; when one above the top of the view changes, the scroll\n * position moves by the same amount, so what the reader sees stays put;\n * a move iOS drops during a touch scroll is made once the scroll is over.\n */\nexport function useMeasuredSizes<K>(options: UseMeasuredSizesOptions<K>) {\n const known = new Map<K, number>()\n const touch = useTouchScroll()\n // Bumped when a measurement changes; reading it ties offsets to it.\n const version = shallowRef(0)\n\n const sizeOf = (index: number) => {\n void version.value\n return known.get(options.keyOf(index)) ?? options.estimate(index)\n }\n const offsets = computed(() => createOffsets(options.count(), sizeOf))\n const startOf = (index: number) => options.start?.(index) ?? offsets.value.start(index)\n\n // An item can span several elements, e.g. a table row and its detail row.\n const tracked = new Map<Element, { key: K; index: number }>()\n const byKey = new Map<K, Set<Element>>()\n const heightOf = (key: K) => {\n let size = 0\n for (const element of byKey.get(key) ?? []) size += (element as HTMLElement).offsetHeight\n return size\n }\n\n /** Record new heights for `keys`, keeping the view steady above the top. */\n function update(items: Iterable<{ key: K; index: number }>) {\n let shift = 0\n let changed = false\n const el = options.container()\n const top = el ? Math.max(0, scrollPositionOf(el).top + touch.pendingShift(el)) : 0\n const count = options.count()\n const seen = new Set<K>()\n for (const { key, index } of items) {\n if (index < 0 || index >= count || seen.has(key)) continue\n seen.add(key)\n const size = heightOf(key)\n const before = sizeOf(index)\n if (size === before && known.has(key)) continue\n known.set(key, size)\n changed = true\n if (startOf(index) < top) shift += size - before\n }\n if (!changed) return\n version.value++\n if (el && shift) {\n touch.shiftScroll(el, shift)\n options.onShift?.(shift, el)\n }\n }\n\n const observer =\n typeof ResizeObserver === 'undefined'\n ? undefined\n : new ResizeObserver((entries) =>\n update(entries.flatMap((entry) => tracked.get(entry.target) ?? [])),\n )\n\n /** Watch an item's element; call it from a function ref on every render. */\n function track(element: Element | null, key: K, index: number) {\n if (!element) return\n const item = tracked.get(element)\n if (item) {\n if (item.key !== key) {\n byKey.get(item.key)?.delete(element)\n item.key = key\n add(element, key)\n }\n item.index = index\n return\n }\n tracked.set(element, { key, index })\n add(element, key)\n observer?.observe(element)\n }\n function add(element: Element, key: K) {\n let set = byKey.get(key)\n if (!set) byKey.set(key, (set = new Set()))\n set.add(element)\n }\n /** Stop watching elements that left the page, and re-measure what they belonged to. */\n function prune() {\n const touched: { key: K; index: number }[] = []\n for (const [element, item] of tracked) {\n if (element.isConnected) continue\n observer?.unobserve(element)\n tracked.delete(element)\n const set = byKey.get(item.key)\n set?.delete(element)\n // Only re-measure an item still on the page, e.g. a row whose detail closed.\n if (set?.size) touched.push(item)\n else byKey.delete(item.key)\n }\n if (touched.length) update(touched)\n }\n onBeforeUnmount(() => observer?.disconnect())\n\n return { offsets, sizeOf, track, prune }\n}\n"],"mappings":";;;;;;;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-62,6 +62,9 @@ function usePopup(options) {
|
|
|
62
62
|
/** The panel was opened by a tap, and stays until tapped away. */
|
|
63
63
|
const tapped = ref(false);
|
|
64
64
|
watch(open, (value) => value || (tapped.value = false));
|
|
65
|
+
watch(() => toValue(options.tap), (value) => {
|
|
66
|
+
if (value === false) press = null;
|
|
67
|
+
});
|
|
65
68
|
useLayer({
|
|
66
69
|
open,
|
|
67
70
|
elements: () => [reference(), panelRef.value],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-popup.js","names":[],"sources":["../../src/composables/use-popup.ts"],"sourcesContent":["import { computed, nextTick, onScopeDispose, ref, toValue, watch } from 'vue'\nimport type { CSSProperties, ComponentPublicInstance, MaybeRefOrGetter, Ref } from 'vue'\nimport { useZIndex } from '../components/config-provider/use-config'\nimport { onlyChildElement } from '../utils/only-child'\nimport { hasHover } from '../utils/pointer'\nimport { useFloating } from './use-floating'\nimport type { Placement, VirtualElement } from './use-floating'\nimport { useLayer } from './use-layer'\nimport { useThemeScope } from './use-theme-scope'\n\n/** What opens a floating panel. `manual` leaves it entirely to `v-model:open`. */\nexport type PopupTrigger = 'hover' | 'click' | 'focus' | 'manual'\n\nexport interface UsePopupOptions {\n open: Ref<boolean>\n trigger: MaybeRefOrGetter<PopupTrigger | PopupTrigger[]>\n disabled?: MaybeRefOrGetter<boolean>\n placement: MaybeRefOrGetter<Placement>\n offset?: MaybeRefOrGetter<number>\n sameWidth?: MaybeRefOrGetter<boolean>\n /** Delay before opening on hover, in ms. */\n showDelay?: MaybeRefOrGetter<number>\n /** Delay before closing once the pointer leaves, in ms. */\n hideDelay?: MaybeRefOrGetter<number>\n /**\n * Close on a press outside. Defaults to on for `click` and `focus` triggers,\n * and for a `hover` panel a tap opened.\n */\n closeOnOutside?: MaybeRefOrGetter<boolean>\n /**\n * A finger or a pen has no hover: a tap on the trigger of a `hover` panel\n * without the `click` trigger opens and closes it instead, and a link\n * there is followed only once the panel is open. Defaults to on.\n */\n tap?: MaybeRefOrGetter<boolean>\n /** What the panel is placed next to, when not the trigger itself (e.g. the pointer). */\n anchor?: () => Element | VirtualElement | null | undefined\n}\n\n// Hover listens to pointer events and leaves fingers and pens out: a tap\n// also sends mouseenter, and the mouseleave that may follow at any time\n// (or only with the next tap elsewhere) would close what the tap opened.\nconst hoverable = (event: Event) => hasHover((event as PointerEvent).pointerType)\n\n/**\n * The shared behavior of panels anchored to a trigger: opening on hover,\n * focus or click, closing on `Escape` or a press outside, stacking above\n * other layers and following the trigger around.\n */\nexport function usePopup(options: UsePopupOptions) {\n const { open } = options\n const triggerRef = ref<ComponentPublicInstance | null>(null)\n const panelRef = ref<HTMLElement | null>(null)\n const reference = () => onlyChildElement(triggerRef.value)\n\n const triggers = computed(() => {\n const value = toValue(options.trigger)\n return new Set(Array.isArray(value) ? value : [value])\n })\n\n let timer: ReturnType<typeof setTimeout> | undefined\n const clear = () => clearTimeout(timer)\n function show(delay = 0) {\n clear()\n if (toValue(options.disabled)) return\n if (delay > 0) timer = setTimeout(() => (open.value = true), delay)\n else open.value = true\n }\n function hide(delay = 0) {\n clear()\n if (delay > 0) timer = setTimeout(() => (open.value = false), delay)\n else open.value = false\n }\n onScopeDispose(clear)\n\n /** Close, handing focus back to the trigger if it was inside the panel. */\n function dismiss() {\n const restore = !!panelRef.value?.contains(document.activeElement)\n hide()\n if (restore) (reference() as HTMLElement | null)?.focus()\n }\n\n watch(\n () => toValue(options.disabled),\n (disabled) => disabled && hide(),\n )\n\n // Render the panel the first time it opens, then keep it.\n const rendered = ref(open.value)\n const zIndex = ref<number>()\n const { next } = useZIndex()\n const { theme, sync } = useThemeScope(reference)\n watch(\n open,\n (value) => {\n if (!value) return\n rendered.value = true\n zIndex.value = next()\n void nextTick(sync)\n },\n { immediate: true },\n )\n\n const floating = useFloating(() => options.anchor?.() ?? reference(), panelRef, {\n open,\n placement: options.placement,\n offset: options.offset,\n sameWidth: options.sameWidth,\n })\n\n // The press the next click comes from, and whether the panel was open\n // then: focus from the same tap may open it before the click arrives.\n let press: { hover: boolean; wasOpen: boolean } | null = null\n /** The panel was opened by a tap, and stays until tapped away. */\n const tapped = ref(false)\n watch(open, (value) => value || (tapped.value = false))\n\n useLayer({\n open,\n elements: () => [reference(), panelRef.value],\n onEscape: dismiss,\n onOutside: () => {\n const close =\n toValue(options.closeOnOutside) ??\n (triggers.value.has('click') || triggers.value.has('focus') || tapped.value)\n if (close) hide()\n },\n })\n\n const showDelay = () => toValue(options.showDelay) ?? 0\n const hideDelay = () => toValue(options.hideDelay) ?? 0\n\n const triggerListeners = computed(() => {\n const on: Record<string, (event: Event) => void> = {}\n if (triggers.value.has('hover')) {\n on.onPointerenter = (event) => hoverable(event) && show(showDelay())\n on.onPointerleave = (event) => hoverable(event) && hide(hideDelay())\n if (!triggers.value.has('click') && (toValue(options.tap) ?? true)) {\n on.onPointerdown = (event) => {\n press = { hover: hoverable(event), wasOpen: open.value }\n }\n // a finger that scrolls the page instead sends no click\n on.onPointercancel = () => (press = null)\n on.onClick = (event) => {\n const from = press\n press = null\n // the keyboard is left to the component, and the mouse has hover;\n // a click with no press before it comes from assistive technology\n if ((event as MouseEvent).detail === 0 || from?.hover) return\n if (from ? from.wasOpen : open.value) return hide()\n // a link opens the panel first; the next tap follows it\n if ((event.currentTarget as Element | null)?.closest('a[href]')) event.preventDefault()\n // before showing: a `v-model:open` only takes the new value later\n tapped.value = !toValue(options.disabled)\n show()\n }\n }\n }\n if (triggers.value.has('focus')) {\n on.onFocusin = () => show()\n on.onFocusout = (event) => {\n const related = (event as FocusEvent).relatedTarget\n if (related instanceof Node && panelRef.value?.contains(related)) return\n hide()\n }\n }\n if (triggers.value.has('click')) {\n on.onClick = () => (open.value ? hide() : show())\n }\n return on\n })\n\n // Moving from the trigger onto the panel keeps a hover panel open.\n const panelListeners = computed(() => {\n if (!triggers.value.has('hover')) return {}\n return {\n onPointerenter: (event: Event) => hoverable(event) && clear(),\n onPointerleave: (event: Event) => hoverable(event) && hide(hideDelay()),\n }\n })\n\n const panelStyle = computed<CSSProperties>(() => ({\n ...floating.style.value,\n zIndex: zIndex.value,\n }))\n\n return {\n triggerRef,\n panelRef,\n reference,\n rendered,\n theme,\n placement: floating.placement,\n panelStyle,\n triggerListeners,\n panelListeners,\n tapped,\n show,\n hide,\n dismiss,\n update: floating.update,\n }\n}\n"],"mappings":";;;;;;;;AA0CA,IAAM,aAAa,UAAiB,SAAU,MAAuB,WAAW;;;;;;AAOhF,SAAgB,SAAS,SAA0B;CACjD,MAAM,EAAE,SAAS;CACjB,MAAM,aAAa,IAAoC,IAAI;CAC3D,MAAM,WAAW,IAAwB,IAAI;CAC7C,MAAM,kBAAkB,iBAAiB,WAAW,KAAK;CAEzD,MAAM,WAAW,eAAe;EAC9B,MAAM,QAAQ,QAAQ,QAAQ,OAAO;EACrC,OAAO,IAAI,IAAI,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK,CAAC;CACvD,CAAC;CAED,IAAI;CACJ,MAAM,cAAc,aAAa,KAAK;CACtC,SAAS,KAAK,QAAQ,GAAG;EACvB,MAAM;EACN,IAAI,QAAQ,QAAQ,QAAQ,GAAG;EAC/B,IAAI,QAAQ,GAAG,QAAQ,iBAAkB,KAAK,QAAQ,MAAO,KAAK;OAC7D,KAAK,QAAQ;CACpB;CACA,SAAS,KAAK,QAAQ,GAAG;EACvB,MAAM;EACN,IAAI,QAAQ,GAAG,QAAQ,iBAAkB,KAAK,QAAQ,OAAQ,KAAK;OAC9D,KAAK,QAAQ;CACpB;CACA,eAAe,KAAK;;CAGpB,SAAS,UAAU;EACjB,MAAM,UAAU,CAAC,CAAC,SAAS,OAAO,SAAS,SAAS,aAAa;EACjE,KAAK;EACL,IAAI,SAAS,UAAW,CAAC,EAAyB,MAAM;CAC1D;CAEA,YACQ,QAAQ,QAAQ,QAAQ,IAC7B,aAAa,YAAY,KAAK,CACjC;CAGA,MAAM,WAAW,IAAI,KAAK,KAAK;CAC/B,MAAM,SAAS,IAAY;CAC3B,MAAM,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,QAAQ;KAGR,IAAK,MAAqB,WAAW,KAAK,MAAM,OAAO;KACvD,IAAI,OAAO,KAAK,UAAU,KAAK,OAAO,OAAO,KAAK;KAElD,IAAK,MAAM,eAAkC,QAAQ,SAAS,GAAG,MAAM,eAAe;KAEtF,OAAO,QAAQ,CAAC,QAAQ,QAAQ,QAAQ;KACxC,KAAK;IACP;GACF;EACF;EACA,IAAI,SAAS,MAAM,IAAI,OAAO,GAAG;GAC/B,GAAG,kBAAkB,KAAK;GAC1B,GAAG,cAAc,UAAU;IACzB,MAAM,UAAW,MAAqB;IACtC,IAAI,mBAAmB,QAAQ,SAAS,OAAO,SAAS,OAAO,GAAG;IAClE,KAAK;GACP;EACF;EACA,IAAI,SAAS,MAAM,IAAI,OAAO,GAC5B,GAAG,gBAAiB,KAAK,QAAQ,KAAK,IAAI,KAAK;EAEjD,OAAO;CACT,CAAC;CAGD,MAAM,iBAAiB,eAAe;EACpC,IAAI,CAAC,SAAS,MAAM,IAAI,OAAO,GAAG,OAAO,CAAC;EAC1C,OAAO;GACL,iBAAiB,UAAiB,UAAU,KAAK,KAAK,MAAM;GAC5D,iBAAiB,UAAiB,UAAU,KAAK,KAAK,KAAK,UAAU,CAAC;EACxE;CACF,CAAC;CAED,MAAM,aAAa,gBAA+B;EAChD,GAAG,SAAS,MAAM;EAClB,QAAQ,OAAO;CACjB,EAAE;CAEF,OAAO;EACL;EACA;EACA;EACA;EACA;EACA,WAAW,SAAS;EACpB;EACA;EACA;EACA;EACA;EACA;EACA;EACA,QAAQ,SAAS;CACnB;AACF"}
|
|
1
|
+
{"version":3,"file":"use-popup.js","names":[],"sources":["../../src/composables/use-popup.ts"],"sourcesContent":["import { computed, nextTick, onScopeDispose, ref, toValue, watch } from 'vue'\nimport type { CSSProperties, ComponentPublicInstance, MaybeRefOrGetter, Ref } from 'vue'\nimport { useZIndex } from '../components/config-provider/use-config'\nimport { onlyChildElement } from '../utils/only-child'\nimport { hasHover } from '../utils/pointer'\nimport { useFloating } from './use-floating'\nimport type { Placement, VirtualElement } from './use-floating'\nimport { useLayer } from './use-layer'\nimport { useThemeScope } from './use-theme-scope'\n\n/** What opens a floating panel. `manual` leaves it entirely to `v-model:open`. */\nexport type PopupTrigger = 'hover' | 'click' | 'focus' | 'manual'\n\nexport interface UsePopupOptions {\n open: Ref<boolean>\n trigger: MaybeRefOrGetter<PopupTrigger | PopupTrigger[]>\n disabled?: MaybeRefOrGetter<boolean>\n placement: MaybeRefOrGetter<Placement>\n offset?: MaybeRefOrGetter<number>\n sameWidth?: MaybeRefOrGetter<boolean>\n /** Delay before opening on hover, in ms. */\n showDelay?: MaybeRefOrGetter<number>\n /** Delay before closing once the pointer leaves, in ms. */\n hideDelay?: MaybeRefOrGetter<number>\n /**\n * Close on a press outside. Defaults to on for `click` and `focus` triggers,\n * and for a `hover` panel a tap opened.\n */\n closeOnOutside?: MaybeRefOrGetter<boolean>\n /**\n * A finger or a pen has no hover: a tap on the trigger of a `hover` panel\n * without the `click` trigger opens and closes it instead, and a link\n * there is followed only once the panel is open. Defaults to on.\n */\n tap?: MaybeRefOrGetter<boolean>\n /** What the panel is placed next to, when not the trigger itself (e.g. the pointer). */\n anchor?: () => Element | VirtualElement | null | undefined\n}\n\n// Hover listens to pointer events and leaves fingers and pens out: a tap\n// also sends mouseenter, and the mouseleave that may follow at any time\n// (or only with the next tap elsewhere) would close what the tap opened.\nconst hoverable = (event: Event) => hasHover((event as PointerEvent).pointerType)\n\n/**\n * The shared behavior of panels anchored to a trigger: opening on hover,\n * focus or click, closing on `Escape` or a press outside, stacking above\n * other layers and following the trigger around.\n */\nexport function usePopup(options: UsePopupOptions) {\n const { open } = options\n const triggerRef = ref<ComponentPublicInstance | null>(null)\n const panelRef = ref<HTMLElement | null>(null)\n const reference = () => onlyChildElement(triggerRef.value)\n\n const triggers = computed(() => {\n const value = toValue(options.trigger)\n return new Set(Array.isArray(value) ? value : [value])\n })\n\n let timer: ReturnType<typeof setTimeout> | undefined\n const clear = () => clearTimeout(timer)\n function show(delay = 0) {\n clear()\n if (toValue(options.disabled)) return\n if (delay > 0) timer = setTimeout(() => (open.value = true), delay)\n else open.value = true\n }\n function hide(delay = 0) {\n clear()\n if (delay > 0) timer = setTimeout(() => (open.value = false), delay)\n else open.value = false\n }\n onScopeDispose(clear)\n\n /** Close, handing focus back to the trigger if it was inside the panel. */\n function dismiss() {\n const restore = !!panelRef.value?.contains(document.activeElement)\n hide()\n if (restore) (reference() as HTMLElement | null)?.focus()\n }\n\n watch(\n () => toValue(options.disabled),\n (disabled) => disabled && hide(),\n )\n\n // Render the panel the first time it opens, then keep it.\n const rendered = ref(open.value)\n const zIndex = ref<number>()\n const { next } = useZIndex()\n const { theme, sync } = useThemeScope(reference)\n watch(\n open,\n (value) => {\n if (!value) return\n rendered.value = true\n zIndex.value = next()\n void nextTick(sync)\n },\n { immediate: true },\n )\n\n const floating = useFloating(() => options.anchor?.() ?? reference(), panelRef, {\n open,\n placement: options.placement,\n offset: options.offset,\n sameWidth: options.sameWidth,\n })\n\n // The press the next click comes from, and whether the panel was open\n // then: focus from the same tap may open it before the click arrives.\n let press: { hover: boolean; wasOpen: boolean } | null = null\n /** The panel was opened by a tap, and stays until tapped away. */\n const tapped = ref(false)\n watch(open, (value) => value || (tapped.value = false))\n watch(\n () => toValue(options.tap),\n (value) => {\n if (value === false) press = null\n },\n )\n\n useLayer({\n open,\n elements: () => [reference(), panelRef.value],\n onEscape: dismiss,\n onOutside: () => {\n const close =\n toValue(options.closeOnOutside) ??\n (triggers.value.has('click') || triggers.value.has('focus') || tapped.value)\n if (close) hide()\n },\n })\n\n const showDelay = () => toValue(options.showDelay) ?? 0\n const hideDelay = () => toValue(options.hideDelay) ?? 0\n\n const triggerListeners = computed(() => {\n const on: Record<string, (event: Event) => void> = {}\n if (triggers.value.has('hover')) {\n on.onPointerenter = (event) => hoverable(event) && show(showDelay())\n on.onPointerleave = (event) => hoverable(event) && hide(hideDelay())\n if (!triggers.value.has('click') && (toValue(options.tap) ?? true)) {\n on.onPointerdown = (event) => {\n press = { hover: hoverable(event), wasOpen: open.value }\n }\n // a finger that scrolls the page instead sends no click\n on.onPointercancel = () => (press = null)\n on.onClick = (event) => {\n const from = press\n press = null\n // the keyboard is left to the component, and the mouse has hover;\n // a click with no press before it comes from assistive technology\n if ((event as MouseEvent).detail === 0 || from?.hover) return\n if (from ? from.wasOpen : open.value) return hide()\n // a link opens the panel first; the next tap follows it\n if ((event.currentTarget as Element | null)?.closest('a[href]')) event.preventDefault()\n // before showing: a `v-model:open` only takes the new value later\n tapped.value = !toValue(options.disabled)\n show()\n }\n }\n }\n if (triggers.value.has('focus')) {\n on.onFocusin = () => show()\n on.onFocusout = (event) => {\n const related = (event as FocusEvent).relatedTarget\n if (related instanceof Node && panelRef.value?.contains(related)) return\n hide()\n }\n }\n if (triggers.value.has('click')) {\n on.onClick = () => (open.value ? hide() : show())\n }\n return on\n })\n\n // Moving from the trigger onto the panel keeps a hover panel open.\n const panelListeners = computed(() => {\n if (!triggers.value.has('hover')) return {}\n return {\n onPointerenter: (event: Event) => hoverable(event) && clear(),\n onPointerleave: (event: Event) => hoverable(event) && hide(hideDelay()),\n }\n })\n\n const panelStyle = computed<CSSProperties>(() => ({\n ...floating.style.value,\n zIndex: zIndex.value,\n }))\n\n return {\n triggerRef,\n panelRef,\n reference,\n rendered,\n theme,\n placement: floating.placement,\n panelStyle,\n triggerListeners,\n panelListeners,\n tapped,\n show,\n hide,\n dismiss,\n update: floating.update,\n }\n}\n"],"mappings":";;;;;;;;AA0CA,IAAM,aAAa,UAAiB,SAAU,MAAuB,WAAW;;;;;;AAOhF,SAAgB,SAAS,SAA0B;CACjD,MAAM,EAAE,SAAS;CACjB,MAAM,aAAa,IAAoC,IAAI;CAC3D,MAAM,WAAW,IAAwB,IAAI;CAC7C,MAAM,kBAAkB,iBAAiB,WAAW,KAAK;CAEzD,MAAM,WAAW,eAAe;EAC9B,MAAM,QAAQ,QAAQ,QAAQ,OAAO;EACrC,OAAO,IAAI,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