Debian Codegraph @

node/@patternfly/react-core/dist/js/components/JumpLinks/JumpLinks.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.JumpLinks = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const react_styles_1 = require("@patternfly/react-styles");
8 const jump_links_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/JumpLinks/jump-links"));
9 const sidebar_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Sidebar/sidebar"));
10 const rh_microns_caret_down_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-down-icon'));
11 const c_jump_links__toggle_Display_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_jump_links__toggle_Display'));
12 const Button_1 = require("../Button");
13 const JumpLinksItem_1 = require("./JumpLinksItem");
14 const JumpLinksList_1 = require("./JumpLinksList");
15 const util_1 = require("../../helpers/util");
16 const helpers_1 = require("../../helpers");
17 // Recursively find JumpLinkItems and return an array of all their scrollNodes
18 const getScrollItems = (children, res) => {
19 react_1.Children.forEach(children, (child) => {
20 if (util_1.canUseDOM && document.getElementById && document.querySelector && child.type === JumpLinksItem_1.JumpLinksItem) {
21 const scrollNode = child.props.node || child.props.href;
22 if (typeof scrollNode === 'string') {
23 if (scrollNode.startsWith('#')) {
24 // Allow spaces and other special characters as `id`s to be nicer to consumers
25 // https://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html
26 res.push(document.getElementById(scrollNode.substr(1)));
27 }
28 else {
29 res.push(document.querySelector(scrollNode));
30 }
31 }
32 else if (scrollNode instanceof HTMLElement) {
33 res.push(scrollNode);
34 }
35 }
36 if ([react_1.Fragment, JumpLinksList_1.JumpLinksList, JumpLinksItem_1.JumpLinksItem].includes(child.type)) {
37 getScrollItems(child.props.children, res);
38 }
39 });
40 return res;
41 };
42 function isResponsive(jumpLinks) {
43 // https://github.com/patternfly/patternfly/blob/main/src/patternfly/components/JumpLinks/jump-links.scss#L103
44 return jumpLinks && getComputedStyle(jumpLinks).getPropertyValue(c_jump_links__toggle_Display_1.default.name).includes('block');
45 }
46 const JumpLinks = (_a) => {
47 var { isCentered, isVertical, children, label, 'aria-label': ariaLabel = typeof label === 'string' ? label : null, scrollableRef, scrollableSelector, activeIndex: activeIndexProp = 0, offset = 0, expandable, isExpanded: isExpandedProp = false, alwaysShowLabel = true, toggleAriaLabel = 'Toggle jump links', className, shouldReplaceNavHistory = false, labelId } = _a, props = tslib_1.__rest(_a, ["isCentered", "isVertical", "children", "label", 'aria-label', "scrollableRef", "scrollableSelector", "activeIndex", "offset", "expandable", "isExpanded", "alwaysShowLabel", "toggleAriaLabel", "className", "shouldReplaceNavHistory", "labelId"]);
48 const generatedId = (0, helpers_1.useSSRSafeId)();
49 const hasScrollSpy = Boolean(scrollableRef || scrollableSelector);
50 const [scrollItems, setScrollItems] = (0, react_1.useState)(hasScrollSpy ? getScrollItems(children, []) : []);
51 const [activeIndex, setActiveIndex] = (0, react_1.useState)(activeIndexProp);
52 const [isExpanded, setIsExpanded] = (0, react_1.useState)(isExpandedProp);
53 // Boolean to disable scroll listener from overriding active state of clicked jumplink
54 const isLinkClicked = (0, react_1.useRef)(false);
55 const navRef = (0, react_1.useRef)(undefined);
56 let scrollableElement;
57 if (!label && !ariaLabel) {
58 // eslint-disable-next-line no-console
59 console.warn('JumpLinks: for accessibility reasons, an aria-label should be specified if no label is provided');
60 }
61 if (!label && !toggleAriaLabel && expandable) {
62 // eslint-disable-next-line no-console
63 console.warn('JumpLinks: for accessibility reasons, a toggleAriaLabel should be specified if no label is provided');
64 }
65 const getScrollableElement = () => {
66 if (scrollableRef) {
67 if (scrollableRef instanceof HTMLElement) {
68 return scrollableRef;
69 }
70 else if (typeof scrollableRef === 'function') {
71 return scrollableRef();
72 }
73 return scrollableRef.current;
74 }
75 else if (scrollableSelector) {
76 return document.querySelector(scrollableSelector);
77 }
78 };
79 const scrollSpy = (0, react_1.useCallback)(() => {
80 if (!util_1.canUseDOM || !hasScrollSpy || !(scrollableElement instanceof HTMLElement)) {
81 return;
82 }
83 if (isLinkClicked.current) {
84 isLinkClicked.current = false;
85 return;
86 }
87 const scrollPosition = Math.ceil(scrollableElement.scrollTop + offset);
88 window.requestAnimationFrame(() => {
89 let newScrollItems = scrollItems;
90 // Items might have rendered after this component or offsetTop values may need
91 // to be updated. Do a quick refresh.
92 const requiresRefresh = newScrollItems.every((e) => !(e === null || e === void 0 ? void 0 : e.offsetTop)) || !newScrollItems[0] || newScrollItems.includes(null);
93 if (requiresRefresh) {
94 newScrollItems = getScrollItems(children, []);
95 setScrollItems(newScrollItems);
96 }
97 const scrollElements = newScrollItems
98 .map((e, index) => ({
99 y: e ? e.offsetTop : null,
100 index
101 }))
102 .filter(({ y }) => y !== null)
103 .sort((e1, e2) => e2.y - e1.y);
104 for (const { y, index } of scrollElements) {
105 if (scrollPosition >= y) {
106 return setActiveIndex(index);
107 }
108 }
109 });
110 }, [scrollItems, hasScrollSpy, scrollableElement, offset]);
111 (0, react_1.useEffect)(() => {
112 scrollableElement = getScrollableElement();
113 if (!(scrollableElement instanceof HTMLElement)) {
114 return;
115 }
116 scrollableElement.addEventListener('scroll', scrollSpy);
117 return () => scrollableElement.removeEventListener('scroll', scrollSpy);
118 }, [scrollableElement, scrollSpy, getScrollableElement]);
119 (0, react_1.useEffect)(() => {
120 scrollSpy();
121 }, []);
122 let jumpLinkIndex = 0;
123 const cloneChildren = (children) => !hasScrollSpy
124 ? children
125 : react_1.Children.map(children, (child) => {
126 if (child.type === JumpLinksItem_1.JumpLinksItem) {
127 const { onClick: onClickProp, isActive: isActiveProp } = child.props;
128 const itemIndex = jumpLinkIndex++;
129 const scrollItem = scrollItems[itemIndex];
130 return (0, react_1.cloneElement)(child, {
131 onClick(ev) {
132 isLinkClicked.current = true;
133 // Items might have rendered after this component. Do a quick refresh.
134 let newScrollItems;
135 if (!scrollItem) {
136 newScrollItems = getScrollItems(children, []);
137 setScrollItems(newScrollItems);
138 }
139 const newScrollItem = scrollItem || newScrollItems[itemIndex];
140 if (newScrollItem) {
141 // we have to support scrolling to an offset due to sticky sidebar
142 const scrollableElement = getScrollableElement();
143 if (scrollableElement instanceof HTMLElement) {
144 if (isResponsive(navRef.current)) {
145 // Remove class immediately so we can get collapsed height
146 if (navRef.current) {
147 navRef.current.classList.remove(jump_links_1.default.modifiers.expanded);
148 }
149 let stickyParent = navRef.current && navRef.current.parentElement;
150 while (stickyParent && !stickyParent.classList.contains(sidebar_1.default.modifiers.sticky)) {
151 stickyParent = stickyParent.parentElement;
152 }
153 setIsExpanded(false);
154 if (stickyParent) {
155 offset += stickyParent.scrollHeight;
156 }
157 }
158 scrollableElement.scrollTo(0, newScrollItem.offsetTop - offset);
159 }
160 newScrollItem.focus();
161 if (shouldReplaceNavHistory) {
162 window.history.replaceState('', '', ev.currentTarget.href);
163 }
164 else {
165 window.history.pushState('', '', ev.currentTarget.href);
166 }
167 ev.preventDefault();
168 setActiveIndex(itemIndex);
169 }
170 if (onClickProp) {
171 onClickProp(ev);
172 }
173 },
174 isActive: isActiveProp || activeIndex === itemIndex,
175 children: cloneChildren(child.props.children)
176 });
177 }
178 else if (child.type === react_1.Fragment) {
179 return cloneChildren(child.props.children);
180 }
181 else if (child.type === JumpLinksList_1.JumpLinksList) {
182 return (0, react_1.cloneElement)(child, { children: cloneChildren(child.props.children) });
183 }
184 return child;
185 });
186 const id = labelId !== null && labelId !== void 0 ? labelId : generatedId;
187 const hasAriaLabelledBy = expandable || (label && alwaysShowLabel);
188 const computedAriaLabel = hasAriaLabelledBy ? null : ariaLabel;
189 const computedAriaLabelledBy = hasAriaLabelledBy ? id : null;
190 return ((0, jsx_runtime_1.jsx)("nav", Object.assign({ className: (0, react_styles_1.css)(jump_links_1.default.jumpLinks, isCentered && jump_links_1.default.modifiers.center, isVertical && jump_links_1.default.modifiers.vertical, (0, util_1.formatBreakpointMods)(expandable, jump_links_1.default), isExpanded && jump_links_1.default.modifiers.expanded, className), "aria-label": computedAriaLabel, ref: navRef, "aria-labelledby": computedAriaLabelledBy }, props, { children: (0, jsx_runtime_1.jsxs)("div", { className: jump_links_1.default.jumpLinksMain, children: [(0, jsx_runtime_1.jsxs)("div", { className: (0, react_styles_1.css)(`${jump_links_1.default.jumpLinks}__header`), children: [expandable && ((0, jsx_runtime_1.jsx)("div", { className: jump_links_1.default.jumpLinksToggle, children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "plain", onClick: () => setIsExpanded(!isExpanded), "aria-label": label ? null : toggleAriaLabel, "aria-expanded": isExpanded, icon: (0, jsx_runtime_1.jsx)("span", { className: jump_links_1.default.jumpLinksToggleIcon, children: (0, jsx_runtime_1.jsx)(rh_microns_caret_down_icon_1.default, {}) }), id: id, children: label && label }) })), label && alwaysShowLabel && !expandable && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(jump_links_1.default.jumpLinksLabel), id: id, children: label }))] }), (0, jsx_runtime_1.jsx)("ul", { "aria-label": computedAriaLabel, "aria-labelledby": computedAriaLabelledBy, className: jump_links_1.default.jumpLinksList, role: "list", children: cloneChildren(children) })] }) })));
191 };
192 exports.JumpLinks = JumpLinks;
193 exports.JumpLinks.displayName = 'JumpLinks';
194 //# sourceMappingURL=JumpLinks.js.map