Editable
A component that allows users to edit text in place.
Anatomy
To set up the editable correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-part
attribute to help identify them in the DOM.
Examples
Learn how to use the Editable
component in your project. Let's take a look at the most basic
example:
import { Editable } from '@ark-ui/react'
export const Basic = () => (
<Editable.Root placeholder="Placeholder">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
</Editable.Root>
)
import { Editable } from '@ark-ui/solid'
export const Basic = () => (
<Editable.Root placeholder="Placeholder">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
</Editable.Root>
)
<script setup lang="ts">
import { Editable } from '@ark-ui/vue'
</script>
<template>
<Editable.Root placeholder="Placeholder">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
</Editable.Root>
</template>
Using custom controls
In some cases, you might need to use custom controls to toggle the edit and read mode. We use the render prop pattern to provide access to the internal state of the component.
import { Editable } from '@ark-ui/react'
export const CustomControls = () => (
<Editable.Root placeholder="enter a value" defaultValue="Chakra">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
<Editable.Context>
{(editable) => (
<Editable.Control>
{editable.editing ? (
<>
<Editable.SubmitTrigger>Save</Editable.SubmitTrigger>
<Editable.CancelTrigger>Cancel</Editable.CancelTrigger>
</>
) : (
<Editable.EditTrigger>Edit</Editable.EditTrigger>
)}
</Editable.Control>
)}
</Editable.Context>
</Editable.Root>
)
import { Show } from 'solid-js'
import { Editable } from '@ark-ui/solid'
export const CustomControls = () => (
<Editable.Root placeholder="enter a value" value="Chakra">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
<Editable.Context>
{(editable) => (
<Editable.Control>
<Show
when={editable().editing}
fallback={<Editable.EditTrigger>Edit</Editable.EditTrigger>}
>
<Editable.SubmitTrigger>Save</Editable.SubmitTrigger>
<Editable.CancelTrigger>Canvel</Editable.CancelTrigger>
</Show>
</Editable.Control>
)}
</Editable.Context>
</Editable.Root>
)
<script setup lang="ts">
import { ref } from 'vue'
import { Editable } from '@ark-ui/vue'
const value = ref('Chakra')
</script>
<template>
<Editable.Root placeholder="enter a value" v-model="value">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
<Editable.Context v-slot="{ editing }">
<Editable.Control v-if="editing">
<Editable.SubmitTrigger>Save</Editable.SubmitTrigger>
<Editable.CancelTrigger>Cancel</Editable.CancelTrigger>
</Editable.Control>
<Editable.Control v-else>
<Editable.EditTrigger>Edit</Editable.EditTrigger>
</Editable.Control>
</Editable.Context>
</Editable.Root>
</template>
Auto-resizing the editable
To auto-grow the editable as the content changes, set the autoResize
prop to true
.
<Editable.Root placeholder="Placeholder" autoResize>
{/*...*/}
</Editable.Root>
Setting a maxWidth
It is a common pattern to set a maximum of the editable as it auto-grows. To achieve this, set the
maxWidth
prop to the desired value.
<Editable.Root placeholder="Placeholder" autoResize maxWidth="320px">
{/*...*/}
</Editable.Root>
Editing with double click
The editable supports two modes of activating the "edit" state:
- when the preview part is focused (with pointer or keyboard).
- when the preview part is double-clicked.
To change the mode to double-click, pass the prop activationMode="dblclick"
.
<Editable.Root placeholder="Placeholder" activationMode="dblclick">
{/*...*/}
</Editable.Root>
Using the Field Component
The Field
component helps manage form-related state and accessibility attributes of an editable.
It includes handling ARIA labels, helper text, and error text to ensure proper accessibility.
import { Editable, Field } from '@ark-ui/react'
export const WithField = (props: Field.RootProps) => (
<Field.Root {...props}>
<Editable.Root placeholder="Placeholder" activationMode="dblclick">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
</Editable.Root>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
)
import { Editable, Field } from '@ark-ui/solid'
export const WithField = (props: Field.RootProps) => (
<Field.Root {...props} readOnly>
<Editable.Root placeholder="Placeholder" activationMode="dblclick">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
</Editable.Root>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
)
<script setup lang="ts">
import { Editable, Field } from '@ark-ui/vue'
</script>
<template>
<Field.Root>
<Editable.Root placeholder="Placeholder" activationMode="dblclick">
<Editable.Label>Label</Editable.Label>
<Editable.Area>
<Editable.Input />
<Editable.Preview />
</Editable.Area>
</Editable.Root>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
</template>
API Reference
Accessibility
Keyboard Support
Key | Description |
---|---|
Enter | Saves the edited content and exits edit mode. |
Escape | Discards the changes and exits edit mode. |