@kne/table-page 0.1.4 → 0.1.5

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.
@@ -3080,29 +3080,47 @@ TableView.useSelectedRow = useSelectedRow;
3080
3080
  TableView.useSort = useSort;
3081
3081
  TableView.sortDataSource = useSort.sortDataSource;
3082
3082
 
3083
- var style$1 = {"table-page":"kne-table-page_ut9rJ","table-content":"kne-table-page_dxN6N","table-with-toolbar":"kne-table-page_sf06r","has-summary":"kne-table-page_IRdzb","table-in-toolbar":"kne-table-page_sawIT","pagination":"kne-table-page_DSTEf","total_text":"kne-table-page_pJhKr","floating-scrollbar":"kne-table-page_iu8E1","floating-scrollbar-thumb":"kne-table-page_RjZsu","is-moving":"kne-table-page_y-4Ou"};
3083
+ var style$1 = {"table-page":"kne-table-page_ut9rJ","table-content":"kne-table-page_dxN6N","table-with-toolbar":"kne-table-page_sf06r","has-summary":"kne-table-page_IRdzb","table-in-toolbar":"kne-table-page_sawIT","pagination":"kne-table-page_DSTEf","total_text":"kne-table-page_pJhKr","floating-scrollbar":"kne-table-page_iu8E1","floating-scrollbar-embedded":"kne-table-page_PIv-C","floating-scrollbar-thumb":"kne-table-page_RjZsu","is-moving":"kne-table-page_y-4Ou"};
3084
3084
 
3085
3085
  const BAR_HEIGHT = 15;
3086
3086
  const THUMB_MARGIN = 2;
3087
3087
  const computeBarMetrics = (scrollEl, viewportState, getPortalContainer) => {
3088
3088
  const rect = scrollEl.getBoundingClientRect();
3089
3089
  const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3090
- const useContainerScroll = !isDocumentScrollContainer(portalContainer);
3091
- const viewport = getViewportRect();
3092
- const anchorRect = useContainerScroll ? portalContainer.getBoundingClientRect() : viewport;
3090
+ const useEmbeddedPlacement = !isDocumentScrollContainer(portalContainer);
3093
3091
  const trackWidth = rect.width;
3094
3092
  const thumbWidth = Math.max(trackWidth * scrollEl.clientWidth / scrollEl.scrollWidth - THUMB_MARGIN * 2, 24);
3095
3093
  const maxThumbOffset = trackWidth - thumbWidth - THUMB_MARGIN * 2;
3096
3094
  const scrollRatio = scrollEl.scrollWidth > scrollEl.clientWidth ? scrollEl.scrollLeft / (scrollEl.scrollWidth - scrollEl.clientWidth) : 0;
3095
+ const visible = shouldShowFloatingScrollbar(scrollEl, viewportState, getPortalContainer);
3096
+ if (useEmbeddedPlacement) {
3097
+ const containerRect = portalContainer.getBoundingClientRect();
3098
+ return {
3099
+ placement: 'embedded',
3100
+ offsetLeft: Math.round(rect.left - containerRect.left),
3101
+ width: Math.round(trackWidth),
3102
+ thumbWidth,
3103
+ thumbLeft: THUMB_MARGIN + maxThumbOffset * scrollRatio,
3104
+ visible
3105
+ };
3106
+ }
3107
+ const viewport = getViewportRect();
3097
3108
  return {
3098
- left: rect.left,
3099
- width: trackWidth,
3100
- bottom: window.innerHeight - anchorRect.bottom,
3109
+ placement: 'fixed',
3110
+ left: Math.round(rect.left),
3111
+ width: Math.round(trackWidth),
3112
+ bottom: window.innerHeight - viewport.bottom,
3101
3113
  thumbWidth,
3102
3114
  thumbLeft: THUMB_MARGIN + maxThumbOffset * scrollRatio,
3103
- visible: shouldShowFloatingScrollbar(scrollEl, viewportState, getPortalContainer)
3115
+ visible
3104
3116
  };
3105
3117
  };
3118
+ const metricsEqual = (prev, next) => {
3119
+ if (!prev || !next) {
3120
+ return prev === next;
3121
+ }
3122
+ return prev.placement === next.placement && prev.visible === next.visible && prev.width === next.width && prev.thumbWidth === next.thumbWidth && prev.thumbLeft === next.thumbLeft && (prev.placement === 'embedded' ? prev.offsetLeft === next.offsetLeft : prev.left === next.left && prev.bottom === next.bottom);
3123
+ };
3106
3124
  const FloatingScrollBar = ({
3107
3125
  metrics,
3108
3126
  onThumbDrag,
@@ -3137,10 +3155,18 @@ const FloatingScrollBar = ({
3137
3155
  if (!(metrics != null && metrics.visible)) {
3138
3156
  return null;
3139
3157
  }
3140
- const portalTarget = document.body;
3158
+ const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3159
+ const useEmbeddedPlacement = metrics.placement === 'embedded';
3160
+ const portalTarget = useEmbeddedPlacement && portalContainer ? portalContainer : document.body;
3141
3161
  return /*#__PURE__*/createPortal(/*#__PURE__*/jsx("div", {
3142
- className: classnames(style$1['floating-scrollbar'], 'table-page-floating-scrollbar'),
3143
- style: {
3162
+ className: classnames(style$1['floating-scrollbar'], 'table-page-floating-scrollbar', {
3163
+ [style$1['floating-scrollbar-embedded']]: useEmbeddedPlacement
3164
+ }),
3165
+ style: useEmbeddedPlacement ? {
3166
+ marginLeft: metrics.offsetLeft,
3167
+ width: metrics.width,
3168
+ height: BAR_HEIGHT
3169
+ } : {
3144
3170
  left: metrics.left,
3145
3171
  width: metrics.width,
3146
3172
  height: BAR_HEIGHT,
@@ -3172,6 +3198,7 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3172
3198
  const containerRef = useRef(null);
3173
3199
  const scrollElRef = useRef(null);
3174
3200
  const viewportStateRef = useRef(null);
3201
+ const metricsRef = useRef(null);
3175
3202
  const setContainerRef = useRefCallback(node => {
3176
3203
  containerRef.current = node;
3177
3204
  if (typeof forwardedRef === 'function') {
@@ -3183,10 +3210,16 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3183
3210
  const updateMetrics = useRefCallback(() => {
3184
3211
  const scrollEl = scrollElRef.current;
3185
3212
  if (!scrollEl) {
3213
+ metricsRef.current = null;
3186
3214
  setMetrics(null);
3187
3215
  return;
3188
3216
  }
3189
- setMetrics(computeBarMetrics(scrollEl, viewportStateRef.current, getPortalContainer));
3217
+ const nextMetrics = computeBarMetrics(scrollEl, viewportStateRef.current, getPortalContainer);
3218
+ if (metricsEqual(metricsRef.current, nextMetrics)) {
3219
+ return;
3220
+ }
3221
+ metricsRef.current = nextMetrics;
3222
+ setMetrics(nextMetrics);
3190
3223
  });
3191
3224
  const handleThumbDrag = useRefCallback(deltaX => {
3192
3225
  const scrollEl = scrollElRef.current;
@@ -3207,6 +3240,7 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3207
3240
  if (!_enabled) {
3208
3241
  scrollElRef.current = null;
3209
3242
  viewportStateRef.current = null;
3243
+ metricsRef.current = null;
3210
3244
  setMetrics(null);
3211
3245
  return undefined;
3212
3246
  }
@@ -3217,6 +3251,8 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3217
3251
  let scrollEl = null;
3218
3252
  let unobserveViewport = null;
3219
3253
  let contentResizeObserver = null;
3254
+ const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3255
+ const useEmbeddedPlacement = !isDocumentScrollContainer(portalContainer);
3220
3256
  const detachScrollEl = () => {
3221
3257
  var _contentResizeObserve;
3222
3258
  unobserveViewport == null || unobserveViewport();
@@ -3238,10 +3274,12 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3238
3274
  detachScrollEl();
3239
3275
  scrollEl = nextScrollEl;
3240
3276
  scrollElRef.current = scrollEl;
3241
- unobserveViewport = observeViewportIntersection(scrollEl, state => {
3242
- viewportStateRef.current = state;
3243
- updateMetrics();
3244
- });
3277
+ if (!useEmbeddedPlacement) {
3278
+ unobserveViewport = observeViewportIntersection(scrollEl, state => {
3279
+ viewportStateRef.current = state;
3280
+ updateMetrics();
3281
+ });
3282
+ }
3245
3283
  scrollEl.addEventListener('scroll', updateMetrics, {
3246
3284
  passive: true
3247
3285
  });
@@ -3258,16 +3296,38 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3258
3296
  const containerResizeObserver = new ResizeObserver(onLayoutChange);
3259
3297
  containerResizeObserver.observe(root);
3260
3298
  onLayoutChange();
3261
- const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3262
- if (!isDocumentScrollContainer(portalContainer)) {
3299
+ const onWindowChange = () => {
3300
+ if (!useEmbeddedPlacement) {
3301
+ updateMetrics();
3302
+ }
3303
+ };
3304
+ if (!useEmbeddedPlacement) {
3305
+ var _window$visualViewpor, _window$visualViewpor2;
3306
+ window.addEventListener('scroll', onWindowChange, true);
3307
+ window.addEventListener('resize', onWindowChange);
3308
+ (_window$visualViewpor = window.visualViewport) == null || _window$visualViewpor.addEventListener('resize', onWindowChange);
3309
+ (_window$visualViewpor2 = window.visualViewport) == null || _window$visualViewpor2.addEventListener('scroll', onWindowChange);
3310
+ }
3311
+ if (useEmbeddedPlacement && portalContainer) {
3263
3312
  portalContainer.addEventListener('scroll', updateMetrics, {
3264
3313
  passive: true
3265
3314
  });
3315
+ window.addEventListener('resize', updateMetrics);
3266
3316
  }
3267
3317
  return () => {
3268
3318
  detachScrollEl();
3269
3319
  containerResizeObserver.disconnect();
3270
- portalContainer == null || portalContainer.removeEventListener('scroll', updateMetrics);
3320
+ if (!useEmbeddedPlacement) {
3321
+ var _window$visualViewpor3, _window$visualViewpor4;
3322
+ window.removeEventListener('scroll', onWindowChange, true);
3323
+ window.removeEventListener('resize', onWindowChange);
3324
+ (_window$visualViewpor3 = window.visualViewport) == null || _window$visualViewpor3.removeEventListener('resize', onWindowChange);
3325
+ (_window$visualViewpor4 = window.visualViewport) == null || _window$visualViewpor4.removeEventListener('scroll', onWindowChange);
3326
+ }
3327
+ if (useEmbeddedPlacement && portalContainer) {
3328
+ portalContainer.removeEventListener('scroll', updateMetrics);
3329
+ window.removeEventListener('resize', updateMetrics);
3330
+ }
3271
3331
  };
3272
3332
  }, [_enabled, updateMetrics, getPortalContainer]);
3273
3333
  return /*#__PURE__*/jsxs(Fragment, {