Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 8e48c72

Browse files
author
Oliver Hausler
authored
Merge pull request #6 from attach-live/master-m4
Master m4
2 parents cc4751c + 77c7edb commit 8e48c72

File tree

3 files changed

+36
-94
lines changed

3 files changed

+36
-94
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ class App extends React.Component {
3838
return (
3939
<>
4040
<div>
41-
<Reactions />
41+
<Reactions item="https://developers.attach.live" />
42+
</div>
43+
<div>
44+
<Reactions properties={{ services: 'videos' }} />
4245
</div>
4346
<div>
4447
<Preview />
@@ -53,18 +56,18 @@ class App extends React.Component {
5356

5457
Embeds API, convenient re-export from `attach-embeds`
5558

56-
## Reactions
57-
58-
React component.
59+
## Components
5960

60-
Props:
61+
All components accept these common props:
6162

62-
- `className`, `style` - forwarded to the iframe
63+
- `className`, `style`
64+
- `properties` - object, set [properties](https://github.com/attach-live/attach-properties) local to the embed
65+
- `item` - same as setting `properties={{ 'attach:item': '...' }}`
6366

64-
## Preview
67+
### Reactions
6568

66-
React component.
69+
Renders the reactions embed.
6770

68-
Props:
71+
### Preview
6972

70-
- `className`, `style` - forwarded to the iframe
73+
Renders the preview embed.

source/index.js

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,55 @@
22
import React from 'react'
33
import uuid from 'nanoid'
44
import * as attach from '@attach/attach-embeds'
5-
6-
// utilities
7-
import units, { basePath } from './units'
85
// endregion
96

107
// region embed
11-
const embed = name => {
12-
const path = units[name].path
13-
const webrtcAttributes = units[name].webrtc
14-
? {
15-
allowusermedia: true,
16-
allowfullscreen: true,
17-
webkitallowfullscreen: true,
18-
mozallowfullscreen: true,
19-
allow: 'camera; microphone; autoplay; fullscreen',
20-
}
21-
: {}
22-
23-
return class Embed extends React.Component {
8+
const embed = name =>
9+
class Embed extends React.Component {
2410
constructor(props) {
2511
super(props)
26-
this.state = { visibility: 'hidden' }
27-
this.unitId = uuid()
28-
this.iframeLoaded = false
29-
this.iframeRef = React.createRef()
30-
this.onLoad = this.onLoad.bind(this)
31-
this.sendMessage = this.sendMessage.bind(this)
12+
this.embedId = props.embedId || uuid()
3213
this.updateProperties = this.updateProperties.bind(this)
14+
this.getMergedProperties = this.getMergedProperties.bind(this)
15+
}
16+
17+
componentDidMount() {
18+
attach.render()
3319
}
3420

3521
componentDidUpdate(prevProps, prevState) {
36-
if (prevProps.properties !== this.props.properties) {
22+
if (prevProps.properties !== this.props.properties || prevProps.item !== this.props.item) {
3723
this.updateProperties()
3824
}
3925
}
4026

41-
sendMessage(type, data) {
42-
const message = {
43-
data,
44-
meta: {
45-
from: 'main',
46-
to: name,
47-
type,
48-
},
27+
getMergedProperties() {
28+
const properties = { ...this.props.properties }
29+
if (this.props.item) {
30+
properties['attach:item'] = this.props.item
4931
}
50-
51-
this.iframeRef.current.contentWindow.postMessage(JSON.stringify(message), basePath)
32+
return properties
5233
}
5334

5435
updateProperties() {
55-
if (this.iframeLoaded) {
56-
this.sendMessage('UPDATE_PROPERTIES', { properties: this.props.properties })
57-
}
58-
}
59-
60-
onLoad() {
61-
this.setState({ visibility: 'visible' })
62-
this.iframeLoaded = true
63-
this.sendMessage('START_EMBED', {
64-
unitId: this.unitId,
65-
instanceId: window.attachInstanceId,
66-
properties: this.props.properties,
67-
})
36+
attach.setEmbedProperties(this.embedId, this.getMergedProperties())
6837
}
6938

7039
render() {
71-
const { className = '', style = {} } = this.props
40+
const { className = '', style = {}, properties = {}, item = '' } = this.props
7241

7342
return (
74-
<iframe
75-
ref={this.iframeRef}
76-
data-unit-id={this.unitId}
77-
className={`attach-embed ${className}`}
78-
onLoad={this.onLoad}
79-
src={path}
80-
style={{
81-
display: 'block',
82-
width: '100%',
83-
height: '100%',
84-
padding: 0,
85-
margin: 0,
86-
border: 'none',
87-
visibility: this.state.visibility,
88-
...style,
89-
}}
90-
{...webrtcAttributes}
43+
<div
44+
data-embed-id={this.embedId}
45+
data-property-item={item}
46+
data-properties={JSON.stringify(properties)}
47+
className={`attach-${name} ${className}`}
48+
style={style}
9149
/>
9250
)
9351
}
9452
}
95-
}
53+
9654
// endregion
9755

9856
// region export

source/units.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)