Affix

Renders children inside portal at fixed position.

Import

import { Affix } from '@tidbcloud/uikit'

Usage

Affix renders a div element with a fixed position inside the Portal component. Use it to display elements fixed at any position on the screen, for example, scroll to top button:

import { IconArrowUp } from '@tabler/icons-react'
import { useWindowScroll } from '@mantine/hooks'
import { Affix, Button, Transition } from '@tidbcloud/uikit'
 
function Demo() {
  const [scroll, scrollTo] = useWindowScroll()
 
  return (
    <>
      <Affix position={{ bottom: 20, right: 20 }}>
        <Transition transition="slide-up" mounted={scroll.y > 0}>
          {(transitionStyles) => (
            <Button leftSection={<IconArrowUp size={16} />} style={transitionStyles} onClick={() => scrollTo({ y: 0 })}>
              Scroll to top
            </Button>
          )}
        </Transition>
      </Affix>
    </>
  )
}

Position

Use position prop to control the position of the Affix:

import { Affix, Button } from '@tidbcloud/uikit'
 
function Demo() {
  return (
    <Affix position={{ bottom: 20, left: 20 }}>
      <Button>Bottom left</Button>
    </Affix>
  )
}

Props

PropTypeDefaultDescription
childrenReact.ReactNode-Content to render inside Affix
portalPropsPortalProps-Props passed to the Portal component
position{ top?: number; right?: number; bottom?: number; left?: number }{ bottom: 0, right: 0 }Affix position on screen
withinPortalbooleantrueDetermines whether component should be rendered within Portal
zIndexnumber-Root element z-index property