UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

192 lines (191 loc) • 6.46 kB
import _extends from "@babel/runtime/helpers/extends"; /** * @jsxRuntime classic * @jsx jsx */ import React, { Fragment, memo, useCallback } from 'react'; // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766 import { css, jsx } from '@emotion/react'; import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext'; import Button from '@atlaskit/button/custom-theme-button'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, fireAnalyticsEvent } from '../../analytics'; import { DEVICE_BREAKPOINT_NUMBERS } from '../constants'; import useFocus from '../hooks/use-focus'; const arrowsKeys = new Set(['ArrowUp', 'ArrowDown']); function CategoryList({ categories = [], ...props }) { const { focusedCategoryIndex, setFocusedCategoryIndex, onSelectCategory } = props; return jsx(Fragment, null, categories.map((category, index) => { const categoriesLength = categories === null || categories === void 0 ? void 0 : categories.length; let selectNextCategory; let selectPreviousCategory; if (categoriesLength > 1) { selectNextCategory = () => { if (index !== categoriesLength - 1) { setFocusedCategoryIndex(index + 1); onSelectCategory(categories[index + 1]); } else { setFocusedCategoryIndex(0); onSelectCategory(categories[0]); } return; }; selectPreviousCategory = () => { if (index !== 0) { setFocusedCategoryIndex(index - 1); onSelectCategory(categories[index - 1]); } else { setFocusedCategoryIndex(categoriesLength - 1); onSelectCategory(categories[categoriesLength - 1]); } return; }; } return jsx(CategoryListItem, _extends({ key: category.title, index: index, category: category, focus: focusedCategoryIndex === index // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading }, props, { setFocusedCategoryIndex: setFocusedCategoryIndex, selectPreviousCategory: selectPreviousCategory, selectNextCategory: selectNextCategory })); })); } function CategoryListItem({ category, onSelectCategory, selectedCategory, index, focus, setFocusedCategoryIndex, createAnalyticsEvent, setFocusedItemIndex, setFocusOnSearch, selectPreviousCategory, selectNextCategory }) { const ref = useFocus(focus); const onClick = useCallback(() => { /** * When user double clicks on same category, focus on first item. */ if (selectedCategory === category.name) { setFocusedCategoryIndex(0); } else { setFocusedCategoryIndex(index); } onSelectCategory(category); fireAnalyticsEvent(createAnalyticsEvent)({ payload: { action: ACTION.CLICKED, actionSubject: ACTION_SUBJECT.BUTTON, actionSubjectId: ACTION_SUBJECT_ID.BUTTON_CATEGORY, eventType: EVENT_TYPE.TRACK } }); }, [onSelectCategory, category, index, selectedCategory, setFocusedCategoryIndex, createAnalyticsEvent]); const onFocus = useCallback(() => { if (!focus) { setFocusedCategoryIndex(index); } }, [focus, index, setFocusedCategoryIndex]); const getTheme = useCallback((currentTheme, themeProps) => { const { buttonStyles, ...rest } = currentTheme(themeProps); return { buttonStyles: { ...buttonStyles, textAlign: 'start', marginLeft: "var(--ds-space-025, 2px)", height: '100%', width: '100%', color: category.name !== selectedCategory ? "var(--ds-text, #292A2E)" : "var(--ds-text-selected, #1868DB)", ...(category.name === selectedCategory && { background: "var(--ds-background-selected, #E9F2FE)" }) }, ...rest }; }, [category.name, selectedCategory]); const onTabPress = useCallback(e => { const isShiftPressed = e.shiftKey; if (!isShiftPressed) { // set focus from focused category to first item in it if (setFocusedItemIndex) { setFocusedItemIndex(0); e.preventDefault(); } } else { // jump from first category back to search if (setFocusOnSearch) { setFocusOnSearch(); e.preventDefault(); } } return; }, [setFocusedItemIndex, setFocusOnSearch]); const onArrowPress = useCallback(e => { if (e.key === 'ArrowUp' && selectPreviousCategory) { return selectPreviousCategory(); } if (e.key === 'ArrowDown' && selectNextCategory) { return selectNextCategory(); } }, [selectPreviousCategory, selectNextCategory]); const onKeyDown = useCallback(e => { const isTabPressed = e.key === 'Tab'; const isArrowPressed = arrowsKeys.has(e.key); if (isTabPressed) { return onTabPress(e); } if (isArrowPressed) { return onArrowPress(e); } }, [onTabPress, onArrowPress]); return jsx("div", { css: buttonWrapper, role: "presentation" }, jsx(Button, { appearance: "subtle", isSelected: selectedCategory === category.name, onClick: onClick, onFocus: onFocus, onKeyDown: onKeyDown, theme: getTheme, role: "tab", "aria-selected": selectedCategory === category.name ? 'true' : 'false', "aria-controls": `browse-category-${category.name}-tab`, id: `browse-category--${category.name}-button`, ref: ref, testId: "element-browser-category-item", tabIndex: -1 }, category.title)); } const buttonWrapper = css({ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766 height: `32px`, margin: `${"var(--ds-space-050, 4px)"} ${"var(--ds-space-050, 4px)"} ${"var(--ds-space-050, 4px)"} 0`, // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766 [`@media (min-width: ${DEVICE_BREAKPOINT_NUMBERS.medium}px)`]: { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors -- Ignored via go/DSP-18766 ':not(:last-child)': { marginBottom: 0 } } }); const MemoizedCategoryListWithAnalytics = /*#__PURE__*/memo(withAnalyticsContext({ component: 'CategoryList' })(CategoryList)); export default MemoizedCategoryListWithAnalytics;