ButtonGroup

The <smart-button-group> component renders a horizontal group of related buttons that share a single-selection state. It uses the InjectionToken pattern: a default ButtonGroupStandardComponent is rendered, which can be replaced via BUTTON_GROUP_STANDARD_COMPONENT_TOKEN.


Usage

<smart-button-group
  [buttons]="buttons"
  [selected]="view()"
  (buttonClick)="onButtonClick($event)"
/>
<p>Showing: {{ view() }}</p>

The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.

Components

ButtonGroupComponent (<smart-button-group>)

Main wrapper component. Renders ButtonGroupStandardComponent by default. When BUTTON_GROUP_STANDARD_COMPONENT_TOKEN is provided, renders the injected component via NgComponentOutlet, passes it every input (class included), and forwards its selected changes and buttonClick to the wrapper.

ButtonGroupStandardComponent (<smart-button-group-standard>)

Default concrete implementation. Renders an unstyled <div role="group"> containing one <button> per item (aria-pressed on the selected one), with optional label and count spans. It does not render icon and ignores options.variant.

ButtonGroupPresetComponent (<smart-button-group-preset>)

Fully-styled, drop-in concrete implementation (Preline button-group look) that extends ButtonGroupBaseComponent. Renders a segmented control with a bg-white/dark:bg-gray-800 surface, shared borders collapsed via -ms-px, and rounded ends. The segment matching selected is emphasised (aria-pressed="true" + blue text). Each button shows its icon text before the label. options.variant adjusts per-button content: icon-only hides labels (and sets aria-label), with-stat styles the count pill with the blue stat palette; all other variants (basic, with-dropdown, with-checkbox-select) render the basic label + count layout. Every segment uses one medium size.

Register it through BUTTON_GROUP_STANDARD_COMPONENT_TOKEN ({ provide: BUTTON_GROUP_STANDARD_COMPONENT_TOKEN, useValue: ButtonGroupPresetComponent }) to restyle every <smart-button-group>, register every preset at once with provideSmartPresets(), or use the <smart-button-group-preset> selector directly (it takes the extra classes as class or [cssClass]).

ButtonGroupBaseComponent (abstract)

Abstract base directive (@Directive()) for extending custom button-group implementations. Exposes select(id: string) which sets selected and emits buttonClick.

API

Inputs / Models / Outputs

NameKindTypeDefaultDescription
buttonsinputIButtonGroupButton[][]Array of button definitions
optionsinputIButtonGroupOptions | undefinedundefinedGroup-wide options (variant, etc.)
selectedmodelstring | undefinedundefinedTwo-way bound id of the currently selected button; selectedChange reports a click
classinputstring''External CSS classes (alias for cssClass)
buttonClickoutputIButtonGroupButtonClick ({ buttonId: string }, exported from @smartsoft001/angular)-Emitted when a button is clicked

IButtonGroupButton

FieldTypeDefaultDescription
idstringrequiredIdentifies the button in selected and buttonClick.
labelstringundefinedVisible text (moved to aria-label by the preset's icon-only variant).
iconstringundefinedPreset only: text or glyph rendered before the label. The standard ignores it.
disabledbooleanundefinedDisables the button.
countnumberundefinedA number shown after the label.

IButtonGroupOptions

FieldTypeDefaultDescription
variantSmartButtonGroupVariant'basic'Preset only: icon-only hides the labels, with-stat styles the count as a blue pill; with-dropdown and with-checkbox-select render like basic. The standard ignores it.

SmartButtonGroupVariant is 'basic' | 'icon-only' | 'with-stat' | 'with-dropdown' | 'with-checkbox-select'.

BUTTON_GROUP_STANDARD_COMPONENT_TOKEN

InjectionToken that allows replacing the default ButtonGroupStandardComponent with a custom implementation. Provide a Type<ButtonGroupBaseComponent> to override; the wrapper passes it every input it declares, class included, and forwards its selected changes and buttonClick.

Extending the base class

import {
  ChangeDetectionStrategy,
  Component,
  ViewEncapsulation,
} from '@angular/core';

import {
  ButtonGroupBaseComponent,
  ButtonGroupComponent,
  BUTTON_GROUP_STANDARD_COMPONENT_TOKEN,
  IButtonGroupButton,
} from '@smartsoft001/angular';

@Component({
  selector: 'docs-custom-button-group',
  template: `
    <div role="group" class="docs-button-group" [class]="cssClass()">
      @for (button of buttons(); track button.id) {
        <button
          type="button"
          class="docs-button-group__button"
          [disabled]="button.disabled ?? false"
          [attr.aria-pressed]="selected() === button.id"
          (click)="select(button.id)"
        >
          {{ button.label }}

          @if (button.count !== undefined) {
            <span class="docs-button-group__count">{{ button.count }}</span>
          }
        </button>
      }
    </div>
  `,
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomButtonGroupComponent extends ButtonGroupBaseComponent {}

@Component({
  selector: 'docs-button-group-custom-example',
  imports: [ButtonGroupComponent],
  providers: [
    {
      provide: BUTTON_GROUP_STANDARD_COMPONENT_TOKEN,
      useValue: CustomButtonGroupComponent,
    },
  ],
  template: ` <smart-button-group [buttons]="buttons" selected="month" /> `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ButtonGroupCustomExampleComponent {
  buttons: IButtonGroupButton[] = [
    { id: 'years', label: 'Years' },
    { id: 'month', label: 'Month' },
    { id: 'date', label: 'Date' },
  ];
}

Source

The component lives in packages/shared/angular/src/lib/components/button-group and is documented for Claude Code by the angular-components-button-group skill.

SmartButtonGroup renders a row of toggle buttons (a segmented control, role="group") where one button is selected. Each button has an id, a label and optionally an icon and a count. The selection is controlled (selected + onSelectedChange) or kept inside the group (defaultSelected); every click is also reported through onButtonClick({ buttonId }). SmartButtonGroupStandard is unstyled; SmartButtonGroupPreset is the styled look.


Usage

import { useState } from 'react';

import {
  IButtonGroupButton,
  IButtonGroupButtonClick,
  SmartButtonGroup,
} from '@smartsoft001/react';

const buttons: IButtonGroupButton[] = [
  { id: 'day', label: 'Day' },
  { id: 'week', label: 'Week' },
  { id: 'month', label: 'Month' },
];

export function ButtonGroupUsageExample() {
  const [view, setView] = useState('week');

  const onButtonClick = ({ buttonId }: IButtonGroupButtonClick) =>
    setView(buttonId);

  return (
    <>
      <SmartButtonGroup
        buttons={buttons}
        selected={view}
        onButtonClick={onButtonClick}
      />
      <p>Showing: {view}</p>
    </>
  );
}

The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.

Exports

All from @smartsoft001/react.

ExportKindWhat it is
SmartButtonGroupcomponentRenders the implementation registered as components['button-group'] on SmartProvider, SmartButtonGroupStandard by default.
SmartButtonGroupPresetcomponentStyled button group variation (preset).
SmartButtonGroupStandardcomponentThe default button group rendering: an unstyled role="group" of toggle buttons with their label and count.
useButtonGrouphookThe behaviour every button group variant shares: the selected button id, controlled through selected / onSelectedChange or kept internally, and select(id), which selects a button and emits onButtonClick.

The preset's class helpers (getButtonGroupClasses, getButtonGroupButtonClasses, getButtonGroupCountClasses, getButtonGroupIconClasses, BUTTON_GROUP_CONTAINER) and the ButtonGroupPresetSize type ('sm' | 'md' | 'lg', the size getButtonGroupButtonClasses takes; the preset itself always uses 'md') are exported too, for a custom implementation that wants the preset look.

Props and Types

SmartButtonGroupProps

PropTypeDefaultDescription
buttons?IButtonGroupButton[][]The buttons, in order.
options?IButtonGroupOptions—The variant.
selected?string—The id of the selected button. Controlled when defined; otherwise the group keeps the selection itself.
defaultSelected?string—The initial selection when selected is not controlled.
onSelectedChange?(selected: string) => void—Called with the id of the button the user selected.
className?string—Classes on the group container.
onButtonClick?(event: IButtonGroupButtonClick) => void—A button was clicked.

IButtonGroupOptions

FieldTypeDefaultDescription
variant?SmartButtonGroupVariant'basic'Read by the preset: icon-only hides the labels, with-stat styles the count as a pill; with-dropdown and with-checkbox-select render like basic.

IButtonGroupButtonClick

Payload of onButtonClick.

FieldTypeDefaultDescription
buttonIdstringrequiredThe id of the clicked button.

IButtonGroupButton

FieldTypeDefaultDescription
idstringrequiredIdentifies the button in selected, onSelectedChange and onButtonClick.
label?string—Visible text (moved to aria-label by the preset's icon-only variant).
icon?string—Text or glyph rendered before the label by the preset.
disabled?boolean—Disables the button.
count?number—A number shown after the label.
  • SmartButtonGroupVariant: 'basic' \| 'icon-only' \| 'with-stat' \| 'with-dropdown' \| 'with-checkbox-select'

Replacing the Implementation

SmartButtonGroup renders the component registered under the 'button-group' key of SmartProvider's components, and SmartButtonGroupStandard when nothing is registered there. Every SmartButtonGroup below the provider then renders the registered component, which receives the same props.

SmartButtonGroupPreset is the styled (preset) implementation: register it under the 'button-group' key of SmartProvider's components, render it directly in place of SmartButtonGroup, or spread SMART_PRESET_COMPONENTS on the provider to register every preset at once (see the react-provider skill). Pass components as a stable object (a module constant or a memoised value), or the context changes on every render.

The useButtonGroup hook

The behaviour every button group variant shares: the selected button id, controlled through selected / onSelectedChange or kept internally, and select(id), which selects a button and emits onButtonClick.

ReturnsTypeDescription
selectedstring | undefinedThe selected id (the selected prop when controlled).
select(id: string) => voidSelects a button: updates the internal state when uncontrolled, then calls onSelectedChange and onButtonClick.
import {
  IButtonGroupButton,
  SmartButtonGroup,
  SmartButtonGroupProps,
  SmartProvider,
  useButtonGroup,
} from '@smartsoft001/react';

export function CustomButtonGroup(props: SmartButtonGroupProps) {
  const { buttons = [], className } = props;
  const { selected, select } = useButtonGroup(props);

  return (
    <div
      role="group"
      className={['docs-button-group', className].filter(Boolean).join(' ')}
    >
      {buttons.map((button) => (
        <button
          key={button.id}
          type="button"
          className="docs-button-group__button"
          disabled={button.disabled ?? false}
          aria-pressed={selected === button.id}
          onClick={() => select(button.id)}
        >
          {button.label}

          {button.count !== undefined && (
            <span className="docs-button-group__count">{button.count}</span>
          )}
        </button>
      ))}
    </div>
  );
}

// A module constant: a new object on every render would change the context.
const components = { 'button-group': CustomButtonGroup };

const buttons: IButtonGroupButton[] = [
  { id: 'years', label: 'Years' },
  { id: 'month', label: 'Month' },
  { id: 'date', label: 'Date' },
];

export function ButtonGroupCustomExample() {
  return (
    <SmartProvider components={components}>
      <SmartButtonGroup buttons={buttons} defaultSelected="month" />
    </SmartProvider>
  );
}

Styling

  • SmartButtonGroupStandard is an unstyled role="group" of buttons (aria-pressed on the selected one, smart-button-group-count on the count).
  • SmartButtonGroupPreset renders the segmented look (collapsed borders, rounded ends, an emphasised active segment) in one medium size, with smart:dark: variants.

Source

The component lives in packages/shared/react/src/lib/components/button-group and is documented for Claude Code by the react-components-button-group skill.