Skip to content

Affix

Make an element stick to the viewport when scrolling. Useful for navigation, toolbars, or action buttons.

import { Affix } from 'asterui'

Basic Affix

Element becomes fixed when scrolled past its position.

import { Affix, Button } from 'asterui'

function App() {
  return (
      <Affix offsetTop={80}>
        <Button color="primary">Affixed Button</Button>
      </Affix>
    )
}

export default App

With Callback

Get notified when affix state changes.

import { Affix, Button } from 'asterui'
import { useState } from 'react'

function App() {
  const [affixed, setAffixed] = useState(false)
  
  return (
      <Affix offsetTop={80} onChange={setAffixed}>
        <Button type={affixed ? 'primary' : 'neutral'}>
          {affixed ? 'Affixed!' : 'Not Affixed'}
        </Button>
      </Affix>
    )
}

export default App

Affix to Bottom

Fix element to bottom of viewport.

import { Affix, Button } from 'asterui'

function App() {
  return (
      <Affix offsetBottom={20}>
        <Button color="secondary">Bottom Affixed</Button>
      </Affix>
    )
}

export default App

Custom Target

Affix within a scrollable container.

Sticky Header
import { Affix } from 'asterui'

function App() {
  return (
      <div id="scroll-container" className="h-64 overflow-auto">
        <Affix offsetTop={0} target={() => document.getElementById('scroll-container')!}>
          <div className="bg-primary text-primary-content p-2">
            Sticky Header
          </div>
        </Affix>
        {/* Scrollable content */}
      </div>
    )
}

export default App
PropertyDescriptionTypeDefault
childrenContent to make stickyReact.ReactNode-
offsetTopOffset from top when fixed (pixels)number-
offsetBottomOffset from bottom when fixed (pixels)number-
targetScroll target element() => HTMLElement | Window-
onChangeCallback when affix state changes(affixed: boolean) => void-
classNameAdditional CSS classesstring-
  • Uses CSS position: sticky for better performance
  • Does not trap keyboard focus when affixed
  • Content remains in document flow when not affixed