forked from bootstrap-vue-next/bootstrap-vue-next
/
BOffcanvas.vue
321 lines (293 loc) · 9.04 KB
/
BOffcanvas.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<template>
<Teleport :to="teleportTo" :disabled="props.teleportDisabled">
<BTransition
:no-fade="true"
:trans-props="{
enterToClass: 'showing',
enterFromClass: '',
leaveToClass: 'hiding show',
leaveFromClass: 'show',
}"
@before-enter="OnBeforeEnter"
@after-enter="OnAfterEnter"
@leave="onLeave"
@after-leave="OnAfterLeave"
>
<div
v-show="modelValue"
:id="computedId"
ref="element"
aria-modal="true"
role="dialog"
:class="computedClasses"
tabindex="-1"
:aria-labelledby="`${computedId}-offcanvas-label`"
data-bs-backdrop="false"
v-bind="$attrs"
>
<template v-if="lazyShowing">
<div v-if="!props.noHeader" class="offcanvas-header" :class="headerClass">
<slot name="header" v-bind="sharedSlots">
<h5 :id="`${computedId}-offcanvas-label`" class="offcanvas-title">
<slot name="title" v-bind="sharedSlots">
{{ title }}
</slot>
</h5>
<template v-if="!props.noHeaderClose">
<BButton v-if="hasHeaderCloseSlot" v-bind="headerCloseAttrs" @click="hide('close')">
<slot name="header-close" />
</BButton>
<BCloseButton
v-else
:aria-label="headerCloseLabel"
v-bind="headerCloseAttrs"
@click="hide('close')"
/>
</template>
</slot>
</div>
<div class="offcanvas-body" :class="bodyClass" v-bind="bodyAttrs">
<slot v-bind="sharedSlots" />
</div>
<div v-if="hasFooterSlot" :class="footerClass">
<slot name="footer" v-bind="sharedSlots" />
</div>
</template>
</div>
</BTransition>
<slot name="backdrop">
<BOverlay
:variant="backdropVariant"
:show="showBackdrop"
fixed
no-wrap
no-spinner
@click="hide('backdrop')"
/>
</slot>
</Teleport>
</template>
<script setup lang="ts">
import {onKeyStroke, useEventListener, useFocus, useVModel} from '@vueuse/core'
import {computed, nextTick, ref, type RendererElement, toRef} from 'vue'
import {useId, useSafeScrollLock} from '../../composables'
import type {AttrsValue, ButtonVariant, ClassValue, ColorVariant} from '../../types'
import {BvTriggerableEvent, isEmptySlot} from '../../utils'
import BButton from '../BButton/BButton.vue'
import BCloseButton from '../BButton/BCloseButton.vue'
import BOverlay from '../BOverlay/BOverlay.vue'
import BTransition from '../BTransition/BTransition.vue'
// TODO once the responsive stuff may be implemented correctly,
// What needs to occur is a fixing of the "body scrolling".
// If the offcanvas is on the screen on a large screen, body scrolling is not disabled
// Even though the modelValue is true
// When it's a small screen and close, it works, as normal,
// But then when it opens up on a small screen, it must disable again
// This is implemented on Layout.vue, but is not officially supported.
defineOptions({
inheritAttrs: false,
})
const props = withDefaults(
defineProps<{
backdrop?: boolean
backdropVariant?: ColorVariant | null
bodyAttrs?: Readonly<AttrsValue>
bodyClass?: ClassValue
bodyScrolling?: boolean
footerClass?: string
headerClass?: string
headerCloseClass?: ClassValue
headerCloseLabel?: string
headerCloseVariant?: ButtonVariant | null
id?: string
lazy?: boolean
modelValue?: boolean
noCloseOnBackdrop?: boolean
noCloseOnEsc?: boolean
noFocus?: boolean
noHeader?: boolean
noHeaderClose?: boolean
// TODO standardize this. Create a dedicated type
// Then in components that use individual props (BImg)
// Make them just use prop placement
placement?: 'top' | 'bottom' | 'start' | 'end'
teleportDisabled?: boolean
teleportTo?: string | RendererElement | null | undefined
title?: string
// responsive?: Breakpoint
// TODO responsive doesn't work
}>(),
{
backdrop: true,
backdropVariant: 'dark',
bodyAttrs: undefined,
bodyClass: undefined,
bodyScrolling: false,
footerClass: undefined,
headerClass: undefined,
headerCloseClass: undefined,
headerCloseLabel: 'Close',
headerCloseVariant: 'secondary',
id: undefined,
lazy: false,
modelValue: false,
noCloseOnBackdrop: false,
noCloseOnEsc: false,
noFocus: false,
noHeader: false,
noHeaderClose: false,
placement: 'start',
teleportDisabled: false,
teleportTo: 'body',
title: undefined,
}
)
const emit = defineEmits<{
'close': [value: BvTriggerableEvent]
'esc': [value: BvTriggerableEvent]
'hidden': [value: BvTriggerableEvent]
'hide': [value: BvTriggerableEvent]
'hide-prevented': []
'show': [value: BvTriggerableEvent]
'show-prevented': []
'shown': [value: BvTriggerableEvent]
'update:modelValue': [value: boolean]
}>()
type SharedSlotsData = {
visible: boolean
placement: 'top' | 'bottom' | 'start' | 'end'
hide: (trigger?: string) => void
}
const slots = defineSlots<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'backdrop'?: (props: Record<string, never>) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'default'?: (props: SharedSlotsData) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'footer'?: (props: SharedSlotsData) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'header'?: (props: SharedSlotsData) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'header-close'?: (props: Record<string, never>) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'title'?: (props: SharedSlotsData) => any
}>()
const modelValue = useVModel(props, 'modelValue', emit, {passive: true})
const computedId = useId(() => props.id, 'offcanvas')
useSafeScrollLock(modelValue, () => props.bodyScrolling)
const element = ref<HTMLElement | null>(null)
onKeyStroke(
'Escape',
() => {
hide('esc')
},
{target: element}
)
const {focused} = useFocus(element, {
initialValue: modelValue.value && props.noFocus === false,
})
const isActive = ref(modelValue.value)
const lazyLoadCompleted = ref(false)
const showBackdrop = toRef(() => props.backdrop === true && modelValue.value === true)
const lazyShowing = toRef(
() =>
props.lazy === false ||
(props.lazy === true && lazyLoadCompleted.value === true) ||
(props.lazy === true && modelValue.value === true)
)
const hasHeaderCloseSlot = toRef(() => !isEmptySlot(slots['header-close']))
const headerCloseClasses = computed(() => [
{'text-reset': !hasHeaderCloseSlot.value},
props.headerCloseClass,
])
const headerCloseAttrs = computed(() => ({
variant: hasHeaderCloseSlot.value ? props.headerCloseVariant : undefined,
class: headerCloseClasses.value,
}))
const hasFooterSlot = toRef(() => !isEmptySlot(slots.footer))
const computedClasses = computed(() => [
// props.responsive === undefined ? 'offcanvas' : `offcanvas-${props.responsive}`,
'offcanvas', // Remove when above check is fixed
`offcanvas-${props.placement}`,
{
show: modelValue.value && isActive.value === true,
},
])
const sharedSlots = computed<SharedSlotsData>(() => ({
visible: modelValue.value,
placement: props.placement,
hide,
}))
const buildTriggerableEvent = (
type: string,
opts: Partial<BvTriggerableEvent> = {}
): BvTriggerableEvent =>
new BvTriggerableEvent(type, {
cancelable: false,
target: element.value || null,
relatedTarget: null,
trigger: null,
...opts,
componentId: computedId.value,
})
const hide = (trigger = '') => {
if (
(trigger === 'backdrop' && props.noCloseOnBackdrop) ||
(trigger === 'esc' && props.noCloseOnEsc)
) {
emit('hide-prevented')
return
}
const event = buildTriggerableEvent('hide', {cancelable: trigger !== '', trigger})
if (trigger === 'close') {
emit(trigger, event)
}
if (trigger === 'esc') {
emit(trigger, event)
}
emit('hide', event)
if (event.defaultPrevented) {
emit('hide-prevented')
return
}
modelValue.value = false
}
const show = () => {
const event = buildTriggerableEvent('show', {cancelable: true})
emit('show', event)
if (event.defaultPrevented) {
modelValue.value = false
emit('show-prevented')
return
}
modelValue.value = true
}
const focus = () => {
nextTick(() => {
if (props.noFocus === false) {
focused.value = true
}
})
}
const OnBeforeEnter = () => show()
const OnAfterEnter = () => {
isActive.value = true
focus()
emit('shown', buildTriggerableEvent('shown'))
if (props.lazy === true) lazyLoadCompleted.value = true
}
const onLeave = () => {
isActive.value = false
}
const OnAfterLeave = () => {
emit('hidden', buildTriggerableEvent('hidden'))
if (props.lazy === true) lazyLoadCompleted.value = false
}
useEventListener(element, 'bv-toggle', () => {
modelValue.value ? hide() : show()
})
defineExpose({
hide,
show,
})
</script>