TBX Docs
ExercisesCatalogue

GroupItems

Drag items from a common pool into labeled groups

GroupItems presents a mixed pool of items and empty labeled zones. Users drag items from the common area into the correct groups. It uses @dnd-kit/react for drag and drop.

Import

import { GroupItems } from '@bside-tech/tbx-ui/components';

Example

Group Items
Sort items into their correct categories.
Items
CatDogOakRoseTulip
Animals
Plants
<GroupItems
  entries={{
    animals: ['Cat', 'Dog'],
    plants: ['Oak', 'Rose', 'Tulip'],
  }}
/>

Props

Prop

Type

Style

Tailwind v4 is available for customisations. Pass classes via the classNames prop, or override CSS variables for fine-grained control.

CSS Classes

KeyElement
rootRoot wrapper
commonAreaCommon pool of items
groupsRowRow of group zones
groupZoneDrop zone for a group
groupLabelZone heading
groupContentZone content area
itemDraggable item
checkButtonCheck button
resetButtonReset button
resultResults text

CSS Variables

Scoped under .tbx-groupitems:

VariablePurpose
--groupitems-zone-bgZone background
--groupitems-zone-borderZone border
--groupitems-zone-dropZone background when drag-over
--groupitems-item-bgItem background (default)
--groupitems-item-borderItem border
--groupitems-item-in-target-bgItem background when in a target zone (before validation). Uses color-mix with --exercise-accent-color.
--groupitems-item-in-target-borderItem border when in a target zone (before validation). Uses --exercise-accent-color.
--groupitems-item-draggingItem background when dragging
--groupitems-action-bg, --groupitems-action-borderCheck/Reset button background and border
--groupitems-result-neutral, --groupitems-result-correct, --groupitems-result-wrongResults text colors (neutral, correct, wrong)
--groupitems-correct-bg, --groupitems-correct-borderCorrect item highlight
--groupitems-wrong-bg, --groupitems-wrong-borderWrong item highlight

These feed into --exercise-action-bg, --exercise-action-border and --exercise-result-* used by ExerciseLayout.

Item labels use --exercise-layout-content-font-size.

Labels

Override labels via the labels prop. Fixed keys: items (common pool area, default "Items"), check, reset (buttons), results (e.g. "correct" in "3 / 5 correct"). Use group ids (keys from entries) to override zone headings—since groups are defined dynamically, any entry key can be used as a label key.

<GroupItems
  entries={{ animals: ['Cat', 'Dog'], plants: ['Oak'] }}
  labels={{
    items: 'Drag here',
    animals: 'Mammals',
    plants: 'Flora',
    check: 'Verify',
    reset: 'Reset',
    results: 'correct',
  }}
/>

Hook

Use useGroupItemsExercise for custom group interfaces. Pass items and groups derived from your entries:

import { useGroupItemsExercise } from '@bside-tech/tbx-ui/hooks';

const entries = { animals: ['Cat', 'Dog'], plants: ['Oak'] };
const items = Object.entries(entries).flatMap(([group, labels]) =>
  labels.map((label) => ({ label, group }))
);
const groups = Object.keys(entries).map(id => ({ id, label: id }));

const { itemsByGroup, moveItem, solution, isCorrect, check, reset } = useGroupItemsExercise({
  items,
  groups,
  checkOn: 'manual',
});
ReturnDescription
itemsByGroupRecord of zone id → item ids in that zone
moveItemMove item from one group to another
setItemsByGroupSet full state (e.g. for shuffle/reset)
solutionCorrect group id for each item id
isCorrectValidation result
checkValidate placements
resetReset to initial state (all items in common)

On this page