8000 feat(BModal): use css var for zindex, add helper vars and ontop class by xvaara · Pull Request #2556 · bootstrap-vue-next/bootstrap-vue-next · GitHub
[go: up one dir, main page]

Skip to content

feat(BModal): use css var for zindex, add helper vars and ontop class #2556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

10000 Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions apps/docs/src/docs/components/modal.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ Feel free to mix vertically `centered` with `scrollable`.
</BModal>
<BModal v-model="nestedModal3" size="sm" title="Third Modal" ok-only>
<p class="my-1">Third Modal</p>
<BButton @click="nestedModal4 = !nestedModal4" size="sm">Open Fouth Modal</BButton>
</BModal>
<BModal v-model="nestedModal4" size="sm" title="Fourth Modal" ok-only>
<p class="my-1">Fourth Modal</p>
</BModal>
<template #html>

Expand All @@ -193,14 +197,38 @@ Feel free to mix vertically `centered` with `scrollable`.

<BModal v-model="nestedModal3" size="sm" title="Third Modal" ok-only>
<p class="my-1">Third Modal</p>
<BButton @click="nestedModal4 = !nestedModal4" size="sm">Open Fouth Modal</BButton>
</BModal>
<BModal v-model="nestedModal4" size="sm" title="Fourth Modal" ok-only>
<p class="my-1">Fourth Modal</p>
</BModal>
</template>

<script setup lang="ts">
const nestedModal1 = ref(false)
const nestedModal2 = ref(false)
const nestedModal3 = ref(false)
const nestedModal4 = ref(false)
</script>
<style>
/* just a little example of the variables and classes for stack */
.modal {
--bs-modal-zindex: 1900;
transform: translate(
calc((var(--b-count, 0) - var(--b-position, 0)) * 20px),
calc((var(--b-count, 0) - var(--b-position, 0)) * 20px)
);
transition:
transform 0.5s,
opacity 0.15s linear !important;
}
.modal:not(.stack-inverse-position-0) {
opacity: calc(1 - ((var(--b-count, 0) - var(--b-position, 0)) * 0.1));
}
.modal-backdrop:not(.stack-inverse-position-0) {
opacity: 0 !important;
}
</style>
```

</template>
Expand Down Expand Up @@ -240,4 +268,22 @@ const preventFn = (e: Event) => {
const nestedModal1 = ref(false)
const nestedModal2 = ref(false)
const nestedModal3 = ref(false)
const nestedModal4 = ref(false)
</script>

<style>
.modal {
--bs-modal-zindex: 1900;
transform: translate(
calc((var(--b-count, 0) - var(--b-position, 0)) * 20px),
calc((var(--b-count, 0) - var(--b-position, 0)) * 20px)
);
transition: transform 0.5s, opacity 0.15s linear !important;
}
.modal:not(.stack-inverse-position-0) {
opacity: calc(1 - ((var(--b-count, 0) - var(--b-position, 0)) * 0.1));
}
.modal-backdrop:not(.stack-inverse-position-0) {
opacity: 0 !important;
}
</style>
51 changes: 45 additions & 6 deletions packages/bootstrap-vue-next/src/components/BModal/BModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
{
fade: !computedNoAnimation,
show: isVisible,
...sharedClasses,
},
]"
role="dialog"
Expand All @@ -24,7 +25,7 @@
v-bind="$attrs"
:style="computedZIndex"
style="display: block"
@mousedown.self="hide('backdrop')"
@mousedown.left.self="hide('backdrop')"
>
<div class="modal-dialog" :class="modalDialogClasses">
<div v-if="contentShowing" class="modal-content" :class="props.contentClass">
Expand Down Expand Up @@ -116,6 +117,7 @@
:class="{
fade: !computedNoAnimation,
show: backdropVisible || computedNoAnimation,
...sharedClasses,
}"
@click="hide('backdrop')"
/>
Expand All @@ -127,7 +129,16 @@
<script setup lang="ts">
import {onKeyStroke, unrefElement} from '@vueuse/core'
import {useActivatedFocusTrap} from '../../composables/useActivatedFocusTrap'
import {computed, type CSSProperties, type EmitFn, useTemplateRef, watch} from 'vue'
import {
computed,
type CSSProperties,
type EmitFn,
nextTick,
onMounted,
ref,
useTemplateRef,
watch,
} from 'vue'
import type {BModalProps} from '../../types/ComponentProps'
import type {BModalEmits} from '../../types/ComponentEmits'
import type {BModalSlots, BModalSlotsData} from '../../types/ComponentSlots'
Expand All @@ -137,7 +148,7 @@ import BCloseButton from '../BButton/BCloseButton.vue'
import {useDefaults} from '../../composables/useDefaults'
import {useId} from '../../composables/useId'
import {useSafeScrollLock} from '../../composables/useSafeScrollLock'
import {isEmptySlot} from '../../utils/dom'
import {getModalZIndex, isEmptySlot} from '../../utils/dom'
import {useColorVariantClasses} from '../../composables/useColorVariantClasses'
import {useModalManager} from '../../composables/useModalManager'
import {useShowHide} from '../../composables/useShowHide'
Expand Down Expand Up @@ -362,6 +373,7 @@ const titleClasses = computed(() => [
['visually-hidden']: props.titleVisuallyHidden,
},
])

const disableCancel = computed(() => props.cancelDisabled || props.busy)
const disableOk = computed(() => props.okDisabled || props.busy)

Expand All @@ -370,12 +382,33 @@ const {activePosition, activeModalCount, stackWithoutSelf} = useModalManager(
modelValue.value
)

const sharedClasses = computed(() => ({
[`stack-position-${activePosition?.value ?? 0}`]: true,
[`stack-inverse-position-${(activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0)}`]:
true,
}))

watch(stackWithoutSelf, (newValue, oldValue) => {
if (newValue.length > oldValue.length && showRef.value === true && props.noStacking === true)
hide()
})

const defaultModalDialogZIndex = 1056
const defaultModalDialogZIndex = ref(getModalZIndex(element.value ?? document.body))

onMounted(() => {
Copy link
Preview
Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The watcher on renderRef triggers a recalculation of the z-index on every render. Consider limiting the watch to CSS variable changes or debouncing to avoid unnecessary getComputedStyle calls.

Copilot uses AI. Check for mistakes.

watch(
renderRef,
(v) => {
if (!v) return
nextTick(() => {
if (!element.value) return
defaultModalDialogZIndex.value = getModalZIndex(element.value)
})
},
{immediate: true}
)
})

const computedZIndexNumber = computed<number>(() =>
// Make sure that newly opened modals have a higher z-index than currently active ones.
// All active modals have a z-index of ('defaultZIndex' - 'stackSize' - 'positionInStack').
Expand All @@ -384,15 +417,21 @@ const computedZIndexNumber = computed<number>(() =>

showRef.value || isLeaving.value
? // Just for reference there is a single frame in which the modal is not active but still has a higher z-index than the active ones due to _when_ it calculates its position. It's a small visual effect
defaultModalDialogZIndex -
defaultModalDialogZIndex.value -
((activeModalCount?.value ?? 0) * 2 - (activePosition?.value ?? 0) * 2)
: defaultModalDialogZIndex
: defaultModalDialogZIndex.value
)
const computedZIndex = computed<CSSProperties>(() => ({
'z-index': computedZIndexNumber.value,
'--b-position': activePosition?.value ?? 0,
'--b-inverse-position': (activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0),
'--b-count': activeModalCount?.value ?? 0,
}))
const computedZIndexBackdrop = computed<CSSProperties>(() => ({
'z-index': computedZIndexNumber.value - 1,
'--b-position': activePosition?.value ?? 0,
'--b-inverse-position': (activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0),
'--b-count': activeModalCount?.value ?? 0,
Comment on lines +426 to +434
Copy link
Preview
Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting CSS custom property names (e.g. --b-position, --b-inverse-position, --b-count) into constants to avoid typos and keep CSS/JS in sync.

Suggested change
'--b-position': activePosition?.value ?? 0,
'--b-inverse-position': (activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0),
'--b-count': activeModalCount?.value ?? 0,
}))
const computedZIndexBackdrop = computed<CSSProperties>(() => ({
'z-index': computedZIndexNumber.value - 1,
'--b-position': activePosition?.value ?? 0,
'--b-inverse-position': (activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0),
'--b-count': activeModalCount?.value ?? 0,
[CSS_VAR_POSITION]: activePosition?.value ?? 0,
[CSS_VAR_INVERSE_POSITION]: (activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0),
[CSS_VAR_COUNT]: activeModalCount?.value ?? 0,
}))
const computedZIndexBackdrop = computed<CSSProperties>(() => ({
'z-index': computedZIndexNumber.value - 1,
[CSS_VAR_POSITION]: activePosition?.value ?? 0,
[CSS_VAR_INVERSE_POSITION]: (activeModalCount?.value ?? 1) - 1 - (activePosition?.value ?? 0),
[CSS_VAR_COUNT]: activeModalCount?.value ?? 0,

Copilot uses AI. Check for mistakes.

}))

const sharedSlots = computed<BModalSlotsData>(() => ({
Expand Down
5 changes: 5 additions & 0 deletions packages/bootstrap-vue-next/src/styles/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@
.no-transition {
transition: none !important;
}

:root {
--bs-modal-zindex: #{$zindex-modal};
--bs-toast-max-width: #{$toast-max-width};
}
8 changes: 8 additions & 0 deletions packages/bootstrap-vue-next/src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ export const sortSlotElementsByPosition = (
if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1
return 0
}

Copy link
Preview
Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a JSDoc comment to explain the purpose, parameters, return value, and fallback behavior of getModalZIndex.

Suggested change
/**
* Retrieves the z-index value for a modal from the `--bs-modal-zindex` CSS variable.
*
* @param {Readonly<HTMLElement | null>} [element] - The target element to retrieve the z-index from. Defaults to `document.body` if not provided.
* @returns {number} The z-index value. Returns 1055 as a fallback if `window` is undefined or the CSS variable is not parsable.
*/

Copilot uses AI. Check for mistakes.

export const getModalZIndex = (element?: Readonly<HTMLElement | null>): number => {
if (typeof window === 'undefined') return 1055
const target = element ?? document.body
const raw = window.getComputedStyle(target).getPropertyValue('--bs-modal-zindex').trim()
const parsed = Number.parseInt(raw, 10)
return Number.isFinite(parsed) ? parsed : 1055
}
0