Segment Builder
1 variantAn 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,308 users, 15.1% of all users
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>
);
}
Source code is locked
Get access to install premium components and view the full source.
Open dashboardInstall
API Reference
Every prop is optional unless marked required. Data, copy, and colors default to the sample content.
SegmentBuilder15 props
defaultSegmentSegment the builder starts with, and returns to on Reset.
SegmentpropertiesProperties a rule can filter on.
SegmentProperty[]operatorsOperators; each property lists the ids it offers.
SegmentOperator[]savedSegmentsSaved segments shown as chips above the header. Pass `[]` to hide the row.
SavedSegment[]estimateEstimates the audience for a set of rules. Defaults to a sample model.
((root: SegmentGroup, context: SegmentEstimateContext) => SegmentEstimate)estimateDelayWait after the last edit before estimating, in ms.
numberDefault
380breakdownSeriesSeries for the breakdown meter, with their colors.
ChartSeries[]trendColorClassNameText color class for the trend line and fill.
stringDefault
text-sky-500allowGroupsShows the Add group button for nested groups.
booleanDefault
trueonChangeCalled with the whole segment after every edit.
((segment: Segment) => void)onSaveSaves the segment. A returned promise keeps the button loading until it settles.
((segment: Segment) => void | Promise<void>)formatNumberFormats audience counts.
((value: number) => string)formatSummaryBuilds the footer summary from the rule and group counts.
((info: SegmentSummaryInfo) => string)labelsCopy overrides; anything left out uses the English defaults.
Partial<SegmentBuilderLabels>classNameClasses merged onto the root element.
string
AudienceDelta3 props
countRequiredCurrent audience count. A change shows the difference for a moment.
numberformatRequiredFormats the difference, without its sign.
(value: number) => stringdurationHow long the chip stays, in ms.
numberDefault
2400
JoinChip5 props
joinRequiredHow the group combines its rules.
"and" | "or"labelsRequiredText for each join.
Record<SegmentJoin, string>hintRequiredAccessible hint, such as "Switch how rules combine".
stringonToggleRequiredFlips the group between AND and OR.
() => voidclassNameClasses merged onto the root element.
string
RuleGroup9 props
groupRequiredThe group to render.
SegmentGroupdepthNested groups (depth 1) cannot add groups of their own.
numberDefault
0propertiesRequiredEvery property a rule can use.
SegmentProperty[]operatorsRequiredEvery operator.
SegmentOperator[]actionsRequiredEdit callbacks.
RuleGroupActionscascadeRequiredRows mounting now cascade in, as after a load or reset.
booleanallowGroupsRequiredShows the Add group button at the top level.
booleanreduceRequiredReduced motion keeps fades only.
booleanlabelsRequiredCopy.
SegmentBuilderLabels
RuleRow8 props
ruleRequiredThe rule shown in this row.
SegmentRulepropertiesRequiredEvery property the rule can use.
SegmentProperty[]operatorsRequiredEvery operator; the row offers the ones its property lists.
SegmentOperator[]onPropertyChangeRequiredCalled with a new property id.
(propertyId: string) => voidonOperatorChangeRequiredCalled with a new operator id.
(operatorId: string) => voidonValueChangeRequiredCalled with a new value.
(value: string) => voidonRemoveRequiredRemoves the rule.
() => voidlabelsRequiredCopy for field names and the remove tooltip.
SegmentBuilderLabels
RuleValueField8 props
inputRequiredField to show: a menu of options, free text or a number.
"number" | "option" | "text"valueRequiredCurrent value.
stringonChangeRequiredCalled with the new value as the user picks or types.
(value: string) => voidoptionsOptions for the menu.
SegmentOption[]Default
[]placeholderPlaceholder for text and number fields.
stringunitUnit shown inside a number field, such as `days`.
stringlabelRequiredAccessible name of the field.
stringchooseRequiredPlaceholder for an empty menu.
string
SavedSegments5 props
savedRequiredSaved segments, oldest first.
SavedSegment[]activeIdRequiredId of the chip whose segment is loaded, if any.
string | nullonLoadRequiredLoads a saved segment into the builder.
(saved: SavedSegment) => voidlabelRequiredAccessible name of the row.
stringreduceRequiredReduced motion keeps fades only.
boolean
SegmentFooter5 props
summaryRequiredSummary on the left, such as "3 rules · matches all".
stringsaveStateRequiredSave button state: idle, loading, success or error.
"error" | "loading" | "idle" | "success"onSaveRequiredSaves the segment.
() => voidonResetRequiredPuts the starting segment back.
() => voidlabelsRequiredCopy.
SegmentBuilderLabels
SegmentHeader7 props
nameRequiredSegment name in the inline field.
stringonNameChangeRequiredCalled as the name is edited.
(name: string) => voidcountRequiredUsers matching the segment.
numbertotalRequiredAll users, for the share figure.
numberestimatingRequiredDims the figures and shows the estimating note.
booleanformatNumberRequiredFormats counts, such as `12,480`.
(value: number) => stringlabelsRequiredCopy.
SegmentBuilderLabels
SegmentPreview6 props
estimateRequiredCurrent estimate: daily trend and breakdown.
SegmentEstimatebreakdownSeriesRequiredSeries for the breakdown meter, with their colors.
ChartSeries[]trendColorClassNameText color class for the trend line and fill.
stringestimatingRequiredDims the charts while a new estimate is computed.
booleanformatNumberRequiredFormats counts in the legend.
(value: number) => stringlabelsRequiredCopy.
SegmentBuilderLabels
SegmentTrendChart4 props
valuesRequiredDaily values, oldest first. Keep the length steady so the shape morphs.
number[]colorClassNameText color class that paints the line and its fill, such as `text-sky-500`.
stringDefault
text-sky-500labelRequiredAccessible description of the chart.
stringclassNameClasses merged onto the root element.
string
useSegmentEstimate4 props
rootRequiredThe rules to estimate.
SegmentGroupestimateRequiredComputes the estimate for a set of rules.
(root: SegmentGroup, context: SegmentEstimateContext) => SegmentEstimatecontextRequiredProperties and breakdown ids passed through to `estimate`.
SegmentEstimateContextdelayWait after the last change before estimating, in ms.
numberDefault
380
useSegmentSave5 props
initialSavedRequiredSaved segments the chip row starts with.
SavedSegment[]initialActiveIdRequiredChip that starts active, if any.
string | nullonSavePersists a segment. A returned promise holds the loading state until it settles.
((segment: Segment) => void | Promise<void>)minLoadingShortest loading state, in ms, so a fast save still reads.
numberDefault
700successForHow long the saved state shows before returning to idle, in ms.
numberDefault
1600
useSegmentState4 props
initialRequiredSegment the builder starts with, and returns to on reset.
SegmentpropertiesRequiredProperty definitions, for new rules and value resets.
SegmentProperty[]operatorsRequiredOperator definitions, for new rules and value resets.
SegmentOperator[]onChangeCalled with the whole segment after every edit.
((segment: Segment) => void)
Types14 types
The object types these props take, field by field.
ChartSeries4 fields
idRequiredUnique key that matches the value keys in each datum.
stringlabelRequiredSeries name shown in the legend and tooltip.
stringcolorRequiredFill class for bars, segments, and dots, such as `bg-blue-500`.
stringstrokeStroke class for lines, such as `stroke-blue-500`.
string
SegmentOperator4 fields
idRequiredStable id stored on the rule, such as `is_not`.
stringlabelRequiredLabel shown in the operator menu, such as `is not`.
stringinputField this operator needs. Omit to use the property's own field.
SegmentValueInputunitUnit shown inside a number field, such as `days`.
string
SegmentProperty10 fields
idRequiredStable id stored on the rule, such as `country`.
stringlabelRequiredLabel shown in the property menu.
stringiconIcon shown before the label in the menu and the trigger.
ComponentType<{ className?: string }>inputRequiredThe property's own field: options for enums, text or a number.
SegmentValueInputoperatorsRequiredOperator ids offered for this property, in menu order.
string[]optionsOptions for `option` fields.
SegmentOption[]defaultValueValue a new rule starts with. Defaults to the first option, or empty.
stringplaceholderPlaceholder for text and number fields.
stringunitUnit shown inside a number field, such as `sessions`.
stringnarrowShare 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
idRequiredStable id, used as the key for the group box.
stringjoinRequiredHow this group's rules and child groups combine.
SegmentJoinrulesRequiredRules in display order.
SegmentRule[]groupsRequiredNested groups, shown after the rules. Only the top level nests.
SegmentGroup[]
Segment2 fields
nameRequiredSegment name shown in the header.
stringrootRequiredThe top-level group holding every rule.
SegmentGroup
SavedSegment2 fields
idRequiredStable id for the chip.
stringsegmentRequiredThe saved segment, loaded into the builder when its chip is clicked.
Segment
SegmentEstimate4 fields
countRequiredUsers matching the segment.
numbertotalRequiredAll users, for the share line.
numberdailyRequiredDaily active users of the segment, oldest first.
number[]breakdownRequiredMatching users split by the breakdown series.
SegmentBreakdownValue[]
SegmentBuilderLabels24 fields
nameRequiredAccessible name of the segment name field.
stringusersRequiredNoun after the audience count.
stringofAllRequiredCaption after the share figure.
stringestimatingRequiredShown while a new estimate is computed.
stringsavedRequiredHeading above the saved segment chips.
stringrulesRequiredAccessible name of the rules list.
stringemptyRequiredShown when a group has no rules.
stringandRequiredJoin chip text for AND.
stringorRequiredJoin chip text for OR.
stringtoggleJoinRequiredAccessible hint on the join chip.
stringaddRuleRequiredAdd rule button.
stringaddGroupRequiredAdd group button.
stringremoveRuleRequiredRemove rule tooltip and accessible name.
stringremoveGroupRequiredRemove group tooltip and accessible name.
stringpropertyRequiredAccessible name of the property menu.
stringoperatorRequiredAccessible name of the operator menu.
stringvalueRequiredAccessible name of the value field.
stringchooseRequiredPlaceholder for an empty value menu.
stringtrendRequiredTitle of the trend chart.
stringbreakdownRequiredTitle of the breakdown meter.
stringsaveRequiredSave button.
stringsavingRequiredSave button while saving.
stringsavedDoneRequiredSave button once saved.
stringresetRequiredReset button.
string
SegmentSummaryInfo3 fields
rulesRequiredRules in the segment, nested groups included.
numbergroupsRequiredNested groups.
numberjoinRequiredHow the top level combines.
SegmentJoin
SegmentEstimateContext2 fields
propertiesRequiredProperty definitions, for options and narrowing functions.
SegmentProperty[]breakdownIdsRequiredIds of the breakdown series, in display order.
string[]
SegmentOption3 fields
valueRequiredStored value, such as `us`.
stringlabelRequiredLabel shown in the menu, such as `United States`.
stringshareShare of all users with this value, 0 to 1. The sample model reads it.
number
SegmentRule4 fields
idRequiredStable id, used as the row key so rows enter, leave and glide.
stringpropertyRequiredProperty id.
stringoperatorRequiredOperator id.
stringvalueRequiredEntered or chosen value, always stored as a string.
string
SegmentBreakdownValue2 fields
idRequiredSeries id from `breakdownSeries`.
stringvalueRequiredUsers in this slice.
number
RuleGroupActions8 fields
addRuleRequiredAdds a rule to a group.
(groupId: string) => voidremoveRuleRequiredRemoves a rule from a group.
(groupId: string, ruleId: string) => voidsetPropertyRequiredChanges a rule's property.
(groupId: string, ruleId: string, propertyId: string) => voidsetOperatorRequiredChanges a rule's operator.
(groupId: string, ruleId: string, operatorId: string) => voidsetValueRequiredChanges a rule's value.
(groupId: string, ruleId: string, value: string) => voidtoggleJoinRequiredFlips a group between AND and OR.
(groupId: string) => voidaddGroupRequiredAdds a nested group to the top level.
() => voidremoveGroupRequiredRemoves a nested group.
(groupId: string) => void