Debian Codegraph @

node/@patternfly/react-core/src/components/Nav/NavList.tsx

raw
1 import { Component, createRef, Fragment } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Nav/nav';
3 import { css } from '@patternfly/react-styles';
4 import { Button } from '../Button';
5 import RhMicronsCaretLeftIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-left-icon';
6 import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon';
7 import { getLanguageDirection, isElementInView } from '../../helpers/util';
8 import { NavContext } from './Nav';
9 import { PageSidebarContext } from '../Page/PageSidebar';
10 import { getResizeObserver } from '../../helpers/resizeObserver';
11
12 export interface NavListProps extends React.DetailedHTMLProps<
13 React.HTMLAttributes<HTMLUListElement>,
14 HTMLUListElement
15 > {
16 /** Children nodes */
17 children?: React.ReactNode;
18 /** Additional classes added to the list */
19 className?: string;
20 /** Aria-label for the back scroll button */
21 backScrollAriaLabel?: string;
22 /** Aria-label for the forward scroll button */
23 forwardScrollAriaLabel?: string;
24 }
25
26 class NavList extends Component<NavListProps> {
27 static displayName = 'NavList';
28 static contextType = NavContext;
29 context!: React.ContextType<typeof NavContext>;
30 static defaultProps: NavListProps = {
31 backScrollAriaLabel: 'Scroll back',
32 forwardScrollAriaLabel: 'Scroll foward'
33 };
34 private direction = 'ltr';
35
36 state = {
37 scrollViewAtStart: false,
38 scrollViewAtEnd: false
39 };
40
41 navList = createRef<HTMLUListElement>();
42 observer: any = () => {};
43
44 handleScrollButtons = () => {
45 const container = this.navList.current;
46 if (container) {
47 // check if it elements are in view
48 const scrollViewAtStart = isElementInView(container, container.firstChild as HTMLElement, false);
49 const scrollViewAtEnd = isElementInView(container, container.lastChild as HTMLElement, false);
50 this.setState({
51 scrollViewAtStart,
52 scrollViewAtEnd
53 });
54 this.context.updateIsScrollable(!scrollViewAtStart || !scrollViewAtEnd);
55 }
56 };
57
58 scrollBack = () => {
59 // find first Element that is fully in view on the left, then scroll to the element before it
60 const container = this.navList.current;
61 if (container) {
62 const childrenArr = Array.from(container.children);
63 let firstElementInView: Element;
64 let lastElementOutOfView: Element;
65 for (let i = 0; i < childrenArr.length && !firstElementInView; i++) {
66 if (isElementInView(container, childrenArr[i] as HTMLElement, false)) {
67 firstElementInView = childrenArr[i];
68 lastElementOutOfView = childrenArr[i - 1];
69 }
70 }
71 if (lastElementOutOfView) {
72 if (this.direction === 'ltr') {
73 // LTR scrolls left to go back
74 container.scrollLeft -= lastElementOutOfView.scrollWidth;
75 } else {
76 // RTL scrolls right to go back
77 container.scrollLeft += lastElementOutOfView.scrollWidth;
78 }
79 }
80 this.handleScrollButtons();
81 }
82 };
83
84 scrollForward = () => {
85 // find last Element that is fully in view on the right, then scroll to the element after it
86 const container = this.navList.current;
87 if (container) {
88 const childrenArr = Array.from(container.children);
89 let lastElementInView: Element;
90 let firstElementOutOfView: Element;
91 for (let i = childrenArr.length - 1; i >= 0 && !lastElementInView; i--) {
92 if (isElementInView(container, childrenArr[i] as HTMLElement, false)) {
93 lastElementInView = childrenArr[i];
94 firstElementOutOfView = childrenArr[i + 1];
95 }
96 }
97 if (firstElementOutOfView) {
98 if (this.direction === 'ltr') {
99 // LTR scrolls right to go forward
100 container.scrollLeft += firstElementOutOfView.scrollWidth;
101 } else {
102 // RTL scrolls left to go forward
103 container.scrollLeft -= firstElementOutOfView.scrollWidth;
104 }
105 }
106 this.handleScrollButtons();
107 }
108 };
109
110 componentDidMount() {
111 this.observer = getResizeObserver(this.navList.current, this.handleScrollButtons, true);
112 this.direction = getLanguageDirection(this.navList.current);
113 this.handleScrollButtons();
114 }
115
116 componentWillUnmount() {
117 this.observer();
118 }
119
120 componentDidUpdate() {
121 this.direction = getLanguageDirection(this.navList.current);
122 }
123
124 render() {
125 const { children, className, backScrollAriaLabel, forwardScrollAriaLabel, ...props } = this.props;
126 const { scrollViewAtStart, scrollViewAtEnd } = this.state;
127
128 return (
129 <NavContext.Consumer>
130 {({ isHorizontal }) => (
131 <PageSidebarContext.Consumer>
132 {({ isSidebarOpen }) => (
133 <Fragment>
134 {isHorizontal && (!scrollViewAtStart || !scrollViewAtEnd) && (
135 <div className={css(styles.navScrollButton)}>
136 <Button
137 variant="plain"
138 aria-label={backScrollAriaLabel}
139 onClick={this.scrollBack}
140 isDisabled={scrollViewAtStart}
141 tabIndex={isSidebarOpen ? null : -1}
142 icon={<RhMicronsCaretLeftIcon />}
143 />
144 </div>
145 )}
146 <ul
147 ref={this.navList}
148 className={css(styles.navList, className)}
149 onScroll={this.handleScrollButtons}
150 role="list"
151 {...props}
152 >
153 {children}
154 </ul>
155 {isHorizontal && (!scrollViewAtStart || !scrollViewAtEnd) && (
156 <div className={css(styles.navScrollButton)}>
157 <Button
158 variant="plain"
159 aria-label={forwardScrollAriaLabel}
160 onClick={this.scrollForward}
161 isDisabled={scrollViewAtEnd}
162 tabIndex={isSidebarOpen ? null : -1}
163 icon={<RhMicronsCaretRightIcon />}
164 />
165 </div>
166 )}
167 </Fragment>
168 )}
169 </PageSidebarContext.Consumer>
170 )}
171 </NavContext.Consumer>
172 );
173 }
174 }
175
176 export { NavList };