Skip to content

Commit

Permalink
Added useModal() helper (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet authored Oct 26, 2024
1 parent cb43631 commit fa04aed
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 2 deletions.
11 changes: 11 additions & 0 deletions demo-app/resources/js/Pages/ComponentThatUsesModalInstance.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useModal } from '@inertiaui/modal-react'

export default function ComponentThatUsesModalInstance() {
const modal = useModal()

return (
modal ? <div>
<button onClick={() => modal.close()}>Close Modal with index {modal.index}</button>
</div> : <div />
)
}
11 changes: 11 additions & 0 deletions demo-app/resources/js/Pages/ComponentThatUsesModalInstance.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup>
import { useModal } from '@inertiaui/modal-vue'
const modal = useModal()
</script>

<template>
<div v-if="modal">
<button @click="modal.close()">Close Modal with index {{ modal.index }}</button>
</div>
</template>
2 changes: 2 additions & 0 deletions demo-app/resources/js/Pages/CreateRole.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useRef } from 'react';
import { useForm } from '@inertiajs/react';
import axios from 'axios';
import { Modal, ModalLink } from '@inertiaui/modal-react';
import ComponentThatUsesModalInstance from './ComponentThatUsesModalInstance.jsx';

export default function CreateRole({ headerValue }) {
const { data, setData, errors, post } = useForm({
Expand Down Expand Up @@ -86,6 +87,7 @@ export default function CreateRole({ headerValue }) {
<Modal name="another-local-modal">
Hawaiian noises?
</Modal>
<ComponentThatUsesModalInstance />
</>
)}
</Modal>
Expand Down
3 changes: 3 additions & 0 deletions demo-app/resources/js/Pages/CreateRole.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useForm } from '@inertiajs/vue3'
import { default as Axios } from 'axios'
import { Modal, ModalLink } from '@inertiaui/modal-vue'
import { ref } from 'vue'
import ComponentThatUsesModalInstance from './ComponentThatUsesModalInstance.vue'
defineProps(['headerValue'])
Expand Down Expand Up @@ -97,5 +98,7 @@ function submit() {
<Modal name="another-local-modal">
Hawaiian noises?
</Modal>

<ComponentThatUsesModalInstance />
</Modal>
</template>
2 changes: 2 additions & 0 deletions demo-app/resources/js/Pages/EditUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { useState, useRef } from 'react';
import { useForm, usePage } from '@inertiajs/react';
import { Modal, ModalLink } from '@inertiaui/modal-react';
import ComponentThatUsesModalInstance from './ComponentThatUsesModalInstance.jsx';

export default function EditUser({ user, roles }) {
const [message, setMessage] = useState('');
Expand Down Expand Up @@ -123,6 +124,7 @@ export default function EditUser({ user, roles }) {
</button>
</div>
</form>
<ComponentThatUsesModalInstance />
</>
)}
</Modal>
Expand Down
3 changes: 3 additions & 0 deletions demo-app/resources/js/Pages/EditUser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { useForm } from '@inertiajs/vue3';
import { Modal, ModalLink } from '@inertiaui/modal-vue';
import { ref } from 'vue';
import ComponentThatUsesModalInstance from './ComponentThatUsesModalInstance.vue';
const props = defineProps({
user: Object,
Expand Down Expand Up @@ -86,5 +87,7 @@ function onMessage(message) {
</button>
</div>
</form>

<ComponentThatUsesModalInstance />
</Modal>
</template>
2 changes: 2 additions & 0 deletions demo-app/resources/js/Pages/Users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { Link } from '@inertiajs/react';
import { ModalLink } from '@inertiaui/modal-react';
import Container from './Container';
import ComponentThatUsesModalInstance from './ComponentThatUsesModalInstance.jsx';

export default function Users({ users, random, navigate }) {
const alertGreeting = (greeting) => {
Expand Down Expand Up @@ -40,6 +41,7 @@ export default function Users({ users, random, navigate }) {
))}
</ul>
</div>
<ComponentThatUsesModalInstance />
</Container>
);
};
2 changes: 2 additions & 0 deletions demo-app/resources/js/Pages/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { onMounted,ref } from 'vue';
import Container from './Container.vue'
import { ModalLink } from '@inertiaui/modal-vue'
import { Link } from '@inertiajs/vue3'
import ComponentThatUsesModalInstance from './ComponentThatUsesModalInstance.vue';
defineProps({
users: Object,
Expand Down Expand Up @@ -57,5 +58,6 @@ function alertGreeting(greeting) {
</li>
</ul>
</div>
<ComponentThatUsesModalInstance />
</Container>
</template>
29 changes: 29 additions & 0 deletions demo-app/tests/Browser/UseModalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Tests\Browser;

use PHPUnit\Framework\Attributes\Test;
use Tests\DuskTestCase;

class UseModalTest extends DuskTestCase
{
#[Test]
public function it_can_inject_the_current_modal_context_from_a_component()
{
$this->browse(function (Browser $browser) {
$browser->visit('/users/1/edit')
->waitForFirstUser()
->waitFor('.im-dialog')
->within('.im-dialog[data-inertiaui-modal-index="0"]', function (Browser $browser) {
$browser->assertSee('Close Modal with index 0');
})
->clickLink('Add Role')
->waitFor('.im-dialog[data-inertiaui-modal-index="1"]')
->within('.im-dialog[data-inertiaui-modal-index="1"]', function (Browser $browser) {
$browser->assertSee('Close Modal with index 1');
})
->press('Close Modal with index 1')
->waitUntilMissing('.im-dialog[data-inertiaui-modal-index="1"]');
});
}
}
3 changes: 2 additions & 1 deletion react/src/inertiauiModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { useModalStack, ModalRoot, ModalStackProvider, renderApp } from './Modal
import HeadlessModal from './HeadlessModal.jsx'
import Modal from './Modal.jsx'
import ModalLink from './ModalLink.jsx'
import useModal from './useModal.js'

export { getConfig, putConfig, resetConfig, useModalStack, useModalIndex, HeadlessModal, Modal, ModalLink, ModalRoot, ModalStackProvider, renderApp }
export { getConfig, putConfig, resetConfig, useModalStack, useModalIndex, HeadlessModal, Modal, ModalLink, ModalRoot, ModalStackProvider, renderApp, useModal }
6 changes: 6 additions & 0 deletions react/src/useModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useModalIndex } from './ModalRenderer.jsx'
import { useModalStack } from './ModalRoot.jsx'

export default function useModal() {
return useModalStack().stack[useModalIndex()] ?? null
}
3 changes: 2 additions & 1 deletion vue/src/inertiauiModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import HeadlessModal from './HeadlessModal.vue'
import Modal from './Modal.vue'
import ModalLink from './ModalLink.vue'
import ModalRoot from './ModalRoot.vue'
import useModal from './useModal.js'

function visitModal(url, options = {}) {
return useModalStack().visit(
Expand All @@ -19,4 +20,4 @@ function visitModal(url, options = {}) {
)
}

export { HeadlessModal, Modal, ModalLink, ModalRoot, getConfig, putConfig, resetConfig, visitModal, renderApp }
export { HeadlessModal, Modal, ModalLink, ModalRoot, getConfig, putConfig, resetConfig, visitModal, renderApp, useModal }
5 changes: 5 additions & 0 deletions vue/src/useModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { inject } from 'vue'

export default function useModal() {
return inject('modalContext', null)
}

0 comments on commit fa04aed

Please sign in to comment.