# Using iVOLT with React, Vue and Astro

> iVOLT inside a component framework: load the CSS once, start the JavaScript on the element you render with init(ref), clean up with destroy(ref). Server rendering, CDN or npm.

Source: https://ivolt.intervolutions.com/foundations/frameworks · iVOLT 1.1.2 · English

iVOLT is HTML and CSS first, so a framework renders it like any other markup: write the classes and the `data-iv-*` attributes in your templates. The only thing to wire is the optional JavaScript, because a framework adds and removes elements after the page has loaded and `auto.min.js` only starts what is there when the page loads.

**Exports:** `init(root)` and `destroy(root)` from `@intervolutions/ivolt`. **Wrappers:** none; these patterns are all you need.

## The pattern

Start what you rendered, stop it before it goes.

`init(root)` starts every component inside `root` and is idempotent: calling it twice on the same element does nothing the second time. `destroy(root)` removes the listeners and the markup that `init` added inside it. Call the first when your component is in the DOM, the second when it is about to leave.

Load the stylesheet once for the whole app (in your entry file or layout): `import "@intervolutions/ivolt/css/ivolt.css";`.

## React

```src
import { useEffect, useRef } from "react";
import { init, destroy } from "@intervolutions/ivolt";

export function SignupDialog() {
  const ref = useRef(null);
  useEffect(() => {
    const root = ref.current;
    init(root);
    return () => destroy(root);
  }, []);
  return (
    <div ref={ref}>
      <button className="iv-button iv-button--primary" type="button" data-iv-open="signup">Create account</button>
      <dialog className="iv-dialog" id="signup" data-iv-component="dialog" aria-labelledby="signup-title">
        <div className="iv-dialog__panel">…</div>
      </dialog>
    </div>
  );
}
```

Let iVOLT own what it manages. If React state decides whether a dialog is open, call the component's API (`Dialog.getOrCreate(el).open()`) from an effect instead of setting `open` yourself, and listen to `iv:closed` to bring your state back in line.

## Vue

```src
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { init, destroy } from "@intervolutions/ivolt";

const root = ref(null);
onMounted(() => init(root.value));
onBeforeUnmount(() => destroy(root.value));
</script>

<template>
  <div ref="root">
    <details class="iv-disclosure" data-iv-component="disclosure">
      <summary class="iv-disclosure__summary">Shipping</summary>
      <div class="iv-disclosure__content">Two to four working days.</div>
    </details>
  </div>
</template>
```

## Astro

An Astro page is HTML once it reaches the browser, so the plain setup works as it is. Import the CSS in the layout and start the JavaScript in a client script:

```src
---
import "@intervolutions/ivolt/css/ivolt.css";
---
<slot />
<script>
  import "@intervolutions/ivolt/auto";
</script>
```

With view transitions (`<ClientRouter />`) the page is swapped without a reload: start the new page on `astro:page-load` with `init(document)` and clean up on `astro:before-swap` with `destroy(document)`. This site does exactly that.

## Server rendering

The modules never touch `document` or `window` when they are imported, so importing them in code that also runs on the server is safe. Only `init` and the component methods need a browser: keep them in `useEffect`, `onMounted` or a client script, which never run on the server. The markup itself renders on the server as it is, and every component works as plain HTML before the script arrives: a dialog link jumps to its section, tabs are links to their panels, a date field is the browser's own.

## CDN or npm

In an app with a bundler, install from npm (`npm install @intervolutions/ivolt`) and import the modules as above: the version is pinned in your lockfile, and a page that needs one component can import that one alone (`@intervolutions/ivolt/dialog`) and start it with `Dialog.initAll(root)`. The CDN lines on the [getting started](https://ivolt.intervolutions.com/getting-started) page suit pages without a build step; in a framework app they would load iVOLT a second time, outside your bundle.
