# Installation

Flux is a robust, hand-crafted, UI component library for your React applications. It's built using [Tailwind CSS](https://tailwindcss.com/) and uses your app's theme.

## Prerequisites

Flux requires the following before installing:

- React: Version 19 or later
- Tailwind CSS: Version 4.2 or later

## Getting started

- **Inertia**: Laravel, Rails, or any backend using Inertia.
- **React frameworks**: Next.js, React Router, or TanStack Start.
- **Vite & other apps**: A Vite app, or React mounted into a server-rendered page.

### 1. Install Flux

Flux can be installed via npm from your project root:

```
npm install @fluxui-dev/react
```

### 2. Set up Tailwind

Add the following to your app's main CSS file, such as `resources/css/app.css`:

```css
@import 'tailwindcss';
@import '@fluxui-dev/react/flux.css';

@custom-variant dark (&:where(.dark, .dark *));
```

If you don't have Tailwind installed, you can learn how to install it on the [Tailwind website](https://tailwindcss.com/docs/installation).

### 3. Add FluxProvider

Inertia:

Import `FluxProvider` from `@fluxui-dev/react/inertia`. Wrap your Inertia app with it in `createInertiaApp`:

```tsx
// resources/js/app.tsx
import { FluxProvider } from '@fluxui-dev/react/inertia';
import { createInertiaApp } from '@inertiajs/react';

createInertiaApp({
    withApp: (app) => <FluxProvider>{app}</FluxProvider>,
});
```

This provider connects Flux to Inertia for links, forms, validation errors, and the current page. See [the Inertia guide](https://fluxui.dev/react/docs/inertia) for the details.

React frameworks, such as Next.js:

Render `FluxProvider` from `@fluxui-dev/react` in a client component. Pass it your framework's link component, such as Next's `Link`:

```tsx
// app/providers.tsx
'use client';

import { FluxProvider } from '@fluxui-dev/react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';

export function Providers({ children }: { children: React.ReactNode }) {
    return (
        <FluxProvider link={Link} currentUrl={usePathname}>
            {children}
        </FluxProvider>
    );
}
```

Then use it in your root layout:

```tsx
// app/layout.tsx
<body>
    <Providers>{children}</Providers>
</body>
```

Server components can render Flux components directly. Inside a server component, use the flat part names like `DropdownTrigger` instead of `Dropdown.Trigger`.

Vite & other apps:

Import `FluxProvider` from `@fluxui-dev/react`. Wrap your app with it where you render it, such as `main.tsx`:

```tsx
// src/main.tsx
import { FluxProvider } from '@fluxui-dev/react';
import { createRoot } from 'react-dom/client';
import App from './App';

createRoot(document.getElementById('root')!).render(
    <FluxProvider>
        <App />
    </FluxProvider>,
);
```

If you use a router, pass its link component to `FluxProvider`. React Router's link component takes `to` instead of `href`, so wrap it in a small component first:

```tsx
import type { ComponentProps } from 'react';
import { Link } from 'react-router';

type RouterLinkProps = ComponentProps<'a'> & { href: string };

function RouterLink({ href, ...props }: RouterLinkProps) {
    return <Link to={href} {...props} />;
}

<FluxProvider link={RouterLink}>{children}</FluxProvider>
```

React frameworks and Vite & other apps:

Flux links now navigate through your router. External links, downloads, and links that open in a new tab stay plain links.

To get your link component's props typed on Flux components, register it once with a module declaration:

```tsx
import type Link from 'next/link';

declare module '@fluxui-dev/react' {
    interface Register {
        link: typeof Link;
    }
}
```

`FluxProvider` can also connect Flux to your router and form library:

- `currentUrl` marks the current page in navigation.
- `errors` shows validation errors on fields.
- `processing` shows a loading state on submit buttons.

### 4. Set up dark mode

Inertia:

Add this to your `head` to set up dark mode and avoid flicker:

```html
<script>
    (() => {
        let saved;
        try { saved = localStorage['flux.appearance']; } catch {}
        const media = matchMedia('(prefers-color-scheme: dark)');
        const dark = saved ? saved === 'dark' : media.matches;
        const html = document.documentElement;
        html.classList.toggle('dark', dark);
        html.style.colorScheme = dark ? 'dark' : 'light';
    })();
</script>
```

React frameworks:

React renders the whole page, so `FluxProvider` adds the script for you. Add `suppressHydrationWarning` to the `html` element in your root layout, because the script changes its class before React hydrates:

```tsx
// app/layout.tsx
<html lang="en" suppressHydrationWarning>
```

Vite & other apps:

Add this to your `head` to set up dark mode and avoid flicker:

```html
<script>
    (() => {
        let saved;
        try { saved = localStorage['flux.appearance']; } catch {}
        const media = matchMedia('(prefers-color-scheme: dark)');
        const dark = saved ? saved === 'dark' : media.matches;
        const html = document.documentElement;
        html.classList.toggle('dark', dark);
        html.style.colorScheme = dark ? 'dark' : 'light';
    })();
</script>
```

### 5. Use the Inter font family

Although completely optional, we recommend using the [Inter font family](https://rsms.me/inter).

```css
@theme {
    --font-sans: Inter, ui-sans-serif, system-ui, sans-serif;

    ...
}
```

### 6. Use a component

Import components from `@fluxui-dev/react` and use them anywhere in your app:

```tsx
import { Button } from '@fluxui-dev/react';

<Button variant="primary">Get started</Button>
```
