Ark Logo

Presence

Helps control the rendering and unmounting of your content based on a given state.

Examples

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 { createSignal } from 'solid-js'
import { Presence } from '@ark-ui/solid'

export const Basic = () => {
  const [present, setPresent] = createSignal(false)
  return (
    <>
      <button type="button" onClick={() => setPresent(!present())}>
        Toggle
      </button>
      <Presence present={present()}>Hidden and Hidden</Presence>
    </>
  )
}

Lazy Mount

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

import { createSignal } from 'solid-js'
import { Presence } from '@ark-ui/solid'

export const LazyMount = () => {
  const [present, setPresent] = createSignal(false)
  return (
    <>
      <button type="button" onClick={() => setPresent(!present())}>
        Toggle
      </button>
      <Presence present={present()} lazyMount>
        Unmounted and Hidden
      </Presence>
    </>
  )
}

Unmount on Exit

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

import { createSignal } from 'solid-js'
import { Presence } from '@ark-ui/solid'

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

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 { createSignal } from 'solid-js'
import { Presence } from '@ark-ui/solid'

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

API Reference

Presence

PropDefaultType
asChild
(props: ParentProps<'div'>) => Element

lazyMountfalse
boolean

Whether to enable lazy mounting

onExitComplete
() => void

Function called when the animation ends in the closed state.

present
boolean

Whether the node is present (controlled by the user)

unmountOnExitfalse
boolean

Whether to unmount on exit.