Skip to content

ContentProvider

The ContentProvider component wraps your content and automatically extracts headings for navigation. It provides context for other components like TableOfContents.

Usage

jsx
import { ContentProvider, TableOfContents } from '@sarshay/react-utils'

function App() {
  return (
    <ContentProvider>
      <div style={{ display: 'flex' }}>
        <aside>
          <TableOfContents />
        </aside>
        <main>
          <h1>My Article</h1>
          <p>Introduction paragraph</p>
          
          <h2>Section 1</h2>
          <p>Section content</p>
          
          <h3>Subsection</h3>
          <p>Subsection content</p>
        </main>
      </div>
    </ContentProvider>
  )
}

Props

PropTypeDefaultDescription
childrenReactNode-The content to wrap
classNamestringundefinedAdditional CSS classes
styleCSSPropertiesundefinedInline styles

Important Notes

  • TableOfContents must be inside ContentProvider - The provider creates the context that TableOfContents needs
  • Automatic heading extraction - Scans for h1-h6 elements automatically
  • SSR compatible - Works with server-side rendering

Examples

Basic Layout

jsx
<ContentProvider>
  <div style={{ display: 'flex' }}>
    <aside style={{ width: '200px' }}>
      <TableOfContents />
    </aside>
    <main style={{ flex: 1 }}>
      <h1>Getting Started</h1>
      <p>Welcome to our documentation</p>
      
      <h2>Installation</h2>
      <p>Install the package</p>
    </main>
  </div>
</ContentProvider>

Released under the MIT License.