# Setup

> NasimProvider and its props, links through the app's router, Server and client components, Persian digits and the locale.

- Describes: @nyxon/nasim unreleased, after 0.12.0
- Page: https://nasim.nyxon.tech/docs/get-started/setup

## NasimProvider [#nasimprovider]

Wrap the app once, in a client component. `NasimProvider` gives the whole tree, in one place:

* the Persian locale, `fa-IR`, through React Aria's `I18nProvider`: Persian numerals, Jalali calendars,
  right to left;
* React Aria's own strings in Persian («بستن» for "Close", and the rest), installed through its global
  dictionary when the module loads, in the browser and on a server;
* in-app links: with the router's `navigate`, `LinkButton`, `Link` and menu items move without a page
  load;
* the theme controller, a tenant's accent and font, and the toast region.

In a Next.js app that client component is `app/providers.tsx` from [Installation](/docs/get-started/installation),
which hands the provider the router and the saved theme; the snippets below use the same file.

Its props (`NasimProviderProps`):

| Prop            | Type                                          | What it does                                                                                                                       |
| --------------- | --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `navigate`      | `(href, options?) => void`                    | The router's push or replace. Enables in-app link navigation. Keep it stable (`useCallback`).                                      |
| `useHref`       | `(href: string) => string`                    | The address a link shows, for a router with a base path; it may be a hook. Called for every link, ignored for an external address. |
| `defaultMode`   | `"system" \| "light" \| "dark" \| "telegram"` | Used until the person picks a theme. Default `"system"`.                                                                           |
| `persist`       | `boolean`                                     | Save the choice in `localStorage` and a cookie. Default `true`; `false` inside Telegram.                                           |
| `initialMode`   | `ThemeMode`                                   | The saved choice as the server read it from the cookie, so the server and the first client render agree.                           |
| `initialScheme` | `"light" \| "dark"`                           | The external scheme when it is known at the first render (Telegram's launch theme).                                                |
| `nativeBack`    | `boolean`                                     | The host app draws its own back button (Telegram's): `PageHeader` then draws none. Default `false`.                                |
| `accent`        | `string` (`#rrggbb`)                          | A reseller's brand colour, fitted to each theme so it keeps its contrast.                                                          |
| `font`          | `string`                                      | A reseller's font id with a rule in `fonts.css`. None, or `vazirmatn`, keeps Vazirmatn.                                            |
| `palette`       | `Palette`                                     | The product's own colours and radius, written as a stylesheet, on the server too.                                                  |

## Links through the router [#links-through-the-router]

`navigate` receives the address and `{ replace?, scroll? }` (`NasimNavigateOptions`, what `routerOptions` on
a `Link` or `LinkButton` carries). In Next.js:

```tsx
const router = useRouter();
const navigate = useCallback(
  (href: string, options?: NasimNavigateOptions) =>
    options?.replace
      ? router.replace(href, { scroll: options.scroll })
      : router.push(href, { scroll: options?.scroll }),
  [router],
);

<NasimProvider navigate={navigate}>{children}</NasimProvider>;
```

The function must be stable: React Aria rebuilds every link when it changes. An address with a scheme or a
`//host` (a Telegram link, a phone number) is never handed to the router. Without `navigate`, links are
plain links that load the page.

## Server and client [#server-and-client]

Every Nasim module that holds state, effects, context or an event handler starts with `'use client'`, and
nothing reads `window`, `document` or `localStorage` while a component renders.

A Server Component may import from `@nyxon/nasim` and render the presentational parts with plain props:
`KeyValueList`, `Stepper`, `Cover`, `LoadingState`, `EmptyState` (with an `href` action), `StatusChip`,
`TrendChip`, `StatCard`, `Sparkline`, `Kbd`, `SectionCard`, `Collapsible`, `StaticTable`, `ColumnChart`,
`BarTable` and the skeletons, and the colour, palette and theme-cookie helpers.

Everything else is a client component. Render it from a Server Component with props that serialize (no
functions), or inside a client component of your own. A Server Component cannot reach into a client
module, so compound primitives (`Card.Header`, `Select.Trigger`) belong in client components. Each part's
page says which it is.

React Aria words some labels itself (the breadcrumb list, a drag handle). Nasim answers its string lookup
on the server as well, so those labels are Persian in the server's HTML too.

## The first paint and the theme [#the-first-paint-and-the-theme]

The theme is written on `<html>` after hydration, so with Nasim's tokens alone a dark device in
`"system"` mode paints the first frame light. A product that passes a `palette` does not have this: its
stylesheet carries a `prefers-color-scheme: dark` rule. A saved light or dark choice can be on `<html>`
from the first byte: the server reads the cookie with `parseThemeMode` and hands the mode on.

```tsx title="app/layout.tsx"
// Installation's layout, now reading the saved theme; Providers is app/providers.tsx from there.
import { parseThemeMode, THEME_COOKIE } from '@nyxon/nasim';
import { cookies } from 'next/headers';
import type { ReactNode } from 'react';
import { vazirmatn } from './fonts';
import './globals.css';
import { Providers } from './providers';

export default async function RootLayout({
  children,
}: {
  children: ReactNode;
}) {
  const mode = parseThemeMode((await cookies()).get(THEME_COOKIE)?.value);
  const explicit = mode === 'light' || mode === 'dark' ? mode : undefined;
  return (
    <html
      lang="fa"
      dir="rtl"
      className={vazirmatn.variable}
      data-theme={explicit}
      suppressHydrationWarning
    >
      <body>
        <Providers initialMode={mode}>{children}</Providers>
      </body>
    </html>
  );
}
```

## Persian digits and the locale [#persian-digits-and-the-locale]

Nasim's locale is `fa-IR` and other locales are not supported yet. Numbers that React Aria formats
(dates, number fields) come out in Persian numerals.

Text you write keeps its ASCII digits: the font draws them as Persian digits. `html` takes the font's
digit feature, `"ss01"` in Vazirmatn, so `۱۲۳` is shown while the text stays `123` and a copied number
still pastes as digits. `font-mono` ignores the feature, so technical values keep Latin digits. Use
`tabular-nums` only where numbers must line up: balances, prices, countdowns.

Technical values (ids, links, codes, phone numbers) keep `dir="ltr"`, so Latin text and digits read in
order inside a right-to-left page, and they sit on the start edge, the right.