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,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>
);
}
安装
API 参考
除标注为必填的属性外,其余均为可选。数据、文案和颜色默认使用示例内容。
SegmentBuilder15 个属性
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.
number默认值
380breakdownSeriesSeries for the breakdown meter, with their colors.
ChartSeries[]trendColorClassNameText color class for the trend line and fill.
string默认值
text-sky-500allowGroupsShows the Add group button for nested groups.
boolean默认值
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 个属性
count必填Current audience count. A change shows the difference for a moment.
numberformat必填Formats the difference, without its sign.
(value: number) => stringdurationHow 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".
stringonToggle必填Flips the group between AND and OR.
() => voidclassNameClasses merged onto the root element.
string
RuleGroup9 个属性
group必填The group to render.
SegmentGroupdepthNested groups (depth 1) cannot add groups of their own.
number默认值
0properties必填Every property a rule can use.
SegmentProperty[]operators必填Every operator.
SegmentOperator[]actions必填Edit callbacks.
RuleGroupActionscascade必填Rows mounting now cascade in, as after a load or reset.
booleanallowGroups必填Shows the Add group button at the top level.
booleanreduce必填Reduced motion keeps fades only.
booleanlabels必填Copy.
SegmentBuilderLabels
RuleRow8 个属性
rule必填The rule shown in this row.
SegmentRuleproperties必填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) => voidonOperatorChange必填Called with a new operator id.
(operatorId: string) => voidonValueChange必填Called with a new value.
(value: string) => voidonRemove必填Removes the rule.
() => voidlabels必填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.
stringonChange必填Called with the new value as the user picks or types.
(value: string) => voidoptionsOptions for the menu.
SegmentOption[]默认值
[]placeholderPlaceholder for text and number fields.
stringunitUnit shown inside a number field, such as `days`.
stringlabel必填Accessible name of the field.
stringchoose必填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 | nullonLoad必填Loads a saved segment into the builder.
(saved: SavedSegment) => voidlabel必填Accessible name of the row.
stringreduce必填Reduced motion keeps fades only.
boolean
SegmentFooter5 个属性
summary必填Summary on the left, such as "3 rules · matches all".
stringsaveState必填Save button state: idle, loading, success or error.
"error" | "loading" | "idle" | "success"onSave必填Saves the segment.
() => voidonReset必填Puts the starting segment back.
() => voidlabels必填Copy.
SegmentBuilderLabels
SegmentHeader7 个属性
name必填Segment name in the inline field.
stringonNameChange必填Called as the name is edited.
(name: string) => voidcount必填Users matching the segment.
numbertotal必填All users, for the share figure.
numberestimating必填Dims the figures and shows the estimating note.
booleanformatNumber必填Formats counts, such as `12,480`.
(value: number) => stringlabels必填Copy.
SegmentBuilderLabels
SegmentPreview6 个属性
estimate必填Current estimate: daily trend and breakdown.
SegmentEstimatebreakdownSeries必填Series for the breakdown meter, with their colors.
ChartSeries[]trendColorClassNameText color class for the trend line and fill.
stringestimating必填Dims the charts while a new estimate is computed.
booleanformatNumber必填Formats counts in the legend.
(value: number) => stringlabels必填Copy.
SegmentBuilderLabels
SegmentTrendChart4 个属性
values必填Daily 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`.
string默认值
text-sky-500label必填Accessible description of the chart.
stringclassNameClasses merged onto the root element.
string
useSegmentEstimate4 个属性
root必填The rules to estimate.
SegmentGroupestimate必填Computes the estimate for a set of rules.
(root: SegmentGroup, context: SegmentEstimateContext) => SegmentEstimatecontext必填Properties and breakdown ids passed through to `estimate`.
SegmentEstimateContextdelayWait 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 | 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.
number默认值
700successForHow 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.
Segmentproperties必填Property definitions, for new rules and value resets.
SegmentProperty[]operators必填Operator definitions, for new rules and value resets.
SegmentOperator[]onChangeCalled with the whole segment after every edit.
((segment: Segment) => void)
类型14 个类型
这些属性使用的对象类型及其字段。
ChartSeries4 个字段
id必填Unique key that matches the value keys in each datum.
stringlabel必填Series name shown in the legend and tooltip.
stringcolor必填Fill class for bars, segments, and dots, such as `bg-blue-500`.
stringstrokeStroke class for lines, such as `stroke-blue-500`.
string
SegmentOperator4 个字段
id必填Stable id stored on the rule, such as `is_not`.
stringlabel必填Label 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 个字段
id必填Stable id stored on the rule, such as `country`.
stringlabel必填Label shown in the property menu.
stringiconIcon 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.
SegmentValueInputoperators必填Operator 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 个字段
id必填Stable id, used as the key for the group box.
stringjoin必填How this group's rules and child groups combine.
SegmentJoinrules必填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.
stringroot必填The top-level group holding every rule.
SegmentGroup
SavedSegment2 个字段
id必填Stable id for the chip.
stringsegment必填The saved segment, loaded into the builder when its chip is clicked.
Segment
SegmentEstimate4 个字段
count必填Users matching the segment.
numbertotal必填All users, for the share line.
numberdaily必填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.
stringusers必填Noun after the audience count.
stringofAll必填Caption after the share figure.
stringestimating必填Shown while a new estimate is computed.
stringsaved必填Heading above the saved segment chips.
stringrules必填Accessible name of the rules list.
stringempty必填Shown when a group has no rules.
stringand必填Join chip text for AND.
stringor必填Join chip text for OR.
stringtoggleJoin必填Accessible hint on the join chip.
stringaddRule必填Add rule button.
stringaddGroup必填Add group button.
stringremoveRule必填Remove rule tooltip and accessible name.
stringremoveGroup必填Remove group tooltip and accessible name.
stringproperty必填Accessible name of the property menu.
stringoperator必填Accessible name of the operator menu.
stringvalue必填Accessible name of the value field.
stringchoose必填Placeholder for an empty value menu.
stringtrend必填Title of the trend chart.
stringbreakdown必填Title of the breakdown meter.
stringsave必填Save button.
stringsaving必填Save button while saving.
stringsavedDone必填Save button once saved.
stringreset必填Reset button.
string
SegmentSummaryInfo3 个字段
rules必填Rules in the segment, nested groups included.
numbergroups必填Nested groups.
numberjoin必填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`.
stringlabel必填Label 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 个字段
id必填Stable id, used as the row key so rows enter, leave and glide.
stringproperty必填Property id.
stringoperator必填Operator id.
stringvalue必填Entered or chosen value, always stored as a string.
string
SegmentBreakdownValue2 个字段
id必填Series id from `breakdownSeries`.
stringvalue必填Users in this slice.
number
RuleGroupActions8 个字段
addRule必填Adds a rule to a group.
(groupId: string) => voidremoveRule必填Removes a rule from a group.
(groupId: string, ruleId: string) => voidsetProperty必填Changes a rule's property.
(groupId: string, ruleId: string, propertyId: string) => voidsetOperator必填Changes a rule's operator.
(groupId: string, ruleId: string, operatorId: string) => voidsetValue必填Changes a rule's value.
(groupId: string, ruleId: string, value: string) => voidtoggleJoin必填Flips a group between AND and OR.
(groupId: string) => voidaddGroup必填Adds a nested group to the top level.
() => voidremoveGroup必填Removes a nested group.
(groupId: string) => void