feat: add InputGroup and verify shared field integration
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
|
||||
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix. */
|
||||
|
||||
/* Input group styles */
|
||||
.hv-input-group {
|
||||
@apply inline-flex min-h-9 items-center rounded-field border bg-field text-sm text-field-foreground shadow-field outline-none;
|
||||
border-width: var(--border-width-field);
|
||||
border-color: var(--field-border);
|
||||
|
||||
/* When textarea is present, align items to start and allow height to grow */
|
||||
&:has([data-slot="input-group-textarea"]) {
|
||||
@apply items-start;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition:
|
||||
background-color 150ms var(--ease-smooth),
|
||||
border-color 150ms var(--ease-smooth),
|
||||
box-shadow 150ms var(--ease-out);
|
||||
@apply motion-reduce:transition-none;
|
||||
|
||||
/* Hover state */
|
||||
@media (hover: hover) {
|
||||
&:hover:not(:focus-within),
|
||||
&[data-hovered="true"]:not([data-focus-within="true"]) {
|
||||
@apply bg-field-hover;
|
||||
border-color: var(--field-border-hover);
|
||||
}
|
||||
}
|
||||
|
||||
/* Focus within state - only when input/textarea is focused */
|
||||
&:has([data-slot="input-group-input"]:focus),
|
||||
&:has([data-slot="input-group-textarea"]:focus) {
|
||||
@apply status-focused-field;
|
||||
border-color: var(--field-border-focus);
|
||||
background-color: var(--field-focus);
|
||||
}
|
||||
|
||||
/* Invalid state */
|
||||
&[data-invalid="true"] {
|
||||
@apply status-invalid-field;
|
||||
background-color: var(--field-focus);
|
||||
border-color: var(--color-field-border-invalid);
|
||||
}
|
||||
|
||||
/* Disabled state */
|
||||
&[data-disabled="true"],
|
||||
&[aria-disabled="true"] {
|
||||
@apply status-disabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Browser autofill state.
|
||||
* The user-agent paints `:-webkit-autofill`/`:autofill` on the inner <input>
|
||||
* element only, inside its content box. Because the inner input has its
|
||||
* border-radius stripped on whichever side touches a prefix/suffix, the
|
||||
* autofill highlight ends up as a sharp-cornered rectangle and the
|
||||
* prefix/suffix slots receive no highlight at all. Lift the highlight
|
||||
* onto the group so the rounded shell + prefix + suffix all share it.
|
||||
*/
|
||||
&:has([data-slot="input-group-input"]:-webkit-autofill),
|
||||
&:has([data-slot="input-group-input"]:autofill) {
|
||||
background-color: var(--field-focus);
|
||||
border-color: var(--field-border-focus);
|
||||
}
|
||||
}
|
||||
|
||||
.hv-input-group__input {
|
||||
@apply flex-1 rounded-none border-0 bg-transparent px-3 py-2 text-base shadow-none outline-none placeholder:text-field-placeholder sm:text-sm;
|
||||
|
||||
/* Remove border radius on left side when prefix is present */
|
||||
.hv-input-group:has([data-slot="input-group-prefix"]) & {
|
||||
@apply rounded-s-none ps-0;
|
||||
}
|
||||
|
||||
/* Remove border radius on right side when suffix is present */
|
||||
.hv-input-group:has([data-slot="input-group-suffix"]) & {
|
||||
@apply rounded-e-none pe-0;
|
||||
}
|
||||
|
||||
/* Focus state - handled by group */
|
||||
&:focus,
|
||||
&:focus-visible {
|
||||
@apply outline-none;
|
||||
}
|
||||
|
||||
/**
|
||||
* Neutralize the browser's per-input autofill paint so the inner <input>
|
||||
* doesn't draw a sharp-cornered yellow rectangle. The unified highlight
|
||||
* is applied to `.hv-input-group` above. Preserve the autofilled text and
|
||||
* caret colors, and pin the background-color transition so the inset
|
||||
* box-shadow trick doesn't flicker on state changes.
|
||||
*/
|
||||
&:-webkit-autofill,
|
||||
&:-webkit-autofill:hover,
|
||||
&:-webkit-autofill:focus,
|
||||
&:-webkit-autofill:active,
|
||||
&:autofill {
|
||||
-webkit-box-shadow: 0 0 0 1000px transparent inset;
|
||||
box-shadow: 0 0 0 1000px transparent inset;
|
||||
-webkit-text-fill-color: var(--field-foreground);
|
||||
caret-color: var(--field-foreground);
|
||||
transition: background-color 9999s ease-in-out 0s;
|
||||
}
|
||||
}
|
||||
|
||||
/* TextArea within InputGroup - uses same base class as input but with textarea-specific styles */
|
||||
.hv-input-group__input[data-slot="input-group-textarea"] {
|
||||
min-height: 38px;
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
.hv-input-group__prefix {
|
||||
@apply flex h-full items-center justify-center rounded-s-field rounded-e-none bg-transparent px-3 text-field-placeholder;
|
||||
border-width: var(--border-width-field);
|
||||
border-color: var(--field-border);
|
||||
border-style: solid;
|
||||
border-inline-end-color: var(--field-border);
|
||||
border-inline-start: none;
|
||||
border-top: none;
|
||||
border-bottom: none;
|
||||
|
||||
/* Align to top when textarea is present */
|
||||
.hv-input-group:has([data-slot="input-group-textarea"]) & {
|
||||
@apply items-start;
|
||||
padding-top: 0.5rem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition:
|
||||
background-color 150ms var(--ease-smooth),
|
||||
border-color 150ms var(--ease-smooth);
|
||||
@apply motion-reduce:transition-none;
|
||||
}
|
||||
|
||||
.hv-input-group__suffix {
|
||||
@apply flex h-full items-center justify-center rounded-s-none rounded-e-field bg-transparent px-3 text-field-placeholder;
|
||||
border-width: var(--border-width-field);
|
||||
border-color: var(--field-border);
|
||||
border-style: solid;
|
||||
border-inline-start-color: var(--field-border);
|
||||
border-inline-end: none;
|
||||
border-top: none;
|
||||
border-bottom: none;
|
||||
|
||||
/* Align to top when textarea is present */
|
||||
.hv-input-group:has([data-slot="input-group-textarea"]) & {
|
||||
@apply items-start;
|
||||
padding-top: 0.5rem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition:
|
||||
background-color 150ms var(--ease-smooth),
|
||||
border-color 150ms var(--ease-smooth);
|
||||
@apply motion-reduce:transition-none;
|
||||
}
|
||||
|
||||
/* Variant property definitions */
|
||||
.hv-input-group--primary {
|
||||
/* Default styles */
|
||||
}
|
||||
|
||||
.hv-input-group--secondary {
|
||||
@apply shadow-none;
|
||||
background-color: var(--input-group-bg);
|
||||
|
||||
--input-group-bg: var(--default);
|
||||
--input-group-bg-hover: var(--default-hover);
|
||||
--input-group-bg-focus: var(--default);
|
||||
|
||||
@media (hover: hover) {
|
||||
&:hover:not(:focus-within),
|
||||
&[data-hovered="true"]:not([data-focus-within="true"]) {
|
||||
background-color: var(--input-group-bg-hover);
|
||||
}
|
||||
}
|
||||
|
||||
&:has([data-slot="input-group-input"]:focus),
|
||||
&:has([data-slot="input-group-textarea"]:focus) {
|
||||
background-color: var(--input-group-bg-focus);
|
||||
}
|
||||
|
||||
/* Invalid state */
|
||||
&[data-invalid="true"] {
|
||||
@apply status-invalid-field;
|
||||
border-color: var(--color-field-border-invalid);
|
||||
background-color: var(--input-group-bg-focus);
|
||||
}
|
||||
|
||||
[data-slot="input-group-input"],
|
||||
[data-slot="input-group-textarea"] {
|
||||
@apply bg-transparent;
|
||||
}
|
||||
}
|
||||
|
||||
/* Full width modifier */
|
||||
.hv-input-group--full-width {
|
||||
@apply w-full;
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
@import "./components/chip.css" layer(components);
|
||||
@import "./components/badge.css" layer(components);
|
||||
@import "./components/alert.css" layer(components);
|
||||
@import "./components/input-group.css" layer(components);
|
||||
/* The Vue Switch uses Reka's native button for its clickable content. */
|
||||
@layer components {
|
||||
.hv-switch:has(.hv-switch__content:focus-visible) .hv-switch__control {
|
||||
|
||||
@@ -498,3 +498,34 @@ export const alertVariants = tv({
|
||||
});
|
||||
|
||||
export type AlertVariants = VariantProps<typeof alertVariants>;
|
||||
|
||||
export const inputGroupVariants = tv({
|
||||
defaultVariants: {
|
||||
fullWidth: false,
|
||||
variant: "primary",
|
||||
},
|
||||
slots: {
|
||||
base: "hv-input-group",
|
||||
input: "hv-input-group__input",
|
||||
prefix: "hv-input-group__prefix",
|
||||
suffix: "hv-input-group__suffix",
|
||||
},
|
||||
variants: {
|
||||
fullWidth: {
|
||||
false: {},
|
||||
true: {
|
||||
base: "hv-input-group--full-width",
|
||||
},
|
||||
},
|
||||
variant: {
|
||||
primary: {
|
||||
base: "hv-input-group--primary",
|
||||
},
|
||||
secondary: {
|
||||
base: "hv-input-group--secondary",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export type InputGroupVariants = VariantProps<typeof inputGroupVariants>;
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, provide, ref, useAttrs } from "vue";
|
||||
import {
|
||||
inputGroupVariants,
|
||||
type InputGroupVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import { textFieldContextKey } from "../textfield/context";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
fullWidth?: boolean;
|
||||
variant?: InputGroupVariants["variant"];
|
||||
role?: "group" | "region" | "presentation";
|
||||
}>(),
|
||||
{
|
||||
fullWidth: false,
|
||||
role: "group",
|
||||
},
|
||||
);
|
||||
|
||||
const attrs = useAttrs();
|
||||
const textField = inject(textFieldContextKey, undefined);
|
||||
|
||||
const groupEl = ref<HTMLElement>();
|
||||
const hovered = ref(false);
|
||||
const focusWithin = ref(false);
|
||||
|
||||
/** Explicit variant wins over TextField inheritance. */
|
||||
const resolvedVariant = computed<InputGroupVariants["variant"]>(
|
||||
() => props.variant ?? textField?.variant.value,
|
||||
);
|
||||
|
||||
const slots = computed(() =>
|
||||
inputGroupVariants({
|
||||
fullWidth: props.fullWidth,
|
||||
variant: resolvedVariant.value,
|
||||
}),
|
||||
);
|
||||
|
||||
provide(inputGroupContextKey, { slots });
|
||||
|
||||
const invalid = computed(() => Boolean(textField?.invalid.value));
|
||||
const disabled = computed(() => Boolean(textField?.disabled.value));
|
||||
|
||||
function focusControl(e: MouseEvent) {
|
||||
const group = groupEl.value;
|
||||
if (!group) return;
|
||||
const target = e.target as HTMLElement | null;
|
||||
const control = group.querySelector<HTMLElement>(
|
||||
'input[data-slot="input-group-input"], textarea[data-slot="input-group-textarea"]',
|
||||
);
|
||||
if (!control || control === target || control.contains(target)) return;
|
||||
// Keep real focus on the actual input/textarea so value and caret stay intact.
|
||||
if (
|
||||
target?.closest(
|
||||
'button, a, input, textarea, select, [role="button"], [tabindex]',
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
control.focus();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
ref="groupEl"
|
||||
v-bind="attrs"
|
||||
:class="[slots.base(), attrs.class]"
|
||||
:role="role"
|
||||
:data-hovered="hovered || undefined"
|
||||
:data-focus-within="focusWithin || undefined"
|
||||
:data-invalid="invalid || undefined"
|
||||
:data-disabled="disabled || undefined"
|
||||
:aria-disabled="disabled || undefined"
|
||||
data-slot="input-group"
|
||||
@click="focusControl"
|
||||
@pointerenter="hovered = true"
|
||||
@pointerleave="hovered = false"
|
||||
@focusin="focusWithin = true"
|
||||
@focusout="focusWithin = false"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { useTextControl } from "../field/useTextControl";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue?: string;
|
||||
defaultValue?: string;
|
||||
invalid?: boolean;
|
||||
type?: string;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
const control = useTextControl(props, attrs, emit);
|
||||
const { element, value } = control;
|
||||
|
||||
defineExpose({ focus: () => element.value?.focus(), element });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<input
|
||||
ref="element"
|
||||
v-bind="{ ...control.inheritedAttrs(), ...attrs }"
|
||||
:value="value"
|
||||
:id="control.id()"
|
||||
:name="control.name()"
|
||||
:type="control.type()"
|
||||
:disabled="control.disabled()"
|
||||
:readonly="control.readOnly()"
|
||||
:required="control.required()"
|
||||
:class="[group?.slots.value.input(), attrs.class]"
|
||||
:data-invalid="control.invalid() || undefined"
|
||||
:aria-invalid="control.invalid() || undefined"
|
||||
:aria-readonly="control.readOnly() || undefined"
|
||||
:aria-describedby="control.describedBy()"
|
||||
:data-validation-behavior="control.validationBehavior()"
|
||||
data-slot="input-group-input"
|
||||
@input="control.update"
|
||||
@invalid="control.onInvalid"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-bind="attrs"
|
||||
:class="[group?.slots.value.prefix(), attrs.class]"
|
||||
data-slot="input-group-prefix"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-bind="attrs"
|
||||
:class="[group?.slots.value.suffix(), attrs.class]"
|
||||
data-slot="input-group-suffix"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { useTextControl } from "../field/useTextControl";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue?: string;
|
||||
defaultValue?: string;
|
||||
invalid?: boolean;
|
||||
rows?: number;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
const control = useTextControl(props, attrs, emit);
|
||||
const { element, value } = control;
|
||||
|
||||
defineExpose({ focus: () => element.value?.focus(), element });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<textarea
|
||||
ref="element"
|
||||
v-bind="{ ...control.inheritedAttrs(), ...attrs }"
|
||||
:value="value"
|
||||
:id="control.id()"
|
||||
:name="control.name()"
|
||||
:rows="rows"
|
||||
:disabled="control.disabled()"
|
||||
:readonly="control.readOnly()"
|
||||
:required="control.required()"
|
||||
:class="[group?.slots.value.input(), attrs.class]"
|
||||
:data-invalid="control.invalid() || undefined"
|
||||
:aria-invalid="control.invalid() || undefined"
|
||||
:aria-readonly="control.readOnly() || undefined"
|
||||
:aria-describedby="control.describedBy()"
|
||||
:data-validation-behavior="control.validationBehavior()"
|
||||
data-slot="input-group-textarea"
|
||||
@input="control.update"
|
||||
@invalid="control.onInvalid"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { ComputedRef, InjectionKey } from "vue";
|
||||
import type { inputGroupVariants } from "@heroui-vue/styles";
|
||||
|
||||
export type InputGroupSlots = ReturnType<typeof inputGroupVariants>;
|
||||
|
||||
export interface InputGroupContextValue {
|
||||
slots: ComputedRef<InputGroupSlots>;
|
||||
}
|
||||
|
||||
export const inputGroupContextKey: InjectionKey<InputGroupContextValue> =
|
||||
Symbol("HeroUIVue.InputGroup");
|
||||
@@ -213,7 +213,12 @@ export default defineComponent({
|
||||
const position = `${path}-${index}`;
|
||||
const type = node.type as { name?: string; __name?: string };
|
||||
const name = type?.name ?? type?.__name;
|
||||
if (name === "Input" || name === "TextArea")
|
||||
if (
|
||||
name === "Input" ||
|
||||
name === "TextArea" ||
|
||||
name === "InputGroupInput" ||
|
||||
name === "InputGroupTextArea"
|
||||
)
|
||||
found.controlId = node.props?.id;
|
||||
if (name === "Description" || name === "FieldError") {
|
||||
const kind = name === "Description" ? "description" : "error";
|
||||
@@ -221,6 +226,22 @@ export default defineComponent({
|
||||
found[kind].push(String(id));
|
||||
return cloneVNode(node, { id });
|
||||
}
|
||||
// InputGroup is a library-owned transparent field wrapper. Prepare its
|
||||
// default slot once, preserving the current render's values and keys.
|
||||
// Arbitrary consumer components remain opaque to SSR planning.
|
||||
if (
|
||||
name === "InputGroup" &&
|
||||
node.children &&
|
||||
typeof node.children === "object"
|
||||
) {
|
||||
const groupSlots = node.children as { default?: () => VNodeChild[] };
|
||||
if (typeof groupSlots.default === "function") {
|
||||
const prepared = prepare(groupSlots.default(), found, position);
|
||||
const clone = cloneVNode(node);
|
||||
clone.children = { ...groupSlots, default: () => prepared };
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
if (
|
||||
(typeof node.type === "string" || node.type === Fragment) &&
|
||||
Array.isArray(node.children)
|
||||
|
||||
@@ -31,6 +31,7 @@ export {
|
||||
chipVariants,
|
||||
badgeVariants,
|
||||
alertVariants,
|
||||
inputGroupVariants,
|
||||
type ButtonVariants,
|
||||
type CardVariants,
|
||||
type InputVariants,
|
||||
@@ -48,6 +49,7 @@ export {
|
||||
type ChipVariants,
|
||||
type BadgeVariants,
|
||||
type AlertVariants,
|
||||
type InputGroupVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
export type {
|
||||
ButtonProps,
|
||||
@@ -82,6 +84,11 @@ export type {
|
||||
AlertContentProps,
|
||||
AlertTitleProps,
|
||||
AlertDescriptionProps,
|
||||
InputGroupProps,
|
||||
InputGroupInputProps,
|
||||
InputGroupTextAreaProps,
|
||||
InputGroupPrefixProps,
|
||||
InputGroupSuffixProps,
|
||||
} from "./types";
|
||||
|
||||
export { tv, cn, type VariantProps } from "@heroui-vue/styles";
|
||||
@@ -181,3 +188,14 @@ export type {
|
||||
DescriptionProps,
|
||||
FieldErrorProps,
|
||||
} from "./types";
|
||||
|
||||
export { default as InputGroup } from "./components/input-group/InputGroup.vue";
|
||||
export { default as InputGroupInput } from "./components/input-group/InputGroupInput.vue";
|
||||
export { default as InputGroupTextArea } from "./components/input-group/InputGroupTextArea.vue";
|
||||
export { default as InputGroupPrefix } from "./components/input-group/InputGroupPrefix.vue";
|
||||
export { default as InputGroupSuffix } from "./components/input-group/InputGroupSuffix.vue";
|
||||
export {
|
||||
inputGroupContextKey,
|
||||
type InputGroupContextValue,
|
||||
type InputGroupSlots,
|
||||
} from "./components/input-group/context";
|
||||
|
||||
@@ -81,3 +81,23 @@ export type TextFieldProps = InstanceType<typeof TextField>["$props"];
|
||||
export type FormProps = InstanceType<typeof Form>["$props"];
|
||||
export type DescriptionProps = InstanceType<typeof Description>["$props"];
|
||||
export type FieldErrorProps = InstanceType<typeof FieldError>["$props"];
|
||||
|
||||
import type InputGroup from "./components/input-group/InputGroup.vue";
|
||||
import type InputGroupInput from "./components/input-group/InputGroupInput.vue";
|
||||
import type InputGroupTextArea from "./components/input-group/InputGroupTextArea.vue";
|
||||
import type InputGroupPrefix from "./components/input-group/InputGroupPrefix.vue";
|
||||
import type InputGroupSuffix from "./components/input-group/InputGroupSuffix.vue";
|
||||
|
||||
export type InputGroupProps = InstanceType<typeof InputGroup>["$props"];
|
||||
export type InputGroupInputProps = InstanceType<
|
||||
typeof InputGroupInput
|
||||
>["$props"];
|
||||
export type InputGroupTextAreaProps = InstanceType<
|
||||
typeof InputGroupTextArea
|
||||
>["$props"];
|
||||
export type InputGroupPrefixProps = InstanceType<
|
||||
typeof InputGroupPrefix
|
||||
>["$props"];
|
||||
export type InputGroupSuffixProps = InstanceType<
|
||||
typeof InputGroupSuffix
|
||||
>["$props"];
|
||||
|
||||
Reference in New Issue
Block a user