UNPKG

@atlaskit/editor-common

Version:

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

114 lines (113 loc) 5.26 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _extends from "@babel/runtime/helpers/extends"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["categories"], _excluded2 = ["buttonStyles"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /** @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(_ref) { var _ref$categories = _ref.categories, categories = _ref$categories === void 0 ? [] : _ref$categories, props = _objectWithoutProperties(_ref, _excluded); var _React$useState = React.useState(null), _React$useState2 = _slicedToArray(_React$useState, 2), focusedCategoryIndex = _React$useState2[0], setFocusedCategoryIndex = _React$useState2[1]; return jsx(Fragment, null, categories.map(function (category, index) { return jsx(CategoryListItem, _extends({ key: category.title, index: index, category: category, focus: focusedCategoryIndex === index, setFocusedCategoryIndex: setFocusedCategoryIndex }, props)); })); } function CategoryListItem(_ref2) { var category = _ref2.category, onSelectCategory = _ref2.onSelectCategory, selectedCategory = _ref2.selectedCategory, index = _ref2.index, focus = _ref2.focus, setFocusedCategoryIndex = _ref2.setFocusedCategoryIndex, createAnalyticsEvent = _ref2.createAnalyticsEvent; var ref = useFocus(focus); var onClick = useCallback(function () { 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]); var onFocus = useCallback(function () { if (!focus) { setFocusedCategoryIndex(index); } }, [focus, index, setFocusedCategoryIndex]); var getTheme = useCallback(function (currentTheme, themeProps) { var _currentTheme = currentTheme(themeProps), buttonStyles = _currentTheme.buttonStyles, rest = _objectWithoutProperties(_currentTheme, _excluded2); return _objectSpread({ buttonStyles: _objectSpread(_objectSpread({}, buttonStyles), {}, { textAlign: 'start', marginLeft: "var(--ds-space-025, 2px)", height: '100%', width: '100%', color: category.name !== selectedCategory ? "var(--ds-text, ".concat(N800, ")") : "var(--ds-text-selected, ".concat(B400, ")") }, category.name === selectedCategory && { background: "var(--ds-background-selected, ".concat(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-".concat(category.name, "-tab"), id: "browse-category--".concat(category.name, "-button"), ref: ref, testId: "element-browser-category-item" }, category.title)); } var buttonWrapper = css(_defineProperty({ height: "".concat(GRID_SIZE * 4, "px"), margin: "var(--ds-space-050, 4px)".concat(" ", "var(--ds-space-050, 4px)", " ", "var(--ds-space-050, 4px)", " 0") }, "@media (min-width: ".concat(DEVICE_BREAKPOINT_NUMBERS.medium, "px)"), { ':not(:last-child)': { marginBottom: 0 } })); var MemoizedCategoryListWithAnalytics = /*#__PURE__*/memo(withAnalyticsContext({ component: 'CategoryList' })(CategoryList)); export default MemoizedCategoryListWithAnalytics;