# Passing Client Context

**Client Context** lets you pass **dynamic values** into your embedded components at runtime - directly from the web component. 

You can use it for lots of tasks, but common use-cases include:

- **Client-specific theming** (e.g. color schemes)
- **Localization** (e.g. language, date/number/currency formats)
- **Time Zones** (e.g. showing data in the user’s local time)
- **Dark/Light mode** (e.g. switching between night and day themes)
- Passing in any other **custom** data you want to expose to your React components.

## How It Works

### **Pass an Object via the Web Component**  
    
You include a `client-context` attribute on your `<em-beddable>` [web component](https://docs.embeddable.com/deployment/embedding). For example:

    ```js
    <em-beddable
      token="${json.token}"
      client-context="${JSON.stringify({ 
        currency: 'dollar',
        currencySymbol: '$',
        flag: '🇺🇸',
        language: 'english',
        name: 'United States'
      })}"
    ></em-beddable>
    ```

You’re not limited to strings - you can pass numbers, booleans, arrays or objects, too.  Any JSON.

You can also use it in your React code like so ([learn more](https://docs.embeddable.com/deployment/interactivity)):

```tsx
return (
    <Embeddable
      token={securityToken}
      clientContext={{ locale: ['en-US'], language: 'en', darkMode: false }}
    />
  );
```

Learn how to generate a `securityToken` [here](https://docs.embeddable.com/deployment/tokens-api).

### **Receive it in `defineComponent`**  
    
    In your `.emb.ts` file, the third argument of the `props` function within [`config`](https://docs.embeddable.com/component-libraries/build-components/defining-components#3-config) is `clientContext`:
   
   ```ts
   props: (inputs, [state, setState], clientContext) => ({
        ...inputs,
        clientContext // e.g. { currency: 'dollar', ... }
   })
   ```

### **Use it in Your React Component**  
   
   Your component sees the `clientContext` in its props, so you can handle theming, localization, or any other logic:

   ```tsx
   // index.ts
   export default function DisplayContext(props) {
     const { title, body, clientContext } = props;
     const { currency, currencySymbol, flag, language, name } = clientContext;
     return (
       <div>
         <h1>{title}</h1>
         <p>{body}</p>
         <ul>
           <li>Country: {name} {flag}</li>
           <li>Language: {language}</li>
           <li>Currency: {currency}</li>
           <li>Symbol: {currencySymbol}</li>
         </ul>
       </div>
     );
   }
   ```

> Client Context **isn’t** available in your data models or server-side security checks. It’s front-facing data only - avoid passing sensitive information. For that, see [Security context](https://docs.embeddable.com/data-modeling/row-level-security/sql-based-security).

---

## Testing Client Context

You can create presets to switch between different context objects in the no-code builder during development. For instance:

```yaml
# src/embeddable/presets/client-contexts.cc.yml
- name: United States
  clientContext:
    currency: "dollar"
    currencySymbol: "$"
    flag: "🇺🇸"
    language: "english"
    name: "United States"
  canvas:
    background: "#fff"

- name: France
  clientContext:
    currency: "euro"
    currencySymbol: "€"
    flag: "🇫🇷"
    language: "français"
    name: "France"
  canvas:
    background: "#fff"
```

Each entry shows up as a selectable context in your dashboards in your Embeddable workspace. Switching contexts updates your components’ `clientContext` prop immediately.

---

## Testing Client-Side Variables

You can also send values to your variables in Embeddable from your client application ([learn more](https://docs.embeddable.com/deployment/interactivity#pass-in-and-update-variables)). To simulate this in the no-code builder, you can add lines to your `client-contexts.cc.yml` file like so:

```yaml
- name: United States
  clientContext:
    currency: 'dollar'
    currencySymbol: '$'
  variables:
    currency: USD

- name: France
  clientContext:
    currency: 'euro'
    currencySymbol: '€'
  variables:
    currency: EUR
```

## Example Use Case: Localization

A common scenario is **localizing** dashboards based on user language or region. For instance, you might pass `clientContext.language` and then load or map text accordingly:

```ts
// A simple dictionary for three languages
const dict = {
  english: { helloText: 'hello', goodbyeText: 'goodbye' },
  french:  { helloText: 'bonjour', goodbyeText: 'au revoir' },
  spanish: { helloText: 'hola', goodbyeText: 'adiós' }
};

function getText(key: string, lang: string) {
  return dict[lang]?.[key] || key;
}
```

In your component:

```tsx
export default function Localized(props) {
  const { clientContext } = props;
  const { language } = clientContext;

  return (
    <>
      <p>{getText('helloText', language)}</p>
      <p>{getText('goodbyeText', language)}</p>
    </>
  );
}
```

- If `clientContext.language` is `english`, the dashboard displays “hello” and “goodbye.”
- If it’s `french`, the user sees “bonjour” and “au revoir.”

For larger, more complex localization needs, consider libraries like **react-intl** or **i18next**, but the basic concept remains the same: pass a language key, load the right translations, and render accordingly.

## Example Use Case: Time Zones

Another common use-case is handling **time zones**. You might pass a user’s time zone in `clientContext.timezone` and then use that to display dates/times in their local time. By default, all [Remarkable Pro](https://docs.embeddable.com/component-libraries/remarkable-pro) components support this behavior. If you pass a `timezone` in the `clientContext`, Remarkable Pro automatically converts any date/time values to that time zone before rendering (otherwise, it defaults to UTC).

For example, if you pass `clientContext.timezone = 'America/New_York'`, all dates/times in your dashboard will show in Eastern Time. If you switch to `clientContext.timezone = 'Europe/Paris'`, they’ll show in Central European Time instead.

> Time zone strings should be in the format of an [IANA time zone identifier](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) (e.g. `America/New_York`, `Europe/Paris`, `Asia/Tokyo`).

To support this in your own custom components, you need only add a `timezone` property to your `loadData` call, like this:

```ts
// YourComponent.emb.ts

export default defineComponent(Component, meta, {
  props: (inputs, _, clientContext) => {
    return {
      ...inputs,
      results: loadData({
        from: inputs.ds, // Receives the chosen dataset
        select: [inputs.myDimension, inputs.myMeasures],
        timezone: clientContext?.timezone // Receives the timezone from clientContext
      })
    };
  }
});
```

This will pass the time zone to Cube.js on every data load (see [Defining Data Models](https://docs.embeddable.com/data-modeling/writing-data-models/defining-models) for further information), which will handle the conversion for you.

---
Source: https://docs.embeddable.com/component-libraries/client-context
