Ant Design Svelte

Segmented

One selected option to switch period or view.

When to use

Segmented selects one value for an already existing area: report period, list view, or map layer. All options are visible at once, and toggling changes the view or options of that area. For individual content panels use Tabs, for regular form selection use Radio.

Limit the set to understandable options that can be conveniently compared side by side. The caption explains the result of the switch. Application example changes the report total when selecting a period.

Select value

The options property accepts strings, numbers, or objects with separate value and label. The model retains the value type: the number 0 is different from the string '0'. The label and icon do not change the type of the selected value.

Problem Setting Example
Link local selection bind:value, options Basic selection
Manage choice externally value, onChange Guided Choice
Save number in model Numeric value ​​ Group name and numeric value
Update Option Set New options and valid value Dynamic options

The defaultValue property specifies the initial local value; without it, the first option options is used. Subsequent changes to defaultValue do not reinitialize the control. For changes and resets, update value or the state associated via bind:value.

The onChange handler reports the new value upon user selection. Selecting the current option again does not change it, and an external assignment to value is not considered a user event. Guided Example separately counts the selection and returns the state of the external button.

Changing options does not automatically select a replacement. When deleting the current option, the application must simultaneously assign a valid value. disabled disables the entire group or individual variant without turning it into a different model value.

Signatures and location

The label can be a string, a number, or a Svelte snippet; icon accepts snippet. Your signatures combines the Avatar and name, while icon with text keeps the regular caption next to the SVG.

For the option with only an icon, set aria-label; tooltip or title provide additional explanation. Don't rely on the drawing as the only name available. Example of icons shows captions and individual Tooltip.

The block property stretches the group to the available width. orientation specifies horizontal or vertical orientation and takes precedence over vertical. shape=round changes shape; size selects small/medium/large, without a local value the overall size of the components is valid. Size examples and full width show these settings separately.

classNames and CSS styles strings for root, item, label and icon are available for decoration - an object or a function from props. Semantic slots show the current settings. The theme.components.Segmented component design tokens are applied to the skin; token section describes the available options.

A non-empty local prefixCls specifies the full CSS prefix of the root, variants, and indicator. The empty string and undefined inherit ConfigProvider.prefixCls with the -segmented suffix, defaulting to ant-segmented. CSS variables and the common marker preserve names. Example ConfigProvider toggles global and local prefix while maintaining radio DOM, selection and focus. Changing classes does not itself trigger onChange.

Keyboard and form

Options use native radios with a common name. Without an explicit name, Segmented creates its own. Tab moves focus in and out of the group; the arrows change the selection, skipping unavailable options and moving through the end of the list. Give the group an accessible name, as in the basic example.

Indicator movement does not create a separate selection event; with prefers-reduced-motion it is disabled. The application works with value and onChange, not the end of the CSS animation. To access the root after mount, bind:nativeElement is provided; There is no separate API instance.

In the current package, the native input does not receive a value variant, so FormData returns the string on rather than the selected string or number. name concatenates radio, but does not provide the required serialization. Priority example sends a number from the Svelte state, preserving its original type.

Examples

Week

Basic selection

Strings in options and bind:value. The value next to it reflects the selected period.

Vertical direction

orientation="vertical" arranges the options from top to bottom. Compatible vertical is also supported.

Full width

block stretches the group and distributes the available width between options.

Rounded shape

shape="round" changes the shape of the group and the selected option.

Unavailable options

Blocking the entire group and a specific month. The keyboard skips an unavailable option.

list; changes: 0

Guided selection

value/onChange and external action. The counter changes only when the user selects a new value.

Your labels

Svelte snippets replace ReactNode: Avatar and name form one available variant label.

Day

Dynamic options

Adding a quarter and year maintains the current selection.

large

medium

small

Three sizes

large, medium and small. Without size, the size from the general settings is used.

Icon and label

icon accepts Svelte snippet with SVG; label remains a regular string.

Icons only

Options have aria-label and separate Tooltip. The available name is saved without visible text.

Not sent

Group name and numeric value

name combines native radio. The form submits a number from the Svelte state; automatic radio serialization is not used.

Semantic slots

The classNames object and the styles function style the real root, item, label and icon. The styles values ​​are CSS strings.

Completed tasks: 58

Report period

Applied example from the review saved: choosing a period changes the summary of the report; week contains Tooltip.

API

All 13 Segmented live page example themes on ant.design are mapped to the cards above. An additional card saves the report from the review. The table describes the installed SegmentedProps; full types belong to the package.

Basic parameters Segmented
PropertyDescriptionTypeDefault
prefixClsFull local CSS prefix for root, variants and indicator. The empty string inherits the common prefix; without configuration, ant-segmented is used. Changing classes saves the radio and the selected value.stringConfigProvider + -segmented
optionsStrings, numbers or objects with value, label, icon, disabled, tooltip, class, title and aria-label.readonly (Value | SegmentedOption<Value>)[]Mandatory
valueCurrent value. Supports bind:value; the number type is preserved.Value extends string | numberundefined
defaultValueInitial local value, read once. Changing defaultValue later does not reset the selection.ValueFirst option
onChangeCalled when another available option is selected. An external assignment to value does not trigger a callback.(value: Value) => void—
disabledBlocks the entire group. A separate option is disabled via options[].disabled.booleanfalse
blockStretches the group and options to the width of the parent.booleanfalse
sizelarge - 40 px, medium - 32 px, small - 24 px at standard density.large | medium | smallFrom ConfigProvider
orientationLayout direction. Takes precedence over compatible vertical.horizontal | verticalhorizontal
verticalCompatible name for vertical mode.booleanfalse
shapeRegular or rounded shape.default | rounddefault
nameCommon name for native radio. Does not replace the available group name.stringGenerated name
classNamesClasses root, item, label and icon. The class of an individual option is specified via option.class, not className.SegmentedSemanticSlots | (info) => SegmentedSemanticSlots—
stylesCSS lines for the same slots. The function receives info.props.SegmentedSemanticSlots | (info) => SegmentedSemanticSlots—
nativeElementbind:nativeElement returns the root div after mount; The component does not export individual focus()/blur().HTMLDivElement—

Unlike React, label and icon accept Svelte snippets, styles accept CSS strings, and the variant class is called class. There is no separate ConfigProvider.segmented in the current package. The general componentSize and direction apply.

Semantic DOM

Group - a div with the role radiogroup, aria-orientation and an available name. Each option is a label with input type="radio". For a group, use aria-label or aria-labelledby; For icons without text, set options[].aria-label.

Available semantic slots
SlotElement
rootRoot div of the group.
itemlabel of each option.
labelLabel container inside label.
iconspan icons with aria-hidden.

Arrows on the radio switch between available options, moving through the end of the list. The native input receives focus. For programmatic focus bind:nativeElement; There is no separate API instance. Indicator movement is disabled with prefers-reduced-motion.

Registration tokens

Eight component tokens checked against ant.design and ComponentTokenMap['Segmented']. theme.components.Segmented applies component tokens to styles. Specify local design via styles, as in example semantic slots.

Segmented Component Tokens
TokenPurpose
itemActiveBgBackground of the clicked option.
itemColorOption text color.
itemHoverBgBackground when hovering.
itemHoverColorText color when hovering.
itemSelectedBgBackground of the selected option.
itemSelectedColorThe color of the selected text.
trackBgGroup background.
trackPaddingGroup indent.

Common tokens of the current topic

The list is checked against the Segmented table on ant.design. The values ​​are read reactively via theme.useToken().

General Segmented Tokens
TokenCurrent value
colorPrimaryBorder#adc6ff
colorTextrgba(0,0,0,0.88)
colorTextDisabledrgba(0,0,0,0.25)
borderRadius6
borderRadiusLG8
borderRadiusSM4
borderRadiusXS2
boxShadowTertiary 0 1px 2px 0 rgba(0,0,0,0.05), 0 1px 6px -1px rgba(0,0,0,0.03), 0 2px 4px 0 rgba(0,0,0,0.03)
controlHeight32
controlHeightLG40
controlHeightSM24
controlPaddingHorizontal12
controlPaddingHorizontalSM8
fontFamily-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'
fontSize14
fontSizeLG16
lineHeight1.5714285714285714
lineWidth1
lineWidthFocus3
marginSM12
motionDurationMid0.2s
motionDurationSlow0.3s
motionEaseInOutcubic-bezier(0.645, 0.045, 0.355, 1)
paddingXXS4

FAQ

How is Segmented different from Tabs?

Segmented selects a value for an existing area: period, sort, or view. Tabs organizes individual content panels and their navigation.

How to get a number instead of a string?

Pass numeric value and typed state as in priority example. label does not define the type of the value.

Is name enough to submit an HTML form?

No, in the current package the native input does not receive the value option: FormData will return the standard on. Read the selection from bind:value or onChange, as in the example form. name is needed for radio grouping.

What happens when you delete the selected option?

Dynamic options do not automatically select replacements. The application must assign a valid value when deleting the current variant. Example of dynamic options shows adding without resetting selection.