You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
carbon/pages/embed/index.js

127 lines
2.8 KiB
JavaScript

// Theirs
import React from 'react'
import Head from 'next/head'
import { withRouter } from 'next/router'
// Ours
5 years ago
import ApiContext from '../../components/ApiContext'
import { StylesheetLink, CodeMirrorLink, MetaTags } from '../../components/Meta'
import Font from '../../components/style/Font'
5 years ago
import Carbon from '../../components/Carbon'
import GlobalHighlights from '../../components/Themes/GlobalHighlights'
import { DEFAULT_CODE, DEFAULT_SETTINGS, THEMES_HASH } from '../../lib/constants'
5 years ago
import { getRouteState } from '../../lib/routing'
const Page = props => (
<React.Fragment>
<Head>
<title>Carbon Embeds</title>
</Head>
<MetaTags />
<StylesheetLink theme={props.theme} />
<CodeMirrorLink />
<Font />
{props.children}
{props.theme === 'a11y-dark' && (
<GlobalHighlights highlights={THEMES_HASH[props.theme].highlights} />
)}
<style jsx global>
{`
html,
body {
margin: 0;
background: transparent;
min-height: 0;
}
`}
</style>
</React.Fragment>
)
class Embed extends React.Component {
static contextType = ApiContext
state = {
...DEFAULT_SETTINGS,
code: DEFAULT_CODE,
mounted: false,
readOnly: true,
}
snippet = {}
async componentDidMount() {
5 years ago
const { queryState } = getRouteState(this.props.router)
6 years ago
this.setState(
{
5 years ago
...this.props.snippet,
...queryState,
copyable: queryState.copy !== false,
readOnly: queryState.readonly !== false,
mounted: true,
6 years ago
},
this.postMessage
)
}
6 years ago
ref = React.createRef()
6 years ago
postMessage = () => {
setTimeout(
() =>
window.top.postMessage(
JSON.stringify({
// Used by embed provider
src: window.location.toString(),
context: 'iframe.resize',
height: this.ref.current.offsetHeight,
}),
'*'
),
0
6 years ago
)
}
updateCode = code => {
this.setState({ code }, this.postMessage)
window.top.postMessage(
{
id: this.state.id ? `carbon:${this.state.id}` : 'carbon',
code,
},
'*'
)
}
6 years ago
render() {
return (
<Page theme={this.state.theme}>
5 years ago
<div hidden={!this.state.mounted}>
<Carbon
5 years ago
key={this.state.mounted}
6 years ago
ref={this.ref}
config={this.state}
readOnly={this.state.readOnly}
copyable={this.state.copyable}
onChange={this.updateCode}
>
{this.state.code}
</Carbon>
5 years ago
</div>
<style jsx global>
{`
.eliminateOnRender,
.twitter-png-fix {
display: none;
}
`}
</style>
</Page>
)
}
}
export default withRouter(Embed)