# Comparison period overrides

Theme namespace: `theme.defaults.comparisonPeriodsOptions`

Comparison periods are closely related to date ranges. Components like the **Comparison Period Select Field** display options such as:

- `Previous period`
- `Previous month`
- `Same period last year`
- etc.

These default options are defined within Remarkable’s default theme, but you can easily override them to:

- hide or re-order options
- change how comparison ranges are calculated
- add entirely new comparison options

This lets you model comparison logic in a way that matches your product and your users’ expectations.

## Modifying the default options

You configure comparison period options via the theme at:

`theme.defaults.comparisonPeriodsOptions`

Each option has the following shape:

```ts
type TimeRange = {
  from?: Date; // UTC
  to?: Date;   // UTC
};

type ComparisonPeriodOption = {
  value: string;                     // e.g. 'Previous period'
  label: string;                     // e.g. 'defaults.comparisonPeriodOptions.previousPeriod | Previous period'
  dateFormat: string;                // e.g. 'DD MMM YYYY'
  getRange: (timeRange: TimeRange) => TimeRange; // how the comparison range is calculated
};
```

You typically start from the defaults in `parentTheme.defaults.comparisonPeriodsOptions`, transform the array, and pass the result back via `defineTheme` - as shown in the examples below.

**Things to notice**

- The default list is just an array of `ComparisonPeriodOption`, so you can manipulate it using normal array utilities like `filter`, `map`, `sort`, `concat`, etc.
- To find the exact `value` of each default option (to filter or replace it), see the [default comparison period options](https://github.com/embeddable-hq/remarkable-pro/blob/main/src/theme/defaults/defaults.ComparisonPeriods.constants.ts) in the Remarkable Pro library.
- `getRange` takes a single `dateRange: TimeRange` argument. This is the **primary** date range (for example, “This month”), and `getRange` returns the **comparison** range.
  - For example, if `dateRange` represents **today** and the comparison period is **“Previous period”**, then `getRange(dateRange)` should return **yesterday** as a `TimeRange`.

### Example: re-order the default options

Here’s a simple example that changes the order of the options (for example, putting `Previous month` first):

```ts
// embeddable.theme.ts

const themeProvider = (clientContext: any, parentTheme: Theme): Theme => {
  const defaults = parentTheme.defaults.comparisonPeriodsOptions;

  const myOptions = [
    // ensure 'Previous month' goes first if it exists
    ...defaults.filter((option) => option.value === 'Previous month'),
    // then everything else
    ...defaults.filter((option) => option.value !== 'Previous month'),
  ];

  const myTheme = {
    defaults: {
      comparisonPeriodsOptions: myOptions,
    },
  };

  return defineTheme(parentTheme, myTheme) as Theme;
};
```

### Example: change the default options

Here’s how we might edit the `Previous period` option, changing:

- its label
- how the comparison range is calculated

```ts

// embeddable.theme.ts

const themeProvider = (clientContext: any, parentTheme: Theme): Theme => {
  const myOptions = parentTheme.defaults.comparisonPeriodsOptions.map((option) => {
    if (option.value === 'Previous period') {
      return {
        ...option,
        label: 'defaults.comparisonPeriodOptions.priorPeriod | Prior period',
        getRange: (dateRange: TimeRange): TimeRange => {
          // custom function that returns a TimeRange
        },
      };
    }

    return option;
  });

  const myTheme: DeepPartial<Theme> = {
    defaults: {
      comparisonPeriodsOptions: myOptions,
    },
  };

  return defineTheme(parentTheme, myTheme) as Theme;
};
```

### Example: add a new option

Let’s say we want to add a new comparison period: **“2 years ago”**.

All we need to do is append a new `ComparisonPeriodOption` to the existing list:

```ts

// embeddable.theme.ts

const themeProvider = (clientContext: any, parentTheme: Theme): Theme => {
  const myOptions: ComparisonPeriodOption[] = [
    ...parentTheme.defaults.comparisonPeriodsOptions,
    {
      value: 'Two years ago',
      label: 'customOptions.twoYearsAgo | Two years ago',
      dateFormat: 'DD MMM YYYY',
      getRange: (dateRange: TimeRange): TimeRange => {
        return myCustomPeriodFunction(dateRange); // must return a UTC-based TimeRange
      },
    },
  ];

  const myTheme: DeepPartial<Theme> = {
    defaults: {
      comparisonPeriodsOptions: myOptions,
    },
  };

  return defineTheme(parentTheme, myTheme) as Theme;
};
```

**Things to notice**

- Comparison ranges should be returned in **UTC**. Remarkable Pro handles the timezone conversion for display.
- The `dateFormat` controls how the comparison dates are rendered in the UI.

## Theme options vs. Builder options

These are two separate lists:

| List | What it controls | How to change it |
|---|---|---|
| `theme.defaults.comparisonPeriodsOptions` | What components show and how ranges are calculated (e.g. the Comparison Period Select Field) | This page |
| `comparisonPeriod` custom type | What you can pick for `comparisonPeriod` variables and inputs in the Builder | [Updating custom types from a component library](https://docs.embeddable.com/component-libraries/build-components/extending-types#updating-custom-types-from-a-component-library) |

Changing the theme does **not** change the Builder list, and vice versa.

If you pick a value in the Builder, components look it up by the theme option's `value`. So for a new option like `'Two years ago'` to work end to end, add it to both lists with the same string.

---
Source: https://docs.embeddable.com/component-libraries/remarkable-pro/customizations/comparison-ranges
