UNPKG

@atlaskit/editor-common

Version:

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

89 lines (87 loc) 3.49 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import React, { useEffect, useState } from 'react'; import debounce from 'lodash/debounce'; import { fg } from '@atlaskit/platform-feature-flags'; // eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- to be migrated to @atlaskit/primitives/compiled – go/akcss import { Box, xcss } from '@atlaskit/primitives'; var MEDIA_BADGE_VISIBILITY_BREAKPOINT = 200; var containerStyles = xcss({ display: 'flex', position: 'absolute', top: 'space.0', right: 'space.0', // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography lineHeight: "var(--ds-space-200, 16px)", gap: 'space.025', zIndex: 'card', height: 'fit-content', width: 'fit-content', margin: 'space.075' }); // The above styles are used for both editor and renderer, and in renderer the // document body is the main scroll area. This means it overscrolls the primary // toolbar, where the z-index is "2". We have to hack in our own z-index less // than that to ensure our badge appears under the toolbar when scrolled. var hackedZIndexStyles = xcss({ // @ts-ignore zIndex: '1' }); var resizeOffsetStyles = xcss({ right: 'space.150' }); var getBadgeVisible = function getBadgeVisible(width, height) { return width && width < MEDIA_BADGE_VISIBILITY_BREAKPOINT || height && height < MEDIA_BADGE_VISIBILITY_BREAKPOINT ? false : true; }; export var MediaBadges = function MediaBadges(_ref) { var children = _ref.children, mediaElement = _ref.mediaElement, mediaWidth = _ref.mediaWidth, mediaHeight = _ref.mediaHeight, extendedResizeOffset = _ref.extendedResizeOffset, _ref$useMinimumZIndex = _ref.useMinimumZIndex, useMinimumZIndex = _ref$useMinimumZIndex === void 0 ? false : _ref$useMinimumZIndex; var _useState = useState(getBadgeVisible(mediaWidth, mediaHeight)), _useState2 = _slicedToArray(_useState, 2), visible = _useState2[0], setVisible = _useState2[1]; useEffect(function () { var observer = new ResizeObserver(debounce(function (entries) { var _entries = _slicedToArray(entries, 1), entry = _entries[0]; var _entry$contentRect = entry.contentRect, width = _entry$contentRect.width, height = _entry$contentRect.height; setVisible(getBadgeVisible(width, height)); })); if (mediaElement) { // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting observer.observe(mediaElement); } return function () { observer.disconnect(); }; }, [mediaElement]); if (typeof children === 'function') { children = children({ visible: visible }); } // delete this block on cleanup of media-perf-uplift-mutation-fix if (!fg('media-perf-uplift-mutation-fix')) { // becuase it is wrapped in a fragment, React.Children.count(children) will always be 1. // That makes this check a source of late mutations that we don't need. if (!mediaElement || React.Children.count(children) === 0) { return null; } } return /*#__PURE__*/React.createElement(Box, { as: "div", testId: "media-badges", "data-media-badges": "true", contentEditable: false // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , xcss: [containerStyles, useMinimumZIndex && hackedZIndexStyles, extendedResizeOffset && resizeOffsetStyles] }, children); };