dce-reactkit 3.5.10 → 3.5.11

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": "dce-reactkit",
3
- "version": "3.5.10",
3
+ "version": "3.5.11",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -215,7 +215,7 @@ const AutoscrollToBottomContainer: React.FC<Props> = (props) => {
215
215
  // Initialize refs
216
216
  const lastItemIds = useRef<(string | number)[]>([]);
217
217
  const container = useRef<HTMLDivElement | null>(null);
218
- const wasScrolledToBottomBeforeItemsUpdate = useRef<boolean>(true);
218
+ const wasLastScrolledToBottom = useRef<boolean>(true);
219
219
 
220
220
  /*------------------------------------------------------------------------*/
221
221
  /* ------------------------- Component Functions ------------------------ */
@@ -253,7 +253,12 @@ const AutoscrollToBottomContainer: React.FC<Props> = (props) => {
253
253
  );
254
254
 
255
255
  // Figure out if we're scrolled to the bottom
256
- return (distanceToBottomRems < SCROLLED_TO_BOTTOM_THRESHOLD_REMS);
256
+ const atBottom = (distanceToBottomRems < SCROLLED_TO_BOTTOM_THRESHOLD_REMS);
257
+
258
+ // Remember if we scrolled to the bottom
259
+ wasLastScrolledToBottom.current = atBottom;
260
+
261
+ return atBottom;
257
262
  };
258
263
 
259
264
  /**
@@ -272,7 +277,13 @@ const AutoscrollToBottomContainer: React.FC<Props> = (props) => {
272
277
  });
273
278
 
274
279
  // Scroll to bottom
275
- container.current.scrollTop = container.current.scrollHeight;
280
+ container.current.scrollTop = (
281
+ container.current.scrollHeight
282
+ - container.current.clientHeight
283
+ );
284
+
285
+ // Remember that we scrolled to the bottom
286
+ wasLastScrolledToBottom.current = true;
276
287
  };
277
288
 
278
289
  /*----------------------------------------*/
@@ -289,8 +300,9 @@ const AutoscrollToBottomContainer: React.FC<Props> = (props) => {
289
300
  return;
290
301
  }
291
302
 
292
- // If scrolled to bottom, hide the button
303
+ // If scrolled to bottom, hide the button and remember position
293
304
  if (isScrolledToBottom()) {
305
+ // Hide button
294
306
  dispatch({
295
307
  type: ActionType.HideJumpToBottomButton,
296
308
  });
@@ -324,22 +336,24 @@ const AutoscrollToBottomContainer: React.FC<Props> = (props) => {
324
336
 
325
337
  // Check if new content appeared at bottom
326
338
  const newContentAppeared = (
339
+ // There are items
327
340
  currentItemIds.length > 0
328
- && lastItemIds.current
329
- && lastItemIds.current.length > 0
330
- && (
331
- currentItemIds[currentItemIds.length - 1]
332
- !== lastItemIds.current[lastItemIds.current.length - 1]
341
+ // The last item is new
342
+ && !lastItemIds.current.includes(
343
+ currentItemIds[currentItemIds.length - 1],
333
344
  )
334
345
  );
335
346
 
347
+ // Update last item ids
348
+ lastItemIds.current = currentItemIds;
349
+
336
350
  // Do nothing if no new content
337
351
  if (!newContentAppeared) {
338
352
  return;
339
353
  }
340
354
 
341
355
  // Check if used to be scrolled to the bottom
342
- if (wasScrolledToBottomBeforeItemsUpdate.current) {
356
+ if (wasLastScrolledToBottom.current) {
343
357
  // Was scrolled to the bottom! Autoscroll.
344
358
  scrollToBottom();
345
359
  } else {
@@ -348,14 +362,6 @@ const AutoscrollToBottomContainer: React.FC<Props> = (props) => {
348
362
  type: ActionType.ShowJumpToBottomButton,
349
363
  });
350
364
  }
351
-
352
- // Update last item ids
353
- lastItemIds.current = currentItemIds;
354
-
355
- // Remember if we were scrolled to the bottom before the update
356
- return () => {
357
- wasScrolledToBottomBeforeItemsUpdate.current = isScrolledToBottom();
358
- };
359
365
  },
360
366
  [items],
361
367
  );