@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
317 lines (313 loc) • 13.4 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
/**
* @jsxRuntime classic
* @jsx jsx
*/
import React 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 withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
import Button from '@atlaskit/button/new';
import LegacyButton from '@atlaskit/button/standard-button';
import LegacyExpandIcon from '@atlaskit/icon/glyph/chevron-down';
import ChevronDownIcon from '@atlaskit/icon/utility/chevron-down';
import { fg } from '@atlaskit/platform-feature-flags';
import { Box, xcss, Inline } from '@atlaskit/primitives';
import Tooltip from '@atlaskit/tooltip';
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, editorAnalyticsChannel, EVENT_TYPE } from '../../analytics';
import { ColorPalette, DEFAULT_BORDER_COLOR, getSelectedRowAndColumnFromPalette } from '../../ui-color';
import { withReactEditorViewOuterListeners } from '../../ui-react';
import { default as Popup } from '../../ui/Popup';
import { ArrowKeyNavigationProvider } from '../ArrowKeyNavigationProvider';
import { ArrowKeyNavigationType } from '../ArrowKeyNavigationProvider/types';
// helps adjusts position of popup
var colorPickerButtonWrapper = css({
position: 'relative'
});
var colorPickerExpandContainer = xcss({
marginTop: 'space.0',
marginBottom: 'space.0',
marginLeft: 'space.negative.050',
marginRight: 'space.negative.050'
});
var colorPickerExpandContainerVisualRefresh = xcss({
marginTop: 'space.negative.025',
marginRight: 'space.negative.050'
});
var colorPickerButtonStyle = css({
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors
button: {
'&::after': {
border: 'none'
},
'&:hover': {
backgroundColor: "var(--ds-background-selected, #E9F2FF)"
}
}
});
// Control the size of color picker buttons and preview
// TODO: https://product-fabric.atlassian.net/browse/DSP-4134
/* eslint-disable @atlaskit/design-system/ensure-design-token-usage */
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766
var colorPickerWrapper = function colorPickerWrapper() {
return css({
borderRadius: "var(--ds-border-radius, 3px)",
backgroundColor: "var(--ds-surface-overlay, #FFFFFF)",
boxShadow: "var(--ds-shadow-overlay, 0px 8px 12px #091E4226, 0px 0px 1px #091E424f)",
padding: "var(--ds-space-100, 8px)".concat(" 0px")
});
};
/* eslint-enable @atlaskit/design-system/ensure-design-token-usage */
var ColorPaletteWithReactViewListeners = withReactEditorViewOuterListeners(ColorPalette);
var ColorPickerButton = function ColorPickerButton(props) {
var buttonRef = React.useRef(null);
var _React$useState = React.useState(false),
_React$useState2 = _slicedToArray(_React$useState, 2),
isPopupOpen = _React$useState2[0],
setIsPopupOpen = _React$useState2[1];
var _React$useState3 = React.useState(false),
_React$useState4 = _slicedToArray(_React$useState3, 2),
isPopupPositioned = _React$useState4[0],
setIsPopupPositioned = _React$useState4[1];
var _React$useState5 = React.useState(false),
_React$useState6 = _slicedToArray(_React$useState5, 2),
isOpenedByKeyboard = _React$useState6[0],
setIsOpenedByKeyboard = _React$useState6[1];
var togglePopup = function togglePopup() {
setIsPopupOpen(!isPopupOpen);
if (!isPopupOpen) {
setIsOpenedByKeyboard(false);
setIsPopupPositioned(false);
}
};
React.useEffect(function () {
if (props.setDisableParentScroll) {
props.setDisableParentScroll(isPopupOpen);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isPopupOpen]);
var focusButton = function focusButton() {
var _buttonRef$current;
(_buttonRef$current = buttonRef.current) === null || _buttonRef$current === void 0 || _buttonRef$current.focus();
};
var handleEsc = React.useCallback(function () {
setIsOpenedByKeyboard(false);
setIsPopupPositioned(false);
setIsPopupOpen(false);
focusButton();
}, []);
var onPositionCalculated = React.useCallback(function (position) {
setIsPopupPositioned(true);
return position;
}, []);
var onChange = props.onChange,
createAnalyticsEvent = props.createAnalyticsEvent,
colorPalette = props.colorPalette,
placement = props.placement,
skipFocusButtonAfterPick = props.skipFocusButtonAfterPick;
var onColorSelected = React.useCallback(function (color, label) {
setIsOpenedByKeyboard(false);
setIsPopupOpen(false);
setIsPopupPositioned(false);
if (onChange) {
if (createAnalyticsEvent) {
// fire analytics
var payload = {
action: ACTION.UPDATED,
actionSubject: ACTION_SUBJECT.PICKER,
actionSubjectId: ACTION_SUBJECT_ID.PICKER_COLOR,
attributes: {
color: color,
label: label,
placement: placement
},
eventType: EVENT_TYPE.TRACK
};
createAnalyticsEvent(payload).fire(editorAnalyticsChannel);
}
var newPalette = colorPalette.find(function (colorPalette) {
return colorPalette.value === color;
});
newPalette && onChange(newPalette);
}
if (!skipFocusButtonAfterPick) {
focusButton();
}
}, [colorPalette, onChange, createAnalyticsEvent, placement, skipFocusButtonAfterPick]);
var renderPopup = function renderPopup() {
if (!isPopupOpen || !buttonRef.current) {
return;
}
var selectedColor = props.currentColor || null;
var _getSelectedRowAndCol = getSelectedRowAndColumnFromPalette(props.colorPalette, selectedColor, props.cols),
selectedRowIndex = _getSelectedRowAndCol.selectedRowIndex,
selectedColumnIndex = _getSelectedRowAndCol.selectedColumnIndex;
return jsx(Popup, {
target: buttonRef.current,
fitHeight: 350,
fitWidth: 350,
offset: [0, 10],
alignX: props.alignX,
mountTo: props.setDisableParentScroll ? props.mountPoint : undefined,
absoluteOffset: props.absoluteOffset
// Confluence inline comment editor has z-index: 500
// if the toolbar is scrollable, this will be mounted in the root editor
// we need an index of > 500 to display over it
,
zIndex: props.setDisableParentScroll ? 600 : undefined,
ariaLabel: "Color picker popup",
onPositionCalculated: onPositionCalculated
}, jsx("div", {
css: colorPickerWrapper,
"data-test-id": "color-picker-menu"
}, jsx(ArrowKeyNavigationProvider, {
type: ArrowKeyNavigationType.COLOR,
selectedRowIndex: selectedRowIndex,
selectedColumnIndex: selectedColumnIndex,
closeOnTab: true,
handleClose: function handleClose() {
return setIsPopupOpen(false);
},
isOpenedByKeyboard: isOpenedByKeyboard,
isPopupPositioned: isPopupPositioned,
ignoreEscapeKey: props.returnEscToButton
}, jsx(ColorPaletteWithReactViewListeners, {
cols: props.cols,
selectedColor: selectedColor,
onClick: onColorSelected,
handleClickOutside: togglePopup,
handleEscapeKeydown: handleEsc,
paletteOptions: {
palette: props.colorPalette,
hexToPaletteColor: props.hexToPaletteColor,
paletteColorTooltipMessages: props.paletteColorTooltipMessages
}
}))));
};
var title = props.title || '';
var currentColor = props.currentColor && props.hexToPaletteColor ? props.hexToPaletteColor(props.currentColor) : props.currentColor;
var buttonStyle = function buttonStyle() {
var _props$size, _props$size2, _props$size3;
return css({
padding: "var(--ds-space-075, 6px)".concat(" 10px"),
backgroundColor: "var(--ds-background-neutral-subtle, transparent)",
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
height: "".concat(!!((_props$size = props.size) !== null && _props$size !== void 0 && _props$size.height) ? 'inherit' : ''),
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors -- Ignored via go/DSP-18766
'&:before': {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'center',
content: "''",
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
border: "1px solid ".concat(DEFAULT_BORDER_COLOR),
borderRadius: "var(--ds-border-radius, 3px)",
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
backgroundColor: currentColor || 'transparent',
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
width: ((_props$size2 = props.size) === null || _props$size2 === void 0 ? void 0 : _props$size2.width) || '14px',
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
height: ((_props$size3 = props.size) === null || _props$size3 === void 0 ? void 0 : _props$size3.height) || '14px',
padding: 0,
margin: "0px ".concat("var(--ds-space-025, 2px)")
},
'&:hover': {
background: "var(--ds-background-neutral-subtle-hovered, #091E420F)"
}
});
};
var buttonStyleVisualRefresh = function buttonStyleVisualRefresh() {
var _props$size4, _props$size5, _props$size6;
return css({
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
height: "".concat(!!((_props$size4 = props.size) !== null && _props$size4 !== void 0 && _props$size4.height) ? 'inherit' : ''),
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors -- Ignored via go/DSP-18766
'&:before': {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'center',
content: "''",
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
border: "1px solid ".concat(DEFAULT_BORDER_COLOR),
borderRadius: "var(--ds-border-radius, 3px)",
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
backgroundColor: currentColor || 'transparent',
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
width: ((_props$size5 = props.size) === null || _props$size5 === void 0 ? void 0 : _props$size5.width) || '14px',
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
height: ((_props$size6 = props.size) === null || _props$size6 === void 0 ? void 0 : _props$size6.height) || '14px',
marginTop: "var(--ds-space-025, 2px)"
}
});
};
return jsx("div", {
css: colorPickerButtonWrapper
}, jsx(Tooltip, {
content: title,
position: "top"
},
// eslint-disable-next-line @atlaskit/platform/ensure-feature-flag-prefix
fg('platform-visual-refresh-icons') ? jsx("div", {
css: colorPickerButtonStyle
}, jsx(Button, {
appearance: 'subtle',
ref: buttonRef,
"aria-label": title,
"aria-expanded": props.isAriaExpanded ? isPopupOpen : undefined,
spacing: "compact",
onClick: togglePopup,
onKeyDown: function onKeyDown(event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
togglePopup();
setIsOpenedByKeyboard(true);
}
},
"data-selected-color": props.currentColor,
isSelected: isPopupOpen
}, jsx(Inline, {
alignBlock: "start"
}, jsx("span", {
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
css: buttonStyleVisualRefresh
}), jsx(Box, {
xcss: colorPickerExpandContainerVisualRefresh
}, jsx(ChevronDownIcon, {
color: "currentColor",
spacing: "spacious",
LEGACY_fallbackIcon: LegacyExpandIcon,
label: "color-picker-chevron-down"
}))))) : jsx(LegacyButton, {
appearance: 'subtle',
ref: buttonRef,
"aria-label": title,
"aria-expanded": props.isAriaExpanded ? isPopupOpen : undefined,
spacing: "compact",
onClick: togglePopup,
onKeyDown: function onKeyDown(event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
togglePopup();
setIsOpenedByKeyboard(true);
}
}
// eslint-disable-next-line @atlaskit/design-system/no-unsafe-style-overrides
,
css: buttonStyle,
iconAfter: jsx(Box, {
xcss: colorPickerExpandContainer
}, jsx(ChevronDownIcon, {
color: "currentColor",
spacing: "spacious",
LEGACY_fallbackIcon: LegacyExpandIcon,
label: "color-picker-chevron-down"
})),
"data-selected-color": props.currentColor
})), renderPopup());
};
export default withAnalyticsContext({
source: 'ConfigPanel'
})(withAnalyticsEvents()(ColorPickerButton));