# @asphalt-react/content-switcher

> Content switcher

Latest version **2.19.0** (published 2026-10-29) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install @asphalt-react/content-switcher
pnpm add @asphalt-react/content-switcher
yarn add @asphalt-react/content-switcher
bun add @asphalt-react/content-switcher
```

## Health

**Score 55/100 (C)** — status: active.

Positive: no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 2.19.0 |
| Published | 2026-10-29 |
| First published | 2022-03-30 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | UX Engineering - Web |
| Maintainers | sayantan1211, dawn29, antoniomontana, goto.abhinav, elayudhanira-gojek, yessyprmtsr, soorajj |
| Keywords | asphalt, content switcher, component, dls, design language system |

## Links

- npm: https://www.npmjs.com/package/@asphalt-react/content-switcher
- npm.io page: https://npm.io/package/@asphalt-react/content-switcher

## Recent versions

- 2.19.0 (latest) — 2026-10-29
- 2.0.0-alpha.29 (next) — 2023-12-15
- 2.18.0 — 2026-10-07
- 2.16.0 — 2026-07-28
- 2.15.1 — 2026-04-28
- 2.15.0 — 2026-04-13
- 2.14.0 — 2026-03-11
- 2.13.0 — 2026-02-19
- 2.12.2 — 2026-01-28
- 2.12.1 — 2025-12-19
- 2.12.0 — 2025-11-28
- 2.11.0 — 2025-11-10
- 2.10.0 — 2025-10-27
- 2.9.0 — 2025-10-02
- 2.8.1 — 2025-09-10
- … 25 more at https://npm.io/package/@asphalt-react/content-switcher/versions

## README

# ContentSwitcher

![npm](https://img.shields.io/npm/dt/@asphalt-react/content-switcher?style=flat-square)
[![npm version](https://badge.fury.io/js/@asphalt-react%2Fcontent-switcher.svg)](https://badge.fury.io/js/@asphalt-react%2Fcontent-switcher)

Component to switch between alternate views of a content within the same page or screen. Only one view is visible at a time. For instance, use ContentSwticher to switch between tabular view and graphical view of a dataset. It's a controlled component where you control which content view should be visible.

ContentSwitcher exports another component named [Switch](#switch). Switch creates the tabs for each view. Switches come in 2 sizes: small and medium (default). Each Switch has three types of caption:

* text only
* text with an icon; icon adds more context to the text
* icon only

Switch accepts most of the [button element's attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button) such as `id` & `name` and supports [data-\* attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes).

## Accessibility

1. ContentSwitcher has a `role="tablist"` and the Switches have `role="tab"`.
2. The selected Switch has "aria-selected" set to `true`.
3. The Switches are focusable using <kbd>tab</kbd>.
4. ContentSwitcher accepts aria-\* attributes for [role="tablist"](https://www.w3.org/TR/wai-aria-1.1/#tablist).
5. Switch accepts aria-\* attributes for [role="tab"](https://www.w3.org/TR/wai-aria-1.1/#tab).

## Accessibility must-haves

1. Add `role="tabpanel"` to the content section.
2. Add an `id` to a Switch. Pass the "id"'s value to `aria-labelledby` attribute to the element of the content section.
3. Add an `id` to the view section element. Pass the "id"'s value to `aria-controls` attribute of its Switch.
4. Add `aria-label` or `aria-labelledby` in Switch with icon as caption.

Check examples to see these in action.

## Usage

```jsx
import React, { useState } from "react"
import { ContentSwitcher, Switch } from "@asphalt-react/content-switcher"

function App() {
  const [active, setActive] = useState(0)
  const clickHandler = (value) => setActive(value)
  return (
    <main>
      <ContentSwitcher>
        <Switch value={0} onAction={clickHandler} active={active === 0}>
          First
        </Switch>
        <Switch value={1} onAction={clickHandler} active={active === 1}>
          Second
        </Switch>
      </ContentSwitcher>
      <div>
        {active === 0 && (<div>First Section</div)}
        {active === 1 && (<div>Second Section</div>)}
      </div>
    </main>
  )
}

export default App
```

[comment]: # "ContentSwitcher Props"

## Props

### children

Switch components.

| type | required | default |
| ---- | -------- | ------- |
| node | true     | N/A     |

# Switch

[comment]: # "Switch Props"

## Props

### children

Switch caption.

| type | required | default |
| ---- | -------- | ------- |
| node | true     | N/A     |

### value

Index of the Switch.

| type   | required | default |
| ------ | -------- | ------- |
| number | true     | N/A     |

### active

Adds styles to show the Switch is active.

| type | required | default |
| ---- | -------- | ------- |
| bool | false    | N/A     |

### onAction

Callback to handle Switch selection.

The function accepts 2 arguments:

* value: `value` prop of the Switch.
* options: object containing the React synthetic event.

```js
onAction(value, { event }) {
 console.log(value)
}
```

| type | required | default |
| ---- | -------- | ------- |
| func | false    | N/A     |

### size

Size of the Switches. Accepts one of "s" or "m" for small & medium.

| type | required | default |
| ---- | -------- | ------- |
| enum | false    | "m"     |

### icon

Renders icon as caption.

| type | required | default |
| ---- | -------- | ------- |
| bool | false    | false   |

### qualifier

Icons to add more context to the textual caption.

Accepts SVG or SVG wrapped React component. Checkout `@asphalt-react/iconpack` for SVG wrapped React components.

> ⚠️ Do not use `qualifier` to render a Switch with icon as caption; use `icon` prop instead.

| type    | required | default |
| ------- | -------- | ------- |
| element | false    | N/A     |

### qualifierEnd

Appends qualifier to the text in caption. Switch prepends the qualifier by default.

| type | required | default |
| ---- | -------- | ------- |
| bool | false    | false   |

### accent

Applies accent intent when Switch is active.

| type | required | default |
| ---- | -------- | ------- |
| bool | false    | false   |

---
_Source: https://npm.io/package/@asphalt-react/content-switcher · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
