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