UNPKG

@atlaskit/side-navigation

Version:

A highly composable side navigation component that supports nested views.

180 lines (175 loc) • 7.18 kB
/* index.tsx generated by @compiled/babel-plugin v3.0.2 */ import _extends from "@babel/runtime/helpers/extends"; import "./index.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; import React, { Fragment, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import ArrowRightIcon from '@atlaskit/icon/core/arrow-right'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { Box } from '@atlaskit/primitives/compiled'; import useChildIdsEffect from '../../common/use-child-ids-effect'; import { ButtonItem } from '../Item/button-item'; import { CustomItem } from '../Item/custom-item'; import { NavigationContent } from '../NavigationContent'; import { ROOT_ID } from '../NestableNavigationContent'; import { NestedContext } from '../NestableNavigationContent/nested-context'; import { useNestedContext } from '../NestableNavigationContent/use-nested-context'; const styles = { iconContainer: "_1e0c1nu9", nestingItem: "_udl3glyw _kqsfglyw _udrwglyw _13qvglyw _9mqr1kw7 _povs1kw7 _uv2d1kw7 _atks1kw7", nestingItemDisabled: "_1c06idpf _udl3glyw" }; // Doesn't extend from ButtonItemProps because it blows ERT up. /** * @deprecated `@atlaskit/side-navigation` is deprecated. Use `@atlaskit/navigation-system` instead. */ /** * NestingItem will render itself differently depending in what context it is rendered in. * When not open - it will render itself as an item. * When open - it will render its children. * * @deprecated `@atlaskit/side-navigation` is deprecated. Use `@atlaskit/navigation-system` instead. */ export const NestingItem = props => { const { children, iconAfter, title, onClick, className, isDisabled, isSelected, id, component, testId, ...rest } = props; const { currentStackId, onNest, onUnNest, backButton: contextualBackButton, stack, childIds, forceShowTopScrollIndicator, activeParentId, goBackButtonRef, isDefaultFocusControl, focusGoBackButton } = useNestedContext(); const [isInteracted, setIsInteracted] = useState(false); const parentItemRef = useRef(null); // To avoid error we need to make sure that the component is wrapped in ForwardRef const isForwardRefCheck = component => { var _component$prototype; if (component !== null && component !== void 0 && (_component$prototype = component.prototype) !== null && _component$prototype !== void 0 && _component$prototype.isReactComponent) { return false; } return (component === null || component === void 0 ? void 0 : component.$$typeof) === Symbol.for('react.forward_ref'); }; const shouldFocus = isDefaultFocusControl && fg('platform-side-navigation-keyboard-focus'); const backButtonRef = shouldFocus ? goBackButtonRef : null; const activeParentRef = shouldFocus ? parentItemRef : null; useEffect(() => { if (shouldFocus) { if (activeParentId === id) { var _parentItemRef$curren; parentItemRef === null || parentItemRef === void 0 ? void 0 : (_parentItemRef$curren = parentItemRef.current) === null || _parentItemRef$curren === void 0 ? void 0 : _parentItemRef$curren.focus(); } if (focusGoBackButton) { var _goBackButtonRef$curr; goBackButtonRef === null || goBackButtonRef === void 0 ? void 0 : (_goBackButtonRef$curr = goBackButtonRef.current) === null || _goBackButtonRef$curr === void 0 ? void 0 : _goBackButtonRef$curr.focus(); } } }, [activeParentId, id, goBackButtonRef, focusGoBackButton, isDefaultFocusControl, shouldFocus]); const backButton = props.overrides && props.overrides.GoBackItem && props.overrides.GoBackItem.render && props.overrides.GoBackItem.render({ onClick: onUnNest, testId: testId && `${testId}--go-back-item`, ref: backButtonRef }) || contextualBackButton; const context = useMemo(() => ({ stack, currentStackId, onNest, onUnNest, backButton, parentId: id, childIds, forceShowTopScrollIndicator, activeParentId, goBackButtonRef, isDefaultFocusControl, focusGoBackButton }), [stack, currentStackId, onNest, onUnNest, backButton, id, childIds, forceShowTopScrollIndicator, activeParentId, goBackButtonRef, isDefaultFocusControl, focusGoBackButton]); useChildIdsEffect(childIds, id); const isNormalClick = e => !(e.button || e.metaKey || e.altKey || e.ctrlKey || e.shiftKey); /** * We want both the on nest handler and the onclick handler to be called. * We create a wrapper function to call both. */ const onClickHandler = useCallback(e => { if (isInteracted) { // We return early if this has been interacted with because its assumed // this will be already exiting - so we don't want to double up the click. return; } // Avoid a nesting transition if a modifier key is detected during click. // @ts-expect-error - Argument of type 'MouseEvent<Element, MouseEvent> | KeyboardEvent<Element>' is not assignable to parameter of type 'MouseEvent' if (isNormalClick(e)) { setIsInteracted(true); onNest(id); } onClick && onClick(e); }, [isInteracted, onClick, onNest, id]); if (currentStackId === id) { return /*#__PURE__*/React.createElement(NestedContext.Provider, { value: context }, stack.length >= 1 && /*#__PURE__*/React.createElement(Box, { paddingBlock: "space.075", paddingInline: "space.100" }, backButton), /*#__PURE__*/React.createElement(NavigationContent, { testId: testId, showTopScrollIndicator: forceShowTopScrollIndicator || stack.length >= 1 }, children)); } if ([ROOT_ID, ...stack].includes(id)) { return children; } const componentProps = { iconAfter: /*#__PURE__*/React.createElement(Fragment, null, iconAfter ? /*#__PURE__*/React.createElement(Box, { xcss: styles.iconContainer, "data-custom-icon": true, as: "span" }, iconAfter) : null, /*#__PURE__*/React.createElement(Box, { "data-right-arrow": true, xcss: styles.iconContainer, as: "span" }, /*#__PURE__*/React.createElement(ArrowRightIcon, { testId: testId && `${testId}--item--right-arrow`, color: "currentColor", label: "" }))), onClick: onClickHandler, isSelected: isSelected, isDisabled: isDisabled, testId: testId && `${testId}--item`, ...rest, children: title }; if (component) { return /*#__PURE__*/React.createElement(CustomItem, _extends({ ref: isForwardRefCheck(component) ? parentItemRef : null }, componentProps, { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop className: ax([styles.nestingItem, isDisabled && styles.nestingItemDisabled, className]) //@ts-ignore TODO Fix legit TypeScript 3.9.6 improved inference error , component: component })); } return /*#__PURE__*/React.createElement(ButtonItem, _extends({ ref: activeParentRef }, componentProps, { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop className: ax([styles.nestingItem, isDisabled && styles.nestingItemDisabled, className]) })); };