Skip to content

Commit 0078ac2

Browse files
neven4nsarrazin
andauthored
Disable text streaming if user prefers reduced animation (#1131)
* Disable text streaming if user prefers reduced animation * Simplify filtering of stream updates --------- Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com>
1 parent f91f981 commit 0078ac2

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/lib/components/chat/ChatMessage.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import type { ToolFront } from "$lib/types/Tool";
3838
import { base } from "$app/paths";
3939
import { useConvTreeStore } from "$lib/stores/convTree";
40+
import { isReducedMotion } from "$lib/utils/isReduceMotion";
4041
import Modal from "../Modal.svelte";
4142
import { toolHasName } from "$lib/utils/tools";
4243
@@ -87,6 +88,7 @@
8788
8889
let initialized = false;
8990
91+
const reducedMotionMode = isReducedMotion(window);
9092
const renderer = new marked.Renderer();
9193
// For code blocks with simple backticks
9294
renderer.codespan = (code) => {
@@ -123,6 +125,10 @@
123125
!message.content && (webSearchIsDone || (searchUpdates && searchUpdates.length === 0));
124126
125127
afterUpdate(() => {
128+
if (reducedMotionMode) {
129+
return;
130+
}
131+
126132
loadingEl?.$destroy();
127133
clearTimeout(pendingTimeout);
128134
@@ -364,6 +370,9 @@
364370
class="prose max-w-none max-sm:prose-sm dark:prose-invert prose-headings:font-semibold prose-h1:text-lg prose-h2:text-base prose-h3:text-base prose-pre:bg-gray-800 dark:prose-pre:bg-gray-900"
365371
bind:this={contentEl}
366372
>
373+
{#if isLast && loading && reducedMotionMode}
374+
<IconLoading classNames="loading inline ml-2 first:ml-0" />
375+
{/if}
367376
{#each tokens as token}
368377
{#if token.type === "code"}
369378
<CodeBlock lang={token.lang} code={unsanitizeMd(token.text)} />

src/lib/utils/isReduceMotion.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function isReducedMotion(window: Window) {
2+
const { matchMedia } = window;
3+
4+
return matchMedia("(prefers-reduced-motion: reduce)").matches;
5+
}

src/routes/conversation/[id]/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import { fetchMessageUpdates } from "$lib/utils/messageUpdates";
2424
import { createConvTreeStore } from "$lib/stores/convTree";
2525
import type { v4 } from "uuid";
26+
import { isReducedMotion } from "$lib/utils/isReduceMotion.js";
2627
import { useSettingsStore } from "$lib/stores/settings.js";
2728
2829
export let data;
@@ -79,6 +80,7 @@
7980
$isAborted = false;
8081
loading = true;
8182
pending = true;
83+
const reducedMotionMode = isReducedMotion(window);
8284
8385
const base64Files = await Promise.all(
8486
(files ?? []).map((file) =>
@@ -215,6 +217,7 @@
215217
files = [];
216218
217219
const messageUpdates: MessageUpdate[] = [];
220+
218221
for await (const update of messageUpdatesIterator) {
219222
if ($isAborted) {
220223
messageUpdatesAbortController.abort();
@@ -234,9 +237,8 @@
234237
235238
messageUpdates.push(update);
236239
237-
if (update.type === MessageUpdateType.Stream) {
240+
if (update.type === MessageUpdateType.Stream && !reducedMotionMode) {
238241
pending = false;
239-
messageToWriteTo.content += update.token;
240242
messages = [...messages];
241243
} else if (
242244
update.type === MessageUpdateType.WebSearch ||

0 commit comments

Comments
 (0)