Presence
Helps control the rendering and unmounting of your content based on a given state.
Helps control the rendering and unmounting of your content based on a given state.
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 { Presence } from '@ark-ui/react'
import { useState } from 'react'
const Basic = () => {
const [present, setPresent] = useState(false)
return (
<>
<button onClick={() => setPresent(!present)}>Toggle</button>
<Presence present={present}>Hidden and Hidden</Presence>
</>
)
}
import { Presence } from '@ark-ui/solid'
import { createSignal } from 'solid-js'
const Basic = () => {
const [present, setPresent] = createSignal(false)
return (
<>
<button onClick={() => setPresent(!present())}>Toggle</button>
<Presence present={present()}>Hidden and Hidden</Presence>
</>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Presence } from '@ark-ui/vue'
const isPresent = ref(false)
</script>
<template>
<div>
<button @click="isPresent = !isPresent">Toggle</button>
<Presence :present="isPresent">Hidden and Hidden </Presence>
</div>
</template>
To delay the mounting of a child component until the present
prop is set to
true, use the lazyMount
prop:
import { Presence } from '@ark-ui/react'
import { useState } from 'react'
const LazyMount = () => {
const [present, setPresent] = useState(false)
return (
<>
<button onClick={() => setPresent(!present)}>Toggle</button>
<Presence present={present} lazyMount>
Unmounted and Hidden
</Presence>
</>
)
}
import { Presence } from '@ark-ui/solid'
import { createSignal } from 'solid-js'
const LazyMount = () => {
const [present, setPresent] = createSignal(false)
return (
<>
<button onClick={() => setPresent(!present())}>Toggle</button>
<Presence present={present()} lazyMount>
Unmounted and Hidden
</Presence>
</>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Presence } from '@ark-ui/vue'
const isPresent = ref(false)
</script>
<template>
<div>
<button @click="isPresent = !isPresent">Toggle</button>
<Presence :present="isPresent" lazyMount>Hidden and Hidden </Presence>
</div>
</template>
To remove the child component from the DOM when it’s not present, use the
unmountOnExit
prop:
import { Presence } from '@ark-ui/react'
import { useState } from 'react'
const UnmountOnExit = () => {
const [present, setPresent] = useState(false)
return (
<>
<button onClick={() => setPresent(!present)}>Toggle</button>
<Presence present={present} unmountOnExit>
Hidden and Unmounted on Exit
</Presence>
</>
)
}
import { Presence } from '@ark-ui/solid'
import { createSignal } from 'solid-js'
const UnmountOnExit = () => {
const [present, setPresent] = createSignal(false)
return (
<>
<button onClick={() => setPresent(!present())}>Toggle</button>
<Presence present={present()} unmountOnExit>
Hidden and Unmounted on Exit
</Presence>
</>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Presence } from '@ark-ui/vue'
const isPresent = ref(false)
</script>
<template>
<div>
<button @click="isPresent = !isPresent">Toggle</button>
<Presence :present="isPresent" unmountOnExit>Hidden and Unmounted on Exit </Presence>
</div>
</template>
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 { Presence } from '@ark-ui/react'
import { useState } from 'react'
const LazyMountAndUnmountOnExit = () => {
const [present, setPresent] = useState(false)
return (
<>
<button onClick={() => setPresent(!present)}>Toggle</button>
<Presence present={present} lazyMount unmountOnExit>
Lazy Mount and Unmounted on Exit
</Presence>
</>
)
}
import { Presence } from '@ark-ui/solid'
import { createSignal } from 'solid-js'
const LazyMountAndUnmountOnExit = () => {
const [present, setPresent] = createSignal(false)
return (
<>
<button onClick={() => setPresent(!present())}>Toggle</button>
<Presence present={present()} lazyMount unmountOnExit>
Lazy Mount and Unmounted on Exit
</Presence>
</>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Presence } from '@ark-ui/vue'
const isPresent = ref(false)
</script>
<template>
<div>
<button @click="isPresent = !isPresent">Toggle</button>
<Presence :present="isPresent" lazyMount unmountOnExit>
Lazy Mount and Unmounted on Exit
</Presence>
</div>
</template>
asChild Render as a different element type. | boolean | |
lazyMount Whether to enable lazy mounting | boolean | false |
onExitComplete Function called when the animation ends in the closed state. | () => void | |
present Whether the node is present (controlled by the user) | boolean | |
unmountOnExit Whether to unmount on exit. | boolean | false |
lazyMount Whether to enable lazy mounting | boolean | false |
onExitComplete Function called when the animation ends in the closed state. | () => void | |
present Whether the node is present (controlled by the user) | boolean | |
unmountOnExit Whether to unmount on exit. | boolean | false |
Previous
PopoverNext
Radio Group