feat: publish ColorField and shared color input parts

This commit is contained in:
maoyingjie
2026-10-27 11:53:02 +08:00
parent 108a120c81
commit 6a7a89be6b
34 changed files with 1511 additions and 39 deletions
@@ -7,6 +7,7 @@ import {
type VNodeChild,
type VNode,
} from "vue";
import { colorFieldVariants } from "@heroui-vue/styles";
import { useColorField } from "./useColorField";
import type { ColorFieldProps, ColorFieldRenderState } from "./context";
import type { Color } from "../color/value";
@@ -45,6 +46,10 @@ const rendered = defineComponent({
props: ["nodes"],
setup: (p) => () => p.nodes as VNodeChild,
});
const ordinaryStyle = computed(() =>
typeof props.style === "function" ? props.style(state.value) : props.style,
);
const computedStyle = computed(() => props.styleFn?.(state.value));
const nativeEvents = new Set([
"aria-label",
"aria-labelledby",
@@ -66,7 +71,9 @@ const nativeEvents = new Set([
]);
const rootAttrs = () =>
Object.fromEntries(
Object.entries(attrs).filter(([key]) => !nativeEvents.has(key)),
Object.entries(attrs).filter(
([key]) => !key.startsWith("aria-") && !nativeEvents.has(key),
),
);
function focusin(event: FocusEvent) {
focusWithin.value = true;
@@ -83,6 +90,7 @@ function focusout(event: FocusEvent) {
defineExpose({
focus: () => control.input.value?.focus(),
element: control.input,
color: control.state.value,
});
</script>
<template>
@@ -90,13 +98,16 @@ defineExpose({
:is="as"
ref="root"
v-bind="rootAttrs()"
class="hv-color-field"
:class="{ 'hv-color-field--full-width': fullWidth }"
:class="colorFieldVariants({ fullWidth })"
:style="[ordinaryStyle, computedStyle]"
data-slot="color-field"
:data-channel="channel ?? 'hex'"
:data-disabled="disabled || undefined"
:data-readonly="readonly || undefined"
:data-required="required || undefined"
:data-focused="focused || undefined"
:data-focus-within="focusWithin || undefined"
:data-focus-visible="focusVisible || undefined"
:data-invalid="invalid || undefined"
@focusin="focusin"
@focusout="focusout"
@@ -1,18 +1,21 @@
<script setup lang="ts">
import { computed, inject, shallowRef, useAttrs } from "vue";
import type {
ColorInputGroupBaseProps,
ColorInputGroupRenderProps,
} from "./types";
import { colorInputGroupVariants } from "@heroui-vue/styles";
import { useColorFieldContext } from "./context";
import { useInheritedDisabled } from "../field/disabled";
import { textFieldContextKey } from "../textfield/context";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
variant?: "primary" | "secondary";
fullWidth?: boolean;
as?: string;
role?: string;
}>(),
{ variant: "primary", as: "div" },
);
const props = withDefaults(defineProps<ColorInputGroupBaseProps>(), {
variant: "primary",
as: "div",
});
defineSlots<{
default?: (state: ColorInputGroupRenderProps) => import("vue").VNode[];
}>();
const attrs = useAttrs(),
field = useColorFieldContext(),
text = inject(textFieldContextKey, undefined);
@@ -29,6 +32,16 @@ const invalid = computed(
attrs["aria-invalid"] === true ||
attrs["aria-invalid"] === "true",
);
const state = computed(() => ({
isDisabled: disabled.value,
isInvalid: invalid.value,
isFocusWithin: focused.value,
isHovered: hovered.value,
}));
const ordinaryStyle = computed(() =>
typeof props.style === "function" ? props.style(state.value) : props.style,
);
const computedStyle = computed(() => props.styleFn?.(state.value));
function click(event: MouseEvent) {
if (event.defaultPrevented || disabled.value) return;
const target = event.target as HTMLElement;
@@ -44,17 +57,23 @@ function blur(event: FocusEvent) {
if (!root.value?.contains(event.relatedTarget as Node | null))
focused.value = false;
}
defineExpose({
element: root,
focus: () =>
root.value
?.querySelector<HTMLInputElement>(
'input[data-slot="color-input-group-input"]',
)
?.focus(),
});
</script>
<template>
<component
:is="as"
ref="root"
v-bind="attrs"
class="hv-color-input-group"
:class="[
`hv-color-input-group--${variant}`,
{ 'hv-color-input-group--full-width': fullWidth },
]"
:class="colorInputGroupVariants({ variant, fullWidth }).base()"
:style="[ordinaryStyle, computedStyle]"
:role="role ?? (field ? 'presentation' : 'group')"
data-slot="color-input-group"
:data-disabled="disabled || undefined"
@@ -77,10 +77,12 @@ function inputAttrs() {
field.field.field.required.value) ||
undefined,
"aria-describedby": mergeIds(
field.nativeAttrs(attrs)["aria-describedby"] as string | undefined,
attrs["aria-describedby"] as string | undefined,
field.field.describedBy(),
),
"aria-labelledby": mergeIds(
field.nativeAttrs(attrs)["aria-labelledby"] as string | undefined,
attrs["aria-labelledby"] as string | undefined,
field.field.labelledBy(),
),
@@ -12,6 +12,10 @@ import type { ValidateResult } from "../field/context";
export interface ColorFieldProps
extends ColorModelProps, ColorTextOptions, ChoiceFieldProps {
as?: string;
style?:
| import("vue").StyleValue
| ((state: ColorFieldRenderState) => import("vue").StyleValue);
styleFn?: (state: ColorFieldRenderState) => import("vue").StyleValue;
fullWidth?: boolean;
autoFocus?: boolean;
autoComplete?: string;
@@ -0,0 +1,50 @@
import type { StyleValue, VNode } from "vue";
import type ColorField from "./ColorField.vue";
import type ColorInputGroup from "./ColorInputGroup.vue";
import type ColorInputGroupInput from "./ColorInputGroupInput.vue";
import type ColorInputGroupPrefix from "./ColorInputGroupPrefix.vue";
import type ColorInputGroupSuffix from "./ColorInputGroupSuffix.vue";
export type {
ColorFieldProps,
ColorFieldProps as ColorFieldRootProps,
ColorFieldRenderState,
ColorFieldRenderState as ColorFieldRenderProps,
} from "./context";
export interface ColorInputGroupRenderProps {
isDisabled: boolean;
isInvalid: boolean;
isFocusWithin: boolean;
isHovered: boolean;
}
export interface ColorInputGroupBaseProps {
variant?: "primary" | "secondary";
fullWidth?: boolean;
as?: string;
role?: string;
style?: StyleValue | ((state: ColorInputGroupRenderProps) => StyleValue);
styleFn?: (state: ColorInputGroupRenderProps) => StyleValue;
}
export type ColorInputGroupProps = InstanceType<
typeof ColorInputGroup
>["$props"];
export type ColorInputGroupRootProps = ColorInputGroupProps;
export type ColorFieldGroupProps = ColorInputGroupProps;
export type ColorInputGroupInputProps = InstanceType<
typeof ColorInputGroupInput
>["$props"];
export type ColorFieldInputProps = ColorInputGroupInputProps;
export type ColorInputGroupPrefixProps = InstanceType<
typeof ColorInputGroupPrefix
>["$props"];
export type ColorFieldPrefixProps = ColorInputGroupPrefixProps;
export type ColorInputGroupSuffixProps = InstanceType<
typeof ColorInputGroupSuffix
>["$props"];
export type ColorFieldSuffixProps = ColorInputGroupSuffixProps;
export type ColorFieldExpose = Pick<
InstanceType<typeof ColorField>,
"focus" | "element" | "color"
>;
export type ColorFieldSlots = {
default?: (state: import("./context").ColorFieldRenderState) => VNode[];
};
+32
View File
@@ -1290,3 +1290,35 @@ export type {
RangeCalendarCellProps,
RangeCalendarCellIndicatorProps,
} from "./types";
// ColorField and its shared ColorInputGroup parts (HeroUI ce90ea3).
export {
default as ColorField,
default as ColorFieldRoot,
} from "./components/color-field/ColorField.vue";
export {
default as ColorInputGroup,
default as ColorInputGroupRoot,
default as ColorFieldGroup,
} from "./components/color-field/ColorInputGroup.vue";
export {
default as ColorInputGroupInput,
default as ColorFieldInput,
} from "./components/color-field/ColorInputGroupInput.vue";
export {
default as ColorInputGroupPrefix,
default as ColorFieldPrefix,
} from "./components/color-field/ColorInputGroupPrefix.vue";
export {
default as ColorInputGroupSuffix,
default as ColorFieldSuffix,
} from "./components/color-field/ColorInputGroupSuffix.vue";
export type * from "./components/color-field/types";
export {
colorFieldVariants,
colorInputGroupVariants,
} from "@heroui-vue/styles";
export type {
ColorFieldVariants,
ColorInputGroupVariants,
} from "@heroui-vue/styles";