|
| 1 | +<script lang="ts"> |
| 2 | + import { getPrefixCls } from '@ikun-ui/utils'; |
| 3 | + import { clsx } from 'clsx'; |
| 4 | + import type { KAffixProps } from './types'; |
| 5 | + import type { ScrollTarget } from './utils'; |
| 6 | + import { unwrapElement, getScrollTop, getRect, beforeNextFrameOnce } from './utils'; |
| 7 | + import { onDestroy, onMount } from 'svelte'; |
| 8 | + export let cls: KAffixProps['cls'] = undefined; |
| 9 | + export let attrs: KAffixProps['attrs'] = {}; |
| 10 | + export let listenTo: KAffixProps['listenTo'] = undefined; |
| 11 | + export let top: KAffixProps['top'] = undefined; |
| 12 | + export let bottom: KAffixProps['bottom'] = undefined; |
| 13 | + export let triggerTop: KAffixProps['triggerTop'] = undefined; |
| 14 | + export let triggerBottom: KAffixProps['triggerBottom'] = undefined; |
| 15 | + export let positionOption: KAffixProps['positionOption'] = 'fixed'; |
| 16 | +
|
| 17 | + let scrollTarget: ScrollTarget | null = null; |
| 18 | + let stickToTopRef = false; |
| 19 | + let stickToBottomRef = false; |
| 20 | + let bottomAffixedTriggerScrollTopRef: number | null = null; |
| 21 | + let topAffixedTriggerScrollTopRef: number | null = null; |
| 22 | + $: affixedRef = stickToBottomRef || stickToTopRef; |
| 23 | + $: mergedOffsetTopRef = triggerTop ?? top; |
| 24 | + $: mergedTopRef = top ?? triggerTop; |
| 25 | + $: mergedBottomRef = bottom ?? triggerBottom; |
| 26 | + $: mergedOffsetBottomRef = triggerBottom ?? bottom; |
| 27 | + let selfRef: Element | null = null; |
| 28 | +
|
| 29 | + const init = (): void => { |
| 30 | + if (listenTo) { |
| 31 | + scrollTarget = unwrapElement(listenTo); |
| 32 | + } else { |
| 33 | + scrollTarget = document; |
| 34 | + } |
| 35 | + if (scrollTarget) { |
| 36 | + scrollTarget.addEventListener('scroll', handleScroll); |
| 37 | + handleScroll(); |
| 38 | + } |
| 39 | + }; |
| 40 | + function handleScroll(): void { |
| 41 | + beforeNextFrameOnce(doHandleScroll); |
| 42 | + } |
| 43 | +
|
| 44 | + function doHandleScroll(): void { |
| 45 | + const selfEl = selfRef; |
| 46 | + if (!scrollTarget || !selfEl) return; |
| 47 | + const scrollTop = getScrollTop(scrollTarget); |
| 48 | + if (affixedRef) { |
| 49 | + if (topAffixedTriggerScrollTopRef !== null && scrollTop < topAffixedTriggerScrollTopRef) { |
| 50 | + stickToTopRef = false; |
| 51 | + topAffixedTriggerScrollTopRef = null; |
| 52 | + } |
| 53 | + if ( |
| 54 | + bottomAffixedTriggerScrollTopRef !== null && |
| 55 | + scrollTop > bottomAffixedTriggerScrollTopRef |
| 56 | + ) { |
| 57 | + stickToBottomRef = false; |
| 58 | + bottomAffixedTriggerScrollTopRef = null; |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + const containerRect = getRect(scrollTarget); |
| 63 | + const affixRect = selfEl.getBoundingClientRect(); |
| 64 | + const pxToTop = affixRect.top - containerRect.top; |
| 65 | + const pxToBottom = containerRect.bottom - affixRect.bottom; |
| 66 | + const mergedOffsetTop = mergedOffsetTopRef; |
| 67 | + const mergedOffsetBottom = mergedOffsetBottomRef; |
| 68 | + if (mergedOffsetTop !== undefined && pxToTop <= mergedOffsetTop) { |
| 69 | + stickToTopRef = true; |
| 70 | + topAffixedTriggerScrollTopRef = scrollTop - (mergedOffsetTop - pxToTop); |
| 71 | + } else { |
| 72 | + stickToTopRef = false; |
| 73 | + topAffixedTriggerScrollTopRef = null; |
| 74 | + } |
| 75 | + if (mergedOffsetBottom !== undefined && pxToBottom <= mergedOffsetBottom) { |
| 76 | + stickToBottomRef = true; |
| 77 | + bottomAffixedTriggerScrollTopRef = scrollTop + mergedOffsetBottom - pxToBottom; |
| 78 | + } else { |
| 79 | + stickToBottomRef = false; |
| 80 | + bottomAffixedTriggerScrollTopRef = null; |
| 81 | + } |
| 82 | + } |
| 83 | + onMount(init); |
| 84 | + onDestroy(() => { |
| 85 | + if (!scrollTarget) return; |
| 86 | + scrollTarget.removeEventListener('scroll', handleScroll); |
| 87 | + }); |
| 88 | +
|
| 89 | + let styleTop: string = ''; |
| 90 | + $: { |
| 91 | + if (stickToTopRef && mergedOffsetTopRef !== undefined && mergedTopRef !== undefined) { |
| 92 | + styleTop = `${mergedTopRef}px`; |
| 93 | + } |
| 94 | + } |
| 95 | +
|
| 96 | + let styleBottom: string = ''; |
| 97 | + $: { |
| 98 | + if (stickToBottomRef && mergedOffsetBottomRef !== undefined && mergedBottomRef !== undefined) { |
| 99 | + styleBottom = `${mergedBottomRef}px`; |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + const prefixCls = getPrefixCls('affix'); |
| 104 | + $: cnames = clsx( |
| 105 | + prefixCls, |
| 106 | + { |
| 107 | + [`${prefixCls}--affixed`]: affixedRef, |
| 108 | + [`${prefixCls}--absolute-positioned`]: positionOption === 'absolute' |
| 109 | + }, |
| 110 | + cls |
| 111 | + ); |
| 112 | +</script> |
| 113 | + |
| 114 | +<div |
| 115 | + class={cnames} |
| 116 | + style:top={styleTop} |
| 117 | + style:bottom={styleBottom} |
| 118 | + bind:this={selfRef} |
| 119 | + {...$$restProps} |
| 120 | + {...attrs} |
| 121 | +> |
| 122 | + <slot /> |
| 123 | +</div> |
0 commit comments