beUI Pro

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 リファレンス

必須と記された props 以外はすべて省略できます。データ、文言、色はサンプルの内容が既定値です。

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

    既定値 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の props
  • 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 個の型

これらの props が受け取るオブジェクト型とそのフィールド。

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