@wordpress/dataviews 13.0.0 → 13.1.0

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.
Files changed (156) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +2 -1
  3. package/build/components/dataform-controls/date.cjs +11 -1
  4. package/build/components/dataform-controls/date.cjs.map +2 -2
  5. package/build/components/dataform-controls/datetime.cjs +23 -32
  6. package/build/components/dataform-controls/datetime.cjs.map +2 -2
  7. package/build/components/dataform-controls/utils/relative-date-control.cjs +2 -1
  8. package/build/components/dataform-controls/utils/relative-date-control.cjs.map +2 -2
  9. package/build/components/dataform-layouts/normalize-form.cjs +19 -1
  10. package/build/components/dataform-layouts/normalize-form.cjs.map +2 -2
  11. package/build/components/dataform-layouts/panel/index.cjs +1 -1
  12. package/build/components/dataform-layouts/panel/index.cjs.map +2 -2
  13. package/build/components/dataform-layouts/panel/modal.cjs +4 -3
  14. package/build/components/dataform-layouts/panel/modal.cjs.map +2 -2
  15. package/build/components/dataviews-layout/index.cjs +12 -3
  16. package/build/components/dataviews-layout/index.cjs.map +2 -2
  17. package/build/components/dataviews-layouts/grid/composite-grid.cjs +5 -1
  18. package/build/components/dataviews-layouts/grid/composite-grid.cjs.map +2 -2
  19. package/build/components/dataviews-layouts/index.cjs +3 -3
  20. package/build/components/dataviews-layouts/index.cjs.map +3 -3
  21. package/build/components/dataviews-layouts/picker-grid/index.cjs +13 -2
  22. package/build/components/dataviews-layouts/picker-grid/index.cjs.map +2 -2
  23. package/build/components/dataviews-layouts/table/index.cjs +98 -89
  24. package/build/components/dataviews-layouts/table/index.cjs.map +2 -2
  25. package/build/components/dataviews-layouts/table/{use-is-horizontal-scroll-end.cjs → use-scroll-state.cjs} +29 -33
  26. package/build/components/dataviews-layouts/table/use-scroll-state.cjs.map +7 -0
  27. package/build/components/dataviews-layouts/utils/density-picker.cjs.map +2 -2
  28. package/build/components/dataviews-layouts/utils/grid-config-options.cjs +45 -0
  29. package/build/components/dataviews-layouts/utils/grid-config-options.cjs.map +7 -0
  30. package/build/dataviews/index.cjs +12 -8
  31. package/build/dataviews/index.cjs.map +2 -2
  32. package/build/dataviews-picker/index.cjs +1 -1
  33. package/build/dataviews-picker/index.cjs.map +2 -2
  34. package/build/types/dataform.cjs.map +1 -1
  35. package/build/types/dataviews.cjs.map +1 -1
  36. package/build-module/components/dataform-controls/date.mjs +11 -1
  37. package/build-module/components/dataform-controls/date.mjs.map +2 -2
  38. package/build-module/components/dataform-controls/datetime.mjs +24 -33
  39. package/build-module/components/dataform-controls/datetime.mjs.map +2 -2
  40. package/build-module/components/dataform-controls/utils/relative-date-control.mjs +2 -1
  41. package/build-module/components/dataform-controls/utils/relative-date-control.mjs.map +2 -2
  42. package/build-module/components/dataform-layouts/normalize-form.mjs +19 -1
  43. package/build-module/components/dataform-layouts/normalize-form.mjs.map +2 -2
  44. package/build-module/components/dataform-layouts/panel/index.mjs +1 -1
  45. package/build-module/components/dataform-layouts/panel/index.mjs.map +2 -2
  46. package/build-module/components/dataform-layouts/panel/modal.mjs +4 -3
  47. package/build-module/components/dataform-layouts/panel/modal.mjs.map +2 -2
  48. package/build-module/components/dataviews-layout/index.mjs +12 -3
  49. package/build-module/components/dataviews-layout/index.mjs.map +2 -2
  50. package/build-module/components/dataviews-layouts/grid/composite-grid.mjs +5 -1
  51. package/build-module/components/dataviews-layouts/grid/composite-grid.mjs.map +2 -2
  52. package/build-module/components/dataviews-layouts/index.mjs +3 -3
  53. package/build-module/components/dataviews-layouts/index.mjs.map +2 -2
  54. package/build-module/components/dataviews-layouts/picker-grid/index.mjs +13 -2
  55. package/build-module/components/dataviews-layouts/picker-grid/index.mjs.map +2 -2
  56. package/build-module/components/dataviews-layouts/table/index.mjs +98 -89
  57. package/build-module/components/dataviews-layouts/table/index.mjs.map +2 -2
  58. package/build-module/components/dataviews-layouts/table/use-scroll-state.mjs +46 -0
  59. package/build-module/components/dataviews-layouts/table/use-scroll-state.mjs.map +7 -0
  60. package/build-module/components/dataviews-layouts/utils/density-picker.mjs.map +2 -2
  61. package/build-module/components/dataviews-layouts/utils/grid-config-options.mjs +14 -0
  62. package/build-module/components/dataviews-layouts/utils/grid-config-options.mjs.map +7 -0
  63. package/build-module/dataviews/index.mjs +12 -8
  64. package/build-module/dataviews/index.mjs.map +2 -2
  65. package/build-module/dataviews-picker/index.mjs +1 -1
  66. package/build-module/dataviews-picker/index.mjs.map +2 -2
  67. package/build-style/style-rtl.css +47 -5
  68. package/build-style/style.css +47 -5
  69. package/build-types/components/dataform-controls/date.d.ts.map +1 -1
  70. package/build-types/components/dataform-controls/datetime.d.ts.map +1 -1
  71. package/build-types/components/dataform-controls/utils/relative-date-control.d.ts.map +1 -1
  72. package/build-types/components/dataform-layouts/normalize-form.d.ts.map +1 -1
  73. package/build-types/components/dataform-layouts/panel/modal.d.ts.map +1 -1
  74. package/build-types/components/dataviews-layout/index.d.ts.map +1 -1
  75. package/build-types/components/dataviews-layouts/grid/composite-grid.d.ts.map +1 -1
  76. package/build-types/components/dataviews-layouts/index.d.ts +3 -3
  77. package/build-types/components/dataviews-layouts/index.d.ts.map +1 -1
  78. package/build-types/components/dataviews-layouts/picker-grid/index.d.ts.map +1 -1
  79. package/build-types/components/dataviews-layouts/table/index.d.ts.map +1 -1
  80. package/build-types/components/dataviews-layouts/table/use-scroll-state.d.ts +25 -0
  81. package/build-types/components/dataviews-layouts/table/use-scroll-state.d.ts.map +1 -0
  82. package/build-types/components/dataviews-layouts/utils/density-picker.d.ts.map +1 -1
  83. package/build-types/components/dataviews-layouts/utils/grid-config-options.d.ts +2 -0
  84. package/build-types/components/dataviews-layouts/utils/grid-config-options.d.ts.map +1 -0
  85. package/build-types/dataform/stories/index.story.d.ts +26 -1
  86. package/build-types/dataform/stories/index.story.d.ts.map +1 -1
  87. package/build-types/dataform/stories/layout-panel.d.ts +3 -1
  88. package/build-types/dataform/stories/layout-panel.d.ts.map +1 -1
  89. package/build-types/dataviews/index.d.ts.map +1 -1
  90. package/build-types/dataviews/stories/empty.d.ts +1 -2
  91. package/build-types/dataviews/stories/empty.d.ts.map +1 -1
  92. package/build-types/dataviews/stories/free-composition.d.ts.map +1 -1
  93. package/build-types/dataviews/stories/index.story.d.ts +18 -10
  94. package/build-types/dataviews/stories/index.story.d.ts.map +1 -1
  95. package/build-types/dataviews/stories/infinite-scroll.d.ts.map +1 -1
  96. package/build-types/dataviews/stories/layout-activity.d.ts.map +1 -1
  97. package/build-types/dataviews/stories/layout-custom.d.ts +3 -1
  98. package/build-types/dataviews/stories/layout-custom.d.ts.map +1 -1
  99. package/build-types/dataviews/stories/layout-grid.d.ts.map +1 -1
  100. package/build-types/dataviews/stories/layout-list.d.ts.map +1 -1
  101. package/build-types/dataviews/stories/layout-table.d.ts.map +1 -1
  102. package/build-types/dataviews/stories/with-card.d.ts +3 -1
  103. package/build-types/dataviews/stories/with-card.d.ts.map +1 -1
  104. package/build-types/types/dataform.d.ts +17 -2
  105. package/build-types/types/dataform.d.ts.map +1 -1
  106. package/build-types/types/dataviews.d.ts +8 -0
  107. package/build-types/types/dataviews.d.ts.map +1 -1
  108. package/build-wp/index.js +883 -866
  109. package/package.json +19 -19
  110. package/src/components/dataform-controls/date.tsx +11 -1
  111. package/src/components/dataform-controls/datetime.tsx +28 -44
  112. package/src/components/dataform-controls/utils/relative-date-control.tsx +2 -1
  113. package/src/components/dataform-layouts/normalize-form.ts +24 -1
  114. package/src/components/dataform-layouts/panel/index.tsx +1 -1
  115. package/src/components/dataform-layouts/panel/modal.tsx +7 -3
  116. package/src/components/dataform-layouts/panel/style.scss +1 -1
  117. package/src/components/dataform-layouts/test/normalize-form.ts +98 -5
  118. package/src/components/dataviews-layout/index.tsx +41 -19
  119. package/src/components/dataviews-layout/style.scss +8 -0
  120. package/src/components/dataviews-layouts/grid/composite-grid.tsx +7 -1
  121. package/src/components/dataviews-layouts/grid/style.scss +18 -2
  122. package/src/components/dataviews-layouts/index.ts +3 -3
  123. package/src/components/dataviews-layouts/picker-grid/index.tsx +17 -2
  124. package/src/components/dataviews-layouts/picker-grid/style.scss +10 -0
  125. package/src/components/dataviews-layouts/table/index.tsx +11 -5
  126. package/src/components/dataviews-layouts/table/style.scss +13 -0
  127. package/src/components/dataviews-layouts/table/use-scroll-state.ts +79 -0
  128. package/src/components/dataviews-layouts/utils/density-picker.tsx +12 -2
  129. package/src/components/dataviews-layouts/utils/grid-config-options.tsx +14 -0
  130. package/src/components/dataviews-layouts/utils/grid-items.scss +9 -1
  131. package/src/dataform/stories/index.story.tsx +15 -0
  132. package/src/dataform/stories/layout-panel.tsx +19 -4
  133. package/src/dataviews/index.tsx +17 -9
  134. package/src/dataviews/stories/empty.tsx +1 -3
  135. package/src/dataviews/stories/free-composition.tsx +32 -34
  136. package/src/dataviews/stories/index.story.tsx +31 -8
  137. package/src/dataviews/stories/infinite-scroll.tsx +0 -6
  138. package/src/dataviews/stories/layout-activity.tsx +1 -0
  139. package/src/dataviews/stories/layout-custom.tsx +7 -3
  140. package/src/dataviews/stories/layout-grid.tsx +1 -0
  141. package/src/dataviews/stories/layout-list.tsx +1 -0
  142. package/src/dataviews/stories/layout-table.tsx +1 -0
  143. package/src/dataviews/stories/style.css +0 -5
  144. package/src/dataviews/stories/with-card.tsx +6 -2
  145. package/src/dataviews/style.scss +0 -1
  146. package/src/dataviews/test/dataviews.tsx +42 -1
  147. package/src/dataviews-picker/index.tsx +1 -1
  148. package/src/style.scss +1 -0
  149. package/src/types/dataform.ts +15 -2
  150. package/src/types/dataviews.ts +10 -0
  151. package/build/components/dataviews-layouts/table/use-is-horizontal-scroll-end.cjs.map +0 -7
  152. package/build-module/components/dataviews-layouts/table/use-is-horizontal-scroll-end.mjs +0 -50
  153. package/build-module/components/dataviews-layouts/table/use-is-horizontal-scroll-end.mjs.map +0 -7
  154. package/build-types/components/dataviews-layouts/table/use-is-horizontal-scroll-end.d.ts +0 -19
  155. package/build-types/components/dataviews-layouts/table/use-is-horizontal-scroll-end.d.ts.map +0 -1
  156. package/src/components/dataviews-layouts/table/use-is-horizontal-scroll-end.ts +0 -82
@@ -18,7 +18,11 @@ import filterSortAndPaginate from '../../utils/filter-sort-and-paginate';
18
18
  import type { View } from '../../types';
19
19
  import { actions, data, fields } from './fixtures';
20
20
 
21
- const WithCardComponent = () => {
21
+ const WithCardComponent = ( {
22
+ containerHeight,
23
+ }: {
24
+ containerHeight: string;
25
+ } ) => {
22
26
  const [ view, setView ] = useState< View >( {
23
27
  type: LAYOUT_TABLE,
24
28
  search: '',
@@ -37,7 +41,7 @@ const WithCardComponent = () => {
37
41
  return (
38
42
  <Card>
39
43
  <CardHeader>Header</CardHeader>
40
- <CardBody>
44
+ <CardBody style={ { height: containerHeight, minHeight: 0 } }>
41
45
  <DataViews
42
46
  getItemId={ ( item ) => item.id.toString() }
43
47
  paginationInfo={ paginationInfo }
@@ -15,7 +15,6 @@
15
15
  .dataviews-wrapper,
16
16
  .dataviews-picker-wrapper {
17
17
  height: 100%;
18
- overflow: auto;
19
18
  box-sizing: border-box;
20
19
  scroll-padding-bottom: $grid-unit-80;
21
20
  /* stylelint-disable-next-line property-no-unknown -- '@container' not globally permitted */
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { render, screen } from '@testing-library/react';
4
+ import { fireEvent, render, screen } from '@testing-library/react';
5
5
  import userEvent from '@testing-library/user-event';
6
6
 
7
7
  /**
@@ -240,6 +240,47 @@ describe( 'DataViews component', () => {
240
240
  expect( screen.getByText( 'TEST TITLE' ) ).toBeInTheDocument();
241
241
  } );
242
242
 
243
+ it( 'should trigger infinite scroll when the layout container scrolls', () => {
244
+ const infiniteScrollHandler = jest.fn();
245
+ const { container } = render(
246
+ <DataViewWrapper
247
+ view={ {
248
+ type: LAYOUT_GRID,
249
+ infiniteScrollEnabled: true,
250
+ perPage: 1,
251
+ } }
252
+ paginationInfo={ {
253
+ totalItems: data.length,
254
+ totalPages: data.length,
255
+ infiniteScrollHandler,
256
+ } }
257
+ />
258
+ );
259
+ // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
260
+ const layoutContainer = container.querySelector(
261
+ '.dataviews-layout__container'
262
+ ) as HTMLDivElement;
263
+
264
+ Object.defineProperties( layoutContainer, {
265
+ scrollTop: {
266
+ configurable: true,
267
+ value: 500,
268
+ },
269
+ scrollHeight: {
270
+ configurable: true,
271
+ value: 1000,
272
+ },
273
+ clientHeight: {
274
+ configurable: true,
275
+ value: 500,
276
+ },
277
+ } );
278
+
279
+ fireEvent.scroll( layoutContainer );
280
+
281
+ expect( infiniteScrollHandler ).toHaveBeenCalledTimes( 1 );
282
+ } );
283
+
243
284
  describe( 'in table view', () => {
244
285
  it( 'should display columns for each field', () => {
245
286
  render( <DataViewWrapper /> );
@@ -241,7 +241,7 @@ function DataViewsPicker< Item >( {
241
241
  hasInfiniteScrollHandler: !! infiniteScrollHandler,
242
242
  } }
243
243
  >
244
- <div className="dataviews-picker-wrapper" ref={ containerRef }>
244
+ <div className="dataviews-picker-wrapper">
245
245
  { children ?? (
246
246
  <DefaultUI search={ search } searchLabel={ searchLabel } />
247
247
  ) }
package/src/style.scss CHANGED
@@ -1,5 +1,6 @@
1
1
  @use "./dataviews/style.scss" as *;
2
2
  @use "./components/dataviews-bulk-actions/style.scss" as *;
3
+ @use "./components/dataviews-layout/style.scss" as *;
3
4
  @use "./components/dataviews-filters/style.scss" as *;
4
5
  @use "./components/dataviews-footer/style.scss" as *;
5
6
  @use "./components/dataviews-pagination/style.scss" as *;
@@ -23,17 +23,30 @@ export type NormalizedRegularLayout = {
23
23
 
24
24
  export type EditVisibility = 'always' | 'on-hover';
25
25
 
26
+ type PanelOpenAsDropdown = {
27
+ type: 'dropdown';
28
+ };
29
+ export type PanelOpenAsModal = {
30
+ type: 'modal';
31
+ applyLabel: string;
32
+ cancelLabel: string;
33
+ };
34
+
26
35
  export type PanelLayout = {
27
36
  type: 'panel';
28
37
  labelPosition?: LabelPosition;
29
- openAs?: 'dropdown' | 'modal';
38
+ openAs?:
39
+ | 'dropdown'
40
+ | 'modal'
41
+ | { type: 'dropdown' }
42
+ | { type: 'modal'; applyLabel?: string; cancelLabel?: string };
30
43
  summary?: PanelSummaryField;
31
44
  editVisibility?: EditVisibility;
32
45
  };
33
46
  export type NormalizedPanelLayout = {
34
47
  type: 'panel';
35
48
  labelPosition: LabelPosition;
36
- openAs: 'dropdown' | 'modal';
49
+ openAs: PanelOpenAsDropdown | PanelOpenAsModal;
37
50
  summary: NormalizedPanelSummaryField;
38
51
  editVisibility: EditVisibility;
39
52
  };
@@ -285,6 +285,11 @@ export interface ViewGrid extends ViewBase {
285
285
  * The preview size of the grid.
286
286
  */
287
287
  previewSize?: number;
288
+
289
+ /**
290
+ * The density of the grid layout.
291
+ */
292
+ density?: Density;
288
293
  };
289
294
  }
290
295
 
@@ -301,6 +306,11 @@ export interface ViewPickerGrid extends ViewBase {
301
306
  * The preview size of the grid.
302
307
  */
303
308
  previewSize?: number;
309
+
310
+ /**
311
+ * The density of the grid layout.
312
+ */
313
+ density?: Density;
304
314
  };
305
315
  }
306
316
 
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../src/components/dataviews-layouts/table/use-is-horizontal-scroll-end.ts"],
4
- "sourcesContent": ["/**\n * External dependencies\n */\nimport type { MutableRefObject } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { useDebounce } from '@wordpress/compose';\nimport { useCallback, useEffect, useState } from '@wordpress/element';\nimport { isRTL } from '@wordpress/i18n';\n\nconst isScrolledToEnd = ( element: Element ) => {\n\tif ( isRTL() ) {\n\t\tconst scrollLeft = Math.abs( element.scrollLeft );\n\t\treturn scrollLeft <= 1;\n\t}\n\n\treturn element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;\n};\n\n/**\n * A hook to check if a given scroll container has reached the horizontal scroll end.\n *\n * The current way receives \"refs\" as arguments, but it lacks a mechanism to detect when a ref has changed.\n * As a result, when the \"ref\" is updated and attached to a new div, the computation should trigger again.\n * However, this isn't possible in the current setup because the hook is unaware that the ref has changed.\n *\n * See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.\n *\n * @param {Object} params The parameters for the hook.\n * @param {MutableRefObject<HTMLDivElement | null>} params.scrollContainerRef The ref to the scroll container element.\n * @param {boolean} [params.enabled=false] Whether the hook is enabled.\n * @return {boolean} - Returns true if the scroll container is scrolled to the end or false otherwise.\n */\nexport function useIsHorizontalScrollEnd( {\n\tscrollContainerRef,\n\tenabled = false,\n}: {\n\tscrollContainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tenabled?: boolean;\n} ): boolean {\n\tconst [ isHorizontalScrollEnd, setIsHorizontalScrollEnd ] =\n\t\tuseState( false );\n\n\tconst handleIsHorizontalScrollEnd = useDebounce(\n\t\tuseCallback( () => {\n\t\t\tconst scrollContainer = scrollContainerRef.current;\n\t\t\tif ( scrollContainer ) {\n\t\t\t\tsetIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );\n\t\t\t}\n\t\t}, [ scrollContainerRef, setIsHorizontalScrollEnd ] ),\n\t\t200\n\t);\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\ttypeof window === 'undefined' ||\n\t\t\t! enabled ||\n\t\t\t! scrollContainerRef.current\n\t\t) {\n\t\t\treturn () => {};\n\t\t}\n\n\t\thandleIsHorizontalScrollEnd();\n\t\tscrollContainerRef.current.addEventListener(\n\t\t\t'scroll',\n\t\t\thandleIsHorizontalScrollEnd\n\t\t);\n\t\twindow.addEventListener( 'resize', handleIsHorizontalScrollEnd );\n\n\t\treturn () => {\n\t\t\tscrollContainerRef.current?.removeEventListener(\n\t\t\t\t'scroll',\n\t\t\t\thandleIsHorizontalScrollEnd\n\t\t\t);\n\t\t\twindow.removeEventListener( 'resize', handleIsHorizontalScrollEnd );\n\t\t};\n\t}, [ scrollContainerRef, enabled ] );\n\n\treturn isHorizontalScrollEnd;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAA4B;AAC5B,qBAAiD;AACjD,kBAAsB;AAEtB,IAAM,kBAAkB,CAAE,YAAsB;AAC/C,UAAK,mBAAM,GAAI;AACd,UAAM,aAAa,KAAK,IAAK,QAAQ,UAAW;AAChD,WAAO,cAAc;AAAA,EACtB;AAEA,SAAO,QAAQ,aAAa,QAAQ,eAAe,QAAQ,cAAc;AAC1E;AAgBO,SAAS,yBAA0B;AAAA,EACzC;AAAA,EACA,UAAU;AACX,GAGa;AACZ,QAAM,CAAE,uBAAuB,wBAAyB,QACvD,yBAAU,KAAM;AAEjB,QAAM,kCAA8B;AAAA,QACnC,4BAAa,MAAM;AAClB,YAAM,kBAAkB,mBAAmB;AAC3C,UAAK,iBAAkB;AACtB,iCAA0B,gBAAiB,eAAgB,CAAE;AAAA,MAC9D;AAAA,IACD,GAAG,CAAE,oBAAoB,wBAAyB,CAAE;AAAA,IACpD;AAAA,EACD;AAEA,gCAAW,MAAM;AAChB,QACC,OAAO,WAAW,eAClB,CAAE,WACF,CAAE,mBAAmB,SACpB;AACD,aAAO,MAAM;AAAA,MAAC;AAAA,IACf;AAEA,gCAA4B;AAC5B,uBAAmB,QAAQ;AAAA,MAC1B;AAAA,MACA;AAAA,IACD;AACA,WAAO,iBAAkB,UAAU,2BAA4B;AAE/D,WAAO,MAAM;AACZ,yBAAmB,SAAS;AAAA,QAC3B;AAAA,QACA;AAAA,MACD;AACA,aAAO,oBAAqB,UAAU,2BAA4B;AAAA,IACnE;AAAA,EACD,GAAG,CAAE,oBAAoB,OAAQ,CAAE;AAEnC,SAAO;AACR;",
6
- "names": []
7
- }
@@ -1,50 +0,0 @@
1
- // packages/dataviews/src/components/dataviews-layouts/table/use-is-horizontal-scroll-end.ts
2
- import { useDebounce } from "@wordpress/compose";
3
- import { useCallback, useEffect, useState } from "@wordpress/element";
4
- import { isRTL } from "@wordpress/i18n";
5
- var isScrolledToEnd = (element) => {
6
- if (isRTL()) {
7
- const scrollLeft = Math.abs(element.scrollLeft);
8
- return scrollLeft <= 1;
9
- }
10
- return element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;
11
- };
12
- function useIsHorizontalScrollEnd({
13
- scrollContainerRef,
14
- enabled = false
15
- }) {
16
- const [isHorizontalScrollEnd, setIsHorizontalScrollEnd] = useState(false);
17
- const handleIsHorizontalScrollEnd = useDebounce(
18
- useCallback(() => {
19
- const scrollContainer = scrollContainerRef.current;
20
- if (scrollContainer) {
21
- setIsHorizontalScrollEnd(isScrolledToEnd(scrollContainer));
22
- }
23
- }, [scrollContainerRef, setIsHorizontalScrollEnd]),
24
- 200
25
- );
26
- useEffect(() => {
27
- if (typeof window === "undefined" || !enabled || !scrollContainerRef.current) {
28
- return () => {
29
- };
30
- }
31
- handleIsHorizontalScrollEnd();
32
- scrollContainerRef.current.addEventListener(
33
- "scroll",
34
- handleIsHorizontalScrollEnd
35
- );
36
- window.addEventListener("resize", handleIsHorizontalScrollEnd);
37
- return () => {
38
- scrollContainerRef.current?.removeEventListener(
39
- "scroll",
40
- handleIsHorizontalScrollEnd
41
- );
42
- window.removeEventListener("resize", handleIsHorizontalScrollEnd);
43
- };
44
- }, [scrollContainerRef, enabled]);
45
- return isHorizontalScrollEnd;
46
- }
47
- export {
48
- useIsHorizontalScrollEnd
49
- };
50
- //# sourceMappingURL=use-is-horizontal-scroll-end.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../src/components/dataviews-layouts/table/use-is-horizontal-scroll-end.ts"],
4
- "sourcesContent": ["/**\n * External dependencies\n */\nimport type { MutableRefObject } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { useDebounce } from '@wordpress/compose';\nimport { useCallback, useEffect, useState } from '@wordpress/element';\nimport { isRTL } from '@wordpress/i18n';\n\nconst isScrolledToEnd = ( element: Element ) => {\n\tif ( isRTL() ) {\n\t\tconst scrollLeft = Math.abs( element.scrollLeft );\n\t\treturn scrollLeft <= 1;\n\t}\n\n\treturn element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;\n};\n\n/**\n * A hook to check if a given scroll container has reached the horizontal scroll end.\n *\n * The current way receives \"refs\" as arguments, but it lacks a mechanism to detect when a ref has changed.\n * As a result, when the \"ref\" is updated and attached to a new div, the computation should trigger again.\n * However, this isn't possible in the current setup because the hook is unaware that the ref has changed.\n *\n * See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.\n *\n * @param {Object} params The parameters for the hook.\n * @param {MutableRefObject<HTMLDivElement | null>} params.scrollContainerRef The ref to the scroll container element.\n * @param {boolean} [params.enabled=false] Whether the hook is enabled.\n * @return {boolean} - Returns true if the scroll container is scrolled to the end or false otherwise.\n */\nexport function useIsHorizontalScrollEnd( {\n\tscrollContainerRef,\n\tenabled = false,\n}: {\n\tscrollContainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tenabled?: boolean;\n} ): boolean {\n\tconst [ isHorizontalScrollEnd, setIsHorizontalScrollEnd ] =\n\t\tuseState( false );\n\n\tconst handleIsHorizontalScrollEnd = useDebounce(\n\t\tuseCallback( () => {\n\t\t\tconst scrollContainer = scrollContainerRef.current;\n\t\t\tif ( scrollContainer ) {\n\t\t\t\tsetIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );\n\t\t\t}\n\t\t}, [ scrollContainerRef, setIsHorizontalScrollEnd ] ),\n\t\t200\n\t);\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\ttypeof window === 'undefined' ||\n\t\t\t! enabled ||\n\t\t\t! scrollContainerRef.current\n\t\t) {\n\t\t\treturn () => {};\n\t\t}\n\n\t\thandleIsHorizontalScrollEnd();\n\t\tscrollContainerRef.current.addEventListener(\n\t\t\t'scroll',\n\t\t\thandleIsHorizontalScrollEnd\n\t\t);\n\t\twindow.addEventListener( 'resize', handleIsHorizontalScrollEnd );\n\n\t\treturn () => {\n\t\t\tscrollContainerRef.current?.removeEventListener(\n\t\t\t\t'scroll',\n\t\t\t\thandleIsHorizontalScrollEnd\n\t\t\t);\n\t\t\twindow.removeEventListener( 'resize', handleIsHorizontalScrollEnd );\n\t\t};\n\t}, [ scrollContainerRef, enabled ] );\n\n\treturn isHorizontalScrollEnd;\n}\n"],
5
- "mappings": ";AAQA,SAAS,mBAAmB;AAC5B,SAAS,aAAa,WAAW,gBAAgB;AACjD,SAAS,aAAa;AAEtB,IAAM,kBAAkB,CAAE,YAAsB;AAC/C,MAAK,MAAM,GAAI;AACd,UAAM,aAAa,KAAK,IAAK,QAAQ,UAAW;AAChD,WAAO,cAAc;AAAA,EACtB;AAEA,SAAO,QAAQ,aAAa,QAAQ,eAAe,QAAQ,cAAc;AAC1E;AAgBO,SAAS,yBAA0B;AAAA,EACzC;AAAA,EACA,UAAU;AACX,GAGa;AACZ,QAAM,CAAE,uBAAuB,wBAAyB,IACvD,SAAU,KAAM;AAEjB,QAAM,8BAA8B;AAAA,IACnC,YAAa,MAAM;AAClB,YAAM,kBAAkB,mBAAmB;AAC3C,UAAK,iBAAkB;AACtB,iCAA0B,gBAAiB,eAAgB,CAAE;AAAA,MAC9D;AAAA,IACD,GAAG,CAAE,oBAAoB,wBAAyB,CAAE;AAAA,IACpD;AAAA,EACD;AAEA,YAAW,MAAM;AAChB,QACC,OAAO,WAAW,eAClB,CAAE,WACF,CAAE,mBAAmB,SACpB;AACD,aAAO,MAAM;AAAA,MAAC;AAAA,IACf;AAEA,gCAA4B;AAC5B,uBAAmB,QAAQ;AAAA,MAC1B;AAAA,MACA;AAAA,IACD;AACA,WAAO,iBAAkB,UAAU,2BAA4B;AAE/D,WAAO,MAAM;AACZ,yBAAmB,SAAS;AAAA,QAC3B;AAAA,QACA;AAAA,MACD;AACA,aAAO,oBAAqB,UAAU,2BAA4B;AAAA,IACnE;AAAA,EACD,GAAG,CAAE,oBAAoB,OAAQ,CAAE;AAEnC,SAAO;AACR;",
6
- "names": []
7
- }
@@ -1,19 +0,0 @@
1
- /**
2
- * A hook to check if a given scroll container has reached the horizontal scroll end.
3
- *
4
- * The current way receives "refs" as arguments, but it lacks a mechanism to detect when a ref has changed.
5
- * As a result, when the "ref" is updated and attached to a new div, the computation should trigger again.
6
- * However, this isn't possible in the current setup because the hook is unaware that the ref has changed.
7
- *
8
- * See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.
9
- *
10
- * @param {Object} params The parameters for the hook.
11
- * @param {MutableRefObject<HTMLDivElement | null>} params.scrollContainerRef The ref to the scroll container element.
12
- * @param {boolean} [params.enabled=false] Whether the hook is enabled.
13
- * @return {boolean} - Returns true if the scroll container is scrolled to the end or false otherwise.
14
- */
15
- export declare function useIsHorizontalScrollEnd({ scrollContainerRef, enabled, }: {
16
- scrollContainerRef: React.MutableRefObject<HTMLDivElement | null>;
17
- enabled?: boolean;
18
- }): boolean;
19
- //# sourceMappingURL=use-is-horizontal-scroll-end.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-is-horizontal-scroll-end.d.ts","sourceRoot":"","sources":["../../../../src/components/dataviews-layouts/table/use-is-horizontal-scroll-end.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,wBAAwB,CAAE,EACzC,kBAAkB,EAClB,OAAe,GACf,EAAE;IACF,kBAAkB,EAAE,KAAK,CAAC,gBAAgB,CAAE,cAAc,GAAG,IAAI,CAAE,CAAC;IACpE,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB,GAAI,OAAO,CAwCX"}
@@ -1,82 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import type { MutableRefObject } from 'react';
5
-
6
- /**
7
- * WordPress dependencies
8
- */
9
- import { useDebounce } from '@wordpress/compose';
10
- import { useCallback, useEffect, useState } from '@wordpress/element';
11
- import { isRTL } from '@wordpress/i18n';
12
-
13
- const isScrolledToEnd = ( element: Element ) => {
14
- if ( isRTL() ) {
15
- const scrollLeft = Math.abs( element.scrollLeft );
16
- return scrollLeft <= 1;
17
- }
18
-
19
- return element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;
20
- };
21
-
22
- /**
23
- * A hook to check if a given scroll container has reached the horizontal scroll end.
24
- *
25
- * The current way receives "refs" as arguments, but it lacks a mechanism to detect when a ref has changed.
26
- * As a result, when the "ref" is updated and attached to a new div, the computation should trigger again.
27
- * However, this isn't possible in the current setup because the hook is unaware that the ref has changed.
28
- *
29
- * See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.
30
- *
31
- * @param {Object} params The parameters for the hook.
32
- * @param {MutableRefObject<HTMLDivElement | null>} params.scrollContainerRef The ref to the scroll container element.
33
- * @param {boolean} [params.enabled=false] Whether the hook is enabled.
34
- * @return {boolean} - Returns true if the scroll container is scrolled to the end or false otherwise.
35
- */
36
- export function useIsHorizontalScrollEnd( {
37
- scrollContainerRef,
38
- enabled = false,
39
- }: {
40
- scrollContainerRef: React.MutableRefObject< HTMLDivElement | null >;
41
- enabled?: boolean;
42
- } ): boolean {
43
- const [ isHorizontalScrollEnd, setIsHorizontalScrollEnd ] =
44
- useState( false );
45
-
46
- const handleIsHorizontalScrollEnd = useDebounce(
47
- useCallback( () => {
48
- const scrollContainer = scrollContainerRef.current;
49
- if ( scrollContainer ) {
50
- setIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );
51
- }
52
- }, [ scrollContainerRef, setIsHorizontalScrollEnd ] ),
53
- 200
54
- );
55
-
56
- useEffect( () => {
57
- if (
58
- typeof window === 'undefined' ||
59
- ! enabled ||
60
- ! scrollContainerRef.current
61
- ) {
62
- return () => {};
63
- }
64
-
65
- handleIsHorizontalScrollEnd();
66
- scrollContainerRef.current.addEventListener(
67
- 'scroll',
68
- handleIsHorizontalScrollEnd
69
- );
70
- window.addEventListener( 'resize', handleIsHorizontalScrollEnd );
71
-
72
- return () => {
73
- scrollContainerRef.current?.removeEventListener(
74
- 'scroll',
75
- handleIsHorizontalScrollEnd
76
- );
77
- window.removeEventListener( 'resize', handleIsHorizontalScrollEnd );
78
- };
79
- }, [ scrollContainerRef, enabled ] );
80
-
81
- return isHorizontalScrollEnd;
82
- }