<script setup lang="ts">
import { AEditableArea, AEditableCancelTrigger, AEditableEditTrigger, AEditableInput, AEditablePreview, AEditableRoot, AEditableSubmitTrigger } from 'akar';
</script>
<template>
<div class="w-[250px]">
<AEditableRoot
v-slot="{ isEditing }"
default-value="Click to edit 'Akar'"
placeholder="Enter text..."
class="flex flex-col gap-4"
auto-resize
>
<AEditableArea class="color-text w-[250px]">
<AEditablePreview />
<AEditableInput class="w-full placeholder:color-text-muted" />
</AEditableArea>
<AEditableEditTrigger
v-if="!isEditing"
class="text-sm color-primary leading-[35px] font-medium px-[15px] outline-none border border-border rounded-lg bg-white inline-flex h-[35px] w-max shadow-sm items-center justify-center hover:bg-stone-50 focus:shadow-[0_0_0_2px] focus:shadow-black"
/>
<div
v-else
class="flex gap-2"
>
<AEditableSubmitTrigger
class="text-sm color-primary leading-[35px] font-medium px-[15px] outline-none border border-border rounded-lg bg-white inline-flex h-[35px] shadow-sm items-center justify-center hover:bg-stone-50 focus:shadow-[0_0_0_2px] focus:shadow-black"
/>
<AEditableCancelTrigger
class="text-sm text-white leading-[35px] font-medium px-[15px] outline-none border border-border rounded-lg bg-error inline-flex h-[35px] shadow-sm items-center justify-center hover:bg-error-600 focus:shadow-[0_0_0_2px] focus:shadow-black"
/>
</div>
</AEditableRoot>
</div>
</template>
Import all parts and piece them together.
<script setup>
import {
AEditableArea,
AEditableCancelTrigger,
AEditableEditTrigger,
AEditableInput,
AEditablePreview,
AEditableRoot,
AEditableSubmitTrigger
} from 'akar';
</script>
<template>
<AEditableRoot>
<AEditableArea>
<AEditablePreview />
<AEditableInput />
</AEditableArea>
<AEditableEditTrigger />
<AEditableSubmitTrigger />
<AEditableCancelTrigger />
</AEditableRoot>
</template>
Contains all the parts of an editable component.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
activationMode | 'focus' | 'dblclick' | 'focus' | 'none'The activation event of the editable field |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
autoResize | false | booleanWhether the editable field should auto resize |
defaultValue | stringThe default value of the editable field | |
dir | 'ltr' | 'rtl'The reading direction of the calendar when applicable. | |
disabled | false | booleanWhether the editable field is disabled |
id | stringThe id of the field | |
maxLength | numberThe maximum number of characters allowed | |
modelValue | string | null | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
placeholder | 'Enter text...' | string | { edit: string; preview: string; }The placeholder for the editable field |
readonly | booleanWhether the editable field is read-only | |
required | false | booleanWhen |
selectOnFocus | false | booleanWhether to select the text in the input when it is focused. |
startWithEditMode | booleanWhether to start with the edit mode active | |
submitMode | 'blur' | 'blur' | 'none' | 'both' | 'enter'The submit event of the editable field |
| Event | Type |
|---|---|
submit | [value: string | null] |
update:modelValue | [value: string]Event handler called whenever the model value changes |
update:state | [state: 'cancel' | 'submit' | 'edit']Event handler called when the editable field changes state |
| Slot | Type |
|---|---|
isEditing | booleanWhether the editable field is in edit mode |
modelValue | string | null The value of the editable field |
isEmpty | booleanWhether the editable field is empty |
submit | (): voidEvent handler called when a value is submitted |
cancel | (): voidFunction to cancel the value of the editable |
edit | (): voidFunction to set the editable in edit mode |
Contains the text parts of an editable component.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Attribute | Value |
|---|---|
[data-readonly] | Present when readonly |
[data-disabled] | Present when disabled |
[data-placeholder-shown] | Present when preview is shown |
[data-empty] | Present when the input is empty |
[data-focus] | Present when the editable field is focused. To be deprecated in favor of [data-focused] |
[data-focused] | Present when the editable field is focused |
Contains the input of an editable component.
| Prop | Default | Type |
|---|---|---|
as | 'input' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Attribute | Value |
|---|---|
[data-disabled] | Present when disabled |
[data-invalid] | Present when invalid |
Contains the preview of the editable component.
| Prop | Default | Type |
|---|---|---|
as | 'span' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Contains the edit trigger of the editable component.
| Prop | Default | Type |
|---|---|---|
as | 'button' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Contains the submit trigger of the editable component.
| Prop | Default | Type |
|---|---|---|
as | 'button' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Contains the cancel trigger of the editable component.
| Prop | Default | Type |
|---|---|---|
as | 'button' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
By default the component will submit when blur event triggers. We can modify the submit-mode prop to alter this behavior.
In this case, we want to submit only when user click on AEditableSubmitTrigger, so we change the submit mode to none.
<template>
<AEditableRoot submit-mode="none">
<AEditableArea>
<AEditablePreview />
<AEditableInput />
</AEditableArea>
<AEditableEditTrigger />
<AEditableSubmitTrigger />
<AEditableCancelTrigger />
</AEditableRoot>
</template>
| Key | Description |
|---|---|
Tab | When focus moves onto the editable field, switches into the editable mode if the |
Enter | If the |
Escape | When the focus is on the editable field, it cancels the changes. |