UNPKG

@onesy/ui-react

Version:
143 lines 6.96 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["value", "size", "name", "IconDocument", "MediaProps", "ImageProps", "AudioProps", "VideoProps", "OtherProps", "className"]; 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; } import React from 'react'; import { cleanValue, is } from '@onesy/utils'; import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react'; import IconMaterialDraft from '@onesy/icons-material-rounded-react/IconMaterialDraftW100'; import AudioPlayerElement from '../AudioPlayer'; import ImageElement from '../Image'; import InteractionElement from '../Interaction'; import LineElement from '../Line'; import SectionElement from '../Section/Section'; import TypeElement from '../Type'; import VideoPlayerElement from '../VideoPlayer'; import { staticClassName } from '../utils'; const useStyle = styleMethod(theme => ({ root: {}, name: { wordBreak: 'break-word' }, other: { position: 'relative', aspectRatio: '3 / 4', cursor: 'pointer', userSelect: 'none', background: theme.palette.background.secondary.tertiary, boxShadow: theme.shadows.values.default[1] }, otherWrapper_size_small: { maxWidth: '74px', width: '90vw' }, otherWrapper_size_regular: { maxWidth: '94px', width: '90vw' }, otherWrapper_size_large: { maxWidth: '114px', width: '90vw' }, other_size_small: { borderRadius: theme.methods.shape.radius.value(1) }, other_size_regular: { borderRadius: theme.methods.shape.radius.value(2) }, other_size_large: { borderRadius: theme.methods.shape.radius.value(3) } }), { name: 'onesy-SectionMedia' }); const Element = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useOnesyTheme(); const l = theme.l; const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesySectionMedia?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Section = React.useMemo(() => theme?.elements?.Section || SectionElement, [theme]); const Image = React.useMemo(() => theme?.elements?.Image || ImageElement, [theme]); const AudioPlayer = React.useMemo(() => theme?.elements?.AudioPlayer || AudioPlayerElement, [theme]); const VideoPlayer = React.useMemo(() => theme?.elements?.VideoPlayer || VideoPlayerElement, [theme]); const Interaction = React.useMemo(() => theme?.elements?.Interaction || InteractionElement, [theme]); const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]); const { value, size = 'regular', name, IconDocument = IconMaterialDraft, MediaProps, ImageProps, AudioProps, VideoProps, OtherProps, className } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const refs = { root: React.useRef(undefined) }; const onOpenMedia = React.useCallback(media => { window.open(media.url || media.urlSmall, 'blank_'); }, []); let element; if (value) { const mime = value.mime; if (mime?.includes('image')) element = /*#__PURE__*/React.createElement(Image, _extends({ src: value?.url || value?.urlSmall || (is('string', value) ? value : ''), maxWidth: "rg", fullWidth: true }, MediaProps, ImageProps, { className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-item', 'onesy-SectionMedia-item-image'], MediaProps?.className, ImageProps?.className]) }));else if (mime?.includes('audio')) element = /*#__PURE__*/React.createElement(AudioPlayer, _extends({ size: size }, value, MediaProps, AudioProps, { className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-item', 'onesy-SectionMedia-item-audio'], MediaProps?.className, AudioProps?.className]) }));else if (mime?.includes('video')) element = /*#__PURE__*/React.createElement(VideoPlayer, _extends({ size: size }, value, MediaProps, VideoProps, { className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-item', 'onesy-SectionMedia-item-video'], MediaProps?.className, VideoProps?.className]) }));else element = /*#__PURE__*/React.createElement(Line, { gap: 1, className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-wrapper', 'onesy-SectionMedia-wrapper-other'], classes.wrapper, classes[`otherWrapper_size_${size}`]]) }, /*#__PURE__*/React.createElement(Line, _extends({ align: "center", justify: "center", onClick: () => onOpenMedia(value), fullWidth: true }, MediaProps, OtherProps, { className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-item', 'onesy-SectionMedia-item-other'], MediaProps?.className, OtherProps?.className, classes.other, classes[`other_size_${size}`]]) }), /*#__PURE__*/React.createElement(Interaction, null), /*#__PURE__*/React.createElement(IconDocument, { size: size === 'large' ? 44 : size === 'regular' ? 34 : 24 }))); } return /*#__PURE__*/React.createElement(Section, _extends({ ref: item => { if (ref) { if (is('function', ref)) ref(item);else ref.current = item; } refs.root.current = item; }, maxWidth: "small", align: "center", className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-root', `onesy-SectionMedia-size-${size}`], className, classes.root]) }, other), /*#__PURE__*/React.createElement(Line, { align: "center", fullWidth: true }, element, name && /*#__PURE__*/React.createElement(Type, { version: size === 'large' ? 't1' : size === 'regular' ? 't2' : 't3', align: "center", fullWidth: true, className: classNames([staticClassName('SectionMedia', theme) && ['onesy-SectionMedia-name'], classes.name]) }, cleanValue(value?.name || l('No name'), { capitalize: true })))); }); Element.displayName = 'onesy-SectionMedia'; export default Element;