Segment Builder

1 variant

An audience builder with AND/OR rules, nested groups, and a live estimate that updates the user count and charts.

Segment rules

Property, operator, and value rules joined by AND/OR, with nested groups and a live estimate that rolls the user count. The 30-day trend and plan mix animate to each new estimate, and saved segments load with one click.

7,308users15.1%of all users

7,308 users, 15.1% of all users

  • days
  • sessions
Daily active, last 30 days1,959
By plan

3 rules · match all

"use client";

import { SegmentBuilder } from "@/components/premium/segment-builder";

export function SegmentBuilderRulesPreview() {
  return (
    <div className="w-full px-4 py-10">
      <SegmentBuilder />
    </div>
  );
}

Install

$ bunx --bun shadcn add @beui-pro/segment-builder-rules

API Reference

Every prop is optional unless marked required. Data, copy, and colors default to the sample content.

SegmentBuilder15 props
  • defaultSegment

    Segment the builder starts with, and returns to on Reset.

    Segment
  • properties

    Properties a rule can filter on.

    SegmentProperty[]
  • operators

    Operators; each property lists the ids it offers.

    SegmentOperator[]
  • savedSegments

    Saved segments shown as chips above the header. Pass `[]` to hide the row.

    SavedSegment[]
  • estimate

    Estimates the audience for a set of rules. Defaults to a sample model.

    ((root: SegmentGroup, context: SegmentEstimateContext) => SegmentEstimate)
  • estimateDelay

    Wait after the last edit before estimating, in ms.

    number

    Default 380

  • breakdownSeries

    Series for the breakdown meter, with their colors.

    ChartSeries[]
  • trendColorClassName

    Text color class for the trend line and fill.

    string

    Default text-sky-500

  • allowGroups

    Shows the Add group button for nested groups.

    boolean

    Default true

  • onChange

    Called with the whole segment after every edit.

    ((segment: Segment) => void)
  • onSave

    Saves the segment. A returned promise keeps the button loading until it settles.

    ((segment: Segment) => void | Promise<void>)
  • formatNumber

    Formats audience counts.

    ((value: number) => string)
  • formatSummary

    Builds the footer summary from the rule and group counts.

    ((info: SegmentSummaryInfo) => string)
  • labels

    Copy overrides; anything left out uses the English defaults.

    Partial<SegmentBuilderLabels>
  • className

    Classes merged onto the root element.

    string
AudienceDelta3 props
  • countRequired

    Current audience count. A change shows the difference for a moment.

    number
  • formatRequired

    Formats the difference, without its sign.

    (value: number) => string
  • duration

    How long the chip stays, in ms.

    number

    Default 2400

JoinChip5 props
  • joinRequired

    How the group combines its rules.

    "and" | "or"
  • labelsRequired

    Text for each join.

    Record<SegmentJoin, string>
  • hintRequired

    Accessible hint, such as "Switch how rules combine".

    string
  • onToggleRequired

    Flips the group between AND and OR.

    () => void
  • className

    Classes merged onto the root element.

    string
RuleGroup9 props
  • groupRequired

    The group to render.

    SegmentGroup
  • depth

    Nested groups (depth 1) cannot add groups of their own.

    number

    Default 0

  • propertiesRequired

    Every property a rule can use.

    SegmentProperty[]
  • operatorsRequired

    Every operator.

    SegmentOperator[]
  • actionsRequired

    Edit callbacks.

    RuleGroupActions
  • cascadeRequired

    Rows mounting now cascade in, as after a load or reset.

    boolean
  • allowGroupsRequired

    Shows the Add group button at the top level.

    boolean
  • reduceRequired

    Reduced motion keeps fades only.

    boolean
  • labelsRequired

    Copy.

    SegmentBuilderLabels
RuleRow8 props
  • ruleRequired

    The rule shown in this row.

    SegmentRule
  • propertiesRequired

    Every property the rule can use.

    SegmentProperty[]
  • operatorsRequired

    Every operator; the row offers the ones its property lists.

    SegmentOperator[]
  • onPropertyChangeRequired

    Called with a new property id.

    (propertyId: string) => void
  • onOperatorChangeRequired

    Called with a new operator id.

    (operatorId: string) => void
  • onValueChangeRequired

    Called with a new value.

    (value: string) => void
  • onRemoveRequired

    Removes the rule.

    () => void
  • labelsRequired

    Copy for field names and the remove tooltip.

    SegmentBuilderLabels
RuleValueField8 props
  • inputRequired

    Field to show: a menu of options, free text or a number.

    "number" | "option" | "text"
  • valueRequired

    Current value.

    string
  • onChangeRequired

    Called with the new value as the user picks or types.

    (value: string) => void
  • options

    Options for the menu.

    SegmentOption[]

    Default []

  • placeholder

    Placeholder for text and number fields.

    string
  • unit

    Unit shown inside a number field, such as `days`.

    string
  • labelRequired

    Accessible name of the field.

    string
  • chooseRequired

    Placeholder for an empty menu.

    string
SavedSegments5 props
  • savedRequired

    Saved segments, oldest first.

    SavedSegment[]
  • activeIdRequired

    Id of the chip whose segment is loaded, if any.

    string | null
  • onLoadRequired

    Loads a saved segment into the builder.

    (saved: SavedSegment) => void
  • labelRequired

    Accessible name of the row.

    string
  • reduceRequired

    Reduced motion keeps fades only.

    boolean
SegmentFooter5 props
  • summaryRequired

    Summary on the left, such as "3 rules · matches all".

    string
  • saveStateRequired

    Save button state: idle, loading, success or error.

    "error" | "loading" | "idle" | "success"
  • onSaveRequired

    Saves the segment.

    () => void
  • onResetRequired

    Puts the starting segment back.

    () => void
  • labelsRequired

    Copy.

    SegmentBuilderLabels
SegmentHeader7 props
  • nameRequired

    Segment name in the inline field.

    string
  • onNameChangeRequired

    Called as the name is edited.

    (name: string) => void
  • countRequired

    Users matching the segment.

    number
  • totalRequired

    All users, for the share figure.

    number
  • estimatingRequired

    Dims the figures and shows the estimating note.

    boolean
  • formatNumberRequired

    Formats counts, such as `12,480`.

    (value: number) => string
  • labelsRequired

    Copy.

    SegmentBuilderLabels
SegmentPreview6 props
  • estimateRequired

    Current estimate: daily trend and breakdown.

    SegmentEstimate
  • breakdownSeriesRequired

    Series for the breakdown meter, with their colors.

    ChartSeries[]
  • trendColorClassName

    Text color class for the trend line and fill.

    string
  • estimatingRequired

    Dims the charts while a new estimate is computed.

    boolean
  • formatNumberRequired

    Formats counts in the legend.

    (value: number) => string
  • labelsRequired

    Copy.

    SegmentBuilderLabels
SegmentTrendChart4 props
  • valuesRequired

    Daily values, oldest first. Keep the length steady so the shape morphs.

    number[]
  • colorClassName

    Text color class that paints the line and its fill, such as `text-sky-500`.

    string

    Default text-sky-500

  • labelRequired

    Accessible description of the chart.

    string
  • className

    Classes merged onto the root element.

    string
useSegmentEstimate4 props
  • rootRequired

    The rules to estimate.

    SegmentGroup
  • estimateRequired

    Computes the estimate for a set of rules.

    (root: SegmentGroup, context: SegmentEstimateContext) => SegmentEstimate
  • contextRequired

    Properties and breakdown ids passed through to `estimate`.

    SegmentEstimateContext
  • delay

    Wait after the last change before estimating, in ms.

    number

    Default 380

useSegmentSave5 props
  • initialSavedRequired

    Saved segments the chip row starts with.

    SavedSegment[]
  • initialActiveIdRequired

    Chip that starts active, if any.

    string | null
  • onSave

    Persists a segment. A returned promise holds the loading state until it settles.

    ((segment: Segment) => void | Promise<void>)
  • minLoading

    Shortest loading state, in ms, so a fast save still reads.

    number

    Default 700

  • successFor

    How long the saved state shows before returning to idle, in ms.

    number

    Default 1600

useSegmentState4 props
  • initialRequired

    Segment the builder starts with, and returns to on reset.

    Segment
  • propertiesRequired

    Property definitions, for new rules and value resets.

    SegmentProperty[]
  • operatorsRequired

    Operator definitions, for new rules and value resets.

    SegmentOperator[]
  • onChange

    Called with the whole segment after every edit.

    ((segment: Segment) => void)
Types14 types

The object types these props take, field by field.

ChartSeries4 fields
  • idRequired

    Unique key that matches the value keys in each datum.

    string
  • labelRequired

    Series name shown in the legend and tooltip.

    string
  • colorRequired

    Fill class for bars, segments, and dots, such as `bg-blue-500`.

    string
  • stroke

    Stroke class for lines, such as `stroke-blue-500`.

    string
SegmentOperator4 fields
  • idRequired

    Stable id stored on the rule, such as `is_not`.

    string
  • labelRequired

    Label shown in the operator menu, such as `is not`.

    string
  • input

    Field this operator needs. Omit to use the property's own field.

    SegmentValueInput
  • unit

    Unit shown inside a number field, such as `days`.

    string
SegmentProperty10 fields
  • idRequired

    Stable id stored on the rule, such as `country`.

    string
  • labelRequired

    Label shown in the property menu.

    string
  • icon

    Icon shown before the label in the menu and the trigger.

    ComponentType<{ className?: string }>
  • inputRequired

    The property's own field: options for enums, text or a number.

    SegmentValueInput
  • operatorsRequired

    Operator ids offered for this property, in menu order.

    string[]
  • options

    Options for `option` fields.

    SegmentOption[]
  • defaultValue

    Value a new rule starts with. Defaults to the first option, or empty.

    string
  • placeholder

    Placeholder for text and number fields.

    string
  • unit

    Unit shown inside a number field, such as `sessions`.

    string
  • narrow

    Share of users (0 to 1) a rule on this property keeps, for the sample estimate. Omit to use the generic model for the field type.

    (operator: string, value: string) => number
SegmentGroup4 fields
  • idRequired

    Stable id, used as the key for the group box.

    string
  • joinRequired

    How this group's rules and child groups combine.

    SegmentJoin
  • rulesRequired

    Rules in display order.

    SegmentRule[]
  • groupsRequired

    Nested groups, shown after the rules. Only the top level nests.

    SegmentGroup[]
Segment2 fields
  • nameRequired

    Segment name shown in the header.

    string
  • rootRequired

    The top-level group holding every rule.

    SegmentGroup
SavedSegment2 fields
  • idRequired

    Stable id for the chip.

    string
  • segmentRequired

    The saved segment, loaded into the builder when its chip is clicked.

    Segment
SegmentEstimate4 fields
  • countRequired

    Users matching the segment.

    number
  • totalRequired

    All users, for the share line.

    number
  • dailyRequired

    Daily active users of the segment, oldest first.

    number[]
  • breakdownRequired

    Matching users split by the breakdown series.

    SegmentBreakdownValue[]
SegmentBuilderLabels24 fields
  • nameRequired

    Accessible name of the segment name field.

    string
  • usersRequired

    Noun after the audience count.

    string
  • ofAllRequired

    Caption after the share figure.

    string
  • estimatingRequired

    Shown while a new estimate is computed.

    string
  • savedRequired

    Heading above the saved segment chips.

    string
  • rulesRequired

    Accessible name of the rules list.

    string
  • emptyRequired

    Shown when a group has no rules.

    string
  • andRequired

    Join chip text for AND.

    string
  • orRequired

    Join chip text for OR.

    string
  • toggleJoinRequired

    Accessible hint on the join chip.

    string
  • addRuleRequired

    Add rule button.

    string
  • addGroupRequired

    Add group button.

    string
  • removeRuleRequired

    Remove rule tooltip and accessible name.

    string
  • removeGroupRequired

    Remove group tooltip and accessible name.

    string
  • propertyRequired

    Accessible name of the property menu.

    string
  • operatorRequired

    Accessible name of the operator menu.

    string
  • valueRequired

    Accessible name of the value field.

    string
  • chooseRequired

    Placeholder for an empty value menu.

    string
  • trendRequired

    Title of the trend chart.

    string
  • breakdownRequired

    Title of the breakdown meter.

    string
  • saveRequired

    Save button.

    string
  • savingRequired

    Save button while saving.

    string
  • savedDoneRequired

    Save button once saved.

    string
  • resetRequired

    Reset button.

    string
SegmentSummaryInfo3 fields
  • rulesRequired

    Rules in the segment, nested groups included.

    number
  • groupsRequired

    Nested groups.

    number
  • joinRequired

    How the top level combines.

    SegmentJoin
SegmentEstimateContext2 fields
  • propertiesRequired

    Property definitions, for options and narrowing functions.

    SegmentProperty[]
  • breakdownIdsRequired

    Ids of the breakdown series, in display order.

    string[]
SegmentOption3 fields
  • valueRequired

    Stored value, such as `us`.

    string
  • labelRequired

    Label shown in the menu, such as `United States`.

    string
  • share

    Share of all users with this value, 0 to 1. The sample model reads it.

    number
SegmentRule4 fields
  • idRequired

    Stable id, used as the row key so rows enter, leave and glide.

    string
  • propertyRequired

    Property id.

    string
  • operatorRequired

    Operator id.

    string
  • valueRequired

    Entered or chosen value, always stored as a string.

    string
SegmentBreakdownValue2 fields
  • idRequired

    Series id from `breakdownSeries`.

    string
  • valueRequired

    Users in this slice.

    number
RuleGroupActions8 fields
  • addRuleRequired

    Adds a rule to a group.

    (groupId: string) => void
  • removeRuleRequired

    Removes a rule from a group.

    (groupId: string, ruleId: string) => void
  • setPropertyRequired

    Changes a rule's property.

    (groupId: string, ruleId: string, propertyId: string) => void
  • setOperatorRequired

    Changes a rule's operator.

    (groupId: string, ruleId: string, operatorId: string) => void
  • setValueRequired

    Changes a rule's value.

    (groupId: string, ruleId: string, value: string) => void
  • toggleJoinRequired

    Flips a group between AND and OR.

    (groupId: string) => void
  • addGroupRequired

    Adds a nested group to the top level.

    () => void
  • removeGroupRequired

    Removes a nested group.

    (groupId: string) => void