Spaces:
Running
Running
Download src/components/ScrollRail.vue from lrigamai/browser: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/spaces/lrigamai/browser/resolve/main/src/components/ScrollRail.vue
- Command line
-
hf download hf://spaces/lrigamai/browser/src/components/ScrollRail.vue
-
curl -L -o ScrollRail.vue https://huggingface.co/spaces/lrigamai/browser/resolve/main/src/components/ScrollRail.vue
2.74 kB
| <script setup lang="ts"> | |
| import { onBeforeUnmount, ref, watch } from "vue"; | |
| /** | |
| * Thin custom scrollbar for an inner scroll area (whose native one is hidden): a hairline track | |
| * with a thumb that widens on hover and can be dragged. Place it in a positioned parent beside `el`. | |
| */ | |
| const props = defineProps<{ el?: HTMLElement }>(); | |
| const top = ref(0); | |
| const size = ref(1); | |
| const dragging = ref(false); | |
| function update() { | |
| const el = props.el; | |
| if (!el) return; | |
| const { scrollTop, scrollHeight, clientHeight } = el; | |
| size.value = scrollHeight > 0 ? clientHeight / scrollHeight : 1; | |
| top.value = scrollHeight > 0 ? scrollTop / scrollHeight : 0; | |
| } | |
| // Content can change height without scrolling (new character, images loading), so watch sizes too. | |
| let resize: ResizeObserver | undefined; | |
| let children: MutationObserver | undefined; | |
| function detach(el?: HTMLElement) { | |
| el?.removeEventListener("scroll", update); | |
| resize?.disconnect(); | |
| children?.disconnect(); | |
| } | |
| watch( | |
| () => props.el, | |
| (el, prev) => { | |
| detach(prev); | |
| if (!el) return; | |
| el.addEventListener("scroll", update, { passive: true }); | |
| resize = new ResizeObserver(update); | |
| const observeAll = () => { | |
| resize!.disconnect(); | |
| resize!.observe(el); | |
| for (const c of el.children) resize!.observe(c); | |
| }; | |
| observeAll(); | |
| children = new MutationObserver(observeAll); | |
| children.observe(el, { childList: true }); | |
| update(); | |
| }, | |
| { immediate: true }, | |
| ); | |
| onBeforeUnmount(() => detach(props.el)); | |
| // Dragging the thumb scrolls proportionally. | |
| let startY = 0; | |
| let startScroll = 0; | |
| function onDown(e: PointerEvent) { | |
| if (!props.el) return; | |
| dragging.value = true; | |
| startY = e.clientY; | |
| startScroll = props.el.scrollTop; | |
| (e.target as HTMLElement).setPointerCapture(e.pointerId); | |
| e.preventDefault(); | |
| } | |
| function onMove(e: PointerEvent) { | |
| if (!dragging.value || !props.el) return; | |
| props.el.scrollTop = startScroll + (e.clientY - startY) / size.value; | |
| } | |
| const onUp = () => (dragging.value = false); | |
| </script> | |
| <template> | |
| <div v-if="size < 1" class="rail" :class="{ dragging }" aria-hidden="true"> | |
| <div | |
| class="thumb" | |
| :style="{ top: `${top * 100}%`, height: `${size * 100}%` }" | |
| @pointerdown="onDown" | |
| @pointermove="onMove" | |
| @pointerup="onUp" | |
| @pointercancel="onUp" | |
| /> | |
| </div> | |
| </template> | |
| <style scoped> | |
| .rail { | |
| position: absolute; | |
| top: 6px; | |
| right: 2px; | |
| bottom: 6px; | |
| width: 10px; | |
| pointer-events: none; | |
| } | |
| .thumb { | |
| position: absolute; | |
| right: 2px; | |
| width: 2px; | |
| min-height: 24px; | |
| border-radius: 2px; | |
| background: var(--line-strong); | |
| pointer-events: auto; | |
| cursor: grab; | |
| transition: width 0.15s, background 0.15s; | |
| } | |
| .thumb:hover, | |
| .dragging .thumb { | |
| width: 5px; | |
| background: var(--accent); | |
| } | |
| .dragging .thumb { | |
| cursor: grabbing; | |
| } | |
| </style> | |