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 リファレンス
必須と記された props 以外はすべて省略できます。データ、文言、色はサンプルの内容が既定値です。
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.
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 個の props
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 個の 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".
stringonToggle必須Flips the group between AND and OR.
() => voidclassNameClasses merged onto the root element.
string
RuleGroup9 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の props
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 個の型
これらの props が受け取るオブジェクト型とそのフィールド。
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