UNPKG

@atlaskit/editor-common

Version:

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

108 lines (107 loc) 3.62 kB
import _extends from "@babel/runtime/helpers/extends"; /** @jsx jsx */ import React, { Fragment, memo, useCallback } from 'react'; import { css, jsx } from '@emotion/react'; import { withAnalyticsContext } from '@atlaskit/analytics-next'; import Button from '@atlaskit/button/custom-theme-button'; import { B400, B50, N800 } from '@atlaskit/theme/colors'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, fireAnalyticsEvent } from '../../analytics'; import { DEVICE_BREAKPOINT_NUMBERS, GRID_SIZE } from '../constants'; import useFocus from '../hooks/use-focus'; function CategoryList({ categories = [], ...props }) { const [focusedCategoryIndex, setFocusedCategoryIndex] = React.useState(null); return jsx(Fragment, null, categories.map((category, index) => jsx(CategoryListItem, _extends({ key: category.title, index: index, category: category, focus: focusedCategoryIndex === index, setFocusedCategoryIndex: setFocusedCategoryIndex }, props)))); } function CategoryListItem({ category, onSelectCategory, selectedCategory, index, focus, setFocusedCategoryIndex, createAnalyticsEvent }) { const ref = useFocus(focus); const onClick = useCallback(() => { onSelectCategory(category); /** * When user double clicks on same category, focus on first item. */ if (selectedCategory === category.name) { setFocusedCategoryIndex(0); } else { setFocusedCategoryIndex(index); } 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, ${N800})` : `var(--ds-text-selected, ${B400})`, ...(category.name === selectedCategory && { background: `var(--ds-background-selected, ${B50})` }) }, ...rest }; }, [category.name, selectedCategory]); return jsx("div", { css: buttonWrapper, role: "presentation" }, jsx(Button, { appearance: "subtle", isSelected: selectedCategory === category.name, onClick: onClick, onFocus: onFocus, 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" }, category.title)); } const buttonWrapper = css({ height: `${GRID_SIZE * 4}px`, margin: `${"var(--ds-space-050, 4px)"} ${"var(--ds-space-050, 4px)"} ${"var(--ds-space-050, 4px)"} 0`, [`@media (min-width: ${DEVICE_BREAKPOINT_NUMBERS.medium}px)`]: { ':not(:last-child)': { marginBottom: 0 } } }); const MemoizedCategoryListWithAnalytics = /*#__PURE__*/memo(withAnalyticsContext({ component: 'CategoryList' })(CategoryList)); export default MemoizedCategoryListWithAnalytics;