# kil-loader

Zero-dependency page & AJAX loader that **auto-injects** its CSS + DOM, shows on **first page load**, and tracks **fetch / XHR / Axios** out of the box.

- ✅ No CSS file required  
- ✅ Auto adds `<div class="kil-main-loader">…</div>` if missing  
- ✅ Works without jQuery  
- ✅ Tiny public API for manual control  
- ✅ UMD build (works with `<script>`, ESM bundlers)

---

## Table of Contents

- [Install](#install)
- [CDN](#cdn)
- [Quick Start](#quick-start)
- [Automatic Behavior](#automatic-behavior)
- [Configuration](#configuration)
- [Public API](#public-api)
- [Optional HTML Markup](#optional-html-markup)
- [Disable Initial Overlay](#disable-initial-overlay)
- [Events](#events)
- [Framework Tips](#framework-tips)
- [Accessibility](#accessibility)
- [SSR / Non-Browser](#ssr--non-browser)
- [Troubleshooting](#troubleshooting)
- [License](#license)

---

## Install

```bash
npm i kil-loader
```

### Bundlers

```js
import KilLoader, { createKilLoader } from "kil-loader";

// (optional) customize BEFORE first use
KilLoader.config.backgroundAjax = "rgba(0,0,0,.06)";

// Next.js or manual control:
// const loader = createKilLoader({ autoShowOnPageLoad: false }, { autoInit: true });
```

---

## CDN

> Single script – no CSS tag required.

```html
<script defer src="https://cdn.jsdelivr.net/npm/kil-loader@1/kil-loader.min.js"></script>
```

It auto-appears on first page load and around `fetch`/XHR/Axios calls.

---
In each project you have to put only cdn link and in common header  following thing But if you dont put not issue becaue  No markup needed—the script auto-inserts: 

```html
<div class="kil-main-loader" aria-busy="true" aria-live="polite">
  <span class="kil-page-loader"></span>
</div>
```

## Quick Start

> **Nothing else required.** The script injects its own DOM and CSS.

```html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Kil Loader Demo</title>
    <script defer src="https://cdn.jsdelivr.net/npm/kil-loader@1/kil-loader.min.js"></script>
  </head>
  <body>
    <h1>Hello</h1>
  </body>
</html>
```

**Optional config (still auto):**

```html
<script defer src="https://cdn.jsdelivr.net/npm/kil-loader@1/kil-loader.min.js"></script>
<script defer>
  KilLoader.config.borderColor   = "#ff1744";
  KilLoader.config.innerRingTint = "rgba(255, 77, 109, 0.55)";
  KilLoader.config.backgroundAjax = "transparent"; // or a tint if you want
</script>
```

---

## Automatic Behavior

- **Page load overlay:** shown until the `window.load` event.
- **Network overlay:** shown while any **fetch / XHR / Axios** request is in flight.
- **Safety timeout:** hides after **15s** max if `load` never fires.
- **Scroll lock:** prevents scroll while visible.

No initialization code is required for the CDN/UMD build or the default import.

---

## Configuration

> Set properties **before** first render for best effect.

```js
KilLoader.config = {
  // timing
  pageSafetyTimeoutMs: 15000,
  fadeInMs: 120,
  fadeOutMs: 220,

  // visuals
  backgroundPage: "#ffffff",           // first page overlay background
  backgroundAjax: "transparent",       // ajax overlay background
  borderColor: "#2E37A4",              // spinner border
  innerRingTint: "rgba(46,55,164,.35)",

  // behavior
  autoTrack: true,                     // intercept fetch/XHR/Axios
  autoShowOnPageLoad: true,            // show on initial page load

  // classes / z-index
  classMain: "kil-main-loader",
  classSpinner: "kil-page-loader",
  lockScrollClass: "kil-lock-scroll",
  zIndex: 999999
};
```

You can also set individual keys:

```js
KilLoader.config.backgroundAjax = "rgba(0,0,0,.06)";
```

---

## Public API

```js
KilLoader.show();         // show (mode="ajax")
KilLoader.show("page");   // force page mode
KilLoader.hide();         // hide (respects pending requests + load state)
KilLoader.destroy();      // remove DOM + injected styles
```

Use manual control if you run long CPU tasks (non-network) and want a visual indicator.

---

## Optional HTML Markup

> **Not required.** The script auto-injects if missing.
> Add it yourself only to control exact placement in the DOM.

```html
<div class="kil-main-loader" aria-busy="true" aria-live="polite" data-mode="page">
  <span class="kil-page-loader"></span>
</div>
```

---

## Disable Initial Overlay

Keep AJAX loader, skip first-page overlay:

```html
<body data-kil-loader="off">
  ...
  <script defer src="https://cdn.jsdelivr.net/npm/kil-loader@1/kil-loader.min.js"></script>
</body>
```

---

## Events

```js
window.addEventListener("kil-loader:show", () => {
  // overlay became visible
});
window.addEventListener("kil-loader:hide", () => {
  // overlay fully hidden
});
```

---

## Framework Tips

**React / Next.js**

```js
// next/script with strategy="afterInteractive" or in _document.js
router.events.on("routeChangeStart", () => KilLoader.show());
router.events.on("routeChangeComplete", () => KilLoader.hide());
router.events.on("routeChangeError", () => KilLoader.hide());
```

**Next.js (App Router)**

Use Next's `app/loading.tsx` for route/server loading, and use kil-loader for client requests:

```tsx
// app/loading.tsx
export default function Loading() {
  return <div className="kil-main-loader"><span className="kil-page-loader" /></div>;
}
```

```tsx
// app/kil-loader-provider.tsx
"use client";
import { useEffect } from "react";
import { createKilLoader } from "kil-loader";

export function KilLoaderProvider() {
  useEffect(() => {
    const loader = createKilLoader({ autoShowOnPageLoad: false }, { autoInit: true });
    return () => loader.destroy();
  }, []);
  return null;
}
```

```tsx
// app/layout.tsx
import { KilLoaderProvider } from "./kil-loader-provider";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <KilLoaderProvider />
        {children}
      </body>
    </html>
  );
}
```

**Vue Router**

```js
router.beforeEach((to, from, next) => { KilLoader.show(); next(); });
router.afterEach(() => KilLoader.hide());
```

**Plain JS (SPA)**

The loader lightly hooks `history.pushState/replaceState` for a tiny flash on client navigation.
You can also call `KilLoader.show()/hide()` around your custom transitions.

---

## Accessibility

- `aria-busy="true"` and `aria-live="polite"` on overlay
- Locks scroll via `body.kil-lock-scroll`
- Honors `prefers-reduced-motion`

---

## SSR / Non-Browser

If `window`/`document` are unavailable, the module exports safe **no-ops**:

```js
import KilLoader from "kil-loader";
KilLoader.show(); // does nothing on the server
```

---

## Troubleshooting

- **Loader never hides on first load**  
  Some assets may block the `load` event. The safety timer (default **15s**) forces a hide:
  ```js
  KilLoader.config.pageSafetyTimeoutMs = 8000;
  ```

- **No overlay during AJAX**  
  Ensure you use `fetch`, raw `XMLHttpRequest`, or `axios`. For custom clients, wrap with `KilLoader.show()/hide()`.

- **Conflicts with other interceptors**  
  If another lib patches `fetch`/XHR after kil-loader initializes, load kil-loader later or control manually.

---

## License

MIT © Vasu Birla
