feat: add Separator Kbd and Chip components
This commit is contained in:
@@ -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");
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user