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

Reactive props with programmatic modals #2274

Open
rigtigeEmil opened this issue Sep 28, 2024 · 4 comments
Open

Reactive props with programmatic modals #2274

rigtigeEmil opened this issue Sep 28, 2024 · 4 comments
Labels
question Further information is requested

Comments

@rigtigeEmil
Copy link

Description

Heya. Is there a way to open a modal, while passing props and keep their reactivity?

const localModalValue = ref(false);
const isLoading = ref(false);

useModal().open(MyModal, {
	modelValue: localModalValue,
	isLoading: isLoading
});
@rigtigeEmil rigtigeEmil added the question Further information is requested label Sep 28, 2024
@noook
Copy link
Collaborator

noook commented Sep 30, 2024

If your MyModal component emits events (the recommended way to update a prop), you should be able to achieve something like this:

const localModalValue = ref(false);
const isLoading = ref(false);

useModal().open(MyModal, {
  modelValue: localModalValue.value,
  isLoading: isLoading.value,
  'onUpdate:modelValue': value => localModalValue.value = value,
  'onUpdate:isLoading': value => isLoading.value = value,
});

This example assumes the events that update those props are update:model-value and update:is-loading adjust depending on your event names.

@rigtigeEmil
Copy link
Author

@noook This part is fine, the issue is that if i update localModalValue (outside of the modal), it's not reflected in the modal itself

@noook
Copy link
Collaborator

noook commented Sep 30, 2024

Yes, this is a caveat of using programmatic modals. Values are only passed at instantiation and are not tracked by the reactive system at this point. Your usage of the modal by passing refs should make the modal reactive to the ref changes. Typescript might complain but it could actually work. Isn't it already reactive ?

@messenjer
Copy link

You can use patch

	const localModalValue = ref(false);
	const isLoading = ref(false);

	const modal = useModal();
	modal.open(MyModal, {
		modelValue: localModalValue,
		isLoading: isLoading,
	});

	watch(isLoading, () => modal.patch<typeof MyModal>({ isLoading: isLoading }));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants