Error closing Dialog when initial state is set with something other than `ref(true)`
See original GitHub issueWhat package within Headless UI are you using?
What version of that package are you using?
0.3.1-6fa6c45
What browser are you using?
Firefox
Reproduction repository
https://codesandbox.io/s/jovial-snowflake-p4s76?file=/src/Modal.vue
Description
If the value passed in as the open
prop of a Dialog
is created with something other than literally ref(true)
or ref(false)
, updating it causes this error: Set operation on key "open" failed: target is readonly.
.
E.g. I’m trying to create a generic Modal
wrapper component to use in a few places, so I want open
and @close
available outside of the component using the Dialog
. If I try to pass in open
as a prop though, I can’t seem to use it without that ‘readonly’ error.
Given this component:
<template>
<Dialog as="template" :open="open" @close="onClose" static>
<div>
<DialogOverlay v-if="open" />
<div v-if="open">
<slot />
</div>
</div>
</Dialog>
</template>
<script>
import { toRefs } from "vue";
import { Dialog, DialogOverlay } from "@headlessui/vue";
export default {
components: { Dialog, DialogOverlay },
props: { open: Boolean },
emits: ["close"],
setup(props, { emit }) {
const { open } = toRefs(props);
return {
open,
onClose(val) {
open.value = val;
emit("close");
},
};
},
};
</script>
Changing the open
prop works (can close the Dialog this way) but produces the error, but clicking outside the Dialog or pressing <kbd>Esc</kbd> produce the error and also don’t actually work.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Yeah, it’s not a Headless UI issue. It is just because you are mutating your props. Here is an example in pure Vue with a button and a count value:
https://codesandbox.io/s/elated-darkness-d6wcg?file=/src/components/HelloWorld.vue
Ah okay I think I was confusing the behaviour of
toRef
and justref
. Thanks a lot!