|
| 1 | +export const getScrollRatioX = (box: HTMLElement | null) => box && box.scrollWidth > 0 |
| 2 | + ? box.clientWidth / box.scrollWidth |
| 3 | + : 0 |
| 4 | + |
| 5 | +export const getScrollRatioY = (box: HTMLElement | null) => box && box.scrollHeight |
| 6 | + ? box.clientHeight / box.scrollHeight |
| 7 | + : 0 |
| 8 | + |
| 9 | +export const getScrollRatio = (box: HTMLElement | null, horizontal: boolean) => horizontal |
| 10 | + ? getScrollRatioX(box) |
| 11 | + : getScrollRatioY(box) |
| 12 | + |
| 13 | +export const getScrollDistanceX = (box: HTMLElement | null) => box ? box.scrollWidth - box.clientWidth : 0 |
| 14 | +export const getScrollDistanceY = (box: HTMLElement | null) => box ? box.scrollHeight - box.clientHeight : 0 |
| 15 | +export const getScrollDistance = (box: HTMLElement | null, horizontal: boolean) => horizontal |
| 16 | + ? getScrollDistanceX(box) |
| 17 | + : getScrollDistanceY(box) |
| 18 | + |
| 19 | +export const getClientWidth = (box: HTMLElement | null) => box ? box.clientWidth : 0 |
| 20 | +export const getClientHeight = (box: HTMLElement | null) => box ? box.clientHeight : 0 |
| 21 | +export const getClientSize = (box: HTMLElement | null, horizontal: boolean) => horizontal |
| 22 | + ? getClientWidth(box) |
| 23 | + : getClientHeight(box) |
| 24 | + |
| 25 | +export const getSliderWidth = (slider: HTMLElement | null) => slider ? parseFloat(slider.style.width) : 0 |
| 26 | +export const getSliderHeight = (slider: HTMLElement | null) => slider ? parseFloat(slider.style.height) : 0 |
| 27 | +export const getSliderSize = (slider: HTMLElement | null, horizontal: boolean) => horizontal |
| 28 | + ? getSliderWidth(slider) |
| 29 | + : getSliderHeight(slider) |
| 30 | + |
| 31 | +export const syncSlider = ( |
| 32 | + box: HTMLElement, |
| 33 | + slider: HTMLElement, |
| 34 | + horizontal: boolean |
| 35 | +) => { |
| 36 | + const scrollDistance = getScrollDistance(box, horizontal) |
| 37 | + const enabled = scrollDistance > 0 |
| 38 | + |
| 39 | + if (enabled) { |
| 40 | + const sliderLength = getClientSize(box, horizontal) * getScrollRatio(box, horizontal) |
| 41 | + const sliderDistance = getClientSize(box, horizontal) - sliderLength |
| 42 | + |
| 43 | + if (horizontal) { |
| 44 | + slider.style.width = sliderLength + 'px' |
| 45 | + slider.style.height = '' |
| 46 | + slider.style.left = box.scrollLeft * sliderDistance / scrollDistance + 'px' |
| 47 | + slider.style.top = '' |
| 48 | + } else { |
| 49 | + slider.style.width = '' |
| 50 | + slider.style.height = sliderLength + 'px' |
| 51 | + slider.style.left = '' |
| 52 | + slider.style.top = box.scrollTop * sliderDistance / scrollDistance + 'px' |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return enabled |
| 57 | +} |
| 58 | + |
| 59 | +export const createRail = (el: HTMLElement, options: { |
| 60 | + horizontal?: boolean |
| 61 | + disabled?: boolean |
| 62 | + onDragStart?: () => void; |
| 63 | + onDragEnd?: () => void; |
| 64 | + onToggle?: (active: boolean) => void; |
| 65 | +}) => { |
| 66 | + let box: HTMLElement | null = null |
| 67 | + let observer: ResizeObserver | null = null |
| 68 | + |
| 69 | + let dragging = false |
| 70 | + let horizontal = options.horizontal ?? false |
| 71 | + let disabled = options.disabled ?? false |
| 72 | + |
| 73 | + const slider = el.querySelector<HTMLElement>('.m3-scroll-rail__slider') |
| 74 | + if (!slider) { |
| 75 | + throw new Error('[@modulify/m3-foundation::M3ScrollRail] Slider element not found') |
| 76 | + } |
| 77 | + |
| 78 | + let lastPageX = 0 |
| 79 | + let lastPageY = 0 |
| 80 | + |
| 81 | + const getPointerOffsetX = (event: MouseEvent) => event.pageX - lastPageX |
| 82 | + const getPointerOffsetY = (event: MouseEvent) => event.pageY - lastPageY |
| 83 | + const getPointerOffset = (event: MouseEvent) => horizontal |
| 84 | + ? getPointerOffsetX(event) |
| 85 | + : getPointerOffsetY(event) |
| 86 | + |
| 87 | + const onMousedown = (event: MouseEvent) => { |
| 88 | + dragging = true |
| 89 | + options.onDragStart?.() |
| 90 | + lastPageY = event.pageY |
| 91 | + lastPageX = event.pageX |
| 92 | + } |
| 93 | + |
| 94 | + const onMousemove = (event: MouseEvent) => { |
| 95 | + if (!box || !dragging) { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + const scrollDistance = getScrollDistance(box, horizontal) |
| 100 | + const sliderDistance = getClientSize(box, horizontal) - getSliderSize(slider, horizontal) |
| 101 | + |
| 102 | + if (scrollDistance > 0 && sliderDistance > 0) { |
| 103 | + const pointerOffset = getPointerOffset(event) |
| 104 | + if (horizontal) { |
| 105 | + box.scrollLeft = (slider.offsetLeft + pointerOffset) * scrollDistance / sliderDistance |
| 106 | + slider.style.left = (box.scrollLeft * sliderDistance / scrollDistance) + 'px' |
| 107 | + slider.style.top = '' |
| 108 | + } else { |
| 109 | + box.scrollTop = (slider.offsetTop + pointerOffset) * scrollDistance / sliderDistance |
| 110 | + slider.style.left = '' |
| 111 | + slider.style.top = (box.scrollTop * sliderDistance / scrollDistance) + 'px' |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + lastPageX = event.pageX |
| 116 | + lastPageY = event.pageY |
| 117 | + } |
| 118 | + |
| 119 | + const onMouseup = () => { |
| 120 | + dragging = false |
| 121 | + options.onDragEnd?.() |
| 122 | + } |
| 123 | + |
| 124 | + const subscribe = (el: HTMLElement) => { |
| 125 | + observer = new ResizeObserver(() => sync()) |
| 126 | + observer.observe(el) |
| 127 | + |
| 128 | + el.addEventListener('scroll', sync, { passive: true }) |
| 129 | + } |
| 130 | + |
| 131 | + const unsubscribe = () => { |
| 132 | + observer?.disconnect() |
| 133 | + observer = null |
| 134 | + |
| 135 | + box?.removeEventListener('scroll', sync) |
| 136 | + box = null |
| 137 | + } |
| 138 | + |
| 139 | + const connect = () => { |
| 140 | + const parent = el.parentElement ?? null |
| 141 | + |
| 142 | + if (parent === box) return |
| 143 | + if (box) { unsubscribe() } |
| 144 | + if (parent) { subscribe(parent) } |
| 145 | + |
| 146 | + box = parent |
| 147 | + } |
| 148 | + |
| 149 | + const sync = () => { |
| 150 | + connect() |
| 151 | + |
| 152 | + if (box && !disabled) { |
| 153 | + const enabled = syncSlider(box, slider, horizontal) |
| 154 | + options.onToggle?.(enabled) |
| 155 | + } else { |
| 156 | + options.onToggle?.(false) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return { |
| 161 | + get horizontal () { return horizontal }, |
| 162 | + set horizontal (isHorizontal: boolean) { |
| 163 | + const wasHorizontal = horizontal |
| 164 | + |
| 165 | + horizontal = isHorizontal |
| 166 | + |
| 167 | + if (!disabled && isHorizontal !== wasHorizontal) { |
| 168 | + sync() |
| 169 | + } |
| 170 | + }, |
| 171 | + |
| 172 | + get disabled () { return disabled }, |
| 173 | + set disabled (disable: boolean) { |
| 174 | + if (!disable && disabled) { |
| 175 | + sync() |
| 176 | + } |
| 177 | + }, |
| 178 | + |
| 179 | + init () { |
| 180 | + slider.addEventListener('mousedown', onMousedown) |
| 181 | + window.addEventListener('mousemove', onMousemove) |
| 182 | + window.addEventListener('mouseup', onMouseup) |
| 183 | + sync() |
| 184 | + }, |
| 185 | + |
| 186 | + sync, |
| 187 | + |
| 188 | + destroy () { |
| 189 | + slider.removeEventListener('mousedown', onMousedown) |
| 190 | + window.removeEventListener('mousemove', onMousemove) |
| 191 | + window.removeEventListener('mouseup', onMouseup) |
| 192 | + unsubscribe() |
| 193 | + }, |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +export type ScrollRail = ReturnType<typeof createRail> |
0 commit comments