Skip to content

Commit e60c0cf

Browse files
committed
feat(shape-7912): add demo and sandbox modal sizing functionality
1 parent 8f6f4d0 commit e60c0cf

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

packages/demo/src/components/ModalToggle.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,53 @@
1-
import { Button, Stack, Typography } from '@mui/material'
1+
import { useState } from 'react'
2+
import { Button, Stack, Typography, Input, Grid } from '@mui/material'
23
import { PluginComponent } from './FieldPluginDemo'
4+
import { ModalSize } from '@storyblok/field-plugin'
35

46
export const ModalToggle: PluginComponent = (props) => {
57
const { actions, data } = props
8+
const [modalSize, setModalSize] = useState<ModalSize>({
9+
width: '50%',
10+
height: '100%',
11+
})
12+
613
return (
714
<Stack gap={2}>
815
<Typography variant="subtitle1">Modal</Typography>
16+
<Grid
17+
container
18+
spacing={2}
19+
>
20+
<Grid
21+
xs={6}
22+
item
23+
>
24+
<Typography>Set modal width: </Typography>
25+
<Input
26+
value={modalSize.width}
27+
fullWidth
28+
onChange={(e) =>
29+
setModalSize({ ...modalSize, width: e.target.value })
30+
}
31+
/>{' '}
32+
</Grid>
33+
<Grid
34+
xs={6}
35+
item
36+
>
37+
<Typography>Set modal height: </Typography>
38+
<Input
39+
value={modalSize.height}
40+
fullWidth
41+
onChange={(e) =>
42+
setModalSize({ ...modalSize, height: e.target.value })
43+
}
44+
/>
45+
</Grid>
46+
</Grid>
947
<Button
1048
variant="outlined"
1149
color="secondary"
12-
onClick={() =>
13-
actions.setModalOpen(!data.isModalOpen, { width: '50%' })
14-
}
50+
onClick={() => actions.setModalOpen(!data.isModalOpen, modalSize)}
1551
>
1652
Toggle Modal
1753
</Button>

packages/demo/src/components/NonModalView.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export const NonModalView: PluginComponent = (props) => (
1313
<Stack gap={6}>
1414
<ModalToggle {...props} />
1515
<ValueMutator {...props} />
16-
<ModalToggle {...props} />
1716
<AssetSelector {...props} />
1817
<ContextRequester {...props} />
1918
<UserContextRequester {...props} />

packages/sandbox/src/components/FieldPluginSandbox.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ const useSandbox = (
119119
// TODO replace with useReducer
120120
const [subscribeState, setSubscribeState] = useState<boolean>(false)
121121
const [isModalOpen, setModalOpen] = useState(false)
122+
const [modalHeight, setModalHeight] = useState<string>('')
123+
const [modalWidth, setModalWidth] = useState<string>('')
122124
const [height, setHeight] = useState(initialHeight)
123125
const [fullHeight, setFullHeight] = useState(false)
124126
const [enablePortalModal, setEnablePortalModal] = useState(false)
@@ -226,8 +228,12 @@ const useSandbox = (
226228
(message: ModalChangeMessage) => {
227229
setModalOpen(message.status)
228230
setStateChangedCallbackId(message.callbackId)
231+
if (message.modalSize) {
232+
setModalHeight(message.modalSize.height || '')
233+
setModalWidth(message.modalSize.width || '')
234+
}
229235
},
230-
[setModalOpen, setStateChangedCallbackId],
236+
[setModalOpen, setStateChangedCallbackId, setModalHeight, setModalWidth],
231237
)
232238

233239
const onHeightChange = useCallback(
@@ -357,6 +363,8 @@ const useSandbox = (
357363
height,
358364
fullHeight,
359365
modalState,
366+
modalHeight,
367+
modalWidth,
360368
schema,
361369
url,
362370
fieldTypeIframe,
@@ -380,6 +388,8 @@ export const FieldPluginSandbox: FunctionComponent = () => {
380388
content,
381389
language,
382390
modalState,
391+
modalHeight,
392+
modalWidth,
383393
fullHeight,
384394
height,
385395
schema,
@@ -420,6 +430,8 @@ export const FieldPluginSandbox: FunctionComponent = () => {
420430
src={iframeSrc}
421431
height={height}
422432
modalState={modalState}
433+
modalHeight={modalHeight}
434+
modalWidth={modalWidth}
423435
fullHeight={fullHeight}
424436
ref={fieldTypeIframe}
425437
onModalChange={setModalOpen}

packages/sandbox/src/components/FieldTypePreview.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,23 @@ export const FieldTypePreview = forwardRef<
134134
src: string | undefined
135135
height: number
136136
modalState: ModalState
137+
modalHeight: string
138+
modalWidth: string
137139
fullHeight: boolean
138140
// Allows the iframe to be refreshed
139141
iframeKey?: number
140142
sx?: SxProps
141143
onModalChange: (isModal: boolean) => void
142144
}
143145
>(function FieldTypePreview(props, ref) {
144-
const { height, fullHeight, modalState, onModalChange } = props
146+
const {
147+
height,
148+
fullHeight,
149+
modalState,
150+
modalHeight,
151+
modalWidth,
152+
onModalChange,
153+
} = props
145154

146155
const setTeleported = (el: HTMLIFrameElement | null) => {
147156
if (modalState === 'modal-with-portal') {
@@ -164,8 +173,13 @@ export const FieldTypePreview = forwardRef<
164173
<Dialog
165174
open={modalState === 'modal-with-portal'}
166175
fullScreen
176+
style={{
177+
maxWidth: modalWidth,
178+
maxHeight: modalHeight,
179+
}}
167180
sx={{
168181
padding: 10,
182+
margin: '0 auto',
169183
}}
170184
PaperProps={{
171185
sx: {

0 commit comments

Comments
 (0)