FloatLabel
A standalone label that moves onto the input border when the field has a value, receives focus, or opens a panel.
Import
ts
import { HuiFloatLabel, HuiInput } from '@nscode/handy-ui'
import type { FloatLabelProps } from '@nscode/handy-ui'
Examples
Basic Usage
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput } from '@nscode/handy-ui'
const name = ref('')
</script>
<template>
<HuiFloatLabel label="Department" style="width: 260px; margin-top: 8px">
<HuiInput v-model="name" clearable />
</HuiFloatLabel>
</template>
Input Controls
vue
<script setup lang="ts">
import { reactive } from 'vue'
import {
HuiFloatLabel,
HuiInput,
HuiTextarea,
HuiSelect,
HuiCascader,
HuiTreeSelect,
HuiTimePicker,
HuiDatePicker,
HuiAutoComplete,
HuiInputTag,
HuiMentions,
HuiIconPicker
} from '@nscode/handy-ui'
const model = reactive({
name: '',
remark: '',
department: undefined as string | undefined,
region: [] as string[],
tree: undefined as string | undefined,
time: '',
date: '',
keyword: '',
tags: [] as string[],
mentions: '',
icon: ''
})
const options = [
{ label: 'Engineering', value: 'dev' },
{ label: 'Operations', value: 'ops' }
]
const regions = [{ label: 'East', value: 'east', children: [{ label: 'Shanghai', value: 'sh' }] }]
const tree = [
{ label: 'Head office', id: 'root', children: [{ label: 'Engineering', id: 'dev' }] }
]
</script>
<template>
<div class="float-grid">
<HuiFloatLabel label="Input"><HuiInput v-model="model.name" clearable /></HuiFloatLabel>
<HuiFloatLabel label="Textarea"><HuiTextarea v-model="model.remark" /></HuiFloatLabel>
<HuiFloatLabel label="Select"
><HuiSelect v-model="model.department" :options="options" clearable
/></HuiFloatLabel>
<HuiFloatLabel label="Cascader"
><HuiCascader v-model="model.region" :options="regions"
/></HuiFloatLabel>
<HuiFloatLabel label="TreeSelect"
><HuiTreeSelect v-model="model.tree" :data="tree"
/></HuiFloatLabel>
<HuiFloatLabel label="TimePicker"><HuiTimePicker v-model="model.time" /></HuiFloatLabel>
<HuiFloatLabel label="DatePicker"><HuiDatePicker v-model="model.date" /></HuiFloatLabel>
<HuiFloatLabel label="AutoComplete"
><HuiAutoComplete v-model="model.keyword" :options="options"
/></HuiFloatLabel>
<HuiFloatLabel label="InputTag"><HuiInputTag v-model="model.tags" /></HuiFloatLabel>
<HuiFloatLabel label="Mentions"
><HuiMentions v-model="model.mentions" :options="options"
/></HuiFloatLabel>
<HuiFloatLabel label="IconPicker"><HuiIconPicker v-model="model.icon" /></HuiFloatLabel>
</div>
</template>
<style scoped>
.float-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 24px;
padding-top: 8px;
}
@media (max-width: 600px) {
.float-grid {
grid-template-columns: minmax(0, 1fr);
}
}
</style>
Labels That Stay Raised
Number controls, date ranges, and fields with leading content keep their labels raised.
vue
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiFloatLabel,
HuiInputNumber,
HuiDatePicker,
HuiInput,
HuiSpace
} from '@nscode/handy-ui'
const count = ref<number | undefined>()
const range = ref<[string, string]>(['', ''])
const keyword = ref('')
</script>
<template>
<HuiSpace wrap align="start" size="large" style="padding-top: 8px">
<HuiFloatLabel label="Quantity" style="width: 200px"
><HuiInputNumber v-model="count"
/></HuiFloatLabel>
<HuiFloatLabel label="Date range" style="width: 300px"
><HuiDatePicker v-model="range" type="daterange"
/></HuiFloatLabel>
<HuiFloatLabel label="Search" style="width: 240px"
><HuiInput v-model="keyword" prefix-icon="search"
/></HuiFloatLabel>
</HuiSpace>
</template>
Sizes
vue
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiFloatLabel, HuiInput, HuiSpace } from '@nscode/handy-ui'
const sizes = ['large', 'default', 'small'] as const
const model = reactive({ large: '', default: '', small: '' })
</script>
<template>
<HuiSpace direction="vertical" fill size="large" style="padding-top: 8px">
<HuiFloatLabel v-for="size in sizes" :key="size" :label="size" style="width: 260px">
<HuiInput v-model="model[size]" :size="size" />
</HuiFloatLabel>
</HuiSpace>
</template>
Toolbar
vue
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiFloatLabel,
HuiInput,
HuiSelect,
HuiButton,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
const name = ref('')
const status = ref<string>()
const options = [
{ label: 'Enabled', value: 'enabled' },
{ label: 'Disabled', value: 'disabled' }
]
const search = () =>
HuiMessage.info(`Search: ${name.value || 'all names'}, ${status.value || 'all statuses'}`)
</script>
<template>
<HuiSpace wrap align="center" style="padding-top: 8px">
<HuiFloatLabel label="Name" style="width: 220px"
><HuiInput v-model="name" clearable
/></HuiFloatLabel>
<HuiFloatLabel label="Status" style="width: 180px"
><HuiSelect v-model="status" :options="options" clearable
/></HuiFloatLabel>
<HuiButton type="primary" icon="search" @click="search">Search</HuiButton>
</HuiSpace>
</template>
Disabled
vue
<script setup lang="ts">
import { HuiFloatLabel, HuiInput, HuiSelect, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap size="large" style="padding-top: 8px">
<HuiFloatLabel label="Empty" style="width: 240px"><HuiInput disabled /></HuiFloatLabel>
<HuiFloatLabel label="Filled" style="width: 240px"
><HuiInput model-value="Engineering" disabled
/></HuiFloatLabel>
<HuiFloatLabel label="Select" style="width: 240px"
><HuiSelect disabled :options="[]"
/></HuiFloatLabel>
</HuiSpace>
</template>
Explicit Validation State
When invalid is true, the label and supported field borders turn red, and the first registered field receives aria-invalid="true". This prop does not run validation.
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput, HuiSwitch, HuiSpace } from '@nscode/handy-ui'
const invalid = ref(true)
const name = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSwitch v-model="invalid" aria-label="Invalid" />
<HuiFloatLabel label="Department" :invalid="invalid" style="width: 260px; margin-top: 8px">
<HuiInput v-model="name" />
</HuiFloatLabel>
</HuiSpace>
</template>
Form Validation
Leave the enclosing form item's label unset. It still displays errors and connects them to the field.
vue
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiFloatLabel,
HuiInput,
HuiForm,
HuiFormItem,
HuiButton,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
const model = reactive({ name: '' })
const form = ref<InstanceType<typeof HuiForm>>()
const rules = {
name: [{ required: true, message: 'Enter a department', trigger: 'blur' as const }]
}
const validate = async () => {
const result = await form.value?.validate()
if (result?.valid) HuiMessage.success('Validation passed')
}
</script>
<template>
<HuiForm ref="form" :model="model" :rules="rules" label-width="0" style="padding-top: 8px">
<HuiFormItem prop="name">
<HuiFloatLabel label="Department" style="width: 260px"
><HuiInput v-model="model.name"
/></HuiFloatLabel>
</HuiFormItem>
<HuiSpace><HuiButton type="primary" @click="validate">Validate</HuiButton></HuiSpace>
</HuiForm>
</template>
Custom Label
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput, HuiIcon } from '@nscode/handy-ui'
const name = ref('')
</script>
<template>
<HuiFloatLabel style="width: 260px; margin-top: 8px">
<template #label><HuiIcon name="user" size="inherit" /> Contact *</template>
<HuiInput v-model="name" />
</HuiFloatLabel>
</template>
Manual Filled State And Native Input
filled overrides automatic detection for HandyUI fields only. A native input does not register itself; its label is placed above it, and its accessible name must be supplied explicitly.
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput, HuiSwitch, HuiSpace } from '@nscode/handy-ui'
const filled = ref(false)
const nativeValue = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSwitch v-model="filled" aria-label="Raise label" />
<HuiFloatLabel label="Manual state" :filled="filled" style="width: 260px; margin-top: 8px"
><HuiInput
/></HuiFloatLabel>
<HuiFloatLabel label="Native input" style="width: 260px">
<input v-model="nativeValue" aria-label="Native input" />
</HuiFloatLabel>
</HuiSpace>
</template>
Placeholder
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFloatLabel, HuiInput } from '@nscode/handy-ui'
const name = ref('')
</script>
<template>
<HuiFloatLabel label="Department" style="width: 260px; margin-top: 8px">
<HuiInput v-model="name" placeholder="For example: Engineering" clearable />
</HuiFloatLabel>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | — | Label text; the label slot takes priority |
| filled | boolean | undefined | Explicit filled state for HandyUI fields only; otherwise detected, or raised when unknown |
| invalid | boolean | undefined | Explicit label and border error state; otherwise follows the form item |
Events
None.
Slots
| Slot | Description |
|---|---|
| default | One input control |
| label | Custom label content |
Methods
None.
Types
ts
interface FloatLabelProps {
label?: string
filled?: boolean
invalid?: boolean
}