feat: migrate ScrollShadow and Tabs with docs and keyboard support

This commit is contained in:
maoyingjie
2026-10-23 00:25:45 +08:00
parent 2b4ac04df1
commit 99df0f1c91
55 changed files with 3290 additions and 5 deletions
@@ -0,0 +1,215 @@
/* Adapted from HeroUI ce90ea3, Apache-2.0. See third-party/HEROUI-LICENSE. */
/* Base scroll-shadow component */
.hv-scroll-shadow {
@apply relative;
--scroll-shadow-size: 40px;
--scroll-shadow-scrollbar-size: 10px;
--scroll-shadow-offset: 0px;
}
/* Fade sizes for the scroll-driven variant below. They stay at 0px whenever the scroll
timeline is inactive, which is exactly the state of a container without overflow. */
@property --scroll-shadow-start-fade {
syntax: "<length>";
inherits: false;
initial-value: 0px;
}
@property --scroll-shadow-end-fade {
syntax: "<length>";
inherits: false;
initial-value: 0px;
}
@keyframes hv-scroll-shadow-start-fade {
from {
--scroll-shadow-start-fade: 0px;
}
to {
--scroll-shadow-start-fade: var(--scroll-shadow-size);
}
}
@keyframes hv-scroll-shadow-end-fade {
from {
--scroll-shadow-end-fade: var(--scroll-shadow-size);
}
to {
--scroll-shadow-end-fade: 0px;
}
}
/* Orientation: Vertical */
.hv-scroll-shadow--vertical {
@apply scrollbar overflow-y-auto;
}
/* Orientation: Horizontal */
.hv-scroll-shadow--horizontal {
@apply scrollbar overflow-x-auto;
}
/* Variant: Fade - Vertical */
.hv-scroll-shadow--fade.hv-scroll-shadow--vertical {
/* `[data-scroll-shadow-mode="auto"]` lets the scroll-driven variant below reuse this
plumbing. It only paints once `--scroll-linear-gradient` is set, since an undefined
variable makes `mask-image` invalid at computed-value time, which resolves to `none`. */
&:where(
[data-top-scroll="true"],
[data-bottom-scroll="true"],
[data-top-bottom-scroll="true"],
[data-scroll-shadow-mode="auto"]
) {
mask-image:
linear-gradient(var(--scroll-linear-gradient)),
linear-gradient(#000, #000);
mask-position:
left top,
right top;
mask-repeat: no-repeat;
mask-size:
calc(100% - var(--scroll-shadow-scrollbar-size)) 100%,
var(--scroll-shadow-scrollbar-size) 100%;
-webkit-mask-image:
linear-gradient(var(--scroll-linear-gradient)),
linear-gradient(#000, #000);
-webkit-mask-position:
left top,
right top;
-webkit-mask-repeat: no-repeat;
-webkit-mask-size:
calc(100% - var(--scroll-shadow-scrollbar-size)) 100%,
var(--scroll-shadow-scrollbar-size) 100%;
}
&[data-top-scroll="true"] {
--scroll-linear-gradient:
0deg, #000 calc(100% - var(--scroll-shadow-size)), transparent;
}
&[data-bottom-scroll="true"] {
--scroll-linear-gradient:
180deg, #000 calc(100% - var(--scroll-shadow-size)), transparent;
}
&[data-top-bottom-scroll="true"] {
--scroll-linear-gradient:
#000, #000, transparent 0, #000 var(--scroll-shadow-size),
#000 calc(100% - var(--scroll-shadow-size)), transparent;
}
}
/* Variant: Fade - Horizontal */
.hv-scroll-shadow--fade.hv-scroll-shadow--horizontal {
&:where(
[data-left-scroll="true"],
[data-right-scroll="true"],
[data-left-right-scroll="true"],
[data-scroll-shadow-mode="auto"]
) {
mask-image:
linear-gradient(var(--scroll-linear-gradient)),
linear-gradient(#000, #000);
mask-position:
left top,
left bottom;
mask-repeat: no-repeat;
mask-size:
100% calc(100% - var(--scroll-shadow-scrollbar-size)),
100% var(--scroll-shadow-scrollbar-size);
-webkit-mask-image:
linear-gradient(var(--scroll-linear-gradient)),
linear-gradient(#000, #000);
-webkit-mask-position:
left top,
left bottom;
-webkit-mask-repeat: no-repeat;
-webkit-mask-size:
100% calc(100% - var(--scroll-shadow-scrollbar-size)),
100% var(--scroll-shadow-scrollbar-size);
}
/* `left`/`right` data attributes are logical (before/end of scroll). In RTL the
start edge is on the right, so the physical fade direction must be swapped. */
&[data-left-scroll="true"] {
--scroll-linear-gradient:
270deg, #000 calc(100% - var(--scroll-shadow-size)), transparent;
&:dir(rtl) {
--scroll-linear-gradient:
90deg, #000 calc(100% - var(--scroll-shadow-size)), transparent;
}
}
&[data-right-scroll="true"] {
--scroll-linear-gradient:
90deg, #000 calc(100% - var(--scroll-shadow-size)), transparent;
&:dir(rtl) {
--scroll-linear-gradient:
270deg, #000 calc(100% - var(--scroll-shadow-size)), transparent;
}
}
&[data-left-right-scroll="true"] {
--scroll-linear-gradient:
to right, #000, #000, transparent 0, #000 var(--scroll-shadow-size),
#000 calc(100% - var(--scroll-shadow-size)), transparent;
}
}
.hv-scroll-shadow--hide-scrollbar {
@apply scrollbar-none;
--scroll-shadow-scrollbar-size: 0px;
}
/* Variant: Fade - scroll-driven
Where scroll timelines are available the fade is derived from the real scroll state,
so it is already correct on the first paint and needs no measurement or hydration.
A container without overflow has an inactive timeline, which leaves both fades at 0px
and collapses the mask to a no-op. The attribute-driven rules above remain the
fallback for engines without scroll timelines, and keep serving controlled mode.
Two consequences of deriving the mask from CSS alone:
- Auto mode always resolves a `mask-image`, even with nothing to scroll, so the root is
always a stacking context and a containing block for fixed descendants. Gating that on
overflow would need a measurement, which is the flicker this variant exists to avoid.
- The `animation` shorthand here lives in the components layer, so an `animate-*` utility
on the root replaces it and pins both fades at 0px, leaving no fade at all. */
@supports (animation-timeline: scroll(self)) {
/* `.scroll-shadow` is repeated so these rules outrank the attribute-driven gradients
above on specificity rather than on source order, which a re-ordering could break. */
.hv-scroll-shadow.hv-scroll-shadow--fade[data-scroll-shadow-mode="auto"] {
animation:
hv-scroll-shadow-start-fade linear both,
hv-scroll-shadow-end-fade linear both;
animation-range:
var(--scroll-shadow-offset)
calc(var(--scroll-shadow-offset) + var(--scroll-shadow-size)),
calc(100% - var(--scroll-shadow-size) - var(--scroll-shadow-offset))
calc(100% - var(--scroll-shadow-offset));
}
/* The orientation rules also outrank the `animation` shorthand above, which resets
`animation-timeline` to `auto`, so the timeline survives regardless of order. */
.hv-scroll-shadow.hv-scroll-shadow--fade.hv-scroll-shadow--vertical[data-scroll-shadow-mode="auto"] {
animation-timeline: scroll(self block), scroll(self block);
--scroll-linear-gradient:
180deg, transparent 0, #000 var(--scroll-shadow-start-fade),
#000 calc(100% - var(--scroll-shadow-end-fade)), transparent 100%;
}
.hv-scroll-shadow.hv-scroll-shadow--fade.hv-scroll-shadow--horizontal[data-scroll-shadow-mode="auto"] {
animation-timeline: scroll(self inline), scroll(self inline);
--scroll-linear-gradient:
90deg, transparent 0, #000 var(--scroll-shadow-start-fade),
#000 calc(100% - var(--scroll-shadow-end-fade)), transparent 100%;
/* Inline-axis progress starts at the right edge in RTL, so flip the physical direction */
&:dir(rtl) {
--scroll-linear-gradient:
270deg, transparent 0, #000 var(--scroll-shadow-start-fade),
#000 calc(100% - var(--scroll-shadow-end-fade)), transparent 100%;
}
}
}
+303
View File
@@ -0,0 +1,303 @@
/* Adapted from HeroUI ce90ea3, Apache-2.0. See third-party/HEROUI-LICENSE. */
/* Base tabs component */
.hv-tabs {
@apply flex gap-2;
/* Orientation styles */
&[data-orientation="horizontal"] {
@apply flex-col;
}
&[data-orientation="vertical"] {
@apply flex-row;
}
}
/* Tab list container */
.hv-tabs__list-container {
@apply relative bg-default;
border-radius: calc(var(--radius) * 2.5);
/* Vertical scroller fills the container height so overflow detection
engages when the wrapper height is constrained (e.g. h-[240px]) */
> .hv-tabs__list-container__scroller[data-orientation="vertical"] {
@apply h-full;
}
/* Overflow chevron buttons */
> .hv-tabs__list-container__scroll-prev,
> .hv-tabs__list-container__scroll-next {
@apply absolute z-2 hidden size-4 cursor-(--cursor-interactive) items-center justify-center rounded-full border-0 bg-transparent p-0 text-foreground outline-none;
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition: opacity 150ms var(--ease-smooth);
@apply motion-reduce:transition-none;
@media (hover: hover) {
&:hover {
@apply opacity-70;
}
}
&:focus-visible {
@apply status-focused;
}
}
/* Chevron position - horizontal orientation */
&:has([data-orientation="horizontal"]) {
> .hv-tabs__list-container__scroll-prev {
@apply start-1 top-1/2 -translate-y-1/2;
}
> .hv-tabs__list-container__scroll-next {
@apply end-1 top-1/2 -translate-y-1/2;
}
> .hv-tabs__list-container__scroll-prev,
> .hv-tabs__list-container__scroll-next {
@apply rtl:rotate-180;
}
}
/* Chevron position - vertical orientation */
&:has([data-orientation="vertical"]) {
> .hv-tabs__list-container__scroll-prev {
@apply top-1 left-1/2 -translate-x-1/2;
}
> .hv-tabs__list-container__scroll-next {
@apply bottom-1 left-1/2 -translate-x-1/2;
}
}
/* Show chevrons only when ScrollShadow reports scrolling is possible */
&:has(
> :is(
[data-left-scroll="true"],
[data-left-right-scroll="true"],
[data-top-scroll="true"],
[data-top-bottom-scroll="true"]
)
)
> .hv-tabs__list-container__scroll-prev {
@apply inline-flex;
}
&:has(
> :is(
[data-right-scroll="true"],
[data-left-right-scroll="true"],
[data-bottom-scroll="true"],
[data-top-bottom-scroll="true"]
)
)
> .hv-tabs__list-container__scroll-next {
@apply inline-flex;
}
}
/* Tab list */
.hv-tabs__list {
@apply inline-flex p-1;
/* Horizontal orientation - grow with content so ScrollShadow can detect overflow */
&[data-orientation="horizontal"] {
@apply w-max min-w-full flex-row;
}
/* Vertical orientation */
&[data-orientation="vertical"] {
@apply flex-col gap-1;
.hv-tabs__tab {
@apply min-w-20;
}
}
}
/* Individual tab */
.hv-tabs__tab {
@apply relative flex h-8 w-full items-center justify-center rounded-3xl px-4 text-center text-sm font-medium text-muted outline-none no-highlight;
z-index: 1;
cursor: var(--cursor-interactive);
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition:
color 150ms var(--ease-smooth),
background-color 150ms var(--ease-smooth),
box-shadow 150ms var(--ease-out),
opacity 150ms var(--ease-smooth);
@apply motion-reduce:transition-none;
/* Selected state */
&[data-selected="true"] {
@apply text-segment-foreground;
/* Hide separator on the selected tab and the one following it */
.hv-tabs__separator,
& + .hv-tabs__tab .hv-tabs__separator {
@apply opacity-0;
}
}
/* Disabled state */
&:disabled,
&[data-disabled],
&[aria-disabled="true"] {
@apply status-disabled;
}
/* Hover states - both approaches for compatibility */
@media (hover: hover) {
&:not([data-selected="true"]):not([data-disabled]):hover,
&[data-hovered="true"]:not([data-selected="true"]):not([data-disabled]) {
@apply opacity-70;
}
}
/* Focus states - comprehensive fallback */
&:focus-visible:not([data-focused]),
&[data-focus-visible="true"] {
@apply status-focused;
}
}
/* Tab content alignment - center is the default, handled by .hv-tabs__tab.
Scoped to the direct child holding the tab list (the list container, or the
list itself when Tabs.ListContainer is omitted) so a nested Tabs inside a
panel keeps its own alignment. */
.hv-tabs--align-start
> :is(.hv-tabs__list-container, .hv-tabs__list)
.hv-tabs__tab {
@apply justify-start text-start;
}
.hv-tabs--align-end
> :is(.hv-tabs__list-container, .hv-tabs__list)
.hv-tabs__tab {
@apply justify-end text-end;
}
/* Tab separator */
.hv-tabs__separator {
@apply pointer-events-none absolute rounded-sm bg-muted/25;
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition: opacity 150ms var(--ease-smooth);
@apply motion-reduce:transition-none;
/* Horizontal tabs - vertical separator */
.hv-tabs__list[data-orientation="horizontal"] & {
@apply start-0 top-1/4 h-1/2 w-px;
}
/* Vertical tabs - horizontal separator */
.hv-tabs__list[data-orientation="vertical"] & {
@apply start-[5%] top-0 h-px w-[90%];
}
}
/* Tab panel */
.hv-tabs__panel {
@apply w-full p-2 outline-none;
/* Exiting animations */
&[data-exiting="true"] {
@apply absolute start-0 top-0 w-full;
}
/* Orientation spacing */
&[data-orientation="horizontal"] {
@apply mt-4;
}
&[data-orientation="vertical"] {
@apply ms-4;
}
}
/* Tab indicator - React Aria's SelectionIndicator */
.hv-tabs__indicator {
@apply absolute start-0 top-0 size-full rounded-3xl bg-segment shadow-surface;
z-index: -1;
/**
* Transitions
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
*/
transition-duration: 250ms;
transition-property: translate, width, height;
transition-timing-function: var(--ease-out-fluid);
@apply motion-reduce:transition-none;
}
/* ==========================================================================
Secondary Variant
========================================================================== */
.hv-tabs--secondary {
/* Container - flat, no pill. Underline on container (not list) so it stays
stable when the inner list scrolls horizontally inside the ScrollShadow. */
> .hv-tabs__list-container {
@apply rounded-none bg-transparent;
/* Tab list - remove padding */
.hv-tabs__list {
@apply p-0;
}
/* Hide separators in secondary variant */
.hv-tabs__separator {
@apply hidden;
}
/* Tab - adjust for secondary variant */
.hv-tabs__tab {
@apply rounded-none;
/* Selected state - use text-foreground */
&[data-selected="true"] {
@apply text-foreground;
}
}
/* Tab indicator - line style */
.hv-tabs__indicator {
@apply rounded-none bg-accent shadow-none;
}
}
/* Horizontal orientation - bottom border + bottom line indicator */
&[data-orientation="horizontal"] > .hv-tabs__list-container {
@apply border-b border-border;
.hv-tabs__indicator {
@apply top-auto bottom-0 h-0.5;
}
}
/* Vertical orientation - left border + left line indicator */
&[data-orientation="vertical"] > .hv-tabs__list-container {
@apply border-s border-border;
.hv-tabs__indicator {
@apply start-0 top-0 h-full w-0.5;
}
}
}
/* Reka roving-focus navigation requires an empty data-disabled marker.
* Match its presence instead of changing it to RAC boolean strings. */
+3
View File
@@ -127,3 +127,6 @@
@import "./components/popover.css" layer(components);
@import "./components/tooltip.css" layer(components);
@import "./components/scroll-shadow.css";
@import "./components/tabs.css";
+78
View File
@@ -978,3 +978,81 @@ export const tooltipVariants = tv({
});
export type TooltipVariants = VariantProps<typeof tooltipVariants>;
export const scrollShadowVariants = tv({
defaultVariants: {
hideScrollBar: false,
orientation: "vertical",
variant: "fade",
},
slots: {
base: "hv-scroll-shadow",
},
variants: {
hideScrollBar: {
false: {},
true: {
base: "hv-scroll-shadow--hide-scrollbar",
},
},
orientation: {
horizontal: {
base: "hv-scroll-shadow--horizontal",
},
vertical: {
base: "hv-scroll-shadow--vertical",
},
},
variant: {
fade: {
base: "hv-scroll-shadow--fade",
},
},
},
});
export type ScrollShadowVariants = VariantProps<typeof scrollShadowVariants>;
export const tabsVariants = tv({
defaultVariants: {
align: "center",
variant: "primary",
},
slots: {
base: "hv-tabs",
scrollNext: "hv-tabs__list-container__scroll-next",
scrollPrev: "hv-tabs__list-container__scroll-prev",
scroller: "hv-tabs__list-container__scroller",
separator: "hv-tabs__separator",
tab: "hv-tabs__tab",
tabIndicator: "hv-tabs__indicator",
tabList: "hv-tabs__list",
tabListContainer: "hv-tabs__list-container",
tabPanel: "hv-tabs__panel",
},
variants: {
align: {
center: {}, // No class as the base already centers tab content
end: {
base: "hv-tabs--align-end",
},
start: {
base: "hv-tabs--align-start",
},
},
variant: {
primary: {},
secondary: {
base: "hv-tabs--secondary",
},
},
},
});
// Render props that should be excluded from TabsVariants (framework-specific)
type TabsRenderPropsKeys = "selectedKey";
export type TabsVariants = Omit<
VariantProps<typeof tabsVariants>,
TabsRenderPropsKeys
>;