Inject Context
Utilize injectContext
to enhances component composition in Reka UI, enabling powerful and flexible UI development.
Reka UI exposes internal injectContext
to further extend the ability to compose and construct complex components. However, this API was primarily designed to be internal use. Thus the API might change without notice.
Introduction to injectContext
In Reka UI, all root component, and some other component exports an injectContext
function, which is a key feature for managing component state and enabling seamless composition. This guide will show you how to craft your own child component based on the provided context.
What is injectContext
?
injectContext
is a function provided by each Reka UI component that allows you to access the internal state and methods of that component.
It leverages Vue's Provide / Inject to provide a powerful way of extending and customizing component behavior.
Basic Usage
Here's a simple example of how to use injectContext
with a Reka UI Accordion component:
<!-- CustomAccordionContent.vue -->
<script setup>
import { injectAccordionItemContext, injectAccordionRootContext } from 'reka-ui'
const accordionRootContext = injectAccordionRootContext()
const accordionItemContext = injectAccordionItemContext()
const isSingleOpen = computed(() =>
accordionRootContext.isSingle.value && accordionItemContext.open.value
)
</script>
<template>
<div>
…
</div>
</template>
Common Use Cases
- Custom Styling: Access internal state to apply dynamic styles based on component state.
- Extended Functionality: Build upon existing component logic to add new features.
- Complex Layouts: Create intricate UI patterns by composing multiple components and sharing state between them.
- Accessibility Enhancements: Utilize internal methods and state to improve keyboard navigation or screen reader support.
Best Practices
- Use
injectContext
in child components or composables, not in the component itself. - Always check if the injected context exists before using it, as it may be
undefined
if used outside the component's scope. - Prefer using provided props and events when possible, and use
injectContext
for more advanced scenarios. - When using TypeScript, leverage the type information provided by
injectContext
for better code quality.