segmented-control
A row of mutually exclusive options. options is [{ value, label }].
Try it: events appear here (valuechange).
Source: segmented-control.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/segmented-control.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/segmented-control.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/segmented-control.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/segmented-control.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/segmented-control.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/segmented-control.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/segmented-control.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/segmented-control.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/segmented-control.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/segmented-control.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/segmented-control.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/segmented-control.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/segmented-control.js"></script>The compiled install writes src/components/zubra/segmented-control.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<segmented-control label="Range"></segmented-control>
<script type="module">
import './components/zubra/segmented-control.js';
const element = document.querySelector('segmented-control');
element.value = 'month';
element.options = [
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
{ value: 'year', label: 'Year' },
];
element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
</script>import './components/zubra/segmented-control.js';
const value = 'month';
const options = [
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
{ value: 'year', label: 'Year' },
];
export function SegmentedControlExample() {
return (
<segmented-control
label="Range"
value={value}
options={options}
onvaluechange={(event) => console.log('valuechange', event.detail)}
></segmented-control>
);
}<script setup>
import './components/zubra/segmented-control.js';
const value = 'month';
const options = [
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
{ value: 'year', label: 'Year' },
];
const onValuechange = (event) => console.log('valuechange', event.detail);
</script>
<template>
<segmented-control
label="Range"
:value.prop="value"
:options.prop="options"
@valuechange="onValuechange"
></segmented-control>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/segmented-control.js';
const value = 'month';
const options = [
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
{ value: 'year', label: 'Year' },
];
</script>
<segmented-control
label="Range"
{value}
{options}
onvaluechange={(event) => console.log('valuechange', event.detail)}
></segmented-control>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/segmented-control.js';
@Component({
selector: 'app-segmented-control-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<segmented-control
label="Range"
[value]="value"
[options]="options"
(valuechange)="onValuechange($event)"
></segmented-control>
`,
})
export class SegmentedControlExample {
value = 'month';
options = [
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
{ value: 'year', label: 'Year' },
];
onValuechange(event: Event) {
console.log('valuechange', (event as CustomEvent).detail);
}
}API
| Property | Type | Default |
|---|---|---|
value | string | "" |
optionsproperty only | { value, label }[] | [] |
label | string | "" |
disabled | boolean | false |
| Event | When |
|---|---|
valuechange | The choice changed; detail is the option's value. |
Size
0.9 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 17.0 KB.