UNPKG

@atlaskit/renderer

Version:
161 lines 5.33 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import React, { PureComponent } from 'react'; import { defaultImageCardDimensions } from '@atlaskit/media-card'; import { FilmstripView } from '@atlaskit/media-filmstrip'; import { fg } from '@atlaskit/platform-feature-flags'; import { VcMediaWrapperProps } from '@atlaskit/react-ufo/vc-media'; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components export default class MediaGroup extends PureComponent { constructor(...args) { super(...args); _defineProperty(this, "state", { animate: false, offset: 0 }); _defineProperty(this, "handleSize", ({ offset }) => this.setState({ offset })); _defineProperty(this, "handleScroll", ({ animate, offset }) => this.setState({ animate, offset })); _defineProperty(this, "onMediaClick", (cardClickHandler, child, surroundingItems) => // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any (event, analyticsEvent) => { const surroundings = { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion collectionName: child.props.collection, list: surroundingItems }; cardClickHandler(event, surroundings, analyticsEvent); }); } render() { const numChildren = React.Children.count(this.props.children); let content; if (numChildren === 1) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any const card = React.Children.toArray(this.props.children)[0]; switch (card.props.type) { case 'file': content = this.renderSingleFile(card); break; case 'link': content = null; break; default: content = this.renderSingleLink(card); } } else { content = this.renderStrip(); } return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", _extends({ className: "MediaGroup" }, VcMediaWrapperProps), content)); } renderSingleFile(child) { return /*#__PURE__*/React.cloneElement(child, { // the media group component renders in crop mode in editor thus this enables consistency // also crop is much easier to make consistent across SSR and hydration resizeMode: fg('media-perf-uplift-mutation-fix') ? 'crop' : 'stretchy-fit', cardDimensions: defaultImageCardDimensions, useInlinePlayer: false, featureFlags: this.props.featureFlags, enableDownloadButton: this.props.enableDownloadButton }); } renderSingleLink(child) { return /*#__PURE__*/React.cloneElement(child, { appearance: 'auto', featureFlags: this.props.featureFlags }); } cloneFileCard(child, surroundingItems) { const cardClickHandler = this.props && this.props.eventHandlers && this.props.eventHandlers.media && this.props.eventHandlers.media.onClick; const onClick = cardClickHandler ? this.onMediaClick(cardClickHandler, child, surroundingItems) : undefined; return /*#__PURE__*/React.cloneElement(child, { useInlinePlayer: false, eventHandlers: { ...child.props.eventHandlers, media: { onClick } }, featureFlags: this.props.featureFlags, enableDownloadButton: this.props.enableDownloadButton }); } renderStrip() { const { children } = this.props; const { animate, offset } = this.state; const childIdentifiers = React.Children.map(children, child => { if ( /*#__PURE__*/React.isValidElement(child)) { return this.mapMediaPropsToIdentifier(child.props); } }); const surroundingItems = (childIdentifiers || []).filter(identifier => !!identifier); return /*#__PURE__*/React.createElement(FilmstripView, { animate: animate, offset: offset, onSize: this.handleSize, onScroll: this.handleScroll }, React.Children.map(children, rawChild => { const child = rawChild; switch (child.props.type) { case 'file': return this.cloneFileCard(child, surroundingItems); case 'link': return null; default: return /*#__PURE__*/React.cloneElement(child, { featureFlags: this.props.featureFlags, enableDownloadButton: this.props.enableDownloadButton }); } })); } mapMediaPropsToIdentifier({ id, type, occurrenceKey, collection }) { switch (type) { case 'file': return { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion id: id, mediaItemType: type, occurrenceKey, collectionName: collection }; case 'link': return undefined; case 'external': return { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion id: id, mediaItemType: 'file', occurrenceKey, collectionName: collection }; } } }