question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error closing Dialog when initial state is set with something other than `ref(true)`

See original GitHub issue

What package within Headless UI are you using?

@headlessui/vue

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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
RobinMalfaitcommented, Apr 13, 2021

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

0reactions
bakerkretzmarcommented, Apr 13, 2021

Ah okay I think I was confusing the behaviour of toRef and just ref. Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to clear react state in modal after closing? - Stack Overflow
I've tried a variable with the initial state then after closing filling the state with it, but it did not work. Tried to...
Read more >
Modal Dialog Example | APG | WAI - W3C
Initial focus is set on the first input, which is the first focusable element. The dialog does not need aria-describedby since there is...
Read more >
Dialog (Modal) - Headless UI
Dialogs have no automatic management of their open/closed state. To show and hide your dialog, pass React state into the open prop. When...
Read more >
useHooks - Easy to understand React Hook recipes
Hooks are a feature in React that allow you use state and other React features ... function returns true then the hook returns...
Read more >
Dialog | Angular Material
API reference for Angular Material dialog ... Stream that emits when all open dialog have finished closing. ... Used to set the aria-modal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found