Input
Preview
<script setup lang="ts">
import { Input } from 'beui-vue'
import { computed, ref } from 'vue'
const email = ref('')
const pass = ref('hunter2')
const query = ref('Ada')
const show = ref(false)
const emailError = computed(() => (email.value.length > 0 && !email.value.includes('@') ? 'Enter a valid email address.' : undefined))
</script>
<template>
<div class="fields">
<Input v-model="email" label="Email" type="email" placeholder="[email protected]" :error="emailError" reserve-error-line>
<template #leftIcon>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m22 7-8.991 5.727a2 2 0 0 1-2.009 0L2 7" /><rect x="2" y="4" width="20" height="16" rx="2" /></svg>
</template>
</Input>
<Input v-model="pass" label="Password" :type="show ? 'text' : 'password'" reserve-error-line>
<template #rightIcon>
<button type="button" class="reveal" :aria-label="show ? 'Hide password' : 'Show password'" @click="show = !show">
<svg v-if="show" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575 1 1 0 0 1 0 .696 10.747 10.747 0 0 1-1.444 2.49" /><path d="M14.084 14.158a3 3 0 0 1-4.242-4.242" /><path d="M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151 1 1 0 0 1 0-.696 10.75 10.75 0 0 1 4.446-5.143" /><path d="m2 2 20 20" /></svg>
<svg v-else viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0" /><circle cx="12" cy="12" r="3" /></svg>
</button>
</template>
</Input>
<Input v-model="query" label="Search" :success="query.length > 1" reserve-error-line>
<template #leftIcon>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m21 21-4.34-4.34" /><circle cx="11" cy="11" r="8" /></svg>
</template>
</Input>
</div>
</template>
<style scoped>
.fields { display: flex; width: 100%; max-width: 20rem; flex-direction: column; gap: 0.25rem; }
.reveal { padding: 0; border: 0; background: transparent; color: inherit; cursor: pointer; pointer-events: auto; }
</style><script lang="ts">
export interface InputClassNames {
root?: string
label?: string
field?: string
input?: string
leftIcon?: string
rightIcon?: string
successIcon?: string
errorMessage?: string
}
</script>
<script setup lang="ts">
import { AnimatePresence, animate, motion } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, shallowRef, useAttrs, useId, useTemplateRef, watch } from 'vue'
defineOptions({ inheritAttrs: false })
const { label, error, success, reserveErrorLine = false, id, modelValue, defaultValue, classNames } = defineProps<{
label?: string
/** Controlled text. Use `v-model`. */
modelValue?: string
/** Initial text of an uncontrolled field. Bind `v-model` instead to control it. */
defaultValue?: string
/** Truthy error triggers a shake, red border and (if a string) a message. */
error?: string | boolean
/** Reserve one message line so validation does not shift nearby content. */
reserveErrorLine?: boolean
success?: boolean
id?: string
/** Extra classes for the inner parts of the field. */
classNames?: InputClassNames
}>()
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
// Unbound, the field keeps its own text after starting at `defaultValue`.
const internal = shallowRef(defaultValue ?? '')
const value = computed({
get: () => modelValue ?? internal.value,
set: next => {
if (modelValue === undefined) internal.value = next
emit('update:modelValue', next)
},
})
defineSlots<{ leftIcon?: () => unknown; rightIcon?: () => unknown }>()
const attrs = useAttrs()
const uid = useId()
const inputId = computed(() => id ?? uid)
const reduce = useReducedMotion()
const focused = shallowRef(false)
const field = useTemplateRef<HTMLDivElement>('field')
const hasError = computed(() => Boolean(error))
const errorMessage = computed(() => (typeof error === 'string' && error ? error : null))
// Right edge shows the success check, otherwise the caller's right icon.
const showRight = computed(() => !success)
const state = computed(() => (hasError.value ? 'error' : success ? 'success' : focused.value ? 'focused' : 'idle'))
const describedBy = computed(() => [attrs['aria-describedby'], errorMessage.value ? `${inputId.value}-error` : null].filter(Boolean).join(' ') || undefined)
// Shake the field when an error appears.
watch(hasError, next => {
const element = field.value
if (!element || reduce.value || !next) return
animate(element, { x: [0, -6, 6, -4, 4, -2, 0] }, { duration: 0.45 })
})
</script>
<template>
<div class="be-input" :class="[attrs.class, classNames?.root]" :style="attrs.style as Record<string, string>">
<label v-if="label" :for="inputId" class="be-input-label" :class="classNames?.label">{{ label }}</label>
<div ref="field" class="be-input-field" :class="classNames?.field" :data-state="state">
<span v-if="$slots.leftIcon" class="be-input-left" :class="classNames?.leftIcon"><slot name="leftIcon" /></span>
<input v-bind="{ ...attrs, class: undefined, style: undefined }" :id="inputId" v-model="value" class="be-input-native" :class="classNames?.input"
:aria-invalid="hasError || undefined" :aria-describedby="describedBy"
:data-left="$slots.leftIcon ? '' : undefined" :data-right="showRight && $slots.rightIcon || success ? '' : undefined"
@focus="focused = true" @blur="focused = false" />
<svg v-if="success" viewBox="0 0 24 24" fill="none" class="be-input-success" :class="classNames?.successIcon">
<motion.path d="M5 12.5l4.5 4.5L19 7.5" stroke="currentColor" :stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"
:initial="reduce ? { pathLength: 1 } : { pathLength: 0 }" :animate="{ pathLength: 1 }" :transition="{ duration: 0.35, ease: 'easeOut' }" />
</svg>
<span v-else-if="$slots.rightIcon" class="be-input-right" :class="classNames?.rightIcon"><slot name="rightIcon" /></span>
</div>
<div :class="reserveErrorLine ? 'be-input-reserve' : 'be-input-contents'">
<AnimatePresence :initial="false">
<motion.p
v-if="errorMessage"
:id="`${inputId}-error`"
role="alert"
class="be-input-error"
:class="classNames?.errorMessage"
:initial="reduce ? { opacity: 0 } : { opacity: 0, y: -4, filter: 'blur(4px)' }"
:animate="{ opacity: 1, y: 0, filter: 'blur(0px)' }"
:exit="reduce ? { opacity: 0 } : { opacity: 0, y: -4, filter: 'blur(4px)' }"
:transition="{ duration: 0.2 }"
>{{ errorMessage }}</motion.p>
</AnimatePresence>
</div>
</div>
</template>
<style scoped>
.be-input { display: flex; flex-direction: column; gap: .375rem; }
.be-input-label { padding-inline: .25rem; font-size: .875rem; line-height: 1.25rem; font-weight: 500; color: var(--beui-foreground); }
.be-input-field { position: relative; box-sizing: border-box; height: 2.75rem; overflow: hidden; border: 1px solid var(--beui-border); border-radius: 999px; transition: color .2s cubic-bezier(.4, 0, .2, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), border-color .2s cubic-bezier(.4, 0, .2, 1); }
.be-input-field:has(.be-input-native:disabled) { opacity: .6; }
.be-input-field[data-state=focused] { border-color: color-mix(in srgb, var(--beui-foreground) 40%, transparent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--beui-foreground) 5%, transparent); }
.be-input-field[data-state=error] { border-color: var(--beui-destructive); box-shadow: 0 0 0 2px color-mix(in srgb, var(--beui-destructive) 25%, transparent); }
.be-input-left { pointer-events: none; position: absolute; left: .75rem; top: 50%; display: flex; transform: translateY(-50%); align-items: center; color: var(--beui-muted-foreground); }
.be-input-left :deep(svg) { width: 1rem; height: 1rem; }
.be-input-native { box-sizing: border-box; height: 100%; width: 100%; padding: 0 .875rem; border: 0; background: transparent; color: var(--beui-foreground); caret-color: var(--beui-foreground); font: inherit; font-size: 1rem; line-height: 1.5rem; outline: none; }
.be-input-native::placeholder { color: color-mix(in srgb, var(--beui-muted-foreground) 60%, transparent); }
.be-input-native[data-left] { padding-left: 2.5rem; }
.be-input-native[data-right] { padding-right: 2.5rem; }
.be-input-native:disabled { cursor: not-allowed; }
.be-input-success { position: absolute; right: .875rem; top: 50%; width: 1.25rem; height: 1.25rem; transform: translateY(-50%); color: var(--beui-success, oklch(70% 0.18 155)); }
.be-input-right { position: absolute; right: 0; top: 0; display: flex; height: 100%; align-items: center; color: var(--beui-muted-foreground); }
.be-input-right :deep(button) { display: grid; width: 2.75rem; height: 2.75rem; place-items: center; }
.be-input-right :deep(svg) { width: 1rem; height: 1rem; }
.be-input-contents { display: contents; }
.be-input-reserve { min-height: 1rem; }
.be-input-error { margin: 0; padding-inline: .25rem; font-size: .75rem; line-height: 1rem; color: var(--beui-destructive); }
</style>Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/input$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import Input from '@/components/beui/controls/Input.vue'
import '@/components/beui/styles.css'API Reference
label?string—v-model?stringControlled text. Use `v-model`.
—defaultValue?stringInitial text of an uncontrolled field. Bind `v-model` instead to control it.
—error?string | booleanTruthy error triggers a shake, red border and (if a string) a message.
—reserveErrorLine?booleanReserve one message line so validation does not shift nearby content.
falsesuccess?boolean—#leftIcon?slot—#rightIcon?slot—class?string—classNames?InputClassNamesExtra classes for the inner parts of the field.
—id?string—Related components
Checkbox
Form choice control with a draw-on checkmark, spring press feedback and indeterminate state support.
Select
Composable select primitives whose panel unfolds out of the trigger on one spring and pulls away into its own pill, plus a Morph variant where the trigger grows into the panel via shared layout.
Radio Group
Single-select choice control with a gliding layoutId indicator dot and spring press feedback.
Updated