@webitel/ui-chats 26.12.1 → 26.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-chats",
3
- "version": "26.12.1",
3
+ "version": "26.12.2",
4
4
  "description": "Reusable Webitel Frontend Code for Chats UI",
5
5
  "workspaces": [
6
6
  "../../",
@@ -1,5 +1,5 @@
1
1
  import { mount } from '@vue/test-utils';
2
- import { describe, expect, it, vi } from 'vitest';
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3
3
  import { computed, defineComponent, h, nextTick, ref } from 'vue';
4
4
 
5
5
  import { useChatScroll } from '../useChatScroll';
@@ -162,3 +162,151 @@ describe('useChatScroll — thread switch during a pending load', () => {
162
162
  wrapper.unmount();
163
163
  });
164
164
  });
165
+
166
+ describe('useChatScroll — fullscreen enter/exit', () => {
167
+ let fullscreenElement: Element | null = null;
168
+
169
+ const toggleFullscreen = (el: Element | null) => {
170
+ fullscreenElement = el;
171
+ document.dispatchEvent(new Event('fullscreenchange'));
172
+ };
173
+
174
+ // [Claude] happy-dom has no layout: emulate scroll metrics and the browser clamping scrollTop
175
+ const mockScrollMetrics = (
176
+ el: HTMLElement,
177
+ metrics: {
178
+ scrollHeight: number;
179
+ clientHeight: number;
180
+ scrollTop: number;
181
+ },
182
+ ) => {
183
+ const clamp = (top: number) =>
184
+ Math.max(0, Math.min(top, metrics.scrollHeight - metrics.clientHeight));
185
+ Object.defineProperty(el, 'scrollHeight', {
186
+ configurable: true,
187
+ get: () => metrics.scrollHeight,
188
+ });
189
+ Object.defineProperty(el, 'clientHeight', {
190
+ configurable: true,
191
+ get: () => metrics.clientHeight,
192
+ });
193
+ Object.defineProperty(el, 'scrollTop', {
194
+ configurable: true,
195
+ get: () => metrics.scrollTop,
196
+ set: (top: number) => {
197
+ metrics.scrollTop = clamp(top);
198
+ },
199
+ });
200
+ el.scrollTo = ((options: ScrollToOptions) => {
201
+ metrics.scrollTop = clamp(options.top ?? 0);
202
+ }) as typeof el.scrollTo;
203
+
204
+ return {
205
+ scroll: () => el.dispatchEvent(new Event('scroll')),
206
+ resizeViewport: (clientHeight: number) => {
207
+ const prevScrollTop = metrics.scrollTop;
208
+ metrics.clientHeight = clientHeight;
209
+ metrics.scrollTop = clamp(metrics.scrollTop);
210
+ if (metrics.scrollTop !== prevScrollTop)
211
+ el.dispatchEvent(new Event('scroll'));
212
+ },
213
+ };
214
+ };
215
+
216
+ const mountChat = async () => {
217
+ const container = ref<HTMLElement | null>(null);
218
+ const content = ref<HTMLElement | null>(null);
219
+ const Host = defineComponent({
220
+ setup() {
221
+ useChatScroll({
222
+ chatContainer: container,
223
+ chatContent: content,
224
+ messages: computed(() => []),
225
+ chatId: computed(() => 'c1'),
226
+ isChatClosed: computed(() => false),
227
+ });
228
+ return () =>
229
+ h(
230
+ 'div',
231
+ {
232
+ ref: container,
233
+ },
234
+ [
235
+ h('div', {
236
+ ref: content,
237
+ }),
238
+ ],
239
+ );
240
+ },
241
+ });
242
+ const wrapper = mount(Host);
243
+ await nextTick();
244
+ await nextTick();
245
+ return {
246
+ wrapper,
247
+ el: container.value as HTMLElement,
248
+ };
249
+ };
250
+
251
+ beforeEach(() => {
252
+ vi.useFakeTimers();
253
+ fullscreenElement = null;
254
+ Object.defineProperty(document, 'fullscreenElement', {
255
+ configurable: true,
256
+ get: () => fullscreenElement,
257
+ });
258
+ });
259
+
260
+ afterEach(() => {
261
+ vi.useRealTimers();
262
+ delete (
263
+ document as {
264
+ fullscreenElement?: Element | null;
265
+ }
266
+ ).fullscreenElement;
267
+ });
268
+
269
+ it('brings the chat back to the bottom after the window shrinks on exit', async () => {
270
+ const { wrapper, el } = await mountChat();
271
+ const metrics = {
272
+ scrollHeight: 2273,
273
+ clientHeight: 380,
274
+ scrollTop: 1893,
275
+ };
276
+ const chat = mockScrollMetrics(el, metrics);
277
+ chat.scroll();
278
+
279
+ toggleFullscreen(document.createElement('div'));
280
+ chat.resizeViewport(557); // the browser clamps scrollTop to 1716
281
+ vi.advanceTimersByTime(1000);
282
+
283
+ toggleFullscreen(null);
284
+ chat.resizeViewport(380);
285
+ vi.advanceTimersByTime(1000);
286
+
287
+ expect(metrics.scrollTop).toBe(1893);
288
+ wrapper.unmount();
289
+ });
290
+
291
+ it('keeps a position above the bottom instead of snapping to the bottom', async () => {
292
+ const { wrapper, el } = await mountChat();
293
+ const metrics = {
294
+ scrollHeight: 2273,
295
+ clientHeight: 380,
296
+ scrollTop: 1800,
297
+ };
298
+ const chat = mockScrollMetrics(el, metrics);
299
+ chat.scroll();
300
+
301
+ toggleFullscreen(document.createElement('div'));
302
+ chat.resizeViewport(557); // clamped to the bottom while in fullscreen
303
+ vi.advanceTimersByTime(1000);
304
+
305
+ toggleFullscreen(null);
306
+ chat.resizeViewport(380);
307
+ vi.advanceTimersByTime(1000);
308
+
309
+ expect(metrics.scrollTop).toBe(1800);
310
+ wrapper.unmount();
311
+ });
312
+ });
@@ -40,6 +40,11 @@ export interface UseChatScrollOptions<TItem = unknown> {
40
40
  onSeen?: () => void;
41
41
  }
42
42
 
43
+ interface ScrollPosition {
44
+ top: number;
45
+ atBottom: boolean;
46
+ }
47
+
43
48
  const isSelfByMember = (item: unknown): boolean =>
44
49
  !!(
45
50
  item as
@@ -65,7 +70,9 @@ export const useChatScroll = <TItem = unknown>({
65
70
  },
66
71
  onSeen,
67
72
  }: UseChatScrollOptions<TItem>) => {
68
- const { arrivedState } = useScroll(chatContainer);
73
+ const { arrivedState } = useScroll(chatContainer, {
74
+ onScroll: () => rememberScrollPosition(),
75
+ });
69
76
 
70
77
  const {
71
78
  showScrollToBottomBtn,
@@ -82,6 +89,9 @@ export const useChatScroll = <TItem = unknown>({
82
89
  let resizeObserver: ResizeObserver | null = null;
83
90
  let isViewportTransition = false;
84
91
  let viewportTransitionTimer: ReturnType<typeof setTimeout> | null = null;
92
+ let fullscreenElement = document.fullscreenElement;
93
+ let lastScrollPosition: ScrollPosition | null = null;
94
+ let scrollPositionBeforeFullscreen: ScrollPosition | null = null;
85
95
  /**
86
96
  * @author PolinaSukhorukova-webitel
87
97
  *
@@ -101,6 +111,23 @@ export const useChatScroll = <TItem = unknown>({
101
111
  el.clientHeight > 0 &&
102
112
  el.scrollHeight - (el.scrollTop + el.clientHeight) <= bottomThreshold;
103
113
 
114
+ const readScrollPosition = (el: HTMLElement): ScrollPosition => ({
115
+ top: el.scrollTop,
116
+ atBottom: isViewingBottom(el),
117
+ });
118
+
119
+ const rememberScrollPosition = () => {
120
+ const el = chatContainer.value;
121
+ // [Claude] while fullscreen toggles, scroll events come from the browser clamping scrollTop, not from the viewer
122
+ if (
123
+ !el ||
124
+ isViewportTransition ||
125
+ document.fullscreenElement !== fullscreenElement
126
+ )
127
+ return;
128
+ lastScrollPosition = readScrollPosition(el);
129
+ };
130
+
104
131
  const markSeenIfAtBottom = () => {
105
132
  const el = chatContainer.value;
106
133
  if (el && isViewingBottom(el)) onSeen?.();
@@ -165,6 +192,7 @@ export const useChatScroll = <TItem = unknown>({
165
192
  const newScrollHeight = el.scrollHeight;
166
193
 
167
194
  if (isViewportTransition) {
195
+ restoreScrollPositionAfterFullscreen();
168
196
  prevScrollHeight = newScrollHeight;
169
197
  prevScrollTop = el.scrollTop;
170
198
  updateScrollToBottomBtnVisibility(el);
@@ -212,13 +240,20 @@ export const useChatScroll = <TItem = unknown>({
212
240
  resizeObserver = null;
213
241
  };
214
242
 
215
- useResizeObserver(chatContainer, () => {
216
- const el = chatContainer.value;
217
- if (!el) return;
243
+ useResizeObserver(
244
+ [
245
+ chatContainer,
246
+ chatContent,
247
+ ],
248
+ () => {
249
+ const el = chatContainer.value;
250
+ if (!el) return;
218
251
 
219
- updateThreshold(el.clientHeight);
220
- updateScrollToBottomBtnVisibility(el);
221
- });
252
+ if (isViewportTransition) restoreScrollPositionAfterFullscreen();
253
+ updateThreshold(el.clientHeight);
254
+ updateScrollToBottomBtnVisibility(el);
255
+ },
256
+ );
222
257
 
223
258
  const resetScrollState = () => {
224
259
  stopStickToBottomObserving();
@@ -226,12 +261,34 @@ export const useChatScroll = <TItem = unknown>({
226
261
  // anchor into this one
227
262
  isLoadingNextMessages = false;
228
263
  lastVisibleMessageEl = null;
264
+ lastScrollPosition = null;
265
+ scrollPositionBeforeFullscreen = null;
229
266
  prevScrollHeight = 0;
230
267
  newUnseenMessagesCount.value = 0;
231
268
  resetScrollToBottomBtn();
232
269
  };
233
270
 
271
+ /**
272
+ * [Claude] The browser window resizes when fullscreen toggles, so the browser clamps
273
+ * scrollTop and does not reliably give it back on exit.
274
+ * Bring back the position the viewer had before entering fullscreen.
275
+ */
276
+ const restoreScrollPositionAfterFullscreen = () => {
277
+ const el = chatContainer.value;
278
+ const position = scrollPositionBeforeFullscreen;
279
+ if (!el || !position || document.fullscreenElement) return;
280
+
281
+ if (position.atBottom) scrollToBottom('instant');
282
+ else el.scrollTop = position.top;
283
+ };
284
+
234
285
  const onFullscreenChange = () => {
286
+ const el = chatContainer.value;
287
+ if (el && document.fullscreenElement && !fullscreenElement) {
288
+ scrollPositionBeforeFullscreen =
289
+ lastScrollPosition ?? readScrollPosition(el);
290
+ }
291
+ fullscreenElement = document.fullscreenElement;
235
292
  isViewportTransition = true;
236
293
 
237
294
  if (viewportTransitionTimer) clearTimeout(viewportTransitionTimer);
@@ -239,6 +296,8 @@ export const useChatScroll = <TItem = unknown>({
239
296
  viewportTransitionTimer = setTimeout(() => {
240
297
  isViewportTransition = false;
241
298
  viewportTransitionTimer = null;
299
+ restoreScrollPositionAfterFullscreen();
300
+ if (!document.fullscreenElement) scrollPositionBeforeFullscreen = null;
242
301
  }, 1000);
243
302
  };
244
303