Page MenuHomec4science

Nav.js
No OneTemporary

File Metadata

Created
Tue, Feb 4, 02:03
const _excluded = ["as", "onSelect", "activeKey", "role", "onKeyDown"];
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import qsa from 'dom-helpers/querySelectorAll';
import * as React from 'react';
import { useContext, useEffect, useRef } from 'react';
import useForceUpdate from '@restart/hooks/useForceUpdate';
import useMergedRefs from '@restart/hooks/useMergedRefs';
import NavContext from './NavContext';
import SelectableContext, { makeEventKey } from './SelectableContext';
import TabContext from './TabContext';
import { dataAttr, dataProp } from './DataKey';
import NavItem from './NavItem';
import { jsx as _jsx } from "react/jsx-runtime";
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
const EVENT_KEY_ATTR = dataAttr('event-key');
const Nav = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'div',
onSelect,
activeKey,
role,
onKeyDown
} = _ref,
props = _objectWithoutPropertiesLoose(_ref, _excluded);
// A ref and forceUpdate for refocus, b/c we only want to trigger when needed
// and don't want to reset the set in the effect
const forceUpdate = useForceUpdate();
const needsRefocusRef = useRef(false);
const parentOnSelect = useContext(SelectableContext);
const tabContext = useContext(TabContext);
let getControlledId, getControllerId;
if (tabContext) {
role = role || 'tablist';
activeKey = tabContext.activeKey; // TODO: do we need to duplicate these?
getControlledId = tabContext.getControlledId;
getControllerId = tabContext.getControllerId;
}
const listNode = useRef(null);
const getNextActiveTab = offset => {
const currentListNode = listNode.current;
if (!currentListNode) return null;
const items = qsa(currentListNode, `[${EVENT_KEY_ATTR}]:not([aria-disabled=true])`);
const activeChild = currentListNode.querySelector('[aria-selected=true]');
if (!activeChild || activeChild !== document.activeElement) return null;
const index = items.indexOf(activeChild);
if (index === -1) return null;
let nextIndex = index + offset;
if (nextIndex >= items.length) nextIndex = 0;
if (nextIndex < 0) nextIndex = items.length - 1;
return items[nextIndex];
};
const handleSelect = (key, event) => {
if (key == null) return;
onSelect == null ? void 0 : onSelect(key, event);
parentOnSelect == null ? void 0 : parentOnSelect(key, event);
};
const handleKeyDown = event => {
onKeyDown == null ? void 0 : onKeyDown(event);
if (!tabContext) {
return;
}
let nextActiveChild;
switch (event.key) {
case 'ArrowLeft':
case 'ArrowUp':
nextActiveChild = getNextActiveTab(-1);
break;
case 'ArrowRight':
case 'ArrowDown':
nextActiveChild = getNextActiveTab(1);
break;
default:
return;
}
if (!nextActiveChild) return;
event.preventDefault();
handleSelect(nextActiveChild.dataset[dataProp('EventKey')] || null, event);
needsRefocusRef.current = true;
forceUpdate();
};
useEffect(() => {
if (listNode.current && needsRefocusRef.current) {
const activeChild = listNode.current.querySelector(`[${EVENT_KEY_ATTR}][aria-selected=true]`);
activeChild == null ? void 0 : activeChild.focus();
}
needsRefocusRef.current = false;
});
const mergedRef = useMergedRefs(ref, listNode);
return /*#__PURE__*/_jsx(SelectableContext.Provider, {
value: handleSelect,
children: /*#__PURE__*/_jsx(NavContext.Provider, {
value: {
role,
// used by NavLink to determine it's role
activeKey: makeEventKey(activeKey),
getControlledId: getControlledId || noop,
getControllerId: getControllerId || noop
},
children: /*#__PURE__*/_jsx(Component, Object.assign({}, props, {
onKeyDown: handleKeyDown,
ref: mergedRef,
role: role
}))
})
});
});
Nav.displayName = 'Nav';
export default Object.assign(Nav, {
Item: NavItem
});

Event Timeline