---
title: "CardViewSwitcher"
slug: "cardviewswitcher"
abstract: "Describes the anatomy, use and API reference of Search CardViewSwitcher primitive UI component."
---
# CardViewSwitcher

Primitive UI components in Search UI Components package, are headless, building blocks used to create the headless widget templates for React applications. You can use them to scaffold your custom widgets.

This topic describes the anatomy, API reference, and variations of the `CardViewSwitcher` primitive component.

<details>
<summary><h2>Anatomy</h2></summary>

For a complete implementation, put together parts of the UI component as shown in the following code block.

```tsx
import { CardViewSwitcher } from '@sitecore-search/ui';
const MyCardViewSwitcher = () => (
    <CardViewSwitcher.Root>
      <CardViewSwitcher.Item value="grid">
        Use icon or text as children
      </CardViewSwitcher.Item>
      <CardViewSwitcher.Item value="list">
        Use icon or text as children
      </CardViewSwitcher.Item>
    </CardViewSwitcher.Root>
);
```

</details>

<details>
<summary><h2>Using the component</h2></summary>

The following tabs describe use of the `CardViewSwitcher` primitive component.

### Plain component { .notintoc }

In your React application, paste the following code block then modify the primitives to match your needs.

![Screenshot of a plain CardViewSwitcher primitive component.](https://doc.sitecore.com/api/markdown-image/search/en/developers/search-js-sdk-for-react/images/cardViewSwitcher-unstyled.png)

```tsx
() => {
  return (
    <CardViewSwitcher.Root onValueChange={onValueChange}>
      <CardViewSwitcher.Item value="grid" aria-label="Grid View">
        <GridIcon />
      </CardViewSwitcher.Item>
      <CardViewSwitcher.Item value="list" aria-label="List View">
        <ListBulletIcon />
      </CardViewSwitcher.Item>
    </CardViewSwitcher.Root>
  );
}
```

### Styled component with default list { .notintoc }

In your React application, paste the following code block then modify the primitives to match your needs.

![Screenshot of a styled CardViewSwitcher primitive component with default list.](https://doc.sitecore.com/api/markdown-image/search/en/developers/search-js-sdk-for-react/images/cardViewSwitcher-styled-list.png)

```tsx
() => {
  const StyledCardViewSwitcher = styled(CardViewSwitcher.Root)`
    display: inline-flex;
    background-color: ${theme.vars.palette.common.white};
  `;

  const StyledCardViewSwitcherItem = styled(CardViewSwitcher.Item)`
    all: unset;
    align-items: center;
    background-color: ${theme.vars.palette.common.white};
    color: ${theme.vars.palette.grey[500]};
    display: flex;
    height: 30px;
    justify-content: center;
    margin-left: ${theme.vars.spacing.s};
    width: 30px;

    &:first-child {
      margin-left: 0;
    }

    &:hover {
      background-color: ${theme.vars.palette.primary.light};
    }
    &[data-state='on'] {
      background-color: ${theme.vars.palette.primary.main};
      color: ${theme.vars.palette.primary.contrastText};
    }

    &:focus {
      position: 'relative';
      border: solid 1px ${theme.vars.palette.primary.main};
    }
  `;

  return (
    <StyledCardViewSwitcher onValueChange={onValueChange} defaultValue="list">
      <StyledCardViewSwitcherItem value="grid">
        <GridIcon />
      </StyledCardViewSwitcherItem>
      <StyledCardViewSwitcherItem value="list">
        <ListBulletIcon />
      </StyledCardViewSwitcherItem>
    </StyledCardViewSwitcher>
  );
}
```

### Styled component with text { .notintoc }

In your React application, paste the following code block then modify the primitives to match your needs.

![Screenshot of a styled CardViewSwitcher primitive component with text.](https://doc.sitecore.com/api/markdown-image/search/en/developers/search-js-sdk-for-react/images/cardViewSwitcher-styled-text.png)

```tsx
() => {
  const StyledCardViewSwitcher = styled(CardViewSwitcher.Root)`
    display: inline-flex;
    background-color: ${theme.vars.palette.common.white};
  `;

  const StyledCardViewSwitcherItem = styled(CardViewSwitcher.Item)`
    all: unset;
    align-items: center;
    background-color: ${theme.vars.palette.common.white};
    color: ${theme.vars.palette.grey[500]};
    display: flex;
    height: 30px;
    justify-content: center;
    margin-left: ${theme.vars.spacing.s};
    padding: ${theme.vars.spacing.m};

    &:first-child {
      margin-left: 0;
    }

    &:hover {
      background-color: ${theme.vars.palette.primary.light};
    }
    &[data-state='on'] {
      background-color: ${theme.vars.palette.primary.main};
      color: ${theme.vars.palette.primary.contrastText};
    }

    &:focus {
      position: relative;
      border: solid 1px ${theme.vars.palette.primary.main};
    }
  `;

  return (
    <StyledCardViewSwitcher onValueChange={onValueChange}>
      <StyledCardViewSwitcherItem value="grid">Grid</StyledCardViewSwitcherItem>
      <StyledCardViewSwitcherItem value="list">List</StyledCardViewSwitcherItem>
    </StyledCardViewSwitcher>
  );
}
```

</details>

<details>
<summary><h2>API reference</h2></summary>

This section describes the component or component parts displayed in the anatomy structure. For every component, you will find:

- Components to which you can add it as a child.
- Components you can add as its child.
- Details of any properties you can set.

### CardViewSwitcher.Root

The `CardViewSwitcher.Root` component contains all the components of the card view switcher.

Children: `CardViewSwitcher.Item`

| Name | Description | Default |
| --- | --- | --- |
| `onValueChange` | Event handler called when any item is clicked.<br><br>```tsx<br>(value) =&gt; { //do something }<br>``` |     |
| `defaultValue` | String.<br><br>The controlled stateful value of the item that is pressed.<br><br>*list* or *grid* | *grid* |

### CardViewSwitcher.Item

The `CardViewSwitcher.Item` component is an item in the group.

Parent: `CardViewSwitcher.Root`

| Name | Description | Default |
| --- | --- | --- |
| `value` | String.<br><br>Card value type.<br><br>*list* or *grid* | *grid* |
| `children` | Content to be displayed. |     |

</details>