Skip to content

Commit 77b77cb

Browse files
authored
feat(VOverlay): add viewport-margin prop (#22243)
1 parent ee4fb38 commit 77b77cb

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

packages/api-generator/src/locale/en/VOverlay-location-strategies.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"props": {
33
"locationStrategy": "A function used to specifies how the component should position relative to its activator.",
44
"offset": "Increases distance from the target. When passed as a pair of numbers, the second value shifts anchor along the side and away from the target.",
5-
"stickToTarget": "Enables the overlay content to go off-screen when scrolling."
5+
"stickToTarget": "Enables the overlay content to go off-screen when scrolling.",
6+
"viewportMargin": "Sets custom viewport margin for the overlay content"
67
}
78
}

packages/docs/src/data/new-in.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@
141141
},
142142
"VDialog": {
143143
"props": {
144-
"stickToTarget": "3.10.0"
144+
"stickToTarget": "3.10.0",
145+
"viewportMargin": "3.11.0"
145146
}
146147
},
147148
"VDivider": {
@@ -202,7 +203,8 @@
202203
"VMenu": {
203204
"props": {
204205
"stickToTarget": "3.10.0",
205-
"submenu": "3.7.0"
206+
"submenu": "3.7.0",
207+
"viewportMargin": "3.11.0"
206208
}
207209
},
208210
"VNavigationDrawer": {
@@ -217,7 +219,8 @@
217219
},
218220
"VOverlay": {
219221
"props": {
220-
"stickToTarget": "3.10.0"
222+
"stickToTarget": "3.10.0",
223+
"viewportMargin": "3.11.0"
221224
}
222225
},
223226
"VPicker": {
@@ -332,7 +335,8 @@
332335
"props": {
333336
"eager": "3.2.0",
334337
"interactive": "3.8.0",
335-
"stickToTarget": "3.10.0"
338+
"stickToTarget": "3.10.0",
339+
"viewportMargin": "3.11.0"
336340
}
337341
},
338342
"VVirtualScroll": {

packages/vuetify/src/components/VOverlay/locationStrategies.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface StrategyProps {
5252
origin: Anchor | 'auto' | 'overlap'
5353
offset?: number | string | number[]
5454
stickToTarget?: boolean
55+
viewportMargin?: number | string
5556
maxHeight?: number | string
5657
maxWidth?: number | string
5758
minHeight?: number | string
@@ -74,6 +75,10 @@ export const makeLocationStrategyProps = propsFactory({
7475
},
7576
offset: [Number, String, Array] as PropType<StrategyProps['offset']>,
7677
stickToTarget: Boolean,
78+
viewportMargin: {
79+
type: [Number, String],
80+
default: 12,
81+
},
7782
}, 'VOverlay-location-strategies')
7883

7984
export function useLocationStrategies (
@@ -279,7 +284,7 @@ function connectedLocationStrategy (data: LocationStrategyData, props: StrategyP
279284

280285
const contentBox = getIntrinsicSize(data.contentEl.value, data.isRtl.value)
281286
const scrollParents = getScrollParents(data.contentEl.value)
282-
const viewportMargin = props.stickToTarget ? 0 : 12 // TOOD: prop.viewportMargin
287+
const viewportMargin = Number(props.viewportMargin)
283288

284289
if (!scrollParents.length) {
285290
scrollParents.push(document.documentElement)
@@ -304,10 +309,16 @@ function connectedLocationStrategy (data: LocationStrategyData, props: StrategyP
304309
}, undefined!)
305310

306311
if (props.stickToTarget) {
307-
viewport.x += Math.min(0, targetBox.x)
308-
viewport.y += Math.min(0, targetBox.y)
309-
viewport.width = Math.max(viewport.width, targetBox.x + targetBox.width)
310-
viewport.height = Math.max(viewport.height, targetBox.y + targetBox.height)
312+
viewport.x += Math.min(viewportMargin, targetBox.x)
313+
viewport.y += Math.min(viewportMargin, targetBox.y)
314+
viewport.width = Math.max(
315+
viewport.width - viewportMargin * 2,
316+
targetBox.x + targetBox.width - viewportMargin
317+
)
318+
viewport.height = Math.max(
319+
viewport.height - viewportMargin * 2,
320+
targetBox.y + targetBox.height - viewportMargin
321+
)
311322
} else {
312323
viewport.x += viewportMargin
313324
viewport.y += viewportMargin

packages/vuetify/src/components/VSnackbar/VSnackbar.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,14 @@ export const makeVSnackbarProps = propsFactory({
8888
...makeThemeProps(),
8989
...omit(makeVOverlayProps({
9090
transition: 'v-snackbar-transition',
91-
}), ['persistent', 'noClickAnimation', 'scrim', 'scrollStrategy', 'stickToTarget']),
91+
}), [
92+
'persistent',
93+
'noClickAnimation',
94+
'scrim',
95+
'scrollStrategy',
96+
'stickToTarget',
97+
'viewportMargin',
98+
]),
9299
}, 'VSnackbar')
93100

94101
export const VSnackbar = genericComponent<VSnackbarSlots>()({

0 commit comments

Comments
 (0)