Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added updateHeight and classes for .v-thumb based on last action #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,50 @@ you can customize scrollbar visibility with `alwaysVisible` and `initiallyVisibl
| ---------------- | ------- | ------------------------------------ |
| alwaysVisible | false | scrollbar is always visible |
| initiallyVisible | false | scrollbar is visible until scrolling |

## working with absolute divs

the `Observer WebAPI` may not recognize a resize change when working with absolute divs.

you can therefore manually trigger an update calculation, which will update the height of the scrollthumb

```js
/**
* @type {(divElement: HTMLElement | undefined, minScrollHeight: HTMLElement | undefined) => void}
* @param divElement - Will base the new height of this elements scrollheight.
* This may be wanted if you're working with absolute divs
* @param minScrollHeight - A reference to an element, which makes up for the minimum scrollheight.
* (computed: min-height + top,bottom margins + top,bottom border widths)
*/
export async function updateHeight(divElement = undefined, minScrollHeight = undefined)
```

in this example, we transition from *div A* to *div B*.

at the start of the transition, both elements will be absolute, and therefore we trigger an update to the thumb height, so it can transition **with** the contents.<br>
when the content has finished the transition, the thumb will therefore have smoothly transitioned aswell

the `updateHeight` in the transitionEnd is just a precaution — but should technically not be necessary

```html
<div class='my-content'>
<!-- Using the let: directive for updateHeight function -->
<Svroller(width='100%' height='100%' alwaysVisible!='{true}' let:updateHeight)>
<MyComponent
on:transitionStart={() => updateHeight(elements.divA, elements.view)}
on:transitionEnd={updateHeight}
>
<div bind:this={elements.view} class='view' style='position: relative; min-height: 400px;'>
<!-- Transitioning to -->
<div bind:this={elements.divA} style='position: absolute; left: 500;'>
<!-- Transitioning from -->
<div bind:this={elements.divB} style='position: absolute; left: 0;'>
...

<style>
.my-content :global .v-thumb {
transition: top .25s ease, height .25s ease;
}
...

```
93 changes: 88 additions & 5 deletions src/Svrollbar.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<svelte:options accessors/>
<script>
import { fade } from 'svelte/transition'
import { createEventDispatcher, onDestroy, onMount } from 'svelte'
import { createEventDispatcher, onDestroy, onMount, tick } from 'svelte'

/**
* the scrolling host element.
Expand Down Expand Up @@ -88,6 +89,10 @@
let windowScrollEnabled = false
let interacted = false

let thumbDown = false
let thumbResize = false
let thumbWheel = false

$: teardownViewport = setupViewport(viewport)
$: teardownContents = setupContents(contents)
$: teardownTrack = setupTrack(vTrack)
Expand All @@ -104,8 +109,77 @@
$: thumbHeight = wholeHeight > 0 ? (trackHeight / wholeHeight) * trackHeight : 0
$: thumbTop = wholeHeight > 0 ? (scrollTop / wholeHeight) * trackHeight : 0



$: scrollable = wholeHeight > trackHeight
$: visible = scrollable && (alwaysVisible || initiallyVisible)
$: if(!visible) thumbResize = false

/** updateHeight variable — ensuring only one while() loop at any given time */
let uH = 0
/**
* @type {(divElement: HTMLElement | undefined, minScrollHeight: HTMLElement | undefined) => void}
* @param divElement - Will base the new height of this elements scrollheight.
* This may be wanted if you're working with absolute divs
* @param minScrollHeight - A reference to an element, which makes up for the minimum scrollheight.
* (computed: min-height + top,bottom margins + top,bottom border widths)
*/
export async function updateHeight(
divElement = undefined,
minScrollHeight = undefined
) {
if(divElement?.nodeType !== Node.ELEMENT_NODE)
divElement = undefined

// Determining scroll height (basing a minimum height of `minScorllHeight`)
wholeHeight = (divElement || viewport).scrollHeight ?? 0
if(minScrollHeight) {
const computed = getComputedStyle(minScrollHeight)
const minHeight =
parseFloat(computed.minHeight)
+ parseFloat(computed.marginTop)
+ parseFloat(computed.marginBottom)
+ parseFloat(computed.borderTopWidth)
+ parseFloat(computed.borderBottomWidth)
if(wholeHeight < minHeight)
wholeHeight = minHeight
}

trackHeight = viewport.clientHeight - (marginTop + marginBottom) ?? 0
thumbHeight = wholeHeight > 0 ? (trackHeight / wholeHeight) * trackHeight : 0

// Determining scroll position
scrollTop = viewport.scrollTop ?? 0
const maxScrollTop = wholeHeight - trackHeight
if(scrollTop > maxScrollTop)
scrollTop = maxScrollTop
thumbTop = wholeHeight > 0 ? (scrollTop / wholeHeight) * trackHeight : 0

scrollable = wholeHeight > trackHeight

thumbResize = true

// This neat trick "saves" the state of the scroll.
if(!divElement) {
viewport.scrollBy({ top: -1, behavior: 'auto' })
await tick()
viewport.scrollBy({ top: 1, behavior: 'auto' })
}

if(scrollable) {
uH++
let _uH = uH
// Checking if thumbHeight has »transitioned« to new height
let parse = (v) => parseInt(parseFloat(v).toFixed(0))
let parsedHeight = parse(thumbHeight)
while(parse(getComputedStyle(vThumb).height) != parsedHeight) {
await new Promise(r => requestAnimationFrame(r))
if(uH != _uH) return
}
uH = 0
}
thumbResize = false
}

function setupViewport(viewport) {
if (!viewport) return
Expand All @@ -126,8 +200,7 @@

const observer = new ResizeObserver((entries) => {
for (const _entry of entries) {
wholeHeight = viewport?.scrollHeight ?? 0
trackHeight = viewport?.clientHeight - (marginTop + marginBottom) ?? 0
updateHeight()
}
})

Expand Down Expand Up @@ -206,13 +279,15 @@

function onScroll() {
if (!scrollable) return
if (thumbDown) return

clearTimer()
setupTimer()

visible = alwaysVisible || (initiallyVisible && !interacted) || true
scrollTop = viewport?.scrollTop ?? 0

thumbWheel = true
interacted = true

dispatch('show')
Expand All @@ -229,7 +304,9 @@

function onThumbDown(event) {
event.stopPropagation()
event.preventDefault()

thumbDown = true
thumbWheel = false

startTop = viewport.scrollTop
startY = event.changedTouches ? event.changedTouches[0].clientY : event.clientY
Expand All @@ -254,6 +331,8 @@
event.stopPropagation()
event.preventDefault()

thumbDown = false

startTop = 0
startY = 0

Expand Down Expand Up @@ -292,7 +371,11 @@
class="v-thumb"
style="height: {thumbHeight}px; top: {thumbTop}px"
in:vThumbIn
out:vThumbOut />
out:vThumbOut
class:down={thumbDown}
class:wheel={thumbWheel}
class:resize={thumbResize}
/>
</div>
{/if}

Expand Down
8 changes: 7 additions & 1 deletion src/Svroller.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<svelte:options accessors/>
<script>
import { fade } from 'svelte/transition'
import Svrollbar from './Svrollbar.svelte'
Expand Down Expand Up @@ -44,15 +45,20 @@

let viewport
let contents

export let svrollbar
/** @type {(divElement: HTMLElement | undefined, minScrollHeight: HTMLElement | undefined) => void} */
export const updateHeight = async (...args) => svrollbar.updateHeight(...args)
</script>

<div class="svlr-wrapper" style="width: {width}; height: {height}">
<div bind:this={viewport} class="svlr-viewport" style="width: {width}; height: {height}">
<div bind:this={contents} class="svlr-contents">
<slot />
<slot {updateHeight} />
</div>
</div>
<Svrollbar
bind:this={svrollbar}
{viewport}
{contents}
{hideAfter}
Expand Down