-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* only snap scroll to bottom if user didn't scroll up * add scroll to bottom button when user scrolls up in the chat * remove unecessary export * rename scrollToBottom action to snapScrollToBottom + remove anim for now as it's stuttering
- Loading branch information
Showing
4 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @param node element to snap scroll to bottom | ||
* @param dependency pass in a dependency to update scroll on changes. | ||
*/ | ||
export const snapScrollToBottom = (node: HTMLElement, dependency: any) => { | ||
let prevScrollValue = node.scrollTop; | ||
let isDetached = false; | ||
|
||
const handleScroll = () => { | ||
// if user scrolled up, we detach | ||
if (node.scrollTop < prevScrollValue) { | ||
isDetached = true; | ||
} | ||
|
||
// if user scrolled back to bottom, we reattach | ||
if (node.scrollTop === node.scrollHeight - node.clientHeight) { | ||
isDetached = false; | ||
} | ||
|
||
prevScrollValue = node.scrollTop; | ||
}; | ||
|
||
const updateScroll = (_options: { force?: boolean } = {}) => { | ||
const defaultOptions = { force: false }; | ||
const options = { ...defaultOptions, ..._options }; | ||
const { force } = options; | ||
|
||
if (!force && isDetached) return; | ||
|
||
node.scroll({ | ||
top: node.scrollHeight | ||
}); | ||
}; | ||
|
||
node.addEventListener('scroll', handleScroll); | ||
|
||
updateScroll({ force: true }); | ||
|
||
return { | ||
update: updateScroll, | ||
destroy: () => { | ||
node.removeEventListener('scroll', handleScroll); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<script lang="ts"> | ||
import { fade } from 'svelte/transition'; | ||
import Chevron from './icons/Chevron.svelte'; | ||
import { onDestroy } from 'svelte'; | ||
export let scrollNode: HTMLElement; | ||
export { className as class }; | ||
let visible: boolean = false; | ||
let className = ''; | ||
$: if (scrollNode) { | ||
scrollNode.addEventListener('scroll', onScroll); | ||
} | ||
function onScroll() { | ||
visible = | ||
Math.ceil(scrollNode.scrollTop) + 200 < scrollNode.scrollHeight - scrollNode.clientHeight; | ||
} | ||
onDestroy(() => { | ||
if (!scrollNode) return; | ||
scrollNode.removeEventListener('scroll', onScroll); | ||
}); | ||
</script> | ||
|
||
{#if visible} | ||
<button | ||
transition:fade={{ duration: 150 }} | ||
on:click={() => scrollNode.scrollTo({ top: scrollNode.scrollHeight, behavior: 'smooth' })} | ||
class="absolute flex rounded-full border w-10 h-10 items-center justify-center shadow bg-white dark:bg-gray-700 dark:border-gray-600 {className}" | ||
><Chevron /></button | ||
> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script lang="ts"> | ||
export let classNames: string = ''; | ||
</script> | ||
|
||
<svg | ||
width="15" | ||
height="8" | ||
viewBox="0 0 15 8" | ||
class={classNames} | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M1.67236 1L7.67236 7L13.6724 1" | ||
stroke="currentColor" | ||
stroke-width="2" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
/> | ||
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters