---
title: "style.when()"
description: "Store a variant sequence or alternatives, such as a ref condition, and use them in several style() calls."
image: "https://stillwind.dev/og.png"
---

> Documentation Index
> Fetch the complete documentation index at: https://stillwind.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# style.when()

`style.when(...variants)` stores a sequence of variants in a variable. Use the
variable in [`style()`](https://stillwind.dev/docs/macros/style) where you would write the variants.
This helps when several elements need the same
[ref condition](https://stillwind.dev/docs/macros/ref/condition), because a ref call must otherwise
be repeated in each `style()` call.

## Example

### Source

```tsx
import { style } from "stillwind/react";

const card = style.ref("card");
const cardHover = style.when(card("hover"));

export const root = style(card, "rounded-lg border");
export const title = style({ [cardHover]: "text-blue-600" });
export const arrow = style(`${cardHover}:translate-x-1`);
```

### Compiled code

```ts
const card = undefined;
const cardHover = undefined;

export const root = {className:"sw_1tsgv sw_card_nudp9"};
export const title = {className:"sw_0m922"};
export const arrow = {className:"sw_nlo24"};
```

### Generated CSS


```css
.sw_1tsgv {
  border-radius: var(--radius-lg);
  border-style: var(--tw-border-style);
  border-width: 1px;
}

@media (hover: hover) {
  .sw_0m922:is(:where(.sw_card_nudp9):hover *) {
    color: var(--color-blue-600);
  }
}

@media (hover: hover) {
  .sw_nlo24:is(:where(.sw_card_nudp9):hover *) {
    --tw-translate-x: calc(var(--spacing) * 1);
    translate: var(--tw-translate-x) var(--tw-translate-y);
  }
}
```

`title` and `arrow` change while the card is hovered, as if each one wrote
`card("hover")` itself.

## Inputs

Each argument is one sequence of variants, and every variant in it must match.
The final colon is optional.

| Form                                                          | Example                                 |
| ------------------------------------------------------------- | --------------------------------------- |
| A string literal of variants                                  | `style.when("hover:md")`                |
| A [ref condition](https://stillwind.dev/docs/macros/ref/condition), with `.in()`   | `style.when(card("hover").in(list))`    |
| A [container query](https://stillwind.dev/docs/macros/ref/at)                      | `style.when(card.at("width >= 40rem"))` |
| A template literal whose interpolations are complete variants | ``style.when(`md:${card("hover")}`)``   |
| Another stored variant                                        | `style.when(cardHover)`                 |

The rules for each variant are the same as when you write it directly in
`style()`. An interpolation in a template literal must be a ref condition, a
container query, or a stored variant, followed by `:` or at the end.

Write the call as the initial value of a variable, outside other Stillwind
calls. Declare a stored variant before another `style.when()` call uses it.
A `style()` call can use one that is declared later in the module.

[invalid-stored-variant](https://stillwind.dev/docs/lints/invalid-stored-variant) reports an
argument that is not a sequence of variants.

## Alternatives

With several arguments, the stored variant matches when any argument matches:

```tsx
const card = style.ref("card");
const active = style.when("hover", card("focus-within"));

export const title = style({ [active]: "text-blue-600" });
```

A use means the same as writing the use once for each argument, in argument
order. `title` is equal to
`style({ "hover:": "text-blue-600", [card("focus-within")]: "text-blue-600" })`.
Unlike an [`or(...)`](https://stillwind.dev/docs/guides/style#match-any-of-several-variants)
variant, the arguments can be ref conditions and container queries.

## Uses

Use a stored variant in these positions:

- a computed key in a `style()` object, such as `{ [cardHover]: "underline" }`;
- an interpolation in a utility template, followed by `:`, such as
  `` `${cardHover}:underline` ``;
- the argument of another `style.when()`.

You can export the variable and import it in another module. Each use must
refer to one stored variant. A choice at runtime, such as
`[active ? cardHover : cardFocus]`, does not work, because the compiler
expands the variants at build time. Write a key for each stored variant instead.

[`style.recipe()`](https://stillwind.dev/docs/macros/recipe) values and [`.in()`](https://stillwind.dev/docs/macros/ref/in)
do not accept a stored variant.

## Output

The compiler replaces the call with `undefined`, and each use with the stored
variants. The call adds no CSS by itself. JavaScript that reads the variable at
runtime gets `undefined`.

Source: https://stillwind.dev/docs/macros/when/index.mdx
