feat: migrate Pagination Header and InputOTP with field and event regressions

This commit is contained in:
maoyingjie
2026-10-23 13:49:44 +08:00
parent b2525bb04d
commit 1fa2929a24
72 changed files with 5784 additions and 16 deletions
+9
View File
@@ -0,0 +1,9 @@
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix. */
/* ==========================================================================
Base header styles (collection / group section heading)
========================================================================== */
.hv-header {
@apply w-full px-2 pt-1.5 pb-1 text-start text-xs font-medium text-muted;
}
+135
View File
@@ -0,0 +1,135 @@
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix; Vue state adapters. */
/* ==========================================================================
InputOTP - one-time password input
========================================================================== */
/* Base container */
.hv-input-otp {
@apply relative flex w-full items-center gap-2;
&[data-disabled="true"] {
@apply cursor-not-allowed opacity-50;
}
}
/* Group - slots separated with gap */
.hv-input-otp__group {
@apply flex min-w-0 items-center gap-2;
}
/* Individual slot box - fully rounded, standalone */
.hv-input-otp__slot {
@apply relative flex h-10 w-9.5 min-w-0 flex-1 items-center justify-center;
@apply border bg-field text-field-foreground shadow-field;
@apply rounded-field text-sm font-semibold outline-none;
border-width: var(--border-width-field);
border-color: var(--field-border);
/**
* 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 */
@media (hover: hover) {
&:hover,
&[data-hovered="true"] {
@apply bg-field-hover;
border-color: var(--field-border-hover);
}
}
/* Active/Focus */
&[data-active="true"] {
@apply z-10 bg-field-focus status-focused-field;
}
/* Filled */
&[data-filled="true"] {
@apply bg-field-focus;
}
/* Disabled */
&[data-disabled="true"] {
@apply status-disabled;
}
/* Invalid */
&[data-invalid="true"] {
@apply status-invalid-field;
background-color: var(--field-focus);
}
}
/* Real inputs sit transparently over the visual slot */
.hv-input-otp__input {
@apply absolute inset-0 z-10 h-full w-full cursor-text bg-transparent text-transparent caret-transparent outline-none;
}
/* Slot value */
.hv-input-otp__slot-value {
@apply text-lg leading-6 tracking-[-0.27px];
/**
* Animations
* CRITICAL: motion-reduce must be AFTER animation for correct override specificity
*/
animation: hv-slot-value-in 250ms var(--ease-smooth) both;
transform-origin: bottom center;
@apply motion-reduce:animate-none;
}
/* Caret */
.hv-input-otp__caret {
@apply absolute h-4 w-[2px] animate-caret-blink rounded-sm bg-field-placeholder;
}
/* Separator */
.hv-input-otp__separator {
@apply h-[2px] w-[6px] shrink-0 rounded-sm bg-separator;
}
/* Variant property definitions */
.hv-input-otp--primary {
/* Default styles */
}
.hv-input-otp--secondary .hv-input-otp__slot {
@apply shadow-none;
background-color: var(--input-otp-slot-bg);
--input-otp-slot-bg: var(--default);
--input-otp-slot-bg-hover: var(--default-hover);
--input-otp-slot-bg-focus: var(--default);
@media (hover: hover) {
&:hover,
&[data-hovered="true"] {
background-color: var(--input-otp-slot-bg-hover);
}
}
&[data-active="true"],
&[data-filled="true"] {
background-color: var(--input-otp-slot-bg-focus);
}
}
/* Animations */
@keyframes hv-slot-value-in {
0% {
opacity: 0;
transform: translateY(8px) scale(0.8);
}
100% {
opacity: 1;
transform: translateY(0) scale(1);
}
}
+154
View File
@@ -0,0 +1,154 @@
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix. */
/* ==========================================================================
Pagination - Page navigation component
========================================================================== */
/* Base styles */
.hv-pagination {
@apply flex w-full flex-col items-center justify-between gap-4 sm:flex-row;
}
/* Summary - left side info container */
.hv-pagination__summary {
@apply flex items-center gap-2 self-start text-sm text-muted sm:self-center;
}
/* Content - container for pagination items */
.hv-pagination__content {
@apply flex items-center gap-1 self-start sm:self-center;
}
/* Item - list item wrapper */
.hv-pagination__item {
@apply inline-flex;
}
/* Link - page number button (ghost button style) */
.hv-pagination__link {
@apply relative isolate inline-flex size-9 origin-center items-center justify-center rounded-3xl text-sm font-medium whitespace-nowrap outline-none select-none no-highlight md:size-8;
cursor: var(--cursor-interactive);
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition:
transform 250ms var(--ease-smooth),
background-color 100ms var(--ease-out),
box-shadow 100ms var(--ease-out);
@apply transform-gpu motion-reduce:transition-none;
/* Ghost button tokens */
--pagination-link-bg: transparent;
--pagination-link-bg-hover: var(--default-hover);
--pagination-link-bg-pressed: var(--default-hover);
--pagination-link-fg: var(--default-foreground);
background-color: var(--pagination-link-bg);
color: var(--pagination-link-fg);
/* Focus state */
&:focus-visible,
&[data-focus-visible="true"] {
@apply status-focused;
}
/* Disabled state */
&:disabled,
&[aria-disabled="true"] {
@apply status-disabled;
}
/* Hover state */
@media (hover: hover) {
&:hover,
&[data-hovered="true"] {
background-color: var(--pagination-link-bg-hover);
}
}
/* Active / pressed */
&:active,
&[data-pressed="true"] {
background-color: var(--pagination-link-bg-pressed);
transform: scale(0.97);
}
/* Active page - tertiary button style */
&[data-active="true"] {
--pagination-link-bg: var(--default);
--pagination-link-bg-hover: var(--default-hover);
--pagination-link-bg-pressed: var(--default-hover);
}
}
/* Ellipsis */
.hv-pagination__ellipsis {
@apply inline-flex size-9 items-center justify-center text-sm text-muted select-none md:size-8;
}
/* Navigation buttons (Previous / Next) */
.hv-pagination__link--nav {
@apply w-auto gap-1.5 px-2.5;
& [data-slot="pagination-previous-icon"],
& [data-slot="pagination-next-icon"] {
@apply rtl:rotate-180;
}
}
/* ==========================================================================
Size variants
========================================================================== */
.hv-pagination--sm {
.hv-pagination__link {
@apply size-8 text-xs md:size-7;
&:active,
&[data-pressed="true"] {
transform: scale(0.98);
}
}
.hv-pagination__link--nav {
@apply w-auto px-2;
}
.hv-pagination__ellipsis {
@apply size-8 text-xs md:size-7;
}
.hv-pagination__summary {
@apply text-xs;
}
}
.hv-pagination--md {
/* No styles as this is the default size */
}
.hv-pagination--lg {
.hv-pagination__link {
@apply size-10 text-base md:size-9;
&:active,
&[data-pressed="true"] {
transform: scale(0.96);
}
}
.hv-pagination__link--nav {
@apply w-auto px-3;
}
.hv-pagination__ellipsis {
@apply size-10 text-base md:size-9;
}
.hv-pagination__summary {
@apply text-base;
}
}
+3
View File
@@ -134,3 +134,6 @@
@import "./components/toggle-button.css" layer(components);
@import "./components/toggle-button-group.css" layer(components);
@import "./components/toolbar.css" layer(components);
@import "./components/pagination.css" layer(components);
@import "./components/header.css" layer(components);
@import "./components/input-otp.css" layer(components);
+62
View File
@@ -1167,3 +1167,65 @@ export const toolbarVariants = tv({
});
export type ToolbarVariants = VariantProps<typeof toolbarVariants>;
export const paginationVariants = tv({
defaultVariants: {
size: "md",
},
slots: {
base: "hv-pagination",
content: "hv-pagination__content",
ellipsis: "hv-pagination__ellipsis",
item: "hv-pagination__item",
link: "hv-pagination__link",
summary: "hv-pagination__summary",
},
variants: {
size: {
lg: {
base: "hv-pagination--lg",
},
md: {
base: "hv-pagination--md",
},
sm: {
base: "hv-pagination--sm",
},
},
},
});
export type PaginationVariants = VariantProps<typeof paginationVariants>;
export const headerVariants = tv({
base: "hv-header",
});
export type HeaderVariants = VariantProps<typeof headerVariants>;
export const inputOTPVariants = tv({
defaultVariants: {
variant: "primary",
},
slots: {
base: "hv-input-otp",
caret: "hv-input-otp__caret",
group: "hv-input-otp__group",
input: "hv-input-otp__input",
separator: "hv-input-otp__separator",
slot: "hv-input-otp__slot",
slotValue: "hv-input-otp__slot-value",
},
variants: {
variant: {
primary: {
base: "hv-input-otp--primary",
},
secondary: {
base: "hv-input-otp--secondary",
},
},
},
});
export type InputOTPVariants = VariantProps<typeof inputOTPVariants>;
@@ -0,0 +1,38 @@
<script lang="ts">
import { defineComponent, h, useAttrs } from "vue";
import { headerVariants } from "@heroui-vue/styles";
/**
* Collection / group section heading (RAC Header).
* Not an application top bar with logo or navigation.
* Renders a native `<header>` by default — matching the fixed RAC contract.
*/
export default defineComponent({
name: "HeaderRoot",
inheritAttrs: false,
props: {
/** Render the root as a custom element instead of `header`. */
as: {
type: String,
default: "header",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
function render() {
return h(
props.as,
{
...attrs,
class: [headerVariants(), attrs.class],
"data-slot": "header",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,33 @@
<script lang="ts">
import { defineComponent, h, inject, useAttrs } from "vue";
import { inputOTPContextKey } from "./context";
export default defineComponent({
name: "InputOTPGroup",
inheritAttrs: false,
props: {
as: {
type: String,
default: "div",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(inputOTPContextKey, null);
function render() {
return h(
props.as,
{
...attrs,
class: [context?.slots.value.group(), attrs.class],
"data-slot": "input-otp-group",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,439 @@
<script lang="ts">
import {
cloneVNode,
Fragment,
isVNode,
computed,
defineComponent,
h,
inject,
nextTick,
onBeforeUnmount,
onMounted,
onUpdated,
provide,
ref,
shallowRef,
useAttrs,
useId,
watch,
type PropType,
type VNode,
type VNodeChild,
} from "vue";
import { PinInputRoot } from "reka-ui";
import { inputOTPVariants, type InputOTPVariants } from "@heroui-vue/styles";
import { useInheritedDisabled } from "../field/disabled";
import {
errorMessages,
fieldContextKey,
formContextKey,
mergeIds,
type FieldContext,
} from "../field/context";
import {
charsFromString,
inputOTPContextKey,
stringFromChars,
} from "./context";
const EMPTY_ERRORS: string[] = [];
function attrBoolean(value: unknown): boolean {
return value != null && value !== false;
}
/** Match one character without leaking lastIndex on global/sticky regexes. */
function charAllowed(re: RegExp, char: string): boolean {
if (!char) return true;
const flags = re.flags.replace(/[gy]/g, "");
return new RegExp(re.source, flags).test(char);
}
export default defineComponent({
name: "InputOTPRoot",
inheritAttrs: false,
props: {
/** Public string value; character at index i occupies slot i. */
modelValue: {
type: String,
default: undefined,
},
defaultValue: {
type: String,
default: "",
},
maxLength: {
type: Number,
required: true,
},
variant: {
type: String as PropType<InputOTPVariants["variant"]>,
default: "primary",
},
isDisabled: {
type: Boolean,
default: false,
},
isReadOnly: {
type: Boolean,
default: false,
},
isInvalid: {
type: Boolean,
default: false,
},
validationErrors: {
type: Array as PropType<string[]>,
default: undefined,
},
/** Class applied to the real PinInput inputs (merged with structure class). */
inputClassName: {
type: String,
default: undefined,
},
name: {
type: String,
default: undefined,
},
required: {
type: Boolean,
default: false,
},
mask: {
type: Boolean,
default: false,
},
/** Enables one-time-code autofill attributes on the real inputs. */
otp: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: "",
},
/** Character filter, e.g. REGEXP_ONLY_DIGITS. */
pattern: {
type: [String, RegExp] as PropType<string | RegExp>,
default: undefined,
},
id: {
type: String,
default: undefined,
},
},
emits: ["update:modelValue", "change", "complete"],
setup(props, { emit, slots }) {
const attrs = useAttrs();
const autoId = useId();
const form = inject(formContextKey, undefined);
const length = computed(() => Math.max(0, props.maxLength | 0));
const pattern = computed<RegExp | null>(() => {
if (!props.pattern) return null;
return typeof props.pattern === "string"
? new RegExp(props.pattern)
: props.pattern;
});
const isControlled = computed(() => props.modelValue !== undefined);
const initial = props.defaultValue ?? "";
const startPublic = isControlled.value ? (props.modelValue ?? "") : initial;
/** Index-preserving slot characters; "" keeps a middle empty slot. */
const slotChars = ref<string[]>(charsFromString(startPublic, length.value));
/** Last string we emitted; parent echo keeps holes in slotChars. */
let lastEmitted = startPublic;
const disabled = useInheritedDisabled(
() => props.isDisabled || attrBoolean(attrs.disabled),
);
const readOnly = computed(
() =>
props.isReadOnly ||
attrBoolean(attrs.readonly) ||
attrBoolean(attrs.readOnly),
);
const descriptionIds = ref<string[]>([]);
const errorIds = ref<string[]>([]);
const labelIds = ref<string[]>([]);
const plannedLabels = ref<string[]>([]);
const labelledBy = computed(
() =>
(attrs["aria-labelledby"] as string | undefined) ??
(attrs["aria-label"]
? undefined
: mergeIds(plannedLabels.value, labelIds.value)),
);
const accessibleLabel = computed(
() => attrs["aria-label"] as string | undefined,
);
// shallowRef keeps object identity for response comparison (see useTextField).
const dismissed = shallowRef<object | undefined>();
const serverErrors = computed(() => {
const source = form?.validationErrors.value;
return source && source !== dismissed.value && props.name
? errorMessages(source[props.name])
: EMPTY_ERRORS;
});
const invalid = computed(
() =>
props.isInvalid ||
(props.validationErrors?.length ?? 0) > 0 ||
serverErrors.value.length > 0 ||
attrs["aria-invalid"] === true ||
attrs["aria-invalid"] === "true",
);
const errors = computed(() => [
...(props.validationErrors ?? EMPTY_ERRORS),
...serverErrors.value,
]);
const resolvedId = computed(() => props.id ?? autoId);
/** Stable id per real focusable input; Label `for` uses index 0. */
function inputId(index: number): string {
return `${resolvedId.value}-slot-${index}`;
}
provide(fieldContextKey, {
controlId: computed(() => inputId(0)),
kind: "text",
disabled,
readOnly,
required: computed(() => props.required),
invalid,
errors,
labelIds,
descriptionIds,
errorIds,
} satisfies FieldContext);
/**
* Display/input characters. When the parent echoes our emission the
* hole-preserving array is kept so middle deletes do not left-shift.
*/
const chars = computed(() => [...slotChars.value]);
watch(
() => props.modelValue,
(next, prev) => {
if (!isControlled.value) return;
if (next === prev) return;
const incoming = next ?? "";
// Echo of our own proposal — keep the index-preserving array.
if (incoming === lastEmitted) return;
lastEmitted = incoming;
slotChars.value = charsFromString(incoming, length.value);
},
);
watch(
() => length.value,
(nextLen) => {
// Pad/crop by index — do not serialize, or empty slots left-shift.
const prev = slotChars.value;
if (nextLen > prev.length) {
slotChars.value = [
...prev,
...Array.from({ length: nextLen - prev.length }, () => ""),
];
} else if (nextLen < prev.length) {
slotChars.value = prev.slice(0, nextLen);
}
const proposed = stringFromChars(slotChars.value);
const currentPublic = isControlled.value
? (props.modelValue ?? "")
: lastEmitted;
if (proposed !== currentPublic) {
lastEmitted = proposed;
emit("update:modelValue", proposed);
emit("change", proposed);
}
},
);
function applyChars(next: unknown[]) {
if (disabled.value || readOnly.value) return;
const clamped = Array.from({ length: length.value }, (_, i) => {
const raw = next[i];
return raw == null ? "" : String(raw);
});
const re = pattern.value;
if (re) {
for (let i = 0; i < clamped.length; i++) {
const char = clamped[i];
if (char && !charAllowed(re, char)) clamped[i] = "";
}
}
const previous = stringFromChars(slotChars.value);
const prevFilled = slotChars.value.filter(Boolean).length;
const nextFilled = clamped.filter(Boolean).length;
slotChars.value = clamped;
const proposed = stringFromChars(clamped);
const currentPublic = isControlled.value
? (props.modelValue ?? "")
: previous;
// Always request a change when the proposal differs from the public
// value so a rejected update can be retried with the same characters.
if (proposed !== currentPublic) {
lastEmitted = proposed;
// Effective edit dismisses the current server-error response.
dismissed.value = form?.validationErrors.value;
emit("update:modelValue", proposed);
emit("change", proposed);
void nextTick(() => {
if (!isControlled.value) return;
const publicValue = props.modelValue ?? "";
if (publicValue !== lastEmitted) {
// Parent rejected the proposal — restore from the public value.
slotChars.value = charsFromString(publicValue, length.value);
lastEmitted = publicValue;
}
});
}
if (
prevFilled < length.value &&
nextFilled === length.value &&
length.value > 0
) {
emit("complete", proposed);
}
}
function handlePinUpdate(value: unknown) {
applyChars(Array.isArray(value) ? value : []);
}
const describedBy = computed(() =>
mergeIds(
attrs["aria-describedby"] as string | undefined,
descriptionIds.value,
invalid.value ? errorIds.value : [],
),
);
function reset(event: Event) {
void nextTick(() => {
if (event.defaultPrevented) return;
if (!isControlled.value) {
slotChars.value = charsFromString(initial, length.value);
lastEmitted = initial;
} else {
const incoming = props.modelValue ?? "";
lastEmitted = incoming;
slotChars.value = charsFromString(incoming, length.value);
}
dismissed.value = form?.validationErrors.value;
});
}
const rootEl = ref<HTMLElement | null>(null);
function setRoot(el: unknown) {
const node = (el as { $el?: HTMLElement } | HTMLElement | null) ?? null;
const next = !node
? null
: node instanceof HTMLElement
? node
: ((node.$el as HTMLElement) ?? null);
rootEl.value = next;
}
let formEl: HTMLFormElement | null = null;
function bindForm() {
const next = rootEl.value?.closest("form") ?? null;
if (next === formEl) return;
formEl?.removeEventListener("reset", reset);
formEl = next;
formEl?.addEventListener("reset", reset);
}
onMounted(bindForm);
onUpdated(bindForm);
onBeforeUnmount(() => {
formEl?.removeEventListener("reset", reset);
formEl = null;
});
const variantSlots = computed(() =>
inputOTPVariants({ variant: props.variant }),
);
provide(inputOTPContextKey, {
slots: variantSlots,
isDisabled: disabled,
isInvalid: invalid,
isReadOnly: readOnly,
maxLength: length,
pattern,
chars,
mask: computed(() => props.mask),
inputClassName: computed(() => props.inputClassName),
describedBy,
inputId,
labelledBy,
accessibleLabel,
});
function prepareLabels(
nodes: VNodeChild[],
ids: string[],
path = "",
): VNodeChild[] {
return nodes.map((node, index) => {
if (!isVNode(node)) return node;
const type = node.type as { name?: string; __name?: string };
if ((type.name ?? type.__name) === "Label") {
const id = String(
node.props?.id ?? `${autoId}-label${path}-${index}`,
);
ids.push(id);
return cloneVNode(node, { id });
}
if (
(typeof node.type === "string" || node.type === Fragment) &&
Array.isArray(node.children)
) {
const copy = cloneVNode(node);
copy.children = prepareLabels(
node.children as VNodeChild[],
ids,
`${path}-${index}`,
) as VNode[];
return copy;
}
return node;
});
}
function render() {
const labels: string[] = [];
const children = prepareLabels(slots.default?.() ?? [], labels);
if (JSON.stringify(labels) !== JSON.stringify(plannedLabels.value))
plannedLabels.value = labels;
return h(
PinInputRoot,
{
ref: setRoot,
modelValue: chars.value,
disabled: disabled.value,
mask: props.mask,
otp: props.otp,
placeholder: props.placeholder,
name: props.name,
required: props.required,
id: resolvedId.value,
"onUpdate:modelValue": handlePinUpdate,
...attrs,
class: [variantSlots.value.base(), attrs.class],
"data-disabled": disabled.value || undefined,
"data-invalid": invalid.value ? "true" : undefined,
"data-slot": "input-otp",
},
{ default: () => children },
);
}
return render;
},
});
</script>
@@ -0,0 +1,33 @@
<script lang="ts">
import { defineComponent, h, inject, useAttrs } from "vue";
import { inputOTPContextKey } from "./context";
export default defineComponent({
name: "InputOTPSeparator",
inheritAttrs: false,
props: {
as: {
type: String,
default: "div",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(inputOTPContextKey, null);
function render() {
return h(
props.as,
{
...attrs,
class: [context?.slots.value.separator(), attrs.class],
"data-slot": "input-otp-separator",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,189 @@
<script lang="ts">
import { computed, defineComponent, h, inject, ref, useAttrs } from "vue";
import { PinInputInput } from "reka-ui";
import { inputOTPContextKey } from "./context";
type Handler = (event: Event) => void;
function toHandlerArray(value: unknown): Handler[] {
if (!value) return [];
if (Array.isArray(value)) {
return value.filter((item): item is Handler => typeof item === "function");
}
return typeof value === "function" ? [value as Handler] : [];
}
export default defineComponent({
name: "InputOTPSlot",
inheritAttrs: false,
props: {
index: {
type: Number,
required: true,
},
as: {
type: String,
default: "div",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(inputOTPContextKey, null);
const focused = ref(false);
const char = computed(() => context?.chars.value[props.index] ?? "");
const isDisabled = computed(() => Boolean(context?.isDisabled.value));
const isInvalid = computed(() => Boolean(context?.isInvalid.value));
const isReadOnly = computed(() => Boolean(context?.isReadOnly.value));
const isFilled = computed(() => char.value.length > 0);
const isActive = computed(() => focused.value && !isDisabled.value);
const mask = computed(() => Boolean(context?.mask.value));
const displayChar = computed(() => (mask.value ? "•" : char.value));
function onInputFocus() {
focused.value = true;
}
function onInputBlur() {
focused.value = false;
}
/** Block edit keys before Reka handlers when readonly/disabled. */
function guardEdit(event: Event) {
if (!isReadOnly.value && !isDisabled.value) return;
const key = (event as KeyboardEvent).key;
if (
key === "Tab" ||
key === "Escape" ||
key === "ArrowLeft" ||
key === "ArrowRight" ||
key === "ArrowUp" ||
key === "ArrowDown" ||
key === "Home" ||
key === "End"
) {
return;
}
event.preventDefault();
event.stopImmediatePropagation();
}
function guardPaste(event: Event) {
if (!isReadOnly.value && !isDisabled.value) return;
event.preventDefault();
event.stopImmediatePropagation();
}
/** After user handlers: preventDefault means cancel the edit. */
function stopIfPrevented(event: Event) {
if (event.defaultPrevented) event.stopImmediatePropagation();
}
function render() {
const slotClass = context?.slots.value.slot();
const valueClass = context?.slots.value.slotValue();
const caretClass = context?.slots.value.caret();
const inputClass = [
context?.slots.value.input(),
context?.inputClassName.value,
];
const {
class: cls,
onKeydown: userKeydown,
onKeydownCapture: userKeydownCapture,
onPaste: userPaste,
onPasteCapture: userPasteCapture,
...rest
} = attrs as Record<string, unknown>;
// Vue native handler arrays honor stopImmediatePropagation between
// items. Bubble-phase keydown/paste are promoted into the capture
// list so user preventDefault can cancel Reka's edit handlers.
const keydownCapture: Handler[] = [
guardEdit,
...toHandlerArray(userKeydownCapture),
...toHandlerArray(userKeydown),
stopIfPrevented,
];
const pasteCapture: Handler[] = [
guardPaste,
...toHandlerArray(userPasteCapture),
...toHandlerArray(userPaste),
stopIfPrevented,
];
return h(
props.as,
{
...rest,
class: [slotClass, cls],
onKeydownCapture: keydownCapture,
onPasteCapture: pasteCapture,
"data-active": isActive.value || undefined,
"data-disabled": isDisabled.value || undefined,
"data-filled": isFilled.value || undefined,
"data-invalid": isInvalid.value || undefined,
"data-readonly": isReadOnly.value || undefined,
"data-slot": "input-otp-slot",
},
[
isFilled.value
? h(
"div",
{
class: valueClass,
"data-slot": "input-otp-slot-value",
},
displayChar.value,
)
: null,
isActive.value
? h("div", {
class: caretClass,
"data-slot": "input-otp-caret",
})
: null,
h(
PinInputInput,
{
asChild: true,
index: props.index,
disabled: isDisabled.value,
readonly: isReadOnly.value || undefined,
class: inputClass,
id: context?.inputId(props.index),
"data-slot": "input-otp-input",
autocomplete: "one-time-code",
"aria-describedby": context?.describedBy.value,
"aria-invalid": isInvalid.value ? "true" : undefined,
onFocus: onInputFocus,
onBlur: onInputBlur,
},
{
// Own the native ARIA so the primitive's default label cannot
// override the composed field name. Retain its input handlers.
default: () =>
h("input", {
"aria-labelledby":
attrs["aria-labelledby"] ??
(attrs["aria-label"]
? undefined
: context?.labelledBy.value),
"aria-label":
attrs["aria-label"] ??
context?.accessibleLabel.value ??
(context?.labelledBy.value
? undefined
: `pin input ${props.index + 1} of ${context?.maxLength.value ?? 0}`),
}),
},
),
slots.default?.(),
],
);
}
return render;
},
});
</script>
@@ -0,0 +1,40 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { inputOTPVariants } from "@heroui-vue/styles";
export type InputOTPSlots = ReturnType<typeof inputOTPVariants>;
export interface InputOTPContextValue {
slots: ComputedRef<InputOTPSlots>;
isDisabled: ComputedRef<boolean>;
isInvalid: ComputedRef<boolean>;
isReadOnly: ComputedRef<boolean>;
maxLength: ComputedRef<number>;
pattern: ComputedRef<RegExp | null>;
/** Index-preserving character array; empty string means an empty slot. */
chars: ComputedRef<string[]>;
mask: ComputedRef<boolean>;
inputClassName: ComputedRef<string | undefined>;
describedBy: ComputedRef<string | undefined>;
labelledBy: ComputedRef<string | undefined>;
accessibleLabel: ComputedRef<string | undefined>;
/** Stable DOM id for the real focusable input at `index`. */
inputId: (index: number) => string;
}
export const inputOTPContextKey: InjectionKey<InputOTPContextValue> = Symbol(
"heroui-vue.input-otp",
);
/**
* Public model is a string. Slot `i` maps to `value[i]`.
* Empty slots stay at their indices in the internal array so the UI
* never left-shifts later characters. Serialization joins characters
* in index order (`arr.join("")`) — slot display always reads the array.
*/
export function charsFromString(value: string, length: number): string[] {
return Array.from({ length }, (_, i) => value[i] ?? "");
}
export function stringFromChars(chars: string[]): string {
return chars.join("");
}
@@ -0,0 +1,30 @@
<script lang="ts">
/** Baseline IconChevronLeft from HeroUI icons.tsx (Apache-2.0). */
export default {
name: "PaginationChevronLeftIcon",
};
</script>
<script setup lang="ts">
defineOptions({ inheritAttrs: false });
</script>
<template>
<svg
aria-hidden="true"
fill="none"
height="16"
role="presentation"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
v-bind="$attrs"
>
<path
clip-rule="evenodd"
d="M10.53 2.97a.75.75 0 0 1 0 1.06L6.56 8l3.97 3.97a.75.75 0 1 1-1.06 1.06l-4.5-4.5a.75.75 0 0 1 0-1.06l4.5-4.5a.75.75 0 0 1 1.06 0"
fill="currentColor"
fill-rule="evenodd"
/>
</svg>
</template>
@@ -0,0 +1,30 @@
<script lang="ts">
/** Baseline IconChevronRight from HeroUI icons.tsx (Apache-2.0). */
export default {
name: "PaginationChevronRightIcon",
};
</script>
<script setup lang="ts">
defineOptions({ inheritAttrs: false });
</script>
<template>
<svg
aria-hidden="true"
fill="none"
height="16"
role="presentation"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
v-bind="$attrs"
>
<path
clip-rule="evenodd"
d="M5.47 2.97a.75.75 0 0 1 1.06 0l4.5 4.5a.75.75 0 0 1 0 1.06l-4.5 4.5a.75.75 0 0 1-1.06-1.06L9.44 8 5.47 4.03a.75.75 0 0 1 0-1.06Z"
fill="currentColor"
fill-rule="evenodd"
/>
</svg>
</template>
@@ -0,0 +1,34 @@
<script lang="ts">
import { defineComponent, h, inject, useAttrs } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationContent",
inheritAttrs: false,
props: {
/** Render the content list as a custom element instead of `ul`. */
as: {
type: String,
default: "ul",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
function render() {
return h(
props.as,
{
...attrs,
class: [context?.slots.value.content(), attrs.class],
"data-slot": "pagination-content",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,36 @@
<script lang="ts">
import { defineComponent, h, inject, useAttrs } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationEllipsis",
inheritAttrs: false,
props: {
/** Render the ellipsis as a custom element instead of `span`. */
as: {
type: String,
default: "span",
},
},
setup(props) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
function render() {
// Upstream always renders &hellip; and does not take custom children.
return h(
props.as,
{
...attrs,
class: [context?.slots.value.ellipsis(), attrs.class],
"aria-hidden": "true",
"data-slot": "pagination-ellipsis",
},
["…"],
);
}
return render;
},
});
</script>
@@ -0,0 +1,34 @@
<script lang="ts">
import { defineComponent, h, inject, useAttrs } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationItem",
inheritAttrs: false,
props: {
/** Render the item as a custom element instead of `li`. */
as: {
type: String,
default: "li",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
function render() {
return h(
props.as,
{
...attrs,
class: [context?.slots.value.item(), attrs.class],
"data-slot": "pagination-item",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,79 @@
<script lang="ts">
import { computed, defineComponent, h, inject, useAttrs } from "vue";
import { Primitive } from "reka-ui";
import type { ButtonHTMLAttributes, PropType } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationLink",
inheritAttrs: false,
props: {
/** Whether this link is the current page. */
isActive: {
type: Boolean,
default: false,
},
isDisabled: {
type: Boolean,
default: undefined,
},
disabled: {
type: Boolean,
default: undefined,
},
type: {
type: String as PropType<ButtonHTMLAttributes["type"]>,
default: "button",
},
asChild: {
type: Boolean,
default: false,
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
const disabled = computed(
() => props.isDisabled ?? props.disabled ?? undefined,
);
function render() {
const {
class: cls,
"aria-current": explicitCurrent,
"data-active": explicitActive,
...rest
} = attrs as Record<string, unknown>;
const bind: Record<string, unknown> = { ...rest };
if (disabled.value !== undefined) bind.disabled = disabled.value;
return h(
Primitive,
{
as: "button",
asChild: props.asChild,
type: props.asChild ? undefined : props.type,
...bind,
class: [context?.slots.value.link(), cls],
// Explicit caller attributes win over isActive defaults.
"aria-current":
explicitCurrent !== undefined
? explicitCurrent
: props.isActive
? "page"
: undefined,
"data-active":
explicitActive !== undefined
? explicitActive
: props.isActive
? "true"
: undefined,
"data-slot": "pagination-link",
},
slots,
);
}
return render;
},
});
</script>
@@ -0,0 +1,60 @@
<script lang="ts">
import { computed, defineComponent, h, inject, useAttrs } from "vue";
import { Primitive } from "reka-ui";
import type { ButtonHTMLAttributes, PropType } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationNext",
inheritAttrs: false,
props: {
isDisabled: {
type: Boolean,
default: undefined,
},
disabled: {
type: Boolean,
default: undefined,
},
type: {
type: String as PropType<ButtonHTMLAttributes["type"]>,
default: "button",
},
asChild: {
type: Boolean,
default: false,
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
const disabled = computed(
() => props.isDisabled ?? props.disabled ?? undefined,
);
const linkClass = computed(() =>
[context?.slots.value.link(), "hv-pagination__link--nav"]
.filter(Boolean)
.join(" "),
);
function render() {
const bind: Record<string, unknown> = { ...attrs };
if (disabled.value !== undefined) bind.disabled = disabled.value;
return h(
Primitive,
{
as: "button",
asChild: props.asChild,
type: props.asChild ? undefined : props.type,
...bind,
class: [linkClass.value, attrs.class],
"data-slot": "pagination-next",
},
slots,
);
}
return render;
},
});
</script>
@@ -0,0 +1,45 @@
<script lang="ts">
import { defineComponent, h, useAttrs, type VNodeChild } from "vue";
import PaginationChevronRightIcon from "./PaginationChevronRightIcon.vue";
function hasContent(nodes: VNodeChild[] | undefined): boolean {
if (!nodes || nodes.length === 0) return false;
return nodes.some((node) => {
if (node == null || node === false || node === true) return false;
if (typeof node === "string") return node.trim() !== "";
if (typeof node === "number") return true;
return true;
});
}
export default defineComponent({
name: "PaginationNextIcon",
inheritAttrs: false,
props: {
/** Render the icon wrapper as a custom element instead of `span`. */
as: {
type: String,
default: "span",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
function render() {
const content = slots.default?.() as VNodeChild[] | undefined;
return h(
props.as,
{
...attrs,
class: attrs.class,
"aria-hidden": "true",
"data-slot": "pagination-next-icon",
},
hasContent(content) ? content : [h(PaginationChevronRightIcon)],
);
}
return render;
},
});
</script>
@@ -0,0 +1,60 @@
<script lang="ts">
import { computed, defineComponent, h, inject, useAttrs } from "vue";
import { Primitive } from "reka-ui";
import type { ButtonHTMLAttributes, PropType } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationPrevious",
inheritAttrs: false,
props: {
isDisabled: {
type: Boolean,
default: undefined,
},
disabled: {
type: Boolean,
default: undefined,
},
type: {
type: String as PropType<ButtonHTMLAttributes["type"]>,
default: "button",
},
asChild: {
type: Boolean,
default: false,
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
const disabled = computed(
() => props.isDisabled ?? props.disabled ?? undefined,
);
const linkClass = computed(() =>
[context?.slots.value.link(), "hv-pagination__link--nav"]
.filter(Boolean)
.join(" "),
);
function render() {
const bind: Record<string, unknown> = { ...attrs };
if (disabled.value !== undefined) bind.disabled = disabled.value;
return h(
Primitive,
{
as: "button",
asChild: props.asChild,
type: props.asChild ? undefined : props.type,
...bind,
class: [linkClass.value, attrs.class],
"data-slot": "pagination-previous",
},
slots,
);
}
return render;
},
});
</script>
@@ -0,0 +1,45 @@
<script lang="ts">
import { defineComponent, h, useAttrs, type VNodeChild } from "vue";
import PaginationChevronLeftIcon from "./PaginationChevronLeftIcon.vue";
function hasContent(nodes: VNodeChild[] | undefined): boolean {
if (!nodes || nodes.length === 0) return false;
return nodes.some((node) => {
if (node == null || node === false || node === true) return false;
if (typeof node === "string") return node.trim() !== "";
if (typeof node === "number") return true;
return true;
});
}
export default defineComponent({
name: "PaginationPreviousIcon",
inheritAttrs: false,
props: {
/** Render the icon wrapper as a custom element instead of `span`. */
as: {
type: String,
default: "span",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
function render() {
const content = slots.default?.() as VNodeChild[] | undefined;
return h(
props.as,
{
...attrs,
class: attrs.class,
"aria-hidden": "true",
"data-slot": "pagination-previous-icon",
},
hasContent(content) ? content : [h(PaginationChevronLeftIcon)],
);
}
return render;
},
});
</script>
@@ -0,0 +1,55 @@
<script lang="ts">
import {
computed,
defineComponent,
h,
provide,
useAttrs,
type PropType,
} from "vue";
import {
paginationVariants,
type PaginationVariants,
} from "@heroui-vue/styles";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationRoot",
inheritAttrs: false,
props: {
/** Pagination size. */
size: {
type: String as PropType<PaginationVariants["size"]>,
default: "md",
},
/** Render the root as a custom element instead of `nav`. */
as: {
type: String,
default: "nav",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const variantSlots = computed(() =>
paginationVariants({ size: props.size }),
);
provide(paginationContextKey, { slots: variantSlots });
function render() {
return h(
props.as,
{
...attrs,
class: [variantSlots.value.base(), attrs.class],
"aria-label": attrs["aria-label"] ?? "pagination",
role: attrs.role ?? "navigation",
"data-slot": "pagination",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,39 @@
<script lang="ts">
import { defineComponent, h, inject, useAttrs } from "vue";
import { paginationContextKey } from "./context";
export default defineComponent({
name: "PaginationSummary",
inheritAttrs: false,
props: {
/** Render the summary as a custom element instead of `div`. */
as: {
type: String,
default: "div",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(paginationContextKey, null);
function render() {
const { class: cls, dir, ...rest } = attrs as Record<string, unknown>;
return h(
props.as,
{
...rest,
// Auto-detect base direction from the summary's own text so mixed
// number/word content is not bidi-reordered by an RTL ancestor.
// Explicit `dir` still overrides.
dir: dir === undefined ? "auto" : dir,
class: [context?.slots.value.summary(), cls],
"data-slot": "pagination-summary",
},
slots.default?.(),
);
}
return render;
},
});
</script>
@@ -0,0 +1,11 @@
import type { InjectionKey, ComputedRef } from "vue";
import type { paginationVariants } from "@heroui-vue/styles";
export type PaginationSlots = ReturnType<typeof paginationVariants>;
export type PaginationContextValue = {
slots: ComputedRef<PaginationSlots>;
};
export const paginationContextKey: InjectionKey<PaginationContextValue> =
Symbol("heroui-vue.pagination");
+53
View File
@@ -536,3 +536,56 @@ export type {
} from "./components/toolbar/context";
export { toolbarVariants, type ToolbarVariants } from "@heroui-vue/styles";
export type { ToolbarProps } from "./types";
export { default as PaginationRoot } from "./components/pagination/PaginationRoot.vue";
export { default as Pagination } from "./components/pagination/PaginationRoot.vue";
export { default as PaginationSummary } from "./components/pagination/PaginationSummary.vue";
export { default as PaginationContent } from "./components/pagination/PaginationContent.vue";
export { default as PaginationItem } from "./components/pagination/PaginationItem.vue";
export { default as PaginationLink } from "./components/pagination/PaginationLink.vue";
export { default as PaginationPrevious } from "./components/pagination/PaginationPrevious.vue";
export { default as PaginationPreviousIcon } from "./components/pagination/PaginationPreviousIcon.vue";
export { default as PaginationNext } from "./components/pagination/PaginationNext.vue";
export { default as PaginationNextIcon } from "./components/pagination/PaginationNextIcon.vue";
export { default as PaginationEllipsis } from "./components/pagination/PaginationEllipsis.vue";
export {
paginationVariants,
type PaginationVariants,
} from "@heroui-vue/styles";
export type {
PaginationRootProps,
PaginationProps,
PaginationSummaryProps,
PaginationContentProps,
PaginationItemProps,
PaginationLinkProps,
PaginationPreviousProps,
PaginationPreviousIconProps,
PaginationNextProps,
PaginationNextIconProps,
PaginationEllipsisProps,
} from "./types";
export { default as HeaderRoot } from "./components/header/HeaderRoot.vue";
export { default as Header } from "./components/header/HeaderRoot.vue";
export { headerVariants, type HeaderVariants } from "@heroui-vue/styles";
export type { HeaderRootProps, HeaderProps } from "./types";
export { default as InputOTPRoot } from "./components/input-otp/InputOTPRoot.vue";
export { default as InputOTP } from "./components/input-otp/InputOTPRoot.vue";
export { default as InputOTPGroup } from "./components/input-otp/InputOTPGroup.vue";
export { default as InputOTPSlot } from "./components/input-otp/InputOTPSlot.vue";
export { default as InputOTPSeparator } from "./components/input-otp/InputOTPSeparator.vue";
export { inputOTPVariants, type InputOTPVariants } from "@heroui-vue/styles";
export type {
InputOTPRootProps,
InputOTPProps,
InputOTPGroupProps,
InputOTPSlotProps,
InputOTPSeparatorProps,
} from "./types";
/** Regular expressions matching upstream input-otp constants. */
export const REGEXP_ONLY_DIGITS = "^\\d+$";
export const REGEXP_ONLY_CHARS = "^[a-zA-Z]+$";
export const REGEXP_ONLY_DIGITS_AND_CHARS = "^[a-zA-Z0-9]+$";
+52
View File
@@ -395,3 +395,55 @@ export type ToggleButtonGroupSeparatorProps = InstanceType<
import type Toolbar from "./components/toolbar/Toolbar.vue";
export type ToolbarProps = InstanceType<typeof Toolbar>["$props"];
import type PaginationRoot from "./components/pagination/PaginationRoot.vue";
import type PaginationSummary from "./components/pagination/PaginationSummary.vue";
import type PaginationContent from "./components/pagination/PaginationContent.vue";
import type PaginationItem from "./components/pagination/PaginationItem.vue";
import type PaginationLink from "./components/pagination/PaginationLink.vue";
import type PaginationPrevious from "./components/pagination/PaginationPrevious.vue";
import type PaginationPreviousIcon from "./components/pagination/PaginationPreviousIcon.vue";
import type PaginationNext from "./components/pagination/PaginationNext.vue";
import type PaginationNextIcon from "./components/pagination/PaginationNextIcon.vue";
import type PaginationEllipsis from "./components/pagination/PaginationEllipsis.vue";
export type PaginationRootProps = InstanceType<typeof PaginationRoot>["$props"];
export type PaginationProps = PaginationRootProps;
export type PaginationSummaryProps = InstanceType<
typeof PaginationSummary
>["$props"];
export type PaginationContentProps = InstanceType<
typeof PaginationContent
>["$props"];
export type PaginationItemProps = InstanceType<typeof PaginationItem>["$props"];
export type PaginationLinkProps = InstanceType<typeof PaginationLink>["$props"];
export type PaginationPreviousProps = InstanceType<
typeof PaginationPrevious
>["$props"];
export type PaginationPreviousIconProps = InstanceType<
typeof PaginationPreviousIcon
>["$props"];
export type PaginationNextProps = InstanceType<typeof PaginationNext>["$props"];
export type PaginationNextIconProps = InstanceType<
typeof PaginationNextIcon
>["$props"];
export type PaginationEllipsisProps = InstanceType<
typeof PaginationEllipsis
>["$props"];
import type HeaderRoot from "./components/header/HeaderRoot.vue";
export type HeaderRootProps = InstanceType<typeof HeaderRoot>["$props"];
export type HeaderProps = HeaderRootProps;
import type InputOTPRoot from "./components/input-otp/InputOTPRoot.vue";
import type InputOTPGroup from "./components/input-otp/InputOTPGroup.vue";
import type InputOTPSlot from "./components/input-otp/InputOTPSlot.vue";
import type InputOTPSeparator from "./components/input-otp/InputOTPSeparator.vue";
export type InputOTPRootProps = InstanceType<typeof InputOTPRoot>["$props"];
export type InputOTPProps = InputOTPRootProps;
export type InputOTPGroupProps = InstanceType<typeof InputOTPGroup>["$props"];
export type InputOTPSlotProps = InstanceType<typeof InputOTPSlot>["$props"];
export type InputOTPSeparatorProps = InstanceType<
typeof InputOTPSeparator
>["$props"];