Skip to content

Commit 926cb49

Browse files
feat: sandbox and demo
1 parent 47ed4a4 commit 926cb49

File tree

6 files changed

+160
-44
lines changed

6 files changed

+160
-44
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Paper, Stack } from '@mui/material'
1+
import { Divider, Paper, Stack } from '@mui/material'
22
import { ModalToggle } from './ModalToggle'
33
import { ValueMutator } from './ValueMutator'
44
import { HeightChangeDemo } from './HeightChangeDemo'
@@ -8,20 +8,28 @@ import { UserContextRequester } from './UserContextRequester'
88
import { PluginComponent } from './FieldPluginDemo'
99
import { LanguageView } from './LanguageView'
1010
import { PromptAI } from './PromptAI'
11-
import { PreviewWidth } from './PreviewWidth'
11+
import { PreviewDimension } from './PreviewDimension'
1212

1313
export const NonModalView: PluginComponent = (props) => (
1414
<Paper>
1515
<Stack gap={6}>
1616
<ModalToggle {...props} />
17+
<Divider />
1718
<ValueMutator {...props} />
19+
<Divider />
1820
<AssetSelector {...props} />
21+
<Divider />
1922
<ContextRequester {...props} />
23+
<Divider />
2024
<UserContextRequester {...props} />
25+
<Divider />
2126
<HeightChangeDemo {...props} />
27+
<Divider />
2228
<LanguageView {...props} />
29+
<Divider />
2330
<PromptAI {...props} />
24-
<PreviewWidth {...props} />
31+
<Divider />
32+
<PreviewDimension {...props} />
2533
</Stack>
2634
</Paper>
2735
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { Button, Input, Stack, Typography } from '@mui/material'
2+
import { PluginComponent } from './FieldPluginDemo'
3+
import { useState } from 'react'
4+
5+
export const PreviewDimension: PluginComponent = (props) => {
6+
const { data, actions } = props
7+
8+
const label =
9+
typeof data.content === 'undefined'
10+
? 'undefined'
11+
: JSON.stringify(data.content)
12+
13+
const [width, setWidth] = useState(300)
14+
15+
const handleClickMobile = () => {
16+
actions.setPreviewDimension({
17+
tag: 'mobile',
18+
})
19+
}
20+
const handleClickTablet = () => {
21+
actions.setPreviewDimension({
22+
tag: 'tablet',
23+
})
24+
}
25+
const handleClickDesktop = () => {
26+
actions.setPreviewDimension({
27+
tag: 'desktop',
28+
})
29+
}
30+
const handleClickCustom = () => {
31+
actions.setPreviewDimension({
32+
tag: 'custom',
33+
width: 1234,
34+
})
35+
}
36+
37+
return (
38+
<Stack gap={2}>
39+
<Typography variant="subtitle1">Field Value</Typography>
40+
<Typography textAlign="center">{label}</Typography>
41+
<Button
42+
variant="outlined"
43+
color="secondary"
44+
onClick={handleClickMobile}
45+
>
46+
Mobile
47+
</Button>
48+
<Button
49+
variant="outlined"
50+
color="secondary"
51+
onClick={handleClickTablet}
52+
>
53+
Tablet
54+
</Button>
55+
<Button
56+
variant="outlined"
57+
color="secondary"
58+
onClick={handleClickDesktop}
59+
>
60+
Desktop
61+
</Button>
62+
<Button
63+
variant="outlined"
64+
color="secondary"
65+
onClick={handleClickCustom}
66+
>
67+
Custom
68+
</Button>
69+
<Input
70+
type="number"
71+
value={width}
72+
onChange={(e) => setWidth(Number(e.target.value))}
73+
placeholder="Custom Width"
74+
/>
75+
</Stack>
76+
)
77+
}

packages/demo/src/components/PreviewWidth.tsx

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

packages/sandbox/src/components/FieldPluginSandbox.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
UserData,
2727
urlSearchParamsFromPluginUrlParams,
2828
ValueChangeMessage,
29+
PreviewDimensionChangeMessage,
2930
} from '@storyblok/field-plugin'
3031
import '@fontsource/roboto/300.css'
3132
import '@fontsource/roboto/400.css'
@@ -56,6 +57,7 @@ import { UrlView } from './UrlView'
5657
import { usePluginParams } from './usePluginParams'
5758
import { LanguageView } from './LanguageView'
5859
import { TranslatableCheckbox } from './TranslatableCheckbox'
60+
import { PreviewDimension } from './PreviewDimension'
5961

6062
const defaultUrl = 'http://localhost:8080'
6163
const initialStory: StoryData = {
@@ -130,6 +132,11 @@ const useSandbox = (
130132
translatable: false,
131133
})
132134
const [content, setContent] = useState<unknown>(initialContent)
135+
const [previewDimension, setPreviewDimension] = useState<
136+
PreviewDimensionChangeMessage['data']
137+
>({
138+
tag: 'desktop',
139+
})
133140
const [language, setLanguage] = useState<string>('default')
134141
const [stateChangedCallbackId, setStateChangedCallbackId] = useState<string>()
135142

@@ -321,6 +328,13 @@ const useSandbox = (
321328
[onError],
322329
)
323330

331+
const onSetPreviewDimension = useCallback(
332+
(message: PreviewDimensionChangeMessage) => {
333+
setPreviewDimension(message.data)
334+
},
335+
[],
336+
)
337+
324338
useEffect(
325339
() =>
326340
createSandboxMessageListener(
@@ -333,6 +347,7 @@ const useSandbox = (
333347
requestUserContext: onUserContextRequested,
334348
selectAsset: onAssetSelected,
335349
promptAI: onPromptAI,
350+
setPreviewDimension: onSetPreviewDimension,
336351
},
337352
{
338353
iframeOrigin: fieldPluginURL?.origin,
@@ -380,6 +395,7 @@ const useSandbox = (
380395
url,
381396
fieldTypeIframe,
382397
iframeSrc,
398+
previewDimension,
383399
},
384400
{
385401
setContent,
@@ -407,6 +423,7 @@ export const FieldPluginSandbox: FunctionComponent = () => {
407423
url,
408424
fieldTypeIframe,
409425
iframeSrc,
426+
previewDimension,
410427
},
411428
{ setModalOpen, setContent, setLanguage, setSchema, setUrl, randomizeUid },
412429
] = useSandbox(error)
@@ -526,6 +543,14 @@ export const FieldPluginSandbox: FunctionComponent = () => {
526543
/>
527544
</AccordionDetails>
528545
</Accordion>
546+
<Accordion defaultExpanded>
547+
<AccordionSummary>
548+
<Typography variant="h3">Data</Typography>
549+
</AccordionSummary>
550+
<AccordionDetails>
551+
<PreviewDimension previewDimension={previewDimension} />
552+
</AccordionDetails>
553+
</Accordion>
529554
</Container>
530555
)
531556
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { FunctionComponent } from 'react'
2+
import { PreviewDimensionChangeMessage } from '@storyblok/field-plugin'
3+
import {
4+
FormControl,
5+
FormControlLabel,
6+
FormLabel,
7+
Radio,
8+
RadioGroup,
9+
} from '@mui/material'
10+
11+
export const PreviewDimension: FunctionComponent<{
12+
previewDimension: PreviewDimensionChangeMessage['data']
13+
}> = (props) => {
14+
const { previewDimension } = props
15+
16+
return (
17+
<FormControl disabled>
18+
<FormLabel id="demo-radio-buttons-group-label">
19+
Preview Dimension
20+
</FormLabel>
21+
<RadioGroup
22+
aria-labelledby="demo-radio-buttons-group-label"
23+
name="radio-buttons-group"
24+
value={previewDimension.tag}
25+
>
26+
<FormControlLabel
27+
value="desktop"
28+
control={<Radio />}
29+
label="Desktop"
30+
/>
31+
<FormControlLabel
32+
value="tablet"
33+
control={<Radio />}
34+
label="Tablet"
35+
/>
36+
<FormControlLabel
37+
value="mobile"
38+
control={<Radio />}
39+
label="Mobile"
40+
/>
41+
</RadioGroup>
42+
</FormControl>
43+
)
44+
}

packages/sandbox/src/dom/createSandboxMessageListener.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
isMessageToContainer,
77
isModalChangeMessage,
88
isPluginLoadedMessage,
9-
isPreviewWidthChangeMessage,
109
isValueChangeMessage,
10+
isPluginPromptAIMessage,
11+
isPreviewDimensionChangeMessage,
1112
type ModalChangeMessage,
1213
type PluginLoadedMessage,
1314
type PluginPromptAIMessage,
@@ -17,7 +18,6 @@ import {
1718
type GetContextMessage,
1819
type GetUserContextMessage,
1920
type HeightChangeMessage,
20-
isPluginPromptAIMessage,
2121
} from '@storyblok/field-plugin'
2222

2323
type SandboxActions = {
@@ -73,7 +73,7 @@ export const createSandboxMessageListener: CreateSandboxListener = (
7373
eventHandlers.promptAI(message)
7474
} else if (isGetUserContextMessage(message)) {
7575
eventHandlers.requestUserContext(message)
76-
} else if (isPreviewWidthChangeMessage(message)) {
76+
} else if (isPreviewDimensionChangeMessage(message)) {
7777
eventHandlers.setPreviewDimension(message)
7878
} else {
7979
console.warn(

0 commit comments

Comments
 (0)