feat: migrate Calendar and calendar year picker

This commit is contained in:
maoyingjie
2026-10-24 20:59:19 +08:00
parent 1c4ecc3def
commit 835d2e4d90
47 changed files with 4283 additions and 87 deletions
@@ -0,0 +1,165 @@
/* Adapted from HeroUI ce90ea3, Apache-2.0; see third-party/HEROUI-LICENSE. */
/* =============================================================================
* Calendar Year Picker Component Styles
*
* Internal component used inside Calendar / RangeCalendar to provide a
* toggleable year selector. Uses CSS :has() on the parent .hv-calendar to
* drive crossfade transitions.
*
* The year grid is absolutely positioned and sized via JS to match the
* day grid's exact area. This keeps the calendar's original block layout
* completely untouched.
*
* BEM naming: calendar-year-picker__element, calendar-year-picker--modifier
* ============================================================================= */
/* -----------------------------------------------------------------------------
* Positioning Context
* -------------------------------------------------------------------------- */
.hv-calendar:has(.hv-calendar-year-picker__year-grid),
.hv-range-calendar:has(.hv-calendar-year-picker__year-grid) {
position: relative;
}
/* -----------------------------------------------------------------------------
* Crossfade + Blur Transitions
*
* The crossfade is staggered to avoid both grids being semi-transparent at
* the same time (ghosting). When opening, the day grid fades out first,
* then the year grid fades in. When closing, the year grid fades out first,
* then the day grid fades in (via transition-delay).
*
* -------------------------------------------------------------------------- */
/* Day grid – default visible */
.hv-calendar:has(.hv-calendar-year-picker__year-grid)
> [data-slot="calendar-grid"],
.hv-range-calendar:has(.hv-calendar-year-picker__year-grid)
> [data-slot="range-calendar-grid"] {
will-change: opacity;
transition:
opacity 150ms var(--ease-out),
visibility 0ms linear;
@apply motion-reduce:transition-none;
}
/* Day grid – hidden when year picker is open (fade + blur out) */
.hv-calendar:has(.hv-calendar-year-picker__year-grid[data-open="true"])
> [data-slot="calendar-grid"],
.hv-range-calendar:has(.hv-calendar-year-picker__year-grid[data-open="true"])
> [data-slot="range-calendar-grid"] {
@apply pointer-events-none opacity-0;
visibility: hidden;
transition:
opacity 150ms var(--ease-out),
visibility 0ms linear 150ms;
@apply motion-reduce:transition-none;
}
/* -----------------------------------------------------------------------------
* Trigger Button
* -------------------------------------------------------------------------- */
.hv-calendar-year-picker__trigger {
@apply flex flex-1 items-center justify-start gap-1 rounded-lg outline-none;
cursor: var(--cursor-interactive);
touch-action: manipulation;
/* Focus state */
&:focus-visible {
@apply status-focused;
}
}
.hv-calendar-year-picker__trigger-heading {
@apply text-sm font-medium;
transition: color 150ms var(--ease-out);
@apply motion-reduce:transition-none;
}
.hv-calendar-year-picker__trigger-indicator {
@apply text-xs text-accent-soft-foreground rtl:[transform:rotate(180deg)];
transition: transform 150ms var(--ease-out);
@apply motion-reduce:transition-none;
}
/* Rotate chevron when open */
.hv-calendar-year-picker__trigger[data-open="true"]
.hv-calendar-year-picker__trigger-indicator {
transform: rotate(90deg);
}
.hv-calendar-year-picker__trigger[data-open="true"]
.hv-calendar-year-picker__trigger-heading {
@apply text-accent-soft-foreground;
}
/* -----------------------------------------------------------------------------
* Year Grid Container
*
* Absolutely positioned over the day grid area. The exact `top` and `height`
* are set via JS to match the sibling day grid element.
* -------------------------------------------------------------------------- */
.hv-calendar-year-picker__year-grid {
@apply pointer-events-none absolute start-0 end-0 grid scrollbar content-start gap-1 overflow-y-auto p-1 opacity-0;
grid-template-columns: repeat(3, 1fr);
/* When closing: instant (no transition) */
will-change: opacity;
}
.hv-calendar-year-picker__year-grid[data-open="true"] {
@apply pointer-events-auto opacity-100;
/* When opening: slight delay so the day grid starts fading out first */
transition: opacity 200ms var(--ease-out) 50ms;
@apply motion-reduce:transition-none;
}
/* -----------------------------------------------------------------------------
* Year Cell
* -------------------------------------------------------------------------- */
.hv-calendar-year-picker__year-cell {
@apply relative inline-flex h-8 items-center justify-center rounded-3xl px-2.5 text-sm font-medium outline-none select-none no-highlight;
touch-action: manipulation;
/**
* Transitions – same as .tag
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition:
color 100ms var(--ease-smooth),
scale 100ms var(--ease-smooth),
opacity 100ms var(--ease-smooth),
background-color 100ms var(--ease-smooth),
box-shadow 100ms var(--ease-out);
@apply origin-center transform-gpu motion-reduce:transition-none;
cursor: var(--cursor-interactive);
/* Hover state (when not selected) – only on devices that support hover */
@media (hover: hover) and (pointer: fine) {
&:is(:hover, [data-hovered="true"]):not([data-selected="true"]) {
@apply bg-default text-default-foreground;
}
}
/* Selected state (current year) – same as .tag[data-selected] */
&[data-selected="true"],
&[aria-selected="true"] {
@apply bg-accent text-accent-foreground;
@media (hover: hover) and (pointer: fine) {
&:is(:hover, [data-hovered="true"]) {
@apply bg-accent-hover;
}
}
}
/* Focus state */
&:focus-visible {
@apply status-focused;
}
}
+274
View File
@@ -0,0 +1,274 @@
/* Adapted from HeroUI ce90ea3, Apache-2.0; see third-party/HEROUI-LICENSE. */
/* =============================================================================
* Calendar Component Styles
*
* A compound component for date selection.
* Uses BEM naming convention: calendar, calendar__element, calendar--modifier
* ============================================================================= */
/* -----------------------------------------------------------------------------
* Base Calendar Styles
* -------------------------------------------------------------------------- */
.hv-calendar {
@apply w-63 max-w-63;
container-type: inline-size;
}
/* Week view – visibleDuration={{ weeks: n }} */
.hv-calendar--week-view {
.hv-calendar__cell {
/* Keep cells circular on the 7-column grid; only filled cells take space. */
@apply aspect-square size-auto w-full place-self-center;
}
}
/* Day view – visibleDuration={{ days: n }} */
.hv-calendar--day-view {
.hv-calendar__cell {
@apply aspect-square size-auto w-full place-self-center;
}
/*
* Isolate header and body into separate 7-column grids. With display:contents
* on the root grid, fewer than 7 weekday headers flow into the same row as dates.
*/
.hv-calendar__grid {
display: flex;
flex-direction: column;
}
.hv-calendar__grid-header {
display: grid;
grid-template-columns: repeat(7, 1fr);
@apply w-full;
& > tr {
display: contents;
}
}
.hv-calendar__grid-body {
display: grid;
grid-template-columns: repeat(7, 1fr);
@apply mt-1 w-full;
& > tr {
display: contents;
}
& > tr:first-child > td {
@apply mt-0;
}
}
}
/* -----------------------------------------------------------------------------
* Calendar Header
* -------------------------------------------------------------------------- */
.hv-calendar__header {
@apply flex items-center justify-between px-0.5 pb-4;
&:has(.hv-calendar-year-picker__trigger[data-open="true"]) {
.hv-calendar__nav-button {
@apply pointer-events-none opacity-0;
}
}
}
.hv-calendar__heading {
@apply flex-1 text-sm font-medium;
}
.hv-calendar__nav-button {
@apply flex size-6 items-center justify-center rounded-2xl text-accent-soft-foreground;
will-change: scale;
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition:
transform 250ms var(--ease-out),
background-color 100ms var(--ease-out),
box-shadow 100ms var(--ease-out),
opacity 150ms var(--ease-out);
@apply transform-gpu motion-reduce:transition-none;
cursor: var(--cursor-interactive);
/* Hover state – only on devices that support hover (avoids false triggers on touch) */
@media (hover: hover) {
&:hover,
&[data-hovered="true"] {
@apply bg-default text-accent-soft-foreground;
}
}
/* Pressed state */
&:active,
&[data-pressed="true"] {
transform: scale(0.95);
}
/* Focus state */
&:focus-visible,
&[data-focus-visible="true"] {
@apply status-focused;
}
/* Disabled state */
&:disabled,
&[data-disabled="true"] {
@apply status-disabled;
}
}
.hv-calendar__nav-button-icon {
@apply size-4 rtl:rotate-180;
}
/* -----------------------------------------------------------------------------
* Calendar Grid
* -------------------------------------------------------------------------- */
.hv-calendar__grid {
/* Override table display with CSS Grid for easier customization */
display: grid;
grid-template-columns: repeat(7, 1fr);
@apply w-full;
&[aria-readonly="true"] {
.hv-calendar__cell {
@apply pointer-events-none;
}
}
}
.hv-calendar__grid-header {
/* Grid header (thead) styles */
display: contents;
& > tr {
display: contents;
}
}
.hv-calendar__grid-body {
/* Grid body (tbody) styles */
display: contents;
& > tr {
display: contents;
}
/* Add top spacing between header and first body row */
& > tr:first-child > td {
@apply mt-1;
}
}
.hv-calendar__grid-row {
/* Use 'contents' so cells participate directly in grid */
/* Note: This class can be used for custom row styling if needed */
display: contents;
}
.hv-calendar__header-cell {
/* Header cell (th - weekday names) styles */
@apply flex items-center justify-center pb-2 text-xs font-medium text-muted;
}
/* -----------------------------------------------------------------------------
* Calendar Cells
* -------------------------------------------------------------------------- */
.hv-calendar__cell {
/* Cell (td) wrapper styles - margins now work with grid layout */
@apply relative flex aspect-square size-full items-center justify-center rounded-3xl text-center text-sm font-medium outline-none no-highlight;
will-change: scale;
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition:
transform 250ms var(--ease-out),
box-shadow 100ms var(--ease-out);
@apply transform-gpu motion-reduce:transition-none;
cursor: var(--cursor-interactive);
/* Focus state */
&:focus-visible:not([data-focused]),
&[data-focus-visible="true"] {
@apply status-focused;
}
/* Today indicator */
&[data-today="true"] {
@apply bg-accent-soft text-accent-soft-foreground;
@media (hover: hover) {
&:hover:not([data-selected="true"]),
&[data-hovered="true"]:not([data-selected="true"]) {
@apply bg-accent-soft-hover;
}
}
}
/* Selected state */
&[data-selected="true"] {
@apply bg-accent text-accent-foreground;
}
/* Pressed state */
&:active,
&[data-pressed="true"] {
@apply bg-default;
transform: scale(0.95);
&[data-selected="true"] {
@apply bg-accent-hover;
}
}
/* Hover state – only on devices that support hover (avoids false triggers on touch) */
@media (hover: hover) {
&:hover:not([data-selected="true"]),
&[data-hovered="true"]:not([data-selected="true"]) {
@apply bg-default;
}
}
/* Outside month (previous/next month days) */
&[data-outside-month="true"] {
@apply text-muted opacity-50;
}
&[data-selected="true"][data-outside-month="true"] {
@apply bg-default;
}
/* Unavailable dates */
&[data-unavailable="true"] {
@apply status-disabled;
}
/* Disabled state */
&:disabled:not([data-outside-month="true"]),
&[data-disabled="true"]:not([data-outside-month="true"]) {
@apply status-disabled;
text-decoration: line-through;
}
}
/* Cell indicator - small dot at bottom center of cell */
.hv-calendar__cell-indicator {
@apply absolute bottom-1 left-1/2 size-[3px] -translate-x-1/2 rounded-xs bg-muted;
/* Indicator color changes when cell is selected */
[data-selected="true"] > & {
@apply bg-accent-foreground;
}
}
+4
View File
@@ -154,3 +154,7 @@
@import "./components/date-input-group.css" layer(components);
@import "./components/date-field.css" layer(components);
@import "./components/calendar.css" layer(components);
@import "./components/calendar-year-picker.css" layer(components);
+47
View File
@@ -1580,3 +1580,50 @@ export const tableVariants = tv({
});
export type TableVariants = VariantProps<typeof tableVariants>;
export const calendarVariants = tv({
defaultVariants: {},
slots: {
/** Root calendar container */
base: "hv-calendar",
/** Calendar cell (td) */
cell: "hv-calendar__cell",
/** Cell indicator (small dot at bottom of cell) */
cellIndicator: "hv-calendar__cell-indicator",
/** Calendar grid (table) */
grid: "hv-calendar__grid",
/** Grid body (tbody) */
gridBody: "hv-calendar__grid-body",
/** Grid header (thead) */
gridHeader: "hv-calendar__grid-header",
/** Grid row (tr) */
gridRow: "hv-calendar__grid-row",
/** Calendar header containing heading and navigation */
header: "hv-calendar__header",
/** Header cell (th - day names) */
headerCell: "hv-calendar__header-cell",
/** Month/year heading text */
heading: "hv-calendar__heading",
/** Previous/Next navigation button */
navButton: "hv-calendar__nav-button",
/** Navigation button icon */
navButtonIcon: "hv-calendar__nav-button-icon",
},
variants: {},
});
export type CalendarVariants = VariantProps<typeof calendarVariants>;
export const calendarYearPickerVariants = tv({
slots: {
trigger: "hv-calendar-year-picker__trigger",
triggerHeading: "hv-calendar-year-picker__trigger-heading",
triggerIndicator: "hv-calendar-year-picker__trigger-indicator",
yearCell: "hv-calendar-year-picker__year-cell",
yearGrid: "hv-calendar-year-picker__year-grid",
},
});
export type CalendarYearPickerVariants = VariantProps<
typeof calendarYearPickerVariants
>;