Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b15995a

Browse files
authored
Show the Inertia Progress Bar on non-Inertia requests (#154)
* wip * wip * Fix styling * wip * Create ProgressBarTest.php * Update ProgressBarTest.php * Update ProgressBarTest.php
1 parent 7aed80a commit b15995a

File tree

10 files changed

+1234
-1008
lines changed

10 files changed

+1234
-1008
lines changed

demo-app/package-lock.json

Lines changed: 381 additions & 337 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tests\Browser;
4+
5+
use InertiaUI\Modal\Support;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Tests\DuskTestCase;
8+
9+
class ProgressBarTest extends DuskTestCase
10+
{
11+
#[Test]
12+
public function it_shows_the_inertia_progress_bar()
13+
{
14+
if (Support::isInertiaV1()) {
15+
return $this->markTestSkipped('Proress Bar API not supported in Inertia v1');
16+
}
17+
18+
$this->browse(function (Browser $browser) {
19+
$browser->visit('/loading-prop')
20+
->waitForText('Loading Prop')
21+
->clickLink('Open Slideover')
22+
->assertPresent('#nprogress')
23+
->waitForModal()
24+
->waitUntilMissing('#nprogress');
25+
});
26+
}
27+
}

react/package-lock.json

Lines changed: 380 additions & 321 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"@headlessui/react": "^2.1.0",
2929
"@heroicons/react": "^2.1.4",
30-
"@inertiajs/react": "^1.3.0||^2.0.0",
30+
"@inertiajs/react": "^1.3.0||^2.1.11",
3131
"@vitejs/plugin-react": "^4.3.1",
3232
"axios": "^1.6.0",
3333
"clsx": "^2.1.1",
@@ -46,7 +46,7 @@
4646
"vite": "^6.1"
4747
},
4848
"peerDependencies": {
49-
"@inertiajs/react": "^1.3.0||^2.0.0",
49+
"@inertiajs/react": "^1.3.0||^2.1.11",
5050
"axios": "^1.6.0",
5151
"react": "^18.2.0||^19.0.0",
5252
"react-dom": "^18.2.0||^19.0.0"

react/src/ModalRoot.jsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createElement, useEffect, useState, useRef } from 'react'
22
import { default as Axios } from 'axios'
3-
import { except, kebabCase, generateId, sameUrlPath } from './helpers'
3+
import { except, kebabCase, generateId, sameUrlPath, isInertiaV2 } from './helpers'
44
import { router, usePage } from '@inertiajs/react'
5+
import * as InertiaReact from '@inertiajs/react'
56
import { mergeDataIntoQueryString } from '@inertiajs/core'
67
import { createContext, useContext } from 'react'
78
import ModalRenderer from './ModalRenderer'
@@ -413,10 +414,22 @@ export const ModalStackProvider = ({ children }) => {
413414
})
414415
}
415416

416-
//
417-
418417
onStart?.()
419418

419+
const withProgress = (callback) => {
420+
if (!isInertiaV2()) {
421+
return
422+
}
423+
424+
try {
425+
callback(InertiaReact.progress)
426+
} catch (e) {
427+
// ignore
428+
}
429+
}
430+
431+
withProgress((progress) => progress.start())
432+
420433
Axios({
421434
url,
422435
method,
@@ -431,6 +444,9 @@ export const ModalStackProvider = ({ children }) => {
431444
onError?.(...args)
432445
reject(...args)
433446
})
447+
.finally(() => {
448+
withProgress((progress) => progress.finish())
449+
})
434450
})
435451
}
436452

react/src/helpers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
import { router } from '@inertiajs/react'
12
import { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase, isStandardDomEvent } from './../../vue/src/helpers.js'
2-
export { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase, isStandardDomEvent }
3+
4+
function isInertiaV2() {
5+
return router.push && typeof router.push === 'function'
6+
}
7+
8+
export { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase, isStandardDomEvent, isInertiaV2 }

0 commit comments

Comments
 (0)