Skip to content

Commit 55f78eb

Browse files
authored
Allow vertical embedding for embeds (codesandbox#569)
1 parent 834c9dc commit 55f78eb

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

packages/app/src/embed/components/App/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type State = {
4242
forceRefresh: boolean,
4343
expandDevTools: boolean,
4444
runOnClick: boolean,
45+
verticalMode: boolean,
4546
highlightedLines: Array<number>,
4647
};
4748

@@ -65,6 +66,7 @@ export default class App extends React.PureComponent<{}, State> {
6566
forceRefresh,
6667
expandDevTools,
6768
runOnClick,
69+
verticalMode,
6870
} = getSandboxOptions(document.location.href);
6971

7072
this.state = {
@@ -85,6 +87,7 @@ export default class App extends React.PureComponent<{}, State> {
8587
forceRefresh,
8688
expandDevTools,
8789
runOnClick,
90+
verticalMode,
8891
highlightedLines: highlightedLines || [],
8992
};
9093
}
@@ -177,7 +180,13 @@ export default class App extends React.PureComponent<{}, State> {
177180
);
178181
}
179182

180-
const { showEditor, showPreview, isInProjectView, runOnClick } = this.state;
183+
const {
184+
showEditor,
185+
verticalMode,
186+
showPreview,
187+
isInProjectView,
188+
runOnClick,
189+
} = this.state;
181190

182191
return (
183192
<ThemeProvider
@@ -214,6 +223,7 @@ export default class App extends React.PureComponent<{}, State> {
214223
forceRefresh={this.state.forceRefresh}
215224
expandDevTools={this.state.expandDevTools}
216225
runOnClick={runOnClick}
226+
verticalMode={verticalMode}
217227
/>
218228
</Container>
219229
</ThemeProvider>

packages/app/src/embed/components/Content/elements.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ export const Split = styled.div`
2727
display: flex;
2828
flex-direction: column;
2929
position: relative;
30-
width: ${props => (props.show ? `${props.size}%` : '0px')};
31-
max-width: ${props => (props.only ? '100%' : `${props.size}%`)};
32-
min-width: ${props => (props.only ? '100%' : `${props.size}%`)};
30+
${props =>
31+
(props.verticalMode ? 'height: ' : 'width: ') +
32+
(props.show ? `${props.size}%` : '0px')};
33+
34+
${props =>
35+
(props.verticalMode ? 'max-height: ' : 'max-width: ') +
36+
(props.only ? '100%' : `${props.size}%`)};
37+
38+
${props =>
39+
(props.verticalMode ? 'min-height: ' : 'min-width: ') +
40+
(props.only ? '100%' : `${props.size}%`)};
41+
3342
height: 100%;
3443
`;

packages/app/src/embed/components/Content/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Props = {
4545
forceRefresh: boolean,
4646
expandDevTools: boolean,
4747
runOnClick: boolean,
48+
verticalMode: boolean,
4849
};
4950

5051
type State = {
@@ -294,6 +295,7 @@ export default class Content extends React.PureComponent<Props, State> {
294295
isInProjectView,
295296
editorSize,
296297
expandDevTools,
298+
verticalMode,
297299
} = this.props;
298300

299301
const mainModule = isInProjectView
@@ -305,12 +307,13 @@ export default class Content extends React.PureComponent<Props, State> {
305307
const { RunOnClick } = this;
306308

307309
return (
308-
<Container>
310+
<Container style={{ flexDirection: verticalMode ? 'column' : 'row' }}>
309311
{showEditor && (
310312
<Split
311313
show={showEditor}
312314
only={showEditor && !showPreview}
313315
size={editorSize}
316+
verticalMode={verticalMode}
314317
>
315318
<Tabs>
316319
{this.state.tabs.map((module, i) => {
@@ -365,6 +368,7 @@ export default class Content extends React.PureComponent<Props, State> {
365368
show={showPreview}
366369
only={showPreview && !showEditor}
367370
size={100 - editorSize}
371+
verticalMode={verticalMode}
368372
>
369373
{!this.state.running ? (
370374
<RunOnClick />

packages/common/url.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const getSandboxOptions = (url: string) => {
5757
result.enableEslint = url.includes('eslint=1');
5858
result.forceRefresh = url.includes('forcerefresh=1');
5959
result.expandDevTools = url.includes('expanddevtools=1');
60+
result.verticalMode = url.includes('verticallayout=1');
6061
result.runOnClick = url.includes('runonclick=0')
6162
? false
6263
: url.includes('runonclick=1') ||

packages/homepage/content/docs/2-embedding.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ description: "It's possible to embed a sandbox on Medium and other websites."
66

77
## What is an Embed?
88

9-
CodeSandbox has a separate application for the embed. This application is specifically built to be as small as possible. If you replace `s` in the URL of a sandbox to `embed` you have the embed version of the sandbox. Example: https://codesandbox.io/**s**/new => https://codesandbox.io/**embed**/new. Notice that the embed doesn't have all features of the full editor.
9+
CodeSandbox has a separate application for the embed. This application is
10+
specifically built to be as small as possible. If you replace `s` in the URL of
11+
a sandbox to `embed` you have the embed version of the sandbox. Example:
12+
https://codesandbox.io/**s**/new => https://codesandbox.io/**embed**/new. Notice
13+
that the embed doesn't have all features of the full editor.
1014

1115
## Generate a URL
1216

13-
You can generate a URL clicking 'Share' in the header and selecting the options you want to have enabled.
17+
You can generate a URL clicking 'Share' in the header and selecting the options
18+
you want to have enabled.
1419

1520
![Share Button](./images/share-button.png)
1621

1722
## Embed on Medium
1823

19-
You can easily embed on Medium by taking your sandbox URL (like https://codesandbox.io/s/new) and pasting it in a Medium article. It should automatically become an embed after you press enter.
24+
You can easily embed on Medium by taking your sandbox URL (like
25+
https://codesandbox.io/s/new) and pasting it in a Medium article. It should
26+
automatically become an embed after you press enter.
2027

2128
## Embed Options
2229

23-
The options shown in the embed modal are not all options available. We need a new UI for the share model to reflect these options, in the meantime you can find them here.
30+
The options shown in the embed modal are not all options available. We need a
31+
new UI for the share model to reflect these options, in the meantime you can
32+
find them here.
2433

2534
| Option | Description | Values | Default |
2635
| ---------------- | ----------------------------------------------------------------------------- | ------------------------------------ | ------------------------------------ |
@@ -38,6 +47,7 @@ The options shown in the embed modal are not all options available. We need a ne
3847
| `fontsize` | Font size of editor | number (in px) | `14` |
3948
| `highlights` | Which lines to highlight (only works in CodeMirror) | comma separated list of line numbers | |
4049
| `editorsize` | Size in percentage of editor. | number | `50` |
50+
| `verticallayout` | Whether to show the editor and preview vertically. | `0`/`1` | `0` |
4151

4252
## Example Embeds
4353

@@ -51,21 +61,29 @@ This embed is focused on being as light as possible:
5161

5262
Use this code to embed:
5363

54-
`<iframe src="https://codesandbox.io/embed/new?codemirror=1" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`
64+
`<iframe src="https://codesandbox.io/embed/new?codemirror=1" style="width:100%;
65+
height:500px; border:0; border-radius: 4px; overflow:hidden;"
66+
sandbox="allow-modals allow-forms allow-popups allow-scripts
67+
allow-same-origin"></iframe>`
5568

5669
That will give to a result like this:
5770

5871
<iframe src="https://codesandbox.io/embed/new?codemirror=1" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>
5972

6073
### Code Example Embed
6174

62-
You can also use CodeSandbox to show code examples, with highlighted lines. This is only supported with the CodeMirror editor currently:
75+
You can also use CodeSandbox to show code examples, with highlighted lines. This
76+
is only supported with the CodeMirror editor currently:
6377

6478
`https://codesandbox.io/embed/new?codemirror=1&highlights=11,12,13,14`
6579

6680
Use this code to embed:
6781

68-
`<iframe src="https://codesandbox.io/embed/new?codemirror=1&highlights=11,12,13,14" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`
82+
`<iframe
83+
src="https://codesandbox.io/embed/new?codemirror=1&highlights=11,12,13,14"
84+
style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"
85+
sandbox="allow-modals allow-forms allow-popups allow-scripts
86+
allow-same-origin"></iframe>`
6987

7088
That will give to a result like this:
7189

0 commit comments

Comments
 (0)