@asphalt-react/content-switcher
v2.12.0
Published
Content switcher
Downloads
1,697
Readme
ContentSwitcher
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 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 such as id & name and supports data-* attributes.
Accessibility
- ContentSwitcher has a
role="tablist"and the Switches haverole="tab". - The selected Switch has "aria-selected" set to
true. - The Switches are focusable using tab.
- ContentSwitcher accepts aria-* attributes for role="tablist".
- Switch accepts aria-* attributes for role="tab".
Accessibility must-haves
- Add
role="tabpanel"to the content section. - Add an
idto a Switch. Pass the "id"'s value toaria-labelledbyattribute to the element of the content section. - Add an
idto the view section element. Pass the "id"'s value toaria-controlsattribute of its Switch. - Add
aria-labeloraria-labelledbyin Switch with icon as caption.
Check examples to see these in action.
Usage
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 AppProps
children
Switch components.
| type | required | default | | ---- | -------- | ------- | | node | true | N/A |
Switch
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:
valueprop of the Switch. - options: object containing the React synthetic event.
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
qualifierto render a Switch with icon as caption; useiconprop 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 |
