Remove badge

Playground

Properties

Size

Usage

Place RemoveBadge beside the item and remove that item in onClick.

'use client';
import { useRef, useState } from 'react';
import { RemoveBadge, Button } from '@mbsw/ui';

export default function Example() {
  const [visible, setVisible] = useState(true);
  const transferFocus = useRef(false);
  const changeVisibility = (next: boolean, button: HTMLButtonElement) => {
    transferFocus.current = button === button.ownerDocument.activeElement;
    setVisible(next);
  };
  const restoreFocus = (button: HTMLButtonElement | null) => {
    if (button && transferFocus.current) {
      transferFocus.current = false;
      button.focus();
    }
  };
  return visible ? (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 8 }}>
      {"Item 1"}
      <RemoveBadge
        ref={restoreFocus}
        aria-label={"Remove Item 1"}
        onClick={(event) => changeVisibility(false, event.currentTarget)}
      />
    </span>
  ) : (
    <Button
      ref={restoreFocus}
      onClick={(event) => changeVisibility(true, event.currentTarget)}
    >
      {"Add again"}
    </Button>
  );
}

The Playground Code tab includes the full example with your current settings. See Installation for the CSS imports.

Composition