Segment Builder

1 个变体

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>
  );
}

安装

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

API 参考

除标注为必填的属性外,其余均为可选。数据、文案和颜色默认使用示例内容。

SegmentBuilder15 个属性
  • 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

    默认值 380

  • breakdownSeries

    Series for the breakdown meter, with their colors.

    ChartSeries[]
  • trendColorClassName

    Text color class for the trend line and fill.

    string

    默认值 text-sky-500

  • allowGroups

    Shows the Add group button for nested groups.

    boolean

    默认值 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 个属性
  • count必填

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

    number
  • format必填

    Formats the difference, without its sign.

    (value: number) => string
  • duration

    How long the chip stays, in ms.

    number

    默认值 2400

JoinChip5 个属性
  • join必填

    How the group combines its rules.

    "and" | "or"
  • labels必填

    Text for each join.

    Record<SegmentJoin, string>
  • hint必填

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

    string
  • onToggle必填

    Flips the group between AND and OR.

    () => void
  • className

    Classes merged onto the root element.

    string
RuleGroup9 个属性
  • group必填

    The group to render.

    SegmentGroup
  • depth

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

    number

    默认值 0

  • properties必填

    Every property a rule can use.

    SegmentProperty[]
  • operators必填

    Every operator.

    SegmentOperator[]
  • actions必填

    Edit callbacks.

    RuleGroupActions
  • cascade必填

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

    boolean
  • allowGroups必填

    Shows the Add group button at the top level.

    boolean
  • reduce必填

    Reduced motion keeps fades only.

    boolean
  • labels必填

    Copy.

    SegmentBuilderLabels
RuleRow8 个属性
  • rule必填

    The rule shown in this row.

    SegmentRule
  • properties必填

    Every property the rule can use.

    SegmentProperty[]
  • operators必填

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

    SegmentOperator[]
  • onPropertyChange必填

    Called with a new property id.

    (propertyId: string) => void
  • onOperatorChange必填

    Called with a new operator id.

    (operatorId: string) => void
  • onValueChange必填

    Called with a new value.

    (value: string) => void
  • onRemove必填

    Removes the rule.

    () => void
  • labels必填

    Copy for field names and the remove tooltip.

    SegmentBuilderLabels
RuleValueField8 个属性
  • input必填

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

    "number" | "option" | "text"
  • value必填

    Current value.

    string
  • onChange必填

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

    (value: string) => void
  • options

    Options for the menu.

    SegmentOption[]

    默认值 []

  • placeholder

    Placeholder for text and number fields.

    string
  • unit

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

    string
  • label必填

    Accessible name of the field.

    string
  • choose必填

    Placeholder for an empty menu.

    string
SavedSegments5 个属性
  • saved必填

    Saved segments, oldest first.

    SavedSegment[]
  • activeId必填

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

    string | null
  • onLoad必填

    Loads a saved segment into the builder.

    (saved: SavedSegment) => void
  • label必填

    Accessible name of the row.

    string
  • reduce必填

    Reduced motion keeps fades only.

    boolean
SegmentFooter5 个属性
  • summary必填

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

    string
  • saveState必填

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

    "error" | "loading" | "idle" | "success"
  • onSave必填

    Saves the segment.

    () => void
  • onReset必填

    Puts the starting segment back.

    () => void
  • labels必填

    Copy.

    SegmentBuilderLabels
SegmentHeader7 个属性
  • name必填

    Segment name in the inline field.

    string
  • onNameChange必填

    Called as the name is edited.

    (name: string) => void
  • count必填

    Users matching the segment.

    number
  • total必填

    All users, for the share figure.

    number
  • estimating必填

    Dims the figures and shows the estimating note.

    boolean
  • formatNumber必填

    Formats counts, such as `12,480`.

    (value: number) => string
  • labels必填

    Copy.

    SegmentBuilderLabels
SegmentPreview6 个属性
  • estimate必填

    Current estimate: daily trend and breakdown.

    SegmentEstimate
  • breakdownSeries必填

    Series for the breakdown meter, with their colors.

    ChartSeries[]
  • trendColorClassName

    Text color class for the trend line and fill.

    string
  • estimating必填

    Dims the charts while a new estimate is computed.

    boolean
  • formatNumber必填

    Formats counts in the legend.

    (value: number) => string
  • labels必填

    Copy.

    SegmentBuilderLabels
SegmentTrendChart4 个属性
  • values必填

    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

    默认值 text-sky-500

  • label必填

    Accessible description of the chart.

    string
  • className

    Classes merged onto the root element.

    string
useSegmentEstimate4 个属性
  • root必填

    The rules to estimate.

    SegmentGroup
  • estimate必填

    Computes the estimate for a set of rules.

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

    Properties and breakdown ids passed through to `estimate`.

    SegmentEstimateContext
  • delay

    Wait after the last change before estimating, in ms.

    number

    默认值 380

useSegmentSave5 个属性
  • initialSaved必填

    Saved segments the chip row starts with.

    SavedSegment[]
  • initialActiveId必填

    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

    默认值 700

  • successFor

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

    number

    默认值 1600

useSegmentState4 个属性
  • initial必填

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

    Segment
  • properties必填

    Property definitions, for new rules and value resets.

    SegmentProperty[]
  • operators必填

    Operator definitions, for new rules and value resets.

    SegmentOperator[]
  • onChange

    Called with the whole segment after every edit.

    ((segment: Segment) => void)
类型14 个类型

这些属性使用的对象类型及其字段。

ChartSeries4 个字段
  • id必填

    Unique key that matches the value keys in each datum.

    string
  • label必填

    Series name shown in the legend and tooltip.

    string
  • color必填

    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 个字段
  • id必填

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

    string
  • label必填

    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 个字段
  • id必填

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

    string
  • label必填

    Label shown in the property menu.

    string
  • icon

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

    ComponentType<{ className?: string }>
  • input必填

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

    SegmentValueInput
  • operators必填

    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 个字段
  • id必填

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

    string
  • join必填

    How this group's rules and child groups combine.

    SegmentJoin
  • rules必填

    Rules in display order.

    SegmentRule[]
  • groups必填

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

    SegmentGroup[]
Segment2 个字段
  • name必填

    Segment name shown in the header.

    string
  • root必填

    The top-level group holding every rule.

    SegmentGroup
SavedSegment2 个字段
  • id必填

    Stable id for the chip.

    string
  • segment必填

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

    Segment
SegmentEstimate4 个字段
  • count必填

    Users matching the segment.

    number
  • total必填

    All users, for the share line.

    number
  • daily必填

    Daily active users of the segment, oldest first.

    number[]
  • breakdown必填

    Matching users split by the breakdown series.

    SegmentBreakdownValue[]
SegmentBuilderLabels24 个字段
  • name必填

    Accessible name of the segment name field.

    string
  • users必填

    Noun after the audience count.

    string
  • ofAll必填

    Caption after the share figure.

    string
  • estimating必填

    Shown while a new estimate is computed.

    string
  • saved必填

    Heading above the saved segment chips.

    string
  • rules必填

    Accessible name of the rules list.

    string
  • empty必填

    Shown when a group has no rules.

    string
  • and必填

    Join chip text for AND.

    string
  • or必填

    Join chip text for OR.

    string
  • toggleJoin必填

    Accessible hint on the join chip.

    string
  • addRule必填

    Add rule button.

    string
  • addGroup必填

    Add group button.

    string
  • removeRule必填

    Remove rule tooltip and accessible name.

    string
  • removeGroup必填

    Remove group tooltip and accessible name.

    string
  • property必填

    Accessible name of the property menu.

    string
  • operator必填

    Accessible name of the operator menu.

    string
  • value必填

    Accessible name of the value field.

    string
  • choose必填

    Placeholder for an empty value menu.

    string
  • trend必填

    Title of the trend chart.

    string
  • breakdown必填

    Title of the breakdown meter.

    string
  • save必填

    Save button.

    string
  • saving必填

    Save button while saving.

    string
  • savedDone必填

    Save button once saved.

    string
  • reset必填

    Reset button.

    string
SegmentSummaryInfo3 个字段
  • rules必填

    Rules in the segment, nested groups included.

    number
  • groups必填

    Nested groups.

    number
  • join必填

    How the top level combines.

    SegmentJoin
SegmentEstimateContext2 个字段
  • properties必填

    Property definitions, for options and narrowing functions.

    SegmentProperty[]
  • breakdownIds必填

    Ids of the breakdown series, in display order.

    string[]
SegmentOption3 个字段
  • value必填

    Stored value, such as `us`.

    string
  • label必填

    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 个字段
  • id必填

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

    string
  • property必填

    Property id.

    string
  • operator必填

    Operator id.

    string
  • value必填

    Entered or chosen value, always stored as a string.

    string
SegmentBreakdownValue2 个字段
  • id必填

    Series id from `breakdownSeries`.

    string
  • value必填

    Users in this slice.

    number
RuleGroupActions8 个字段
  • addRule必填

    Adds a rule to a group.

    (groupId: string) => void
  • removeRule必填

    Removes a rule from a group.

    (groupId: string, ruleId: string) => void
  • setProperty必填

    Changes a rule's property.

    (groupId: string, ruleId: string, propertyId: string) => void
  • setOperator必填

    Changes a rule's operator.

    (groupId: string, ruleId: string, operatorId: string) => void
  • setValue必填

    Changes a rule's value.

    (groupId: string, ruleId: string, value: string) => void
  • toggleJoin必填

    Flips a group between AND and OR.

    (groupId: string) => void
  • addGroup必填

    Adds a nested group to the top level.

    () => void
  • removeGroup必填

    Removes a nested group.

    (groupId: string) => void