feat: add Separator Kbd and Chip components

This commit is contained in:
maoyingjie
2026-10-21 18:35:24 +08:00
parent 3d05fea7bd
commit 3515a47f5e
51 changed files with 2813 additions and 21 deletions
+115
View File
@@ -0,0 +1,115 @@
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix. */
/* Base styles */
.hv-chip {
@apply inline-flex w-fit shrink-0 items-center gap-0.5 rounded-2xl px-2 py-0.5 text-xs leading-5 font-medium;
/* Default tokens */
--chip-bg: var(--default);
--chip-fg: currentColor;
background-color: var(--chip-bg);
color: var(--chip-fg);
}
.hv-chip__label {
@apply px-0.5;
}
/* Color variants - set foreground color */
.hv-chip--accent {
--chip-fg: var(--accent-soft-foreground);
}
.hv-chip--danger {
--chip-fg: var(--danger-soft-foreground);
}
.hv-chip--default {
--chip-fg: var(--default-foreground);
}
.hv-chip--success {
--chip-fg: var(--success-soft-foreground);
}
.hv-chip--warning {
--chip-fg: var(--warning-soft-foreground);
}
/* Variant styles */
.hv-chip--primary {
}
.hv-chip--secondary {
/* No additional styles for secondary variant */
}
.hv-chip--tertiary {
--chip-bg: transparent;
}
/* Size variants */
.hv-chip--sm {
@apply px-1 py-0 text-xs;
}
.hv-chip--md {
@apply text-xs;
}
.hv-chip--lg {
@apply px-3 py-1 text-sm font-medium;
}
/* ====================== */
/* = Compound variants = */
/* ====================== */
/* Primary variants - solid backgrounds */
.hv-chip--primary.hv-chip--accent {
--chip-bg: var(--accent);
--chip-fg: var(--accent-foreground);
}
.hv-chip--primary.hv-chip--success {
--chip-bg: var(--success);
--chip-fg: var(--success-foreground);
}
.hv-chip--primary.hv-chip--warning {
--chip-bg: var(--warning);
--chip-fg: var(--warning-foreground);
}
.hv-chip--primary.hv-chip--danger {
--chip-bg: var(--danger);
--chip-fg: var(--danger-foreground);
}
/* Soft variants - muted backgrounds */
.hv-chip--accent.hv-chip--soft {
--chip-bg: var(--accent-soft);
--chip-fg: var(--accent-soft-foreground);
}
.hv-chip--success.hv-chip--soft {
--chip-bg: var(--success-soft);
--chip-fg: var(--success-soft-foreground);
}
.hv-chip--warning.hv-chip--soft {
--chip-bg: var(--warning-soft);
--chip-fg: var(--warning-soft-foreground);
}
.hv-chip--danger.hv-chip--soft {
--chip-bg: var(--danger-soft);
--chip-fg: var(--danger-soft-foreground);
}
.hv-chip--default.hv-chip--soft {
--chip-bg: var(--default-soft);
--chip-fg: var(--default-soft-foreground);
}
+30
View File
@@ -0,0 +1,30 @@
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix. */
/* Base styles */
.hv-kbd {
@apply inline-flex h-6 items-center space-x-0.5 rounded-lg bg-default px-2 text-center font-sans text-sm font-medium whitespace-nowrap text-muted rtl:space-x-reverse;
word-spacing: -0.25rem;
}
/* Key abbreviation styles */
.hv-kbd__abbr {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
text-decoration: none;
}
/* Content wrapper */
.hv-kbd__content {
display: flex;
justify-content: center;
align-items: center;
}
/* Variant styles */
.hv-kbd--light {
@apply bg-transparent;
}
+32
View File
@@ -0,0 +1,32 @@
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix. */
/* Base separator with default horizontal orientation */
.hv-separator {
@apply shrink-0 rounded-sm border-t-0 border-b-0 bg-separator;
/* Default size - horizontal orientation */
@apply h-px w-full;
}
/* Orientation variants */
.hv-separator--horizontal {
@apply h-px w-full;
}
.hv-separator--vertical {
@apply h-auto min-h-2 w-px self-stretch;
}
/* Color variants */
.hv-separator--default {
@apply bg-separator;
}
.hv-separator--secondary {
@apply bg-separator-secondary;
}
.hv-separator--tertiary {
@apply bg-separator-tertiary;
}
+3
View File
@@ -20,6 +20,9 @@
@import "./components/avatar.css" layer(components);
@import "./components/avatar-group.css" layer(components);
@import "./components/skeleton.css" layer(components);
@import "./components/separator.css" layer(components);
@import "./components/kbd.css" layer(components);
@import "./components/chip.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 {
+98
View File
@@ -295,3 +295,101 @@ export const skeletonVariants = tv({
});
export type SkeletonVariants = VariantProps<typeof skeletonVariants>;
export const separatorVariants = tv({
base: "hv-separator",
defaultVariants: {
orientation: "horizontal",
variant: "default",
},
variants: {
orientation: {
horizontal: "hv-separator--horizontal",
vertical: "hv-separator--vertical",
},
variant: {
default: "hv-separator--default",
secondary: "hv-separator--secondary",
tertiary: "hv-separator--tertiary",
},
},
});
export type SeparatorVariants = VariantProps<typeof separatorVariants>;
export const kbdVariants = tv({
defaultVariants: {
variant: "default",
},
slots: {
abbr: "hv-kbd__abbr",
base: "hv-kbd",
content: "hv-kbd__content",
},
variants: {
variant: {
default: "hv-kbd--default",
light: "hv-kbd--light",
},
},
});
export type KbdVariants = VariantProps<typeof kbdVariants>;
export const chipVariants = tv({
defaultVariants: {
color: "default",
size: "md",
variant: "secondary",
},
slots: {
base: "hv-chip",
label: "hv-chip__label",
},
variants: {
color: {
accent: {
base: "hv-chip--accent",
},
danger: {
base: "hv-chip--danger",
},
default: {
base: "hv-chip--default",
},
success: {
base: "hv-chip--success",
},
warning: {
base: "hv-chip--warning",
},
},
size: {
lg: {
base: "hv-chip--lg",
},
md: {
base: "hv-chip--md",
},
sm: {
base: "hv-chip--sm",
},
},
variant: {
primary: {
base: "hv-chip--primary",
},
secondary: {
base: "hv-chip--secondary",
},
soft: {
base: "hv-chip--soft",
},
tertiary: {
base: "hv-chip--tertiary",
},
},
},
});
export type ChipVariants = VariantProps<typeof chipVariants>;
+124
View File
@@ -0,0 +1,124 @@
<script lang="ts">
import {
Comment,
Fragment,
Text,
computed,
defineComponent,
h,
isVNode,
provide,
useAttrs,
type PropType,
type VNode,
type VNodeChild,
} from "vue";
import { chipVariants, type ChipVariants } from "@heroui-vue/styles";
import { chipContextKey } from "./context";
import ChipLabel from "./ChipLabel.vue";
function flattenChildren(
nodes: VNodeChild[],
out: VNodeChild[] = [],
): VNodeChild[] {
for (const child of nodes) {
if (Array.isArray(child)) {
flattenChildren(child, out);
continue;
}
if (
isVNode(child) &&
child.type === Fragment &&
Array.isArray(child.children)
) {
flattenChildren(child.children as VNodeChild[], out);
continue;
}
out.push(child);
}
return out;
}
function isIgnorable(node: VNodeChild): boolean {
if (node == null || node === false || node === true) return true;
if (typeof node === "string") return node.trim() === "";
if (typeof node === "number") return false;
if (!isVNode(node)) return true;
if (node.type === Comment) return true;
if (node.type === Text && String(node.children ?? "").trim() === "")
return true;
return false;
}
/** True when the slot is only plain text/number content (auto-wrapped in ChipLabel). */
function isPlainTextSlot(nodes: VNodeChild[]): boolean {
const meaningful = flattenChildren(nodes).filter(
(node) => !isIgnorable(node),
);
if (meaningful.length === 0) return false;
return meaningful.every((node) => {
if (typeof node === "number" || typeof node === "string") return true;
return isVNode(node) && node.type === Text;
});
}
export default defineComponent({
name: "Chip",
inheritAttrs: false,
props: {
color: {
type: String as PropType<ChipVariants["color"]>,
default: "default",
},
size: {
type: String as PropType<ChipVariants["size"]>,
default: "md",
},
variant: {
type: String as PropType<ChipVariants["variant"]>,
default: "secondary",
},
},
setup(props, { attrs, slots }) {
const variantSlots = computed(() =>
chipVariants({
color: props.color,
size: props.size,
variant: props.variant,
}),
);
provide(chipContextKey, { slots: variantSlots });
/**
* Re-read the default slot on every render so text ↔ composed content
* switches and parent slot-swap updates stay in sync. Slot-function
* identity is not a reactive dependency.
*/
function render() {
const children = (slots.default?.() ?? []) as VNodeChild[];
const content: VNodeChild[] = isPlainTextSlot(children)
? [
h(
ChipLabel,
{ key: "chip-auto-label" },
{ default: () => children },
),
]
: children;
return h(
"span",
{
...attrs,
class: [variantSlots.value.base(), attrs.class],
"data-slot": "chip",
},
content,
);
}
return render;
},
});
</script>
@@ -0,0 +1,15 @@
<script setup lang="ts">
import { computed, inject, useAttrs } from "vue";
import { chipContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(chipContextKey, undefined);
</script>
<template>
<span v-bind="attrs" :class="ctx?.slots.value.label()" data-slot="chip-label">
<slot />
</span>
</template>
@@ -0,0 +1,11 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { chipVariants } from "@heroui-vue/styles";
export type ChipSlots = ReturnType<typeof chipVariants>;
export interface ChipContextValue {
slots: ComputedRef<ChipSlots>;
}
export const chipContextKey: InjectionKey<ChipContextValue> =
Symbol("HeroUIVue.Chip");
+26
View File
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { computed, provide, useAttrs } from "vue";
import { kbdVariants, type KbdVariants } from "@heroui-vue/styles";
import { kbdContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
variant?: KbdVariants["variant"];
}>(),
{ variant: "default" },
);
const attrs = useAttrs();
const slots = computed(() => kbdVariants({ variant: props.variant }));
provide(kbdContextKey, { slots });
</script>
<template>
<kbd v-bind="attrs" :class="slots.base()">
<slot />
</kbd>
</template>
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { kbdContextKey } from "./context";
import { kbdKeysLabelMap, kbdKeysMap, type KbdKey } from "./constants";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
/** The keyboard key to display. */
keyValue: KbdKey;
}>();
const attrs = useAttrs();
const ctx = inject(kbdContextKey, undefined);
</script>
<template>
<abbr
v-bind="attrs"
:class="ctx?.slots.value.abbr()"
:title="kbdKeysLabelMap[props.keyValue]"
>
{{ kbdKeysMap[props.keyValue] }}
</abbr>
</template>
@@ -0,0 +1,15 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { kbdContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(kbdContextKey, undefined);
</script>
<template>
<span v-bind="attrs" :class="ctx?.slots.value.content()">
<slot />
</span>
</template>
@@ -0,0 +1,76 @@
/** Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. */
export type KbdKey =
| "command"
| "shift"
| "ctrl"
| "option"
| "enter"
| "delete"
| "escape"
| "tab"
| "capslock"
| "up"
| "right"
| "down"
| "left"
| "pageup"
| "pagedown"
| "home"
| "end"
| "help"
| "space"
| "fn"
| "win"
| "alt";
export const kbdKeysMap: Record<KbdKey, string> = {
command: "⌘",
shift: "⇧",
ctrl: "⌃",
option: "⌥",
enter: "↵",
delete: "⌫",
escape: "⎋",
tab: "⇥",
capslock: "⇪",
up: "↑",
right: "→",
down: "↓",
left: "←",
pageup: "⇞",
pagedown: "⇟",
home: "↖",
end: "↘",
help: "?",
space: "␣",
fn: "Fn",
win: "⌘",
alt: "⌥",
};
export const kbdKeysLabelMap: Record<KbdKey, string> = {
command: "Command",
shift: "Shift",
ctrl: "Control",
option: "Option",
enter: "Enter",
delete: "Delete",
escape: "Escape",
tab: "Tab",
capslock: "Caps Lock",
up: "Up",
right: "Right",
down: "Down",
left: "Left",
pageup: "Page Up",
pagedown: "Page Down",
home: "Home",
end: "End",
help: "Help",
space: "Space",
fn: "Fn",
win: "Win",
alt: "Alt",
};
@@ -0,0 +1,11 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { KbdVariants, kbdVariants } from "@heroui-vue/styles";
export type KbdSlots = ReturnType<typeof kbdVariants>;
export interface KbdContextValue {
slots: ComputedRef<KbdSlots>;
}
export const kbdContextKey: InjectionKey<KbdContextValue> =
Symbol("HeroUIVue.Kbd");
@@ -0,0 +1,50 @@
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import { Separator as SeparatorPrimitive } from "reka-ui";
import { separatorVariants, type SeparatorVariants } from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
orientation?: SeparatorVariants["orientation"];
variant?: SeparatorVariants["variant"];
/** When true, the separator is decorative (role="none"). */
decorative?: boolean;
/** Render the root as a custom element instead of `hr`. */
as?: string;
/** Merge separator props onto the single default-slot element. */
asChild?: boolean;
}>(),
{
orientation: "horizontal",
variant: "default",
decorative: false,
as: "hr",
asChild: false,
},
);
const attrs = useAttrs();
const baseClass = computed(() =>
separatorVariants({
orientation: props.orientation,
variant: props.variant,
}),
);
</script>
<template>
<SeparatorPrimitive
v-bind="attrs"
:as="as"
:as-child="asChild"
:orientation="orientation"
:decorative="decorative"
:class="baseClass"
data-slot="separator"
>
<slot />
</SeparatorPrimitive>
</template>
+34
View File
@@ -26,6 +26,9 @@ export {
avatarVariants,
avatarGroupVariants,
skeletonVariants,
separatorVariants,
kbdVariants,
chipVariants,
type ButtonVariants,
type CardVariants,
type InputVariants,
@@ -38,6 +41,9 @@ export {
type AvatarVariants,
type AvatarGroupVariants,
type SkeletonVariants,
type SeparatorVariants,
type KbdVariants,
type ChipVariants,
} from "@heroui-vue/styles";
export type {
ButtonProps,
@@ -58,6 +64,12 @@ export type {
AvatarGroupProps,
AvatarGroupCountProps,
SkeletonProps,
SeparatorProps,
KbdProps,
KbdAbbrProps,
KbdContentProps,
ChipProps,
ChipLabelProps,
} from "./types";
export { tv, cn, type VariantProps } from "@heroui-vue/styles";
@@ -91,3 +103,25 @@ export {
type AvatarSlots,
} from "./components/avatar/context";
export { default as Skeleton } from "./components/skeleton/Skeleton.vue";
export { default as Separator } from "./components/separator/Separator.vue";
export { default as Kbd } from "./components/kbd/Kbd.vue";
export { default as KbdAbbr } from "./components/kbd/KbdAbbr.vue";
export { default as KbdContent } from "./components/kbd/KbdContent.vue";
export {
kbdKeysMap,
kbdKeysLabelMap,
type KbdKey,
} from "./components/kbd/constants";
export {
kbdContextKey,
type KbdContextValue,
type KbdSlots,
} from "./components/kbd/context";
export { default as Chip } from "./components/chip/Chip.vue";
export { default as ChipLabel } from "./components/chip/ChipLabel.vue";
export {
chipContextKey,
type ChipContextValue,
type ChipSlots,
} from "./components/chip/context";
+14
View File
@@ -38,3 +38,17 @@ import type SwitchGroup from "./components/switch/SwitchGroup.vue";
import type Label from "./components/label/Label.vue";
export type SwitchGroupProps = InstanceType<typeof SwitchGroup>["$props"];
export type LabelProps = InstanceType<typeof Label>["$props"];
import type Separator from "./components/separator/Separator.vue";
import type Kbd from "./components/kbd/Kbd.vue";
import type KbdAbbr from "./components/kbd/KbdAbbr.vue";
import type KbdContent from "./components/kbd/KbdContent.vue";
import type Chip from "./components/chip/Chip.vue";
import type ChipLabel from "./components/chip/ChipLabel.vue";
export type SeparatorProps = InstanceType<typeof Separator>["$props"];
export type KbdProps = InstanceType<typeof Kbd>["$props"];
export type KbdAbbrProps = InstanceType<typeof KbdAbbr>["$props"];
export type KbdContentProps = InstanceType<typeof KbdContent>["$props"];
export type ChipProps = InstanceType<typeof Chip>["$props"];
export type ChipLabelProps = InstanceType<typeof ChipLabel>["$props"];