# @synqro/tab-cycler

Create an automatic rotating tab experience in Webflow (or any site) with customizable timing, optional disabling of clicks, and external link support.

## Installation

1. Add the following script to your Webflow project:

   ```html
   <!-- [Synqro Labs] Tab Cycler -->
   <script src="https://cdn.jsdelivr.net/npm/@synqro/tab-cycler@1/dist/index.min.js"></script>
   ```

2. Create your tab structure in Webflow:

   ```html
   <div class="w-tabs" sl-tab-list>
     <a class="w-tab-link w--current" data-w-tab="Tab 1">Tab 1</a>
     <a class="w-tab-link" data-w-tab="Tab 2" sl-tab-click="none"
       >Tab 2 (disable)</a
     >
     <a class="w-tab-link" data-w-tab="Tab 3">Tab 3</a>
   </div>
   ```

3. Add or adjust these custom attributes to control behavior:

   | Attribute      | Value                     | Description                                                |
   | -------------- | ------------------------- | ---------------------------------------------------------- |
   | sl-tab-list    |                           | Activate the tab cycler                                    |
   | sl-tab-timer   | `5` (seconds)             | Time between tab change (default: 5 seconds)               |
   | sl-tab-click   | `none`, `https://…` or `/relative/path` | On a tab: disable clicks or link to URL. On any other element inside the tabs: turn it into a link. Absolute **and** relative URLs work. |
   | sl-tab-target  | `blank` or `self`         | Open the link in a new tab or the same tab. Default: external URLs open a new tab, internal (relative) links open the same tab. |
   | sl-tab-loading | `vertical` (optional)     | Animate a 0% → 100% loading bar. Animates width by default; set the value to `vertical` to animate height instead. |

4. Publish your Webflow project

## Links inside a tab

`sl-tab-click` with a URL isn't limited to the tab headers — you can put it on **any element inside the tabs container** (a button, a link, a card…). That element becomes clickable and opens the URL, while the tabs keep auto-cycling.

The URL can be **absolute** (`https://…`, opens a new tab by default) or **relative/internal** (`/expertises/…`, `#anchor`, opens the same tab by default). Add `sl-tab-target="self"` or `sl-tab-target="blank"` to override.

```html
<div class="w-tabs" sl-tab-list sl-tab-timer="5">
  <div class="w-tab-menu">
    <a class="w-tab-link w--current" data-w-tab="Tab 1">
      Tab 1
      <!-- CTA linking to an internal page — opens in the same tab -->
      <div sl-tab-click="/expertises/eco-conception" class="button">Learn more</div>
    </a>
    <a class="w-tab-link" data-w-tab="Tab 2">Tab 2</a>
  </div>
  <div class="w-tab-content">
    <div class="w-tab-pane w--tab-active" data-w-tab="Tab 1">
      <p>Some content…</p>
      <!-- external link — opens in a new tab -->
      <a class="button" sl-tab-click="https://synqro.co">Learn more</a>
    </div>
  </div>
</div>
```

> **Note:** the URL value must not contain spaces. `/expertises/ eco-conception` (note the space) will break — use `/expertises/eco-conception`.

## Loading bar direction

By default the loading bar fills horizontally (it animates `width` from 0% to 100%). To make it fill vertically instead — handy for a side progress indicator — set the attribute value to `vertical`, which animates `height`:

```html
<a class="w-tab-link w--current" data-w-tab="Tab 1">
  Tab 1
  <!-- vertical bar: give it a fixed width and 100% height container in your CSS -->
  <div sl-tab-loading="vertical" class="loading-bar-vertical"></div>
</a>
```

Style the bar yourself in Webflow — the script only animates the relevant dimension from 0% to 100%.

## Loading bar placement

A loading bar doesn't have to live inside the tab header. A single tab can drive **several bars at once**, each with its own orientation — useful when you want, say, a vertical bar inside the tab on desktop and a horizontal bar inside its pane on mobile (toggled with CSS). The script looks for bars in three places:

1. **Inside the tab** (`.w-tab-link`) — the default.
2. **Inside the matching pane** — Webflow pairs each tab with a `.w-tab-pane` (via `aria-controls` / `data-w-tab`). Drop a bar in the pane and it's linked automatically, no extra attribute needed. This works whether you put `sl-tab-list` on the `.w-tabs` wrapper or on the inner `.w-tab-menu` (panes then live in a sibling `.w-tab-content`).
3. **Anywhere on the page** — add `sl-tab-loading-for="<tab name>"` to link a bar to a tab by its `data-w-tab` value, wherever the bar sits in the DOM.

```html
<div class="w-tabs" sl-tab-list sl-tab-timer="5">
  <div class="w-tab-menu">
    <a class="w-tab-link w--current" data-w-tab="Tab 1">
      Tab 1
      <!-- desktop: vertical bar inside the tab -->
      <div sl-tab-loading="vertical" class="bar-desktop"></div>
    </a>
  </div>
  <div class="w-tab-content">
    <div class="w-tab-pane w--tab-active" data-w-tab="Tab 1">
      <p>Pane content…</p>
      <!-- mobile: horizontal bar inside the pane, linked via data-w-tab -->
      <div sl-tab-loading class="bar-mobile"></div>
    </div>
  </div>
</div>

<!-- or anywhere else on the page, linked explicitly -->
<div sl-tab-loading sl-tab-loading-for="Tab 1" class="bar-anywhere"></div>
```

## Custom events (hook your own script)

Every time a tab's cycle **(re)starts**, the script fires a bubbling `sl-tab-active` CustomEvent on the tab; every time a tab **stops**, it fires `sl-tab-inactive`. This is the extension point: listen on `document` and run whatever you need. Nothing else about the cycler (timer, loading bar) changes.

`sl-tab-active` fires on **every** way a tab relaunches — not just when the tab changes:

| Trigger                                   | Events                                   |
| ----------------------------------------- | ---------------------------------------- |
| Init / auto-cycle / click another tab     | `sl-tab-inactive` (old) → `sl-tab-active` (new) |
| Click the already-active tab              | `sl-tab-active` (restarts in place)       |
| Scroll the tabs **out** of view           | `sl-tab-inactive`                         |
| Scroll the tabs **back into** view        | `sl-tab-active` (relaunch)                |

So a listener stays in sync with the loading bar, which restarts in exactly these same situations. The `sl-tab-active` event is dispatched inside a `requestAnimationFrame`, so by the time it fires Webflow has already revealed the matching pane.

`event.detail` contains:

| Field       | What it is                                            |
| ----------- | ----------------------------------------------------- |
| `tab`       | The `.w-tab-link` element that became (in)active       |
| `pane`      | The `.w-tab-pane` paired with the tab (or `null`)      |
| `container` | The element carrying `sl-tab-list`                     |
| `index`     | The tab's position among all tabs (0-based)            |

```js
document.addEventListener("sl-tab-active", (e) => {
    console.log("Tab active:", e.detail.index, e.detail.pane);
});
document.addEventListener("sl-tab-inactive", (e) => {
    console.log("Tab inactive:", e.detail.tab);
});
```

### Example: notify an `<iframe>` inside the pane

A common use case is telling an embed (in a `<iframe>`) to start/stop when its tab becomes active — for example to play an animation only while visible. Drop this custom script anywhere on the page (Webflow: **Page Settings → Custom Code**, or an Embed):

```html
<script>
(function () {
    var post = function (iframe, msg) {
        try { iframe.contentWindow && iframe.contentWindow.postMessage(msg, "*"); } catch (e) {}
    };
    document.addEventListener("sl-tab-active", function (e) {
        var iframe = e.detail.pane && e.detail.pane.querySelector("iframe");
        if (!iframe) return;
        // Post now if the iframe is ready…
        post(iframe, "sl-tab-active");
        // …and again once it finishes loading (loading="lazy" iframes).
        iframe.addEventListener("load", function () { post(iframe, "sl-tab-active"); }, { once: true });
    });
    // Optional: pause the embed when its tab is left.
    document.addEventListener("sl-tab-inactive", function (e) {
        var iframe = e.detail.pane && e.detail.pane.querySelector("iframe");
        if (iframe) post(iframe, "sl-tab-inactive");
    });
})();
</script>
```

The embedded page just listens for the message and reacts — it can accept the string `"sl-tab-active"` or an object like `{ type: "sl-tab-active" }`, and ignore anything it doesn't recognize:

```js
// Inside the iframe's own page
window.addEventListener("message", (e) => {
    var type = typeof e.data === "string" ? e.data : e.data && e.data.type;
    if (type === "sl-tab-active") startAnimation();
    if (type === "sl-tab-inactive") pauseAnimation();
});
```

Cross-origin is fine — we post with `"*"` and never read anything back.
