Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit f074826

Browse files
committed
feat(modal): update theme and add modal close button
1 parent 4e890c9 commit f074826

File tree

10 files changed

+557
-416
lines changed

10 files changed

+557
-416
lines changed

packages/c-modal/examples/base-c-modal.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@
1212
<c-modal-overlay />
1313
<c-modal-content>
1414
<c-modal-header>Modal header</c-modal-header>
15-
<chakra.div p="10">
16-
<c-button @click="isOpen = false">Hello, Modal! ⚡️</c-button>
17-
</chakra.div>
15+
<c-modal-close-button />
16+
<c-modal-body>
17+
<chakra.p>
18+
Sit nulla est ex deserunt exercitation anim occaecat. Nostrud
19+
ullamco deserunt aute id consequat veniam incididunt duis in sint
20+
irure nisi.
21+
</chakra.p>
22+
</c-modal-body>
23+
24+
<c-modal-footer>
25+
<c-button @click="isOpen = false" mr="3"> Close </c-button>
26+
<c-button>Secondary action</c-button>
27+
</c-modal-footer>
1828
</c-modal-content>
1929
</c-modal>
2030
</chakra.div>

packages/c-modal/src/c-modal.ts

Lines changed: 153 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,14 @@ import {
1414
PropType,
1515
reactive,
1616
ComputedRef,
17-
Ref,
1817
toRefs,
1918
computed,
2019
ToRefs,
2120
mergeProps,
22-
onMounted,
23-
watchEffect,
2421
UnwrapRef,
2522
watch,
26-
ref,
27-
cloneVNode,
28-
getCurrentInstance,
2923
withDirectives,
30-
nextTick,
3124
unref,
32-
Transition,
33-
onUnmounted,
3425
} from 'vue'
3526
import {
3627
chakra,
@@ -39,25 +30,15 @@ import {
3930
useMultiStyleConfig,
4031
useStyles,
4132
} from '@chakra-ui/vue-system'
42-
import {
43-
createContext,
44-
TemplateRef,
45-
useRef,
46-
VueComponentInstance,
47-
} from '@chakra-ui/vue-utils'
33+
import { createContext, TemplateRef, useRef } from '@chakra-ui/vue-utils'
4834
import { CPortal } from '@chakra-ui/c-portal'
49-
import {
50-
CFocusLock,
51-
FocusLockProps,
52-
useFocusLock,
53-
} from '@chakra-ui/c-focus-lock'
35+
import { CFocusLock, FocusLockProps } from '@chakra-ui/c-focus-lock'
5436
import { CScrollLock, BodyScrollLockDirective } from '@chakra-ui/c-scroll-lock'
5537
import { CMotion } from '@chakra-ui/c-motion'
38+
import { CCloseButton } from '@chakra-ui/c-close-button'
5639
import { MotionDirective } from '@vueuse/motion'
5740

5841
import { useModal, UseModalOptions, UseModalReturn } from './use-modal'
59-
import { focus, FocusableElement } from '@chakra-ui/utils'
60-
import { FocusTarget } from 'focus-trap'
6142

6243
type ScrollBehavior = 'inside' | 'outside'
6344
type MotionPreset = 'slideInBottom' | 'slideInRight' | 'scale' | 'none'
@@ -231,6 +212,7 @@ export const CModal = defineComponent({
231212
setup(props, { slots, attrs, emit }) {
232213
const closeModal = () => {
233214
emit('update:is-open', false)
215+
console.log('CLOSING MODAL =============')
234216
}
235217

236218
const handleEscape = (event: KeyboardEvent) => {
@@ -246,8 +228,6 @@ export const CModal = defineComponent({
246228
// @ts-expect-error
247229
const modal = useModal(modalOptions)
248230

249-
console.log(modal)
250-
251231
ModalContextProvider(
252232
computed(() => ({
253233
...modal,
@@ -353,49 +333,41 @@ export const CModalContent = defineComponent({
353333
finalFocusRef: context.value.finalFocusRef?.value,
354334
restoreFocus: context.value.returnFocusOnClose?.value,
355335
},
356-
() =>
357-
h(
358-
Transition,
359-
{
360-
css: false,
361-
},
362-
() =>
363-
context.value.isOpen.value && [
364-
withDirectives(
365-
h(
366-
chakra('section', {
367-
__css: dialogStyles.value,
368-
label: 'modal__content',
369-
}),
370-
{
371-
...attrs,
372-
...context.value.dialogProps.value,
373-
},
374-
slots
375-
),
376-
[
377-
[
378-
MotionDirective,
379-
{
380-
initial: {
381-
scale: 0.5,
382-
opacity: 0,
383-
},
384-
enter: {
385-
scale: 1,
386-
opacity: 1,
387-
translateY: 0,
388-
},
389-
leave: {
390-
scale: 0.5,
391-
opacity: 0,
392-
},
393-
},
394-
],
395-
]
396-
),
397-
]
398-
)
336+
() => [
337+
withDirectives(
338+
h(
339+
chakra('section', {
340+
__css: dialogStyles.value,
341+
label: 'modal__content',
342+
}),
343+
{
344+
...attrs,
345+
...context.value.dialogProps.value,
346+
},
347+
slots
348+
),
349+
[
350+
[
351+
MotionDirective,
352+
{
353+
initial: {
354+
scale: 0.5,
355+
opacity: 0,
356+
},
357+
enter: {
358+
scale: 1,
359+
opacity: 1,
360+
translateY: 0,
361+
},
362+
leave: {
363+
scale: 0.5,
364+
opacity: 0,
365+
},
366+
},
367+
],
368+
]
369+
),
370+
]
399371
),
400372
]
401373
),
@@ -433,23 +405,26 @@ export const CModalOverlay = defineComponent({
433405
h(
434406
chakra('div', {
435407
label: 'modal__overlay',
408+
__css: overlayStyle.value,
436409
}),
437-
{
438-
__css: {
439-
...overlayStyle.value,
440-
},
441-
...attrs,
442-
}
410+
attrs
443411
),
444412
]
445413
)
446414
},
447415
})
448416

417+
/**
418+
* CModalHeader
419+
*
420+
* Component that houses the title of the modal.
421+
*
422+
* @see Docs https://next.vue.chakra-ui.com/docs/components/modal
423+
*/
449424
export const CModalHeader = defineComponent({
450425
name: 'CModalHeader',
451426
setup(_, { attrs, slots }) {
452-
const context = useModalContext()
427+
const { hasHeader, headerId } = unref(useModalContext())
453428
const styles = useStyles()
454429
const headerStyles = computed<SystemStyleObject>(() => ({
455430
flex: 0,
@@ -459,20 +434,122 @@ export const CModalHeader = defineComponent({
459434
const [headerRef, headerEl] = useRef()
460435

461436
watch(headerEl, (el) => {
462-
context.value.hasHeader.value = !!el
437+
hasHeader.value = !!el
463438
})
464439

465440
return () =>
466441
h(
467442
chakra('header', {
443+
label: 'modal__header',
468444
__css: headerStyles.value,
469445
}),
470446
{
471447
...attrs,
472448
ref: headerRef,
473-
id: context.value.headerId.value,
449+
id: headerId.value,
450+
},
451+
slots
452+
)
453+
},
454+
})
455+
456+
/**
457+
* CModalBody
458+
*
459+
* Component that houses the body of the modal.
460+
*
461+
* @see Docs https://next.vue.chakra-ui.com/docs/components/modal
462+
*/
463+
export const CModalBody = defineComponent({
464+
name: 'CModalBody',
465+
setup(_, { slots, attrs }) {
466+
const { bodyId, hasBody } = unref(useModalContext())
467+
const styles = useStyles()
468+
469+
const [bodyRef, bodyEl] = useRef()
470+
471+
/**
472+
* Used to bind the `aria-descibedby` attribute
473+
*/
474+
watch(bodyEl, (el) => {
475+
hasBody.value = !!el
476+
})
477+
478+
return () =>
479+
h(
480+
chakra('div', {
481+
label: 'modal__body',
482+
__css: styles.value.body,
483+
}),
484+
{
485+
id: bodyId.value,
486+
...attrs,
487+
ref: bodyRef,
474488
},
475489
slots
476490
)
477491
},
478492
})
493+
494+
/**
495+
* CModalFooter
496+
*
497+
* Component that houses the footer of the modal.
498+
*
499+
* @see Docs https://next.vue.chakra-ui.com/docs/components/modal
500+
*/
501+
export const CModalFooter = defineComponent({
502+
name: 'CModalFooter',
503+
setup(_, { slots, attrs }) {
504+
const styles = useStyles()
505+
506+
const footerStyles = computed<SystemStyleObject>(() => ({
507+
display: 'flex',
508+
alignItems: 'center',
509+
justifyContent: 'flex-end',
510+
...styles.value.footer,
511+
}))
512+
513+
return () =>
514+
h(
515+
chakra('div', {
516+
label: 'modal__body',
517+
__css: footerStyles.value,
518+
}),
519+
attrs,
520+
slots
521+
)
522+
},
523+
})
524+
525+
/**
526+
* CModalCloseButton
527+
*
528+
* Used to close the modal. It internally invokes the `closeModal` event,
529+
* but also emits the `@click` event to the user.
530+
*
531+
* @see Docs https://next.vue.chakra-ui.com/docs/components/modal
532+
*/
533+
export const CModalCloseButton = defineComponent({
534+
name: 'CModalCloseButton',
535+
emits: ['click'],
536+
setup(_, { attrs, emit }) {
537+
const { closeModal } = unref(useModalContext())
538+
const styles = useStyles()
539+
540+
return () =>
541+
h(
542+
chakra(CCloseButton, {
543+
label: 'modal__close-button',
544+
__css: styles.value.closeButton,
545+
}),
546+
{
547+
...attrs,
548+
onClick: (e: MouseEvent | TouchEvent) => {
549+
closeModal()
550+
emit('click', e)
551+
},
552+
}
553+
)
554+
},
555+
})

packages/c-modal/src/use-modal.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ export function useModal(options: UseModalOptions) {
113113
},
114114
}))
115115

116-
watchEffect(() => {
117-
console.log(hasHeader, 'hasHeader HAS CHANGED FORM HOOK')
118-
})
119-
120116
const handleOverlayClick = (event: MouseEvent) => {
121117
event.stopPropagation()
122118
// @click.self modifier

0 commit comments

Comments
 (0)