Skip to content

Commit df104a1

Browse files
feat: AlertDialog
1 parent 23d67b2 commit df104a1

28 files changed

+638
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Enter the component you want most in the components, leave the emojis and follow
3434
| Component | Status |
3535
| --------------------------------------------------------------------------------------------- | ------ |
3636
| [Accordion](https://vue-primitives.netlify.app/?path=/story/components-accordion--single) ||
37-
| AlertDialog | 🚧 |
37+
| [AlertDialog](https://vue-primitives.netlify.app/?path=/story/components-alertdialog--styled) | |
3838
| [AspectRatio](https://vue-primitives.netlify.app/?path=/story/components-aspectratio--styled) ||
3939
| [Avatar](https://vue-primitives.netlify.app/?path=/story/components-avatar--styled) ||
4040
| [Checkbox](https://vue-primitives.netlify.app/?path=/story/components-checkbox--styled) ||
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script setup lang="ts">
2+
import { DialogClose } from '../dialog/index.ts'
3+
import { useAlertDialogContentContext } from './AlertDialogContent.ts'
4+
5+
defineOptions({
6+
name: 'AlertDialogCancel',
7+
})
8+
9+
const context = useAlertDialogContentContext('AlertDialogCancel')
10+
11+
function setCancelRef(nodeRef: any) {
12+
const node = nodeRef ? nodeRef.$el : undefined
13+
context.cancelRef.current = node
14+
}
15+
</script>
16+
17+
<template>
18+
<DialogClose :ref="setCancelRef">
19+
<slot />
20+
</DialogClose>
21+
</template>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { DialogContentImplEmits } from '../dialog/DialogContentImpl.ts'
2+
import { type MutableRefObject, createContext } from '../hooks/index.ts'
3+
4+
// eslint-disable-next-line ts/consistent-type-definitions
5+
export type AlertDialogContentEmits = {
6+
/**
7+
* Event handler called when auto-focusing on open.
8+
* Can be prevented.
9+
*/
10+
openAutoFocus: DialogContentImplEmits['openAutoFocus']
11+
}
12+
13+
export type AlertDialogContentElement = HTMLDivElement
14+
15+
export type AlertDialogCancelElement = HTMLButtonElement
16+
17+
export interface AlertDialogContentContextValue {
18+
cancelRef: MutableRefObject<AlertDialogCancelElement | undefined>
19+
}
20+
21+
export const [provideAlertDialogContentContext, useAlertDialogContentContext] = createContext<AlertDialogContentContextValue>('AlertDialogContent')
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script setup lang="ts">
2+
import { DialogContent } from '../dialog/index.ts'
3+
import { useRef } from '../hooks/index.ts'
4+
import { composeEventHandlers } from '../utils/vue.ts'
5+
import { type AlertDialogCancelElement, type AlertDialogContentElement, type AlertDialogContentEmits, provideAlertDialogContentContext } from './AlertDialogContent.ts'
6+
7+
defineOptions({
8+
name: 'AlertDialogContent',
9+
})
10+
11+
const emit = defineEmits<AlertDialogContentEmits>()
12+
13+
const cancelRef = useRef<AlertDialogCancelElement>()
14+
15+
provideAlertDialogContentContext({
16+
cancelRef,
17+
})
18+
19+
function preventDefault(event: Event) {
20+
event.preventDefault()
21+
}
22+
23+
const onOpenAutoFocus = composeEventHandlers((event) => {
24+
emit('openAutoFocus', event)
25+
}, (event) => {
26+
event.preventDefault()
27+
cancelRef.current?.focus({ preventScroll: true })
28+
})
29+
</script>
30+
31+
<template>
32+
<DialogContent
33+
role="alertdialog"
34+
@open-auto-focus="onOpenAutoFocus"
35+
@pointerdown-outside="preventDefault"
36+
@interact-outside="preventDefault"
37+
>
38+
<slot />
39+
</DialogContent>
40+
</template>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
import { Dialog } from '../dialog/index.ts'
3+
4+
defineOptions({
5+
name: 'AlertDialogRoot',
6+
})
7+
</script>
8+
9+
<template>
10+
<Dialog :modal="true">
11+
<slot />
12+
</Dialog>
13+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export { default as AlertDialogRoot } from './AlertDialogRoot.vue'
2+
export { default as AlertDialogContent } from './AlertDialogContent.vue'
3+
export { DialogTrigger as AlertDialogTrigger } from '../dialog/index.ts'
4+
export { DialogTitle as AlertDialogTitle } from '../dialog/index.ts'
5+
export { DialogDescription as AlertDialogDescription } from '../dialog/index.ts'
6+
export { default as AlertDialogCancel } from './AlertDialogCancel.vue'
7+
export { DialogClose as AlertDialogAction } from '../dialog/index.ts'
8+
export { DialogOverlay as AlertDialogOverlay } from '../dialog/index.ts'
9+
export { DialogPortal as AlertDialogPortal } from '../dialog/index.ts'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import CStyled from './Styled.vue'
2+
import CControlled from './Controlled.vue'
3+
import CChromatic from './Chromatic.vue'
4+
5+
export default { title: 'Components/AlertDialog' }
6+
7+
export function Styled() {
8+
return CStyled
9+
}
10+
11+
export function Controlled() {
12+
return CControlled
13+
}
14+
15+
export function Chromatic() {
16+
return CChromatic
17+
}
18+
19+
Chromatic.parameters = { chromatic: { disable: false } }

0 commit comments

Comments
 (0)