UNPKG

@fluentui/react-northstar

Version:
192 lines (191 loc) 6.62 kB
import _invoke from "lodash/invoke"; import _isNil from "lodash/isNil"; import { embedBehavior } from '@fluentui/accessibility'; import * as PropTypes from 'prop-types'; import * as React from 'react'; import * as customPropTypes from '@fluentui/react-proptypes'; import { createShorthandFactory, commonPropTypes } from '../../utils'; import { Image } from '../Image/Image'; import { Video } from '../Video/Video'; import { Box } from '../Box/Box'; import { Ref } from '@fluentui/react-component-ref'; import { getElementType, useUnhandledProps, useFluentContext, useAutoControlled, useAccessibility, useTelemetry, useStyles } from '@fluentui/react-bindings'; export var embedClassName = 'ui-embed'; export var embedSlotClassNames = { control: embedClassName + "__control" }; /** * An Embed displays content from external websites, like a post from external social network. * * @accessibility * A `placeholder` slot represents an [`Image`](/components/image/definition) component, please follow recommendations from its * accessibility section. */ export var Embed = /*#__PURE__*/function () { var Embed = /*#__PURE__*/React.forwardRef(function (props, ref) { var context = useFluentContext(); var _useTelemetry = useTelemetry(Embed.displayName, context.telemetry), setStart = _useTelemetry.setStart, setEnd = _useTelemetry.setEnd; setStart(); var alt = props.alt, title = props.title, control = props.control, iframe = props.iframe, placeholder = props.placeholder, video = props.video, variables = props.variables, styles = props.styles, className = props.className, design = props.design; var ElementType = getElementType(props); var unhandledProps = useUnhandledProps(Embed.handledProps, props); var getA11yProps = useAccessibility(props.accessibility, { debugName: Embed.displayName, actionHandlers: { performClick: function performClick(event) { return handleClick(event); } }, mapPropsToBehavior: function mapPropsToBehavior() { return { alt: alt, title: title }; }, rtl: context.rtl }); var _useAutoControlled = useAutoControlled({ defaultValue: props.defaultActive, value: props.active, initialValue: false }), active = _useAutoControlled[0], setACtive = _useAutoControlled[1]; var _React$useState = React.useState(false), iframeLoaded = _React$useState[0], setIframeLoaded = _React$useState[1]; var frameRef = React.useRef(); var handleClick = function handleClick(e) { e.stopPropagation(); e.preventDefault(); var iframeNil = _isNil(props.iframe); var newActive = !active; if (iframeNil || !iframeNil && newActive) { setACtive(newActive); _invoke(props, 'onActiveChange', e, Object.assign({}, props, { active: newActive })); } _invoke(props, 'onClick', e, Object.assign({}, props, { active: newActive })); }; var handleFrameOverrides = function handleFrameOverrides(predefinedProps) { return { onLoad: function onLoad(e) { _invoke(predefinedProps, 'onLoad', e); setIframeLoaded(true); frameRef.current.contentWindow.focus(); } }; }; var _useStyles = useStyles(Embed.displayName, { className: embedClassName, mapPropsToStyles: function mapPropsToStyles() { return { iframeLoaded: iframeLoaded, active: active }; }, mapPropsToInlineStyles: function mapPropsToInlineStyles() { return { className: className, design: design, styles: styles, variables: variables }; }, rtl: context.rtl }), classes = _useStyles.classes, resolvedStyles = _useStyles.styles; var placeholderElement = placeholder ? /*#__PURE__*/React.createElement(Image, { src: placeholder, styles: resolvedStyles.image, variables: { width: variables.width, height: variables.height } }) : null; var hasIframe = !_isNil(iframe); var hasVideo = !_isNil(video); var controlVisible = !active || hasVideo; var placeholderVisible = !active || hasIframe && active && !iframeLoaded; var element = /*#__PURE__*/React.createElement(ElementType, getA11yProps('root', Object.assign({ className: classes.root, onClick: handleClick, ref: ref }, unhandledProps)), active && /*#__PURE__*/React.createElement(React.Fragment, null, Video.create(video, { defaultProps: function defaultProps() { return getA11yProps('video', { autoPlay: true, controls: false, loop: true, muted: true, poster: placeholder, styles: resolvedStyles.video, variables: { width: variables.width, height: variables.height } }); } }), iframe && /*#__PURE__*/React.createElement(Ref, { innerRef: frameRef }, Box.create(iframe, { defaultProps: function defaultProps() { return getA11yProps('iframe', { as: 'iframe', styles: resolvedStyles.iframe }); }, overrideProps: handleFrameOverrides }))), placeholderVisible && placeholderElement, controlVisible && Box.create(control, { defaultProps: function defaultProps() { return getA11yProps('control', { className: embedSlotClassNames.control, styles: resolvedStyles.control }); } })); setEnd(); return element; }); Embed.displayName = 'Embed'; Embed.propTypes = Object.assign({}, commonPropTypes.createCommon({ children: false, content: false }), { active: PropTypes.bool, defaultActive: PropTypes.bool, control: customPropTypes.itemShorthand, iframe: customPropTypes.every([customPropTypes.disallow(['video']), customPropTypes.itemShorthand]), onActiveChange: PropTypes.func, onClick: PropTypes.func, placeholder: PropTypes.string, video: customPropTypes.every([customPropTypes.disallow(['iframe']), customPropTypes.itemShorthand]) }); Embed.defaultProps = { as: 'span', accessibility: embedBehavior, control: {}, variables: {} }; Embed.handledProps = Object.keys(Embed.propTypes); Embed.create = createShorthandFactory({ Component: Embed }); return Embed; }(); //# sourceMappingURL=Embed.js.map