-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathToolbarSticky.tsx
More file actions
88 lines (80 loc) · 2.79 KB
/
ToolbarSticky.tsx
File metadata and controls
88 lines (80 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Fragment, useLayoutEffect, useRef, useState } from 'react';
import { Toolbar, ToolbarItem, ToolbarContent, SearchInput, Checkbox } from '@patternfly/react-core';
const useTheadPinnedFromScrollParent = ({ track, scrollRootRef, theadRef }): { isPinned } => {
const [isPinned, setIsPinned] = useState(false);
useLayoutEffect(() => {
if (!track) {
setIsPinned(false);
return;
}
const scrollRoot = scrollRootRef.current;
if (!scrollRoot) {
setIsPinned(false);
return;
}
const syncFromScroll = () => {
setIsPinned(scrollRoot.scrollTop > 0);
};
syncFromScroll();
scrollRoot.addEventListener('scroll', syncFromScroll, { passive: true });
return () => scrollRoot.removeEventListener('scroll', syncFromScroll);
}, [track, scrollRootRef, theadRef]);
return { isPinned };
};
export const ToolbarSticky = () => {
const [isSticky, setIsSticky] = useState(true);
const [showEvenOnly, setShowEvenOnly] = useState(true);
const [searchValue, setSearchValue] = useState('');
const array = Array.from(Array(30), (_, x) => x); // create array of numbers from 1-30 for demo purposes
const numbers = showEvenOnly ? array.filter((number) => number % 2 === 0) : array;
const innerScrollRef = useRef<HTMLDivElement>(null);
const toolbarRef = useRef<HTMLDivElement>(null);
const { isPinned } = useTheadPinnedFromScrollParent({
track: true,
scrollRootRef: innerScrollRef,
theadRef: toolbarRef
});
return (
<Fragment>
<div style={{ overflowY: 'scroll', height: '200px' }} ref={innerScrollRef}>
<Toolbar
className={isPinned ? 'PINNED' : ''}
id="toolbar-sticky"
inset={{ default: 'insetNone' }}
isSticky={isSticky}
ref={toolbarRef}
>
<ToolbarContent>
<ToolbarItem>
<SearchInput
aria-label="Sticky example search input"
value={searchValue}
onChange={(_event, value) => setSearchValue(value)}
onClear={() => setSearchValue('')}
/>
</ToolbarItem>
<ToolbarItem alignSelf="center">
<Checkbox
label="Show only even number items"
isChecked={showEvenOnly}
onChange={(_event, checked) => setShowEvenOnly(checked)}
id="showOnlyEvenCheckbox"
/>
</ToolbarItem>
</ToolbarContent>
</Toolbar>
<ul>
{numbers.map((number) => (
<li key={number}>{`item ${number}`}</li>
))}
</ul>
</div>
<Checkbox
label="Is toolbar sticky"
isChecked={isSticky}
onChange={(_event, checked) => setIsSticky(checked)}
id="isStickyCheckbox"
/>
</Fragment>
);
};