@atlaskit/editor-plugin-expand
Version:
Expand plugin for @atlaskit/editor-core
107 lines (105 loc) • 3.96 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
/**
* @jsxRuntime classic
* @jsx jsx
*/
import React, { useCallback } from 'react';
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled, @typescript-eslint/consistent-type-imports
import { jsx } from '@emotion/react';
import Button from '@atlaskit/button/custom-theme-button';
import { expandClassNames } from '@atlaskit/editor-common/styles';
import { expandLayoutWrapperStyle, ExpandLayoutWrapperWithRef, expandMessages } from '@atlaskit/editor-common/ui';
import { akEditorSwoopCubicBezier } from '@atlaskit/editor-shared-styles';
import ChevronRightIcon from '@atlaskit/icon/core/chevron-right';
import Tooltip from '@atlaskit/tooltip';
function withTooltip(Component) {
return function WithTooltip(props) {
return (
// @ts-ignore: [PIT-1685] Fails in post-office due to backwards incompatibility issue with React 18
jsx(Tooltip, {
content: props.label,
position: "top",
tag: ExpandLayoutWrapperWithRef
}, jsx(Component
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
, props))
);
};
}
export const ExpandButtonInner = props => {
const useTheme = useCallback(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(currentTheme, themeProps) => {
const {
buttonStyles,
...rest
} = currentTheme(themeProps);
return {
buttonStyles: {
...buttonStyles,
height: '100%',
'& svg': {
transform: props.expanded ? 'transform: rotate(90deg);' : 'transform: rotate(0deg);',
transition: `transform 0.2s ${akEditorSwoopCubicBezier};`
}
},
...rest
};
}, [props]);
const {
intl,
expanded
} = props;
const label = expanded ? expandMessages.collapseNode : expandMessages.expandNode;
const labelIntl = intl ? intl.formatMessage(label) : label.defaultMessage;
return jsx(Button, {
appearance: "subtle"
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
,
className: expandClassNames.iconContainer,
iconBefore: jsx(ChevronRightIcon, {
label: '',
size: "small"
}),
shouldFitContainer: true,
theme: useTheme,
"aria-label": labelIntl,
"aria-expanded": props.expanded,
isDisabled: !props.allowInteractiveExpand
});
};
const ButtonWithTooltip = withTooltip(ExpandButtonInner);
const ButtonWithoutTooltip = ExpandButtonInner;
export const ExpandButton = props => {
const {
expanded,
intl
} = props;
const message = expanded ? expandMessages.collapseNode : expandMessages.expandNode;
const label = intl && intl.formatMessage(message) || message.defaultMessage;
// check to ensure device supports any-hover
const supportsAnyHover = !!window.matchMedia ? window.matchMedia('(any-hover: hover)').matches !== window.matchMedia('(any-hover: none)').matches : false;
const hoverEventCheck = supportsAnyHover ? window.matchMedia('(any-hover: hover)').matches : true;
// hoverEventCheck is to disable tooltips for mobile to prevent incorrect hover state causing issues on iOS
if (props.allowInteractiveExpand && hoverEventCheck) {
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
return jsx(ButtonWithTooltip, _extends({
label: label
}, props));
}
return (
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage, @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766
jsx("div", {
css: expandLayoutWrapperStyle
}, jsx(ButtonWithoutTooltip, _extends({
label: label
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
}, props)))
);
};