# Page Loading Spinner Setup

This setup provides comprehensive loading indicators for your Laravel React Inertia.js application that handle **ALL** types of navigation:

## ✨ Enhanced Loading Detection

The loading system now detects and shows loading spinners for:
- ✅ **Initial page loads** (typing URL and pressing enter)
- ✅ **Inertia.js navigation** (clicking links, form submissions)
- ✅ **Browser navigation** (back/forward buttons)
- ✅ **Manual URL changes** (address bar navigation)
- ✅ **Page refreshes** (F5, Ctrl+R)
- ✅ **Tab switching** (returning to tab after navigation)

## 1. Full-Screen Loading Spinner (`PageLoadingSpinner`)

A full-screen overlay with a centered spinner that appears during page transitions.

### Features:
- ✅ Full-screen backdrop blur overlay
- ✅ Centered loading spinner with animation
- ✅ Smooth fade in/out animations
- ✅ **Handles ALL navigation types** (not just Inertia)
- ✅ Different loading text for initial load vs navigation
- ✅ Automatic cleanup and memory management

### Usage:
The `PageLoadingSpinner` is already integrated into:
- Main app layout (`app.tsx`)
- App sidebar layout (`app-sidebar-layout.tsx`)

No additional setup required - it will automatically show during **any** type of page navigation.

## 2. Top Loading Bar (`TopLoadingBar`)

A slim progress bar at the top of the page, similar to GitHub's loading indicator.

### Features:
- ✅ Minimal top bar design
- ✅ Gradient color animation
- ✅ Smooth width transition
- ✅ **Handles ALL navigation types** (not just Inertia)
- ✅ Less intrusive than full-screen spinner

### Usage:
To use the top loading bar instead of the full-screen spinner:

1. Replace `PageLoadingSpinner` with `TopLoadingBar` in your layouts:

```tsx
// In app.tsx
import { TopLoadingBar } from './components/top-loading-bar';

// Replace PageLoadingSpinner with TopLoadingBar
root.render(
    <>
        <App {...props} />
        <TopLoadingBar />
    </>
);
```

2. Do the same in `app-sidebar-layout.tsx`:

```tsx
import { TopLoadingBar } from '@/components/top-loading-bar';

export default function AppSidebarLayout({ children, breadcrumbs = [] }) {
    return (
        <>
            <AppShell variant="sidebar">
                {/* ... existing code ... */}
            </AppShell>
            <TopLoadingBar />
        </>
    );
}
```

## 3. Basic Loading Spinner Component (`LoadingSpinner`)

A reusable loading spinner component for forms, buttons, or any loading states.

### Usage:
```tsx
import { LoadingSpinner } from '@/components/ui/loading-spinner';

// Basic usage
<LoadingSpinner />

// With custom size and text
<LoadingSpinner size="lg" text="Processing..." />

// In a button
<Button disabled={isLoading}>
    {isLoading ? <LoadingSpinner size="sm" text="" /> : 'Submit'}
</Button>
```

### Props:
- `size`: "sm" | "md" | "lg" (default: "md")
- `text`: string (default: "Loading...")
- `className`: string (optional)

## 4. usePageLoading Hook (`use-page-loading.tsx`)

A sophisticated React hook that detects all types of page loading and navigation states.

### Features:
- ✅ **Comprehensive detection** of all navigation types
- ✅ **Initial page load detection** (document ready state monitoring)
- ✅ **Browser navigation** (back/forward button detection)
- ✅ **Manual URL navigation** (address bar changes)
- ✅ **Tab visibility** handling (proper cleanup when switching tabs)
- ✅ **Memory efficient** with proper event cleanup

### Usage:
```tsx
import { usePageLoading } from '@/hooks/use-page-loading';

function MyComponent() {
    const { 
        isLoading,        // Overall loading state
        isInitialLoad,    // True during initial page load
        isNavigating,     // True during Inertia navigation
        isBrowserNav      // True during browser back/forward
    } = usePageLoading();

    return (
        <div>
            {isLoading && <div>Loading...</div>}
            {isInitialLoad && <div>Loading page...</div>}
            {isNavigating && <div>Navigating...</div>}
        </div>
    );
}
```

### Return Values:
- `isLoading`: boolean - True when any type of loading is happening
- `isInitialLoad`: boolean - True during initial page load only
- `isNavigating`: boolean - True during Inertia.js navigation
- `isBrowserNav`: boolean - True during browser back/forward navigation

## Customization

### Colors:
- Edit the gradient colors in `TopLoadingBar`: `from-blue-500 to-green-500`
- Edit the spinner color in `LoadingSpinner`: `text-primary`

### Animation Duration:
- Modify `transition={{ duration: 0.8 }}` in `TopLoadingBar`
- Modify `transition={{ duration: 0.2 }}` in `PageLoadingSpinner`

### Inertia Progress Bar:
The existing Inertia progress bar is configured in `app.tsx`:
```tsx
progress: {
    color: '#4B5563',
},
```

You can disable it by setting `progress: false` if you prefer using only the custom loading components.

## Troubleshooting

### ✅ FIXED: Loading spinner not showing for manual URL entry:
**Issue**: The loading spinner only responded to Inertia.js navigation, not when typing URLs manually.  
**Solution**: Updated components to use the `usePageLoading` hook which detects all navigation types including:
- Manual URL entry (typing and pressing enter)
- Browser back/forward buttons
- Page refreshes
- Tab switching

### Loading spinner not showing for Inertia navigation:
1. Ensure the component is imported correctly
2. Check that Inertia.js events are firing (check browser dev tools)
3. Verify that `motion/react` is installed

### TypeScript errors:
- Ensure `@inertiajs/react` and `motion/react` are properly installed
- Check that all import paths are correct
- Ensure the `usePageLoading` hook is imported from the correct path

### Z-index issues:
- The loading components use `z-50` - adjust if needed for your layout
- Ensure no other elements have higher z-index values
