@onesy/ui-react
Version:
UI for React
400 lines (398 loc) • 16.9 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["ref", "size", "values", "imageGallery", "show", "noName", "IconDocument", "NameProps", "WrapperProps", "EmbedProps", "ImageProps", "AudioProps", "VideoProps", "OtherProps", "ItemsProps", "EmbedItemsProps", "ImageItemsProps", "AudioItemsProps", "VideoItemsProps", "OtherItemsProps", "className", "children"];
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 { is } from '@onesy/utils';
import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react';
import IconMaterialDraft from '@onesy/icons-material-rounded-react/IconMaterialDraftW100';
import LinkElement from '../Link';
import TypeElement from '../Type';
import InteractionElement from '../Interaction';
import LineElement from '../Line/Line';
import AudioPlayerElement from '../AudioPlayer';
import VideoPlayerElement from '../VideoPlayer';
import PortalElement from '../Portal';
import ImageGalleryElement from '../ImageGallery';
import { staticClassName } from '../utils';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const useStyle = styleMethod(theme => ({
root: {},
items: {
maxWidth: '100%',
padding: theme.methods.space.value(0.5, 'px'),
overflow: 'auto hidden'
},
item: {},
wrapper: {
position: 'relative',
flex: '0 0 auto'
},
wrapper_image: {
transition: theme.methods.transitions.make('transform'),
'&:active': {
transform: 'scale(0.94)'
}
},
wrapper_other: {
transition: theme.methods.transitions.make('transform'),
'&:active': {
transform: 'scale(0.94)'
}
},
name: {
wordBreak: 'break-word'
},
image: {
position: 'relative',
backgroundSize: 'cover',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
aspectRatio: '16 / 9'
},
imageWrapper_size_small: {
width: '174px'
},
imageWrapper_size_regular: {
width: '214px'
},
imageWrapper_size_large: {
width: '254px'
},
image_size_small: {
borderRadius: theme.methods.shape.radius.value(1)
},
image_size_regular: {
borderRadius: theme.methods.shape.radius.value(2)
},
image_size_large: {
borderRadius: theme.methods.shape.radius.value(3)
},
audioWrapper_size_small: {
width: '324px'
},
audioWrapper_size_regular: {
width: '340px'
},
audioWrapper_size_large: {
width: '440px'
},
videoWrapper_size_small: {
width: '324px'
},
videoWrapper_size_regular: {
width: '340px'
},
videoWrapper_size_large: {
width: '440px'
},
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: {
width: '74px'
},
otherWrapper_size_regular: {
width: '94px'
},
otherWrapper_size_large: {
width: '114px'
},
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)
},
externalLinks: {
overflow: 'auto hidden'
},
aspectRatioYoutube: {
aspectRatio: '16 / 9',
width: '100%',
maxWidth: '750px'
},
aspectRatioVimeo: {
aspectRatio: '16 / 9',
width: '440px'
},
aspectRatioInstagram: {
aspectRatio: '3 / 5.6',
width: '340px'
},
aspectRatioTiktok: {
aspectRatio: '3 / 6.84',
width: '324px'
},
customEmbed: {
maxWidth: '750px'
}
}), {
name: 'onesy-Medias'
});
const Medias = props_ => {
const theme = useOnesyTheme();
const l = theme.l;
const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyMedias?.props?.default), props_);
const Line = theme?.elements?.Line || LineElement;
const Link = theme?.elements?.Link || LinkElement;
const Type = theme?.elements?.Type || TypeElement;
const Interaction = theme?.elements?.Interaction || InteractionElement;
const AudioPlayer = theme?.elements?.AudioPlayer || AudioPlayerElement;
const VideoPlayer = theme?.elements?.VideoPlayer || VideoPlayerElement;
const Portal = theme?.elements?.Portal || PortalElement;
const ImageGallery = theme?.elements?.ImageGallery || ImageGalleryElement;
const {
ref,
size = 'regular',
values: values_ = [],
imageGallery = true,
show = ['embed', 'image', 'video', 'audio', 'other'],
noName = true,
IconDocument = IconMaterialDraft,
NameProps,
WrapperProps,
EmbedProps,
ImageProps,
AudioProps,
VideoProps,
OtherProps,
ItemsProps,
EmbedItemsProps = {
fullWidth: true
},
ImageItemsProps,
AudioItemsProps,
VideoItemsProps,
OtherItemsProps,
className,
children
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [open, setOpen] = React.useState();
const refs = {
root: React.useRef(undefined)
};
const values = is('array', values_) ? values_ : [values_];
const onOpen = index => {
setOpen({
index
});
};
const onClose = () => {
setOpen(null);
};
const onOpenMedia = media => {
window.open(media.url || media.urlSmall, 'blank_');
};
const items = React.useMemo(() => {
const items_ = {
image: [],
audio: [],
video: [],
other: [],
embed: []
};
values.filter(item => item?.value && !!Object.keys(item.value).length).forEach(item_0 => {
const media_0 = item_0.value;
if (media_0) {
if (media_0.urlEmbed !== undefined) items_.embed.push(item_0);else if (media_0.mime?.startsWith('image')) items_.image.push(item_0);else if (media_0.mime?.startsWith('audio')) items_.audio.push(item_0);else if (media_0.mime?.startsWith('video')) items_.video.push(item_0);else items_.other.push(item_0);
}
});
return items_;
}, [values]);
const getLink = (item_1, index_0) => {
const urlEmbed = item_1?.urlEmbed;
if (!urlEmbed) return null;
const otherProps = _objectSpread({
key: index_0
}, EmbedProps);
const urlEmbedStart = urlEmbed.replace('http://', '').replace('https://', '');
const isPlatform = urlEmbedStart.startsWith('youtu') || urlEmbedStart.startsWith('www.youtu') || urlEmbedStart.startsWith('instagram') || urlEmbedStart.startsWith('www.instagram') || urlEmbedStart.startsWith('tiktok') || urlEmbedStart.startsWith('www.tiktok') || urlEmbedStart.startsWith('vimeo') || urlEmbedStart.startsWith('www.vimeo');
// predefined
if (isPlatform) {
let url;
try {
url = new URL(item_1.urlEmbed);
} catch (error) {
console.log('Invalid embed URL', item_1);
return null;
}
const id = url.pathname?.split('/').filter(Boolean).slice(-1)[0];
if (!url?.hostname || !id) return null;
if (item_1.urlEmbed.includes('youtu.be')) {
return /*#__PURE__*/_jsx("iframe", _objectSpread(_objectSpread({}, otherProps), {}, {
title: item_1.name || '',
src: `https://www.youtube.com/embed/${id}`,
allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share",
allowFullScreen: true,
className: classNames([otherProps?.className, classes.aspectRatioYoutube])
}));
}
if (item_1.urlEmbed.includes('instagram.com')) {
return /*#__PURE__*/_jsx("iframe", _objectSpread(_objectSpread({}, otherProps), {}, {
title: item_1.name || '',
src: `https://www.instagram.com/reel/${id}/embed`,
allowFullScreen: true,
className: classNames([otherProps?.className, classes.aspectRatioInstagram])
}));
}
if (item_1.urlEmbed.includes('tiktok.com')) {
return /*#__PURE__*/_jsx("iframe", _objectSpread(_objectSpread({}, otherProps), {}, {
title: item_1.name || '',
src: `https://www.tiktok.com/embed/v2/${id}`,
allowFullScreen: true,
className: classNames([otherProps?.className, classes.aspectRatioTiktok])
}));
}
if (item_1.urlEmbed.includes('vimeo.com')) {
return /*#__PURE__*/_jsx("iframe", _objectSpread(_objectSpread({}, otherProps), {}, {
title: item_1.name || '',
src: `https://player.vimeo.com/video/${id}`,
allowFullScreen: true,
className: classNames([otherProps?.className, classes.aspectRatioVimeo])
}));
}
return /*#__PURE__*/_jsx(Link, _objectSpread(_objectSpread({}, otherProps), {}, {
href: item_1?.urlEmbed,
target: "blank",
children: item_1.name || item_1.urlEmbed
}));
}
// custom embed code
return /*#__PURE__*/_jsx(Line, _objectSpread(_objectSpread({}, otherProps), {}, {
className: classNames([otherProps?.className, classes.customEmbed]),
fullWidth: true,
dangerouslySetInnerHTML: {
__html: urlEmbed
}
}));
};
const getItem = (version, item_2, index_1) => {
const media_1 = item_2.value;
const name = (item_2?.props?.name || !noName) && /*#__PURE__*/_jsx(Type, _objectSpread(_objectSpread({
version: size === 'large' ? 'l2' : size === 'regular' ? 'l3' : 'l3'
}, NameProps), {}, {
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-name'], NameProps?.className, classes.name]),
children: media_1?.name || l('No name')
}));
if (version === 'embed') {
return getLink(media_1, index_1);
}
if (version === 'image') {
return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({
gap: 1
}, WrapperProps), {}, {
onClick: () => imageGallery && onOpen(index_1),
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-wrapper', 'onesy-Medias-wrapper-image'], WrapperProps?.className, classes.wrapper, classes.wrapper_image, classes[`imageWrapper_size_${size}`]]),
children: [/*#__PURE__*/_jsx(Line, _objectSpread(_objectSpread(_objectSpread({}, ImageProps), item_2?.props), {}, {
fullWidth: true,
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-item', 'onesy-Medias-item-image'], ImageProps?.className, item_2?.props?.className, classes.item, classes.image, classes[`image_size_${size}`]]),
style: _objectSpread({
backgroundImage: `url(${media_1.urlSmall || media_1.url})`
}, item_2?.props?.style),
children: /*#__PURE__*/_jsx(Interaction, {})
}), index_1), name]
}), index_1);
}
if (version === 'audio') {
return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({
gap: 1
}, WrapperProps), {}, {
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-wrapper', 'onesy-Medias-wrapper-audio'], WrapperProps?.className, classes.wrapper, classes[`audioWrapper_size_${size}`]]),
children: [/*#__PURE__*/_jsx(AudioPlayer, _objectSpread(_objectSpread(_objectSpread(_objectSpread({
tonal: true,
color: "primary",
src: media_1.url || media_1.urlSmall,
size: size
}, media_1), AudioProps), item_2?.props), {}, {
fullWidth: true,
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-item', 'onesy-Medias-item-audio'], VideoProps?.className, item_2?.props?.className, classes.item, classes.audio])
}), index_1), name]
}), index_1);
}
if (version === 'video') {
return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({
gap: 1
}, WrapperProps), {}, {
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-wrapper', 'onesy-Medias-wrapper-video'], WrapperProps?.className, classes.wrapper, classes[`videoWrapper_size_${size}`]]),
children: [/*#__PURE__*/_jsx(VideoPlayer, _objectSpread(_objectSpread(_objectSpread(_objectSpread({
tonal: true,
color: "primary",
src: media_1.url || media_1.urlSmall,
size: size
}, media_1), VideoProps), item_2?.props), {}, {
fullWidth: true,
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-item', 'onesy-Medias-item-video'], VideoProps?.className, item_2?.props?.className, classes.item, classes.video])
}), index_1), name]
}), index_1);
}
if (version === 'other') {
return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({
gap: 1
}, WrapperProps), {}, {
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-wrapper', 'onesy-Medias-wrapper-other'], WrapperProps?.className, classes.wrapper, classes[`otherWrapper_size_${size}`]]),
children: [/*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread(_objectSpread({}, OtherProps), item_2?.props), {}, {
align: "center",
justify: "center",
onClick: () => onOpenMedia(media_1),
fullWidth: true,
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-item', 'onesy-Medias-item-other'], OtherProps?.className, item_2?.props?.className, classes.item, classes.other, classes[`other_size_${size}`]]),
children: [/*#__PURE__*/_jsx(Interaction, {}), /*#__PURE__*/_jsx(IconDocument, {
size: size === 'large' ? 44 : size === 'regular' ? 34 : 24
})]
}), index_1), name]
}), index_1);
}
return null;
};
const usable = show.filter(item_3 => !!items[item_3]?.length && !!items[item_3].map((itemMedia, indexItemMedia) => getItem(item_3, itemMedia, indexItemMedia)).filter(Boolean).length);
if (!usable?.length) return null;
return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({
ref: item_4 => {
if (ref) {
if (is('function', ref)) ref(item_4);else ref.current = item_4;
}
refs.root.current = item_4;
},
gap: size === 'large' ? 3 : size === 'regular' ? 2 : 1.5,
fullWidth: true,
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-root', `onesy-Medias-size-${size}`], className, classes.root])
}, other), {}, {
children: [usable.map(item_5 => /*#__PURE__*/_jsx(Line, _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({
gap: size === 'large' ? 2 : size === 'regular' ? 1.5 : 1,
direction: item_5 === 'embed' ? 'column' : 'row',
align: item_5 === 'embed' ? 'center' : 'flex-start',
justify: item_5 === 'embed' ? 'center' : 'flex-start',
fullWidth: true
}, ItemsProps), item_5 === 'embed' && EmbedItemsProps), item_5 === 'image' && ImageItemsProps), item_5 === 'audio' && AudioItemsProps), item_5 === 'video' && VideoItemsProps), item_5 === 'other' && OtherItemsProps), {}, {
className: classNames([staticClassName('Medias', theme) && ['onesy-Medias-items', `onesy-Medias-items-${item_5}`], ItemsProps?.className, item_5 === 'embed' && EmbedItemsProps?.className, item_5 === 'image' && ImageItemsProps?.className, item_5 === 'audio' && AudioItemsProps?.className, item_5 === 'video' && VideoItemsProps?.className, item_5 === 'other' && OtherItemsProps?.className, classes.items]),
children: items[item_5].map((itemMedia_0, indexItemMedia_0) => getItem(item_5, itemMedia_0, indexItemMedia_0))
}), item_5)), refs.root.current && imageGallery && !!items.image?.length && /*#__PURE__*/_jsx(Portal, {
element: refs.root.current?.ownerDocument?.body,
children: /*#__PURE__*/_jsx(ImageGallery, {
open: !!open,
onClose: onClose,
value: open?.index,
items: items.image.map(item_6 => _objectSpread(_objectSpread({}, item_6.value), {}, {
url: item_6.value.url || item_6.value.urlSmall,
urlSmall: item_6.value.urlSmall || item_6.value.url
}))
})
})]
}));
};
Medias.displayName = 'onesy-Medias';
export default Medias;