Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to handle reactive props/attrs between parent and child components #429

Open
retikaellis opened this issue Feb 12, 2024 · 3 comments
Open
Assignees
Labels
question Further information is requested

Comments

@retikaellis
Copy link

Version

vue-final-modal: 4.5.3
vue: 3.4.15
nuxt: 3.10.0

Hi! First of all, thank you so much for your work. I'm starting to try the modal in a nuxt 3 app, using useModal(). I don't know if I'm missing something, but I realized the props you pass as attrs are not reactive/are not changing in the child component (ModalConfirm in this case). Here's a code sample. What I'm actually trying to achieve is to change a isLoading boolean prop, so I can show a loading spinner on the confirm button, while making an async request to my API on the onConfirm action.

const modalTitle = ref('')

const { open, close } = useModal({ component: ModalConfirm, attrs: { title: modalTitle.value, onClose() { close() }, onConfirm() { modalTitle.value = "Hello" }, }, slots: { default: "Whatever" }, })

@retikaellis retikaellis added the question Further information is requested label Feb 12, 2024
@xthree
Copy link

xthree commented Mar 6, 2024

I also need reactive props/attrs.

I think I am going to attempt making a wrapping composable that watches any prop changes and updates the props using the patchOptions function provided by useModal

This is function is demonstrated here https://vue-final-modal.org/api/composables/use-modal

@xthree
Copy link

xthree commented Mar 7, 2024

Here's my implementation. Seems to work well. @retikaellis

// reactiveModal.ts
import { useModal, type UseModalOptions } from 'vue-final-modal';

type useReactiveModalOptions = UseModalOptions<any> & {
	attrs: ComputedRef;
};

export const useReactiveModal = (options: useReactiveModalOptions) => {
	const modal = useModal(options);
	const { patchOptions } = modal;

	watch(
		() => options.attrs,
		newVal => {
			patchOptions({ attrs: newVal });
		}
	);

	return modal;
};

@Tsyklop
Copy link

Tsyklop commented Aug 22, 2024

Works for me:

const modalProps = reactive<ContentProps>({});

const { open, close} = useModal({
  component: ModalContent,
  attrs: modalProps ,
})

const toggleModalHandler = (data: ContentProps) => {
  modalProps.someProp = data.someProp;
  other props
  open();
};

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

No branches or pull requests

4 participants