Skip to content
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

backdrop static #1

Merged
merged 1 commit into from
Jun 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions packages/@headlessui-vue/src/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface StateDefinition {

titleId: Ref<string | null>

backdrop: Ref<string | null>

setTitleId(id: string | null): void

close(): void
Expand All @@ -69,9 +71,10 @@ export let Dialog = defineComponent({
inheritAttrs: false, // Manually handling this
props: {
as: { type: [Object, String], default: 'div' },
backdrop: { type: String, default: 'normal' },
static: { type: Boolean, default: false },
unmount: { type: Boolean, default: true },
open: { type: Boolean, default: Missing },
open: { type: [Boolean, String], default: Missing },
initialFocus: { type: Object as PropType<HTMLElement | null>, default: null },
},
emits: ['close'],
Expand Down Expand Up @@ -116,7 +119,6 @@ export let Dialog = defineComponent({

let usesOpenClosedState = useOpenClosed()
let open = computed(() => {
// @ts-expect-error We are comparing to a uuid stirng at runtime
if (props.open === Missing && usesOpenClosedState !== null) {
// Update the `open` prop based on the open closed state
return match(usesOpenClosedState.value, {
Expand All @@ -128,7 +130,6 @@ export let Dialog = defineComponent({
})

// Validations
// @ts-expect-error We are comparing to a uuid stirng at runtime
let hasOpen = props.open !== Missing || usesOpenClosedState !== null

if (!hasOpen) {
Expand All @@ -137,13 +138,13 @@ export let Dialog = defineComponent({

if (typeof open.value !== 'boolean') {
throw new Error(
`You provided an \`open\` prop to the \`Dialog\`, but the value is not a boolean. Received: ${
open.value === Missing ? undefined : props.open
`You provided an \`open\` prop to the \`Dialog\`, but the value is not a boolean. Received: ${open.value === Missing ? undefined : props.open
}`
)
}

let dialogState = computed(() => (props.open ? DialogStates.Open : DialogStates.Closed))
let backdrop = computed(() => (props.backdrop))
let visible = computed(() => {
if (usesOpenClosedState !== null) {
return usesOpenClosedState.value === State.Open
Expand Down Expand Up @@ -184,6 +185,7 @@ export let Dialog = defineComponent({
let api = {
titleId,
dialogState,
backdrop,
setTitleId(id: string | null) {
if (titleId.value === id) return
titleId.value = id
Expand All @@ -196,16 +198,20 @@ export let Dialog = defineComponent({
provide(DialogContext, api)

// Handle outside click
useWindowEvent('mousedown', event => {
let target = event.target as HTMLElement

if (dialogState.value !== DialogStates.Open) return
if (containers.value.size !== 1) return
if (contains(containers.value, target)) return
if (backdrop.value !== 'static') {
useWindowEvent('mousedown', event => {
let target = event.target as HTMLElement

api.close()
nextTick(() => target?.focus())
})
if (dialogState.value !== DialogStates.Open) return
if (containers.value.size !== 1) return
if (contains(containers.value, target)) return


api.close()
nextTick(() => target?.focus())
})
}

// Scroll lock
watchEffect(onInvalidate => {
Expand Down Expand Up @@ -259,6 +265,7 @@ export let Dialog = defineComponent({
describedby,
visible,
open,
backdrop,
handleClick(event: MouseEvent) {
event.stopPropagation()
},
Expand Down Expand Up @@ -302,12 +309,14 @@ export let DialogOverlay = defineComponent({
})
},
setup() {
const { backdrop } = useDialogContext('DialogContext');
let api = useDialogContext('DialogOverlay')
let id = `headlessui-dialog-overlay-${useId()}`

return {
id,
handleClick(event: MouseEvent) {
if (backdrop.value == 'static') return;
event.preventDefault()
event.stopPropagation()
api.close()
Expand Down