Skip to content

Commit 1e8fff5

Browse files
committed
Made util helper back to inline code
1 parent 96d953b commit 1e8fff5

8 files changed

Lines changed: 144 additions & 135 deletions

File tree

packages/react-core/src/components/Pagination/examples/PaginationDynamicStickyBottom.tsx

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
1-
import { useState, useRef } from 'react';
2-
import {
3-
Pagination,
4-
PaginationVariant,
5-
Gallery,
6-
GalleryItem,
7-
Card,
8-
CardBody,
9-
useIsStuckFromScrollParent
10-
} from '@patternfly/react-core';
1+
import { useLayoutEffect, useState, useRef } from 'react';
2+
import { Pagination, PaginationVariant, Gallery, GalleryItem, Card, CardBody } from '@patternfly/react-core';
3+
4+
const useIsStuckFromScrollParent = ({
5+
shouldTrack,
6+
scrollParentRef
7+
}: {
8+
/** Indicates whether to track the scroll top position of the scroll parent element */
9+
shouldTrack: boolean;
10+
/** Reference to the scroll parent element */
11+
scrollParentRef: React.RefObject<any>;
12+
}): boolean => {
13+
const [isStuck, setIsStuck] = useState(false);
14+
15+
useLayoutEffect(() => {
16+
if (!shouldTrack) {
17+
setIsStuck(false);
18+
return;
19+
}
20+
21+
const scrollElement = scrollParentRef.current;
22+
if (!scrollElement) {
23+
setIsStuck(false);
24+
return;
25+
}
26+
27+
const syncFromScroll = () => {
28+
setIsStuck(scrollElement.scrollTop + scrollElement.clientHeight < scrollElement.scrollHeight);
29+
};
30+
syncFromScroll();
31+
scrollElement.addEventListener('scroll', syncFromScroll, { passive: true });
32+
return () => scrollElement.removeEventListener('scroll', syncFromScroll);
33+
}, [shouldTrack, scrollParentRef]);
34+
35+
return isStuck;
36+
};
1137

1238
export const PaginationDynamicStickyBottom: React.FunctionComponent = () => {
1339
const scrollParentRef = useRef<HTMLDivElement>(null);
14-
const isStickyStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef, position: 'bottom' });
40+
const isStickyStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef });
1541
const [page, setPage] = useState(1);
1642
const [perPage, setPerPage] = useState(50);
1743
const itemCount = 523;

packages/react-core/src/components/Pagination/examples/PaginationDynamicStickyTop.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
1-
import { useState, useRef } from 'react';
2-
import { Pagination, Gallery, GalleryItem, Card, CardBody, useIsStuckFromScrollParent } from '@patternfly/react-core';
1+
import { useLayoutEffect, useState, useRef } from 'react';
2+
import { Pagination, Gallery, GalleryItem, Card, CardBody } from '@patternfly/react-core';
3+
4+
const useIsStuckFromScrollParent = ({
5+
shouldTrack,
6+
scrollParentRef
7+
}: {
8+
/** Indicates whether to track the scroll top position of the scroll parent element */
9+
shouldTrack: boolean;
10+
/** Reference to the scroll parent element */
11+
scrollParentRef: React.RefObject<any>;
12+
}): boolean => {
13+
const [isStuck, setIsStuck] = useState(false);
14+
15+
useLayoutEffect(() => {
16+
if (!shouldTrack) {
17+
setIsStuck(false);
18+
return;
19+
}
20+
21+
const scrollElement = scrollParentRef.current;
22+
if (!scrollElement) {
23+
setIsStuck(false);
24+
return;
25+
}
26+
27+
const syncFromScroll = () => {
28+
setIsStuck(scrollElement.scrollTop > 0);
29+
};
30+
syncFromScroll();
31+
scrollElement.addEventListener('scroll', syncFromScroll, { passive: true });
32+
return () => scrollElement.removeEventListener('scroll', syncFromScroll);
33+
}, [shouldTrack, scrollParentRef]);
34+
35+
return isStuck;
36+
};
337

438
export const PaginationDynamicStickyTop: React.FunctionComponent = () => {
539
const scrollParentRef = useRef<HTMLDivElement>(null);

packages/react-core/src/components/Pagination/examples/PaginationPlainBottom.tsx

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/react-core/src/components/Pagination/examples/PaginationPlainTop.tsx

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/react-core/src/components/Toolbar/examples/ToolbarDynamicSticky.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
import { useState, useRef } from 'react';
2-
import {
3-
Toolbar,
4-
ToolbarItem,
5-
ToolbarContent,
6-
SearchInput,
7-
Checkbox,
8-
useIsStuckFromScrollParent
9-
} from '@patternfly/react-core';
1+
import { useLayoutEffect, useState, useRef } from 'react';
2+
import { Toolbar, ToolbarItem, ToolbarContent, SearchInput, Checkbox } from '@patternfly/react-core';
3+
4+
const useIsStuckFromScrollParent = ({
5+
shouldTrack,
6+
scrollParentRef
7+
}: {
8+
/** Indicates whether to track the scroll top position of the scroll parent element */
9+
shouldTrack: boolean;
10+
/** Reference to the scroll parent element */
11+
scrollParentRef: React.RefObject<any>;
12+
}): boolean => {
13+
const [isStuck, setIsStuck] = useState(false);
14+
15+
useLayoutEffect(() => {
16+
if (!shouldTrack) {
17+
setIsStuck(false);
18+
return;
19+
}
20+
21+
const scrollElement = scrollParentRef.current;
22+
if (!scrollElement) {
23+
setIsStuck(false);
24+
return;
25+
}
26+
27+
const syncFromScroll = () => {
28+
setIsStuck(scrollElement.scrollTop > 0);
29+
};
30+
syncFromScroll();
31+
scrollElement.addEventListener('scroll', syncFromScroll, { passive: true });
32+
return () => scrollElement.removeEventListener('scroll', syncFromScroll);
33+
}, [shouldTrack, scrollParentRef]);
34+
35+
return isStuck;
36+
};
1037

1138
export const ToolbarDynamicSticky = () => {
1239
const scrollParentRef = useRef<HTMLDivElement>(null);

packages/react-core/src/helpers/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ export * from './SSRSafeIds/SSRSafeIds';
1313
export * from './KeyboardHandler';
1414
export * from './resizeObserver';
1515
export * from './useInterval';
16-
export * from './useIsStuckFromScrollParent';
1716
export * from './datetimeUtils';

packages/react-core/src/helpers/useIsStuckFromScrollParent.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

packages/react-table/src/components/Table/examples/TableStickyHeaderDynamic.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { useRef } from 'react';
1+
import { useLayoutEffect, useRef, useState } from 'react';
22
import { Table, Thead, Tr, Th, Tbody, Td, InnerScrollContainer } from '@patternfly/react-table';
33
import RhUiBlueprintIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-blueprint-icon';
4-
import { useIsStuckFromScrollParent } from '@patternfly/react-core';
54

65
interface Fact {
76
name: string;
@@ -15,6 +14,40 @@ interface Fact {
1514
detail7: string;
1615
}
1716

17+
const useIsStuckFromScrollParent = ({
18+
shouldTrack,
19+
scrollParentRef
20+
}: {
21+
/** Indicates whether to track the scroll top position of the scroll parent element */
22+
shouldTrack: boolean;
23+
/** Reference to the scroll parent element */
24+
scrollParentRef: React.RefObject<any>;
25+
}): boolean => {
26+
const [isStuck, setIsStuck] = useState(false);
27+
28+
useLayoutEffect(() => {
29+
if (!shouldTrack) {
30+
setIsStuck(false);
31+
return;
32+
}
33+
34+
const scrollElement = scrollParentRef.current;
35+
if (!scrollElement) {
36+
setIsStuck(false);
37+
return;
38+
}
39+
40+
const syncFromScroll = () => {
41+
setIsStuck(scrollElement.scrollTop > 0);
42+
};
43+
syncFromScroll();
44+
scrollElement.addEventListener('scroll', syncFromScroll, { passive: true });
45+
return () => scrollElement.removeEventListener('scroll', syncFromScroll);
46+
}, [shouldTrack, scrollParentRef]);
47+
48+
return isStuck;
49+
};
50+
1851
export const TableStickyHeaderDynamic: React.FunctionComponent = () => {
1952
const scrollContainerRef = useRef<HTMLDivElement>(null);
2053
const isStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef: scrollContainerRef });

0 commit comments

Comments
 (0)