Skip to content

Commit

Permalink
[epicmaxco#2968] fix-body prop for va-modal to prevent document scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
aluarius committed Feb 15, 2023
1 parent 5cff994 commit 2ec913d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/docs/src/locales/en/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
"overlayOpacity": "Set the overlay's opacity",
"anchorClass": "Set class name to the `anchor` slot container",
"zIndex": "Set the modal's `z-index`",
"fixBody": "Set `body` position to `fixed` to prevent document scroll while modal is open.",
"blur": "Use `blur` filter to overlay. Root `css` variable `--va-modal-overlay-background-blur-radius` sets the blur radius"
},
"events": {
Expand Down
1 change: 1 addition & 0 deletions packages/docs/src/locales/ru/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
"overlayOpacity": "Прозрачность оверлея",
"anchorClass": "Назначение имени класса для контейнера слота `anchor`",
"zIndex": " CSS свойство `z-index`",
"fixBody": "Запрещает прокрутку документа при открытом модальном окне.",
"blur": "Устанавливает `blur` фильтр оверлея. Рутовая `css` переменная `--va-modal-overlay-background-blur-radius` устанавливает радиус `blur` фильтра"
},
"events": {
Expand Down
13 changes: 13 additions & 0 deletions packages/ui/src/components/va-modal/VaModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ import {
useModalLevel,
useTranslation,
useClickOutside,
useDocument,
} from '../../composables'
import { VaButton } from '../va-button'
Expand Down Expand Up @@ -170,6 +171,7 @@ export default defineComponent({
...useStatefulProps,
modelValue: { type: Boolean, default: false },
attachElement: { type: String, default: 'body' },
fixBody: { type: Boolean, default: false },
disableAttachment: { type: Boolean, default: false },
title: { type: String, default: '' },
message: { type: String, default: '' },
Expand Down Expand Up @@ -295,14 +297,25 @@ export default defineComponent({
useBlur(toRef(props, 'blur'), valueComputed)
const documentRef = useDocument()
const setBodyPosition = (position: string) => {
if (!documentRef.value || !props.fixBody) { return }
documentRef.value.body.style.position = position
}
const setFixedBodyPosition = () => setBodyPosition('fixed')
const resetBodyPosition = () => setBodyPosition('')
watch(valueComputed, newValueComputed => { // watch for open/close modal
if (newValueComputed) {
registerModal()
setFixedBodyPosition()
return
}
if (isLowestLevelModal.value) {
freeFocus()
resetBodyPosition()
}
unregisterModal()
})
Expand Down

0 comments on commit 2ec913d

Please sign in to comment.