-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathEmbed.js
More file actions
193 lines (161 loc) · 5.02 KB
/
Embed.js
File metadata and controls
193 lines (161 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import {
childrenUtils,
createHTMLIframe,
customPropTypes,
getElementType,
getUnhandledProps,
useKeyOnly,
useAutoControlledValue,
} from '../../lib'
import Icon from '../../elements/Icon'
/**
* An embed displays content from other websites like YouTube videos or Google Maps.
*/
const Embed = React.forwardRef(function (props, ref) {
const {
aspectRatio,
autoplay = true,
brandedUI = false,
children,
className,
color = '#444444',
content,
hd = true,
icon,
id,
iframe,
placeholder,
source,
url,
} = props
const [active, setActive] = useAutoControlledValue({
state: props.active,
defaultState: props.defaultActive,
initialState: false,
})
const getSrc = () => {
if (source === 'youtube') {
return [
`//www.youtube.com/embed/${id}`,
'?autohide=true',
`&autoplay=${autoplay}`,
`&color=${encodeURIComponent(color)}`,
`&hq=${hd}`,
'&jsapi=false',
`&modestbranding=${brandedUI}`,
`&rel=${brandedUI ? 0 : 1}`,
].join('')
}
if (source === 'vimeo') {
return [
`//player.vimeo.com/video/${id}`,
'?api=false',
`&autoplay=${autoplay}`,
'&byline=false',
`&color=${encodeURIComponent(color)}`,
'&portrait=false',
'&title=false',
].join('')
}
return url
}
const handleClick = (e) => {
_.invoke(props, 'onClick', e, { ...props, active: true })
if (!active) {
setActive(true)
}
}
const renderEmbed = () => {
if (!active) {
return null
}
if (!childrenUtils.isNil(children)) {
return <div className='embed'>{children}</div>
}
if (!childrenUtils.isNil(content)) {
return <div className='embed'>{content}</div>
}
return (
<div className='embed'>
{createHTMLIframe(childrenUtils.isNil(iframe) ? getSrc() : iframe, {
defaultProps: {
allowFullScreen: false,
frameBorder: 0,
height: '100%',
scrolling: 'no',
src: getSrc(),
title: `Embedded content from ${source}.`,
width: '100%',
},
autoGenerateKey: false,
})}
</div>
)
}
const classes = cx('ui', aspectRatio, useKeyOnly(active, 'active'), 'embed', className)
const rest = getUnhandledProps(Embed, props)
const ElementType = getElementType(Embed, props)
const iconShorthand = icon !== undefined ? icon : 'video play'
return (
<ElementType {...rest} className={classes} onClick={handleClick} ref={ref}>
{Icon.create(iconShorthand, { autoGenerateKey: false })}
{placeholder && <img alt="Placeholder" className='placeholder' src={placeholder} />}
{renderEmbed()}
</ElementType>
)
})
Embed.displayName = 'Embed'
Embed.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** An embed can be active. */
active: PropTypes.bool,
/** An embed can specify an alternative aspect ratio. */
aspectRatio: PropTypes.oneOf(['4:3', '16:9', '21:9']),
/** Setting to true or false will force autoplay. */
autoplay: customPropTypes.every([customPropTypes.demand(['source']), PropTypes.bool]),
/** Whether to show networks branded UI like title cards, or after video calls to action. */
brandedUI: customPropTypes.every([customPropTypes.demand(['source']), PropTypes.bool]),
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** Specifies a default chrome color with Vimeo or YouTube. */
color: customPropTypes.every([customPropTypes.demand(['source']), PropTypes.string]),
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** Initial value of active. */
defaultActive: PropTypes.bool,
/** Whether to prefer HD content. */
hd: customPropTypes.every([customPropTypes.demand(['source']), PropTypes.bool]),
/** Specifies an icon to use with placeholder content. */
icon: customPropTypes.itemShorthand,
/** Specifies an id for source. */
id: customPropTypes.every([customPropTypes.demand(['source']), PropTypes.string]),
/** Shorthand for HTML iframe. */
iframe: customPropTypes.every([
customPropTypes.demand(['source']),
customPropTypes.itemShorthand,
]),
/**
* Called on click.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props and proposed value.
*/
onClick: PropTypes.func,
/** A placeholder image for embed. */
placeholder: PropTypes.string,
/** Specifies a source to use. */
source: customPropTypes.every([
customPropTypes.disallow(['sourceUrl']),
PropTypes.oneOf(['youtube', 'vimeo']),
]),
/** Specifies a url to use for embed. */
url: customPropTypes.every([customPropTypes.disallow(['source']), PropTypes.string]),
}
export default Embed