Skip to content

Commit 30a9ff9

Browse files
author
Breno Calazans
authored
Merge pull request #4 from vtex-apps/feature/product
Add sandbox.product
2 parents ae46174 + 6badb71 commit 30a9ff9

File tree

8 files changed

+93
-45
lines changed

8 files changed

+93
-45
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Added
10+
- `sandbox.product` a Sandbox that receives the Product context as props. That way it's possible to use sandbox inside a `flex-layout` and still have the product context.
911

1012
## [0.1.0] - 2019-07-05
1113
### Added

manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"react": "3.x"
1212
},
1313
"mustUpdateAt": "2019-04-02",
14+
"dependencies": {
15+
"vtex.product-context": "0.x"
16+
},
1417
"scripts": {
1518
"postreleasy": "vtex publish --verbose"
1619
},

react/Sandbox.tsx

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import { NoSSR, canUseDOM } from 'vtex.render-runtime'
33
import stringify from 'safe-json-stringify'
4+
import { schema, Props } from './modules/schema'
45

56
// Avoid breaking SSR by loading this only in the client side
67
let iFrameResizer: any = null
@@ -20,15 +21,6 @@ interface IframeOptions {
2021
styles: StyleContainer[]
2122
}
2223

23-
interface Props {
24-
initialContent?: string
25-
width?: string
26-
height?: string
27-
allowCookies?: boolean
28-
allowStyles?: boolean
29-
hidden?: boolean
30-
}
31-
3224
// Create objects representing styles, either with href or the actual rules, to be passed to the iframe.
3325
function getExistingStyles() {
3426
const styles: StyleContainer[] = []
@@ -101,42 +93,11 @@ function initIframe (options: IframeOptions) {
10193
})
10294
}
10395

104-
export default class Sandbox extends Component<Props> {
96+
class Sandbox extends Component<Props> {
10597
public iframeRef: HTMLIFrameElement | null = null
10698

107-
public schema = {
108-
title: 'admin/editor.sandbox.title',
109-
description: 'admin/editor.sandbox.description',
110-
type: 'object',
111-
properties: {
112-
width: {
113-
title: 'admin/editor.sandbox.width.title',
114-
description: 'admin/editor.sandbox.width.description',
115-
type: 'string',
116-
default: null,
117-
},
118-
height: {
119-
title: 'admin/editor.sandbox.height.title',
120-
description: 'admin/editor.sandbox.height.description',
121-
type: 'string',
122-
default: null,
123-
},
124-
initialContent: {
125-
title: 'admin/editor.sandbox.initialContent.title',
126-
description: 'admin/editor.sandbox.initialContent.description',
127-
type: 'string',
128-
default: null,
129-
},
130-
allowCookies: {
131-
title: 'admin/editor.sandbox.allowCookies.title',
132-
description: 'admin/editor.sandbox.allowCookies.description',
133-
type: 'boolean',
134-
default: false,
135-
},
136-
},
137-
}
138-
13999
private injectedDocument: string = ''
100+
static schema: object;
140101

141102
public constructor(props: Props) {
142103
super(props)
@@ -207,3 +168,7 @@ export default class Sandbox extends Component<Props> {
207168
}
208169
}
209170
}
171+
172+
Sandbox.schema = schema
173+
174+
export default Sandbox

react/SandboxProduct.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react'
2+
import { useProduct } from 'vtex.product-context'
3+
import { Props, schema } from './modules/schema'
4+
import Sandbox from './Sandbox'
5+
6+
const SandboxProduct: StorefrontFunctionComponent<Props> = ({
7+
initialContent,
8+
width,
9+
height,
10+
allowCookies,
11+
allowStyles,
12+
hidden,
13+
}) => {
14+
const productContext = useProduct()
15+
16+
return (
17+
<Sandbox
18+
{...productContext}
19+
initialContent={initialContent}
20+
width={width}
21+
height={height}
22+
allowCookies={allowCookies}
23+
allowStyles={allowStyles}
24+
hidden={hidden} />
25+
)
26+
}
27+
28+
SandboxProduct.schema = schema
29+
30+
export default SandboxProduct

react/modules/schema.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export interface Props {
2+
initialContent?: string
3+
width?: string
4+
height?: string
5+
allowCookies?: boolean
6+
allowStyles?: boolean
7+
hidden?: boolean
8+
}
9+
10+
export const schema = {
11+
title: 'admin/editor.sandbox.title',
12+
description: 'admin/editor.sandbox.description',
13+
type: 'object',
14+
properties: {
15+
width: {
16+
title: 'admin/editor.sandbox.width.title',
17+
description: 'admin/editor.sandbox.width.description',
18+
type: 'string',
19+
default: null,
20+
},
21+
height: {
22+
title: 'admin/editor.sandbox.height.title',
23+
description: 'admin/editor.sandbox.height.description',
24+
type: 'string',
25+
default: null,
26+
},
27+
initialContent: {
28+
title: 'admin/editor.sandbox.initialContent.title',
29+
description: 'admin/editor.sandbox.initialContent.description',
30+
type: 'string',
31+
default: null,
32+
},
33+
allowCookies: {
34+
title: 'admin/editor.sandbox.allowCookies.title',
35+
description: 'admin/editor.sandbox.allowCookies.description',
36+
type: 'boolean',
37+
default: false,
38+
},
39+
},
40+
}

react/tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
},
1818
"include": [
1919
"./typings/*.d.ts",
20-
"./**/*.tsx",
21-
"components/withImage.d.ts",
22-
"Footer.js"
20+
"./**/*.tsx"
2321
],
2422
"exclude": ["node_modules"]
2523
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module 'vtex.product-context' {
2+
interface ProductContext {
3+
product: object
4+
}
5+
6+
export const useProduct = ProductContext
7+
}

store/interfaces.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"sandbox": {
33
"component": "Sandbox"
4+
},
5+
"sandbox.product": {
6+
"component": "SandboxProduct"
47
}
58
}

0 commit comments

Comments
 (0)