suiss/ui ยท Interaction
Section Index
An A to Z index bar for jumping between sections of a long list by tapping, dragging, or typing.
Section Index is an open-source interaction from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
SectionIndex recreates the iOS list index. Sections marked with SectionIndexSection carry sticky SectionIndexHeader titles, and SectionIndexBar sits along the trailing edge. Tapping or dragging over the bar scrolls the nearest scrollable ancestor so the matching section is at the top; while dragging, a bubble shows the current letter. If a letter has no section, it jumps to the next one that exists.
When the container is too short to fit every letter, the bar collapses to fewer letters separated by dots, while still mapping the full alphabet across its height. Use it for long alphabetized lists such as contacts.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
-
Add the
cnhelper. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Compact
In a short list, the bar collapses its letters with dots and still jumps to any section.
Accessibility
- The bar is a vertical
sliderlabelledSection index, witharia-valuetextset to the current letter. - Arrow keys move one letter,
PageUpandPageDownmove five, andHomeandEndreach the first and last letters. - Typing a letter jumps directly to it.
- The visible letters and drag bubble are
aria-hidden.
API Reference
SectionIndex
The relative container that holds the list and the index bar.
Also accepts the props of React.ComponentProps<"div">.
Wrap the scrolling list and SectionIndexBar in SectionIndex, which positions the bar. The list can scroll in its own container, such as ScrollArea, or in the page. Each part sets a data-slot, such as section-index-bar, for styling.
SectionIndexSection
A section of the list identified by a letter.
| Prop | Type | Default |
|---|---|---|
value | string | Required |
Also accepts the props of React.ComponentProps<"section">.
Set value to a letter from the bar's letters so the bar can find the section. Sections expose data-value.
SectionIndexHeader
The sticky title at the top of a section.
Also accepts the props of React.ComponentProps<"h3">.
SectionIndexBar
The vertical letter bar that jumps between sections.
| Prop | Type | Default |
|---|---|---|
letters | string[] | LETTERS |
onValueChange | (value: string) => void | - |
Also accepts the props of React.ComponentProps<"div">.
letters defaults to A through Z followed by #. onValueChange is called with the letter whenever the bar jumps to a section. The bar sets data-dragging during a drag and extends its hit area toward the list so it is easier to grab.
Web preview
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
SectionIndex pairs a scroll view of lettered sections with SectionIndexBar, a column of letters on the end edge. Tapping a letter scrolls to that section; dragging along the bar scrubs through sections and shows the current letter in a large bubble beside the finger.
When the bar is too short for every letter, it shows a subset with โข between them. Use it for long alphabetical lists such as contacts.
Installation
The CLI also adds what it builds on: Text. It also adds the animated view components/ui/animated-view.tsx.
- Install the following dependencies:
-
Add the pieces it builds on: Text.
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/section-index.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Add the pieces it builds on: Text.
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/section-index.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Add the pieces it builds on: Text.
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/section-index.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
Examples
Compact
A short list where the bar collapses letters into dots.
Web preview
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
Accessibility
- The bar is
adjustable: swiping up or down with a screen reader moves to the previous or next section, and the current letter is announced as its value. SectionIndexHeaderhas theheadingrole.
API Reference
SectionIndex
The root that tracks section positions.
Also accepts the props of React.ComponentProps<typeof SectionIndexPrimitive.Root>.
Put a SectionIndexScrollView and a SectionIndexBar inside SectionIndex.
SectionIndexScrollView
The scroll view that holds the sections.
Also accepts the props of React.ComponentProps<typeof SectionIndexPrimitive.ScrollView>.
SectionIndexSection
One section, registered by its value.
Also accepts the props of React.ComponentProps<typeof SectionIndexPrimitive.Section>.
Wrap each group in a SectionIndexSection with its letter as value, and start it with a SectionIndexHeader.
SectionIndexHeader
A section heading; string children are styled.
Also accepts the props of React.ComponentProps<typeof View>.
SectionIndexBar
The letter bar with the drag bubble.
Also accepts the props of React.ComponentProps<typeof SectionIndexPrimitive.Bar>.
The bar shows A to Z and # by default; pass letters to match your sections. onValueChange is called with the letter when the user jumps. A selection haptic plays on each letter change from touch, if the app provides an adapter through HapticsProvider. The bubble animation respects the system reduce motion setting.
SectionIndex
The root that tracks section positions.
| Parameter | Type | Default |
|---|---|---|
sections | [String]? | nil |
content | () -> Content | - |
SectionIndexSection
One section, registered by its value.
| Parameter | Type | Default |
|---|---|---|
value | String | - |
content | () -> Content | - |
header | () -> Header | - |
_ย value | String | - |
Overloads: init(value:content:header:), init(_:content:).
SectionIndexHeader
A section heading; string children are styled.
| Parameter | Type | Default |
|---|---|---|
_ย title | LocalizedStringKey | - |
verbatim | String | - |
Overloads: init(_:), init(verbatim:).
SectionIndexBar
The letter bar with the drag bubble.
| Parameter | Type | Default |
|---|---|---|
letters | [String]? | nil |
onValueChange | ((String) -> Void)? | nil |
SectionIndexSectionScope.item
| Parameter | Type | Default |
|---|---|---|
key | Any? | null |
content | @Composable LazyItemScope.() -> Unit | - |
SectionIndexSectionScope.items
| Parameter | Type | Default |
|---|---|---|
count | Int | - |
key | ((Int) -> Any)? | null |
content | @Composable LazyItemScope.(index: Int) -> Unit | - |
SectionIndexScope.SectionIndexSection
| Parameter | Type | Default |
|---|---|---|
value | String | - |
header | String? | value |
content | SectionIndexSectionScope.() -> Unit | - |
SectionIndex
The root that tracks section positions.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
state | LazyListState | rememberLazyListState() |
letters | List<String> | SectionIndexLetters |
onValueChange | ((String) -> Unit)? | null |
contentPadding | PaddingValues | PaddingValues(0.dp) |
content | SectionIndexScope.() -> Unit | - |
SectionIndexHeader
A section heading; string children are styled.
| Parameter | Type | Default |
|---|---|---|
text | String | - |
modifier | Modifier | Modifier |
SectionIndexBar
The letter bar with the drag bubble.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
letters | List<String> | SectionIndexLetters |
onValueChange | ((String) -> Unit)? | null |
accessibilityLabel | String | "Section index" |