Command palette

@yasinsensoy/native

React Native · Components

TableOfContents

In-page table of contents (same behavior as web): links to sections and highlights the section being read.

8 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

Introduction

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Installation

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

With npm

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

With pnpm

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Usage

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Theme

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

FAQ

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Code

import { TableOfContents } from "@yasinsensoy/native";

<TableOfContents container="#post" className="sticky top-20 w-56" updateHash={false} />

Events

  1. Events appear here when you interact with the preview.

Import

import { TableOfContents, useScrollSpy } from "@yasinsensoy/native";

In-page table of contents (same behavior as web): links to sections and highlights the section being read.

  • Active section: found while scrolling via useScrollSpy; in the line appearance the marker slides smoothly. At the end the last section becomes active.
  • Navigation: tapping smoothly scrolls to the section (instantly with reduced motion) and announces the section's name when a screen reader is on.
  • Collapsed: with collapsed the list collapses into an "On this page · Installation" row; tapping opens it, selecting closes it.
TableOfContents({ ref, items, spy, active: activeProp, onSelect, title, variant: variantProp, effect, collapsed, style, styles: parts, testID, }: TableOfContentsProps): JSX.Element | null

Examples

const spy = useScrollSpy({ offset: 56 });
const items = [
  { id: "intro", label: "Introduction" },
  { id: "install", label: "Installation" },
  { id: "npm", label: "With npm", level: 3 },
];
<TableOfContents spy={spy} items={items} collapsed />
<ScrollView {...spy.scrollProps}>
  <View {...spy.anchor("intro")}><Text variant="h2">Introduction</Text></View>
  …
</ScrollView>

useScrollSpy

function

Tracks which section is being read in a scroll view (same rule as the web useScrollSpy): the last section whose heading is above the reading line (offset from the top + threshold of the visible area) is active; when scrolled to the end the last section is active. TableOfContents uses this; it can also be used for your own navigation.

Section positions are measured only when the layout changes; scrolling only compares numbers, so it stays light even with hundreds of sections.

useScrollSpy(options?: ScrollSpyOptions): ScrollSpy

Examples

const spy = useScrollSpy({ offset: 56 });
<ScrollView {...spy.scrollProps}>
  <View {...spy.anchor("install")}><Text variant="h2">Installation</Text></View>
</ScrollView>
<TableOfContents spy={spy} items={[{ id: "install", label: "Installation" }]} />

TocItem

interface

A heading inside TableOfContents (same as web).

NameTypeDefaultDescription
idstringSection id; the name given with ScrollSpy.anchor.
labelReactNodeText shown in the list.
level?number2Level: 2, 3… The lowest level has no indent.

Styles of the TableOfContents inner parts.

NameTypeDescription
title?StyleProp<TextStyle>Title ("Bu sayfada", "On this page").
list?StyleProp<ViewStyle>List.
link?StyleProp<ViewStyle>Each link.

Props of the TableOfContents component.

NameTypeDefaultDescription
ref?Ref<ViewRef>Reference to the root view.
itemsreadonly TocItem[]Headings.
spy?ScrollSpyScroll spy (useScrollSpy). When given, the active section is read from it and tapping scrolls to the section.
active?stringId of the active section (manually, instead of spy).
onSelect?(id: string) => voidCalled when a heading is tapped (after scrolling when spy is given).
title?ReactNode"Bu sayfada"` (`messages.tableOfContents`)List title. null removes the title.
variant?"line" | "plain" | (string & {})"line"Appearance: line (a thin line on the left with a sliding active marker), plain (text color only) or a custom appearance added via defineAppearance. Defaults to UIProvider appearance.defaults.tableOfContents.variant.
effect?EffectPropEffect of the links; also plays on the active heading. Defaults to appearance.defaults.tableOfContents.effect.
collapsed?booleanfalseShould the list collapse into a dropdown row showing the current section (for tight space above the content on phones)?
style?StyleProp<ViewStyle>Root style.
styles?TableOfContentsStylesStyles of the inner parts.
testID?stringTest id.

useScrollSpy options (same names as web).

NameTypeDefaultDescription
offset?number0This many pixels below the top of the visible area count as the "reading line". If a sticky header covers the top of the scroll view, give its height; the same gap is left when navigating to a heading.
threshold?number0.25How far below the reading line a section may be to count as active (a fraction of the visible area height).
enabled?booleantrueTracking stops when false.
onActiveChange?(id: string | undefined) => voidCalled with the id when the active section changes.

Props to give a section heading (ScrollSpy.anchor).

NameTypeDescription
ref(node: unknown) => voidRef of the anchor view.
onLayout(event: LayoutChangeEvent) => voidMeasures the anchor's position.
collapsablefalseKeeps the view in the native tree so it can be measured.

ScrollSpy

interface

Result of useScrollSpy.

NameTypeDescription
activestring | undefinedId of the section being read.
scrollProps{ ref: RefObject<ScrollRef | null>; onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void; onLayout: (event: LayoutChangeEvent) => void; onContentSizeChange: (width: number, height: number) => void; /** The reader's own drag: hands the active section back to the spy after a `scrollTo`. */ onScrollBeginDrag: () => void; scrollEventThrottle: number; /** Android: scrolls even inside another scroll view. */ nestedScrollEnabled: boolean; }Props to spread on the scroll view: <ScrollView {...spy.scrollProps}>. If you have your own onScroll or onScrollBeginDrag, call the spy's one from it too (spy.scrollProps.onScroll(event)).
anchor(id: string) => ScrollSpyAnchorPropsRegisters a section heading by id: <View {...spy.anchor("install")}>. The heading can be at any depth of the scroll content.
scrollTo(id: string, options?: { animated?: boolean; }) => voidScrolls to the section (with an offset gap). The section stays active until the reader drags the scroll view, and the scroll follows it if the content above it changes height on the way.