Components
Presence

Presence

The Presence is a practical and versatile component that helps control the rendering and unmounting of your content based on a given state. It's designed to work seamlessly with animations, ensuring smooth transitions and full visibility of animations before any changes in presence are made.

No preview available for presence

Usage

To use the Presence component, import it from the package:

import { Presence } from '@ark-ui/react'

By default the child component starts out as hidden and remains hidden after the present state is toggled off. This is useful for situations where the element needs to be hidden initially and continue to stay hidden after its presence is no longer required.

import { useState } from 'react'
import { Presence } from './presence'

const Basic = () => {
  const [present, setPresent] = useState(false)
  return (
    <div>
      <button onClick={() => setPresent(!present)}>Toggle</button>
      <Presence present={present}>
        <div className="box">Hidden and Hidden</div>
      </Presence>
    </div>
  )
}

Lazy Mount

To delay the mounting of a child component until the present prop is set to true, use the lazyMount prop:

import { useState } from 'react'
import { Presence } from './presence'

const LazyMount = () => {
  const [present, setPresent] = useState(false)
  return (
    <div>
      <button onClick={() => setPresent(!present)}>Toggle</button>
      <Presence present={present} lazyMount>
        <div className="box">Unmounted and Hidden</div>
      </Presence>
    </div>
  )
}

Unmount on Exit

To remove the child component from the DOM when it’s not present, use the unmountOnExit prop:

import { useState } from 'react'
import { Presence } from './presence'

const UnmountOnExit = () => {
  const [present, setPresent] = useState(false)
  return (
    <div>
      <button onClick={() => setPresent(!present)}>Toggle</button>
      <Presence present={present} unmountOnExit>
        <div className="box">Hidden and Unmounted on Exit</div>
      </Presence>
    </div>
  )
}

Combining Lazy Mount and Unmount on Exit

Both lazyMount and unmountOnExit can be combined for a component to be mounted only when it’s present and to be unmounted when it’s no longer present:

import { useState } from 'react'
import { Presence } from './presence'

const LazyMountAndUnmountOnExit = () => {
  const [present, setPresent] = useState(false)
  return (
    <div>
      <button onClick={() => setPresent(!present)}>Toggle</button>
      <Presence present={present} lazyMount unmountOnExit>
        <div className="box">Lazy Mount and Unmounted on Exit</div>
      </Presence>
    </div>
  )
}

Conclusion

The Presence component provides an efficient way to control the rendering of components based on a specific condition. Its compatibility with animations offers a smooth and polished user experience. It is highly flexible and can be used to greatly improve the performance and user experience of your React application.

Previous

Popover

Next

Pressable