@celestia-island/hikari 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +54 -0
  2. package/package.json +63 -0
  3. package/src/DemoApp.vue +400 -0
  4. package/src/components/HkActionBar.tsx +15 -0
  5. package/src/components/HkAlert.scss +121 -0
  6. package/src/components/HkAlert.tsx +85 -0
  7. package/src/components/HkAvatar.tsx +29 -0
  8. package/src/components/HkBadge.scss +117 -0
  9. package/src/components/HkBadge.tsx +50 -0
  10. package/src/components/HkBreadcrumb.tsx +39 -0
  11. package/src/components/HkButton.scss +128 -0
  12. package/src/components/HkButton.tsx +74 -0
  13. package/src/components/HkCard.scss +49 -0
  14. package/src/components/HkCard.tsx +38 -0
  15. package/src/components/HkCheckbox.scss +102 -0
  16. package/src/components/HkCheckbox.tsx +85 -0
  17. package/src/components/HkCollapse.tsx +48 -0
  18. package/src/components/HkColorPicker.scss +148 -0
  19. package/src/components/HkColorPicker.tsx +204 -0
  20. package/src/components/HkConfirmDialog.scss +12 -0
  21. package/src/components/HkConfirmDialog.tsx +75 -0
  22. package/src/components/HkDateTimePicker.scss +361 -0
  23. package/src/components/HkDateTimePicker.tsx +424 -0
  24. package/src/components/HkDivider.scss +85 -0
  25. package/src/components/HkDivider.tsx +51 -0
  26. package/src/components/HkDraggableGrid.scss +132 -0
  27. package/src/components/HkDraggableGrid.tsx +291 -0
  28. package/src/components/HkDraggableList.scss +110 -0
  29. package/src/components/HkDraggableList.tsx +238 -0
  30. package/src/components/HkDrawer.scss +175 -0
  31. package/src/components/HkDrawer.tsx +171 -0
  32. package/src/components/HkEmptyState.scss +42 -0
  33. package/src/components/HkEmptyState.tsx +41 -0
  34. package/src/components/HkErrorBoundary.scss +64 -0
  35. package/src/components/HkErrorBoundary.tsx +82 -0
  36. package/src/components/HkGaugeRing.scss +4 -0
  37. package/src/components/HkGaugeRing.tsx +130 -0
  38. package/src/components/HkHoverRevealAction.scss +66 -0
  39. package/src/components/HkHoverRevealAction.tsx +77 -0
  40. package/src/components/HkIcon.tsx +43 -0
  41. package/src/components/HkIconButton.tsx +42 -0
  42. package/src/components/HkImageViewer.scss +33 -0
  43. package/src/components/HkImageViewer.tsx +250 -0
  44. package/src/components/HkInput.scss +172 -0
  45. package/src/components/HkInput.tsx +175 -0
  46. package/src/components/HkKbd.tsx +26 -0
  47. package/src/components/HkKeywordSearchModal.scss +148 -0
  48. package/src/components/HkKeywordSearchModal.tsx +308 -0
  49. package/src/components/HkListTransition.scss +131 -0
  50. package/src/components/HkListTransition.tsx +29 -0
  51. package/src/components/HkLocalePickerPopup.tsx +42 -0
  52. package/src/components/HkLogo.scss +36 -0
  53. package/src/components/HkLogo.tsx +93 -0
  54. package/src/components/HkMarkdownRenderer.scss +141 -0
  55. package/src/components/HkMarkdownRenderer.tsx +123 -0
  56. package/src/components/HkMediaControlBar.scss +65 -0
  57. package/src/components/HkMediaControlBar.tsx +115 -0
  58. package/src/components/HkMediaPlayer.scss +29 -0
  59. package/src/components/HkMediaPlayer.tsx +210 -0
  60. package/src/components/HkMediaSlider.scss +54 -0
  61. package/src/components/HkMediaSlider.tsx +81 -0
  62. package/src/components/HkMediaVisualizer.scss +7 -0
  63. package/src/components/HkMediaVisualizer.tsx +119 -0
  64. package/src/components/HkMinimap.scss +76 -0
  65. package/src/components/HkMinimap.tsx +255 -0
  66. package/src/components/HkModal.scss +318 -0
  67. package/src/components/HkModal.tsx +481 -0
  68. package/src/components/HkModalBreadcrumb.scss +55 -0
  69. package/src/components/HkModalBreadcrumb.tsx +113 -0
  70. package/src/components/HkMorphingTabs.scss +141 -0
  71. package/src/components/HkMorphingTabs.tsx +113 -0
  72. package/src/components/HkNavItem.scss +81 -0
  73. package/src/components/HkNavItem.tsx +157 -0
  74. package/src/components/HkNumberInput.scss +154 -0
  75. package/src/components/HkNumberInput.tsx +161 -0
  76. package/src/components/HkPasswordInput.scss +289 -0
  77. package/src/components/HkPasswordInput.tsx +533 -0
  78. package/src/components/HkPhaseTransition.scss +25 -0
  79. package/src/components/HkPhaseTransition.tsx +40 -0
  80. package/src/components/HkPopover.scss +82 -0
  81. package/src/components/HkPopover.tsx +410 -0
  82. package/src/components/HkPopupSelect.scss +136 -0
  83. package/src/components/HkPopupSelect.tsx +199 -0
  84. package/src/components/HkProgressBar.scss +106 -0
  85. package/src/components/HkProgressBar.tsx +61 -0
  86. package/src/components/HkProgressDialog.tsx +67 -0
  87. package/src/components/HkProgressRing.scss +56 -0
  88. package/src/components/HkProgressRing.tsx +82 -0
  89. package/src/components/HkRadio.scss +119 -0
  90. package/src/components/HkRadio.tsx +75 -0
  91. package/src/components/HkRollingNumber.scss +37 -0
  92. package/src/components/HkRollingNumber.tsx +97 -0
  93. package/src/components/HkScrollContainer.scss +101 -0
  94. package/src/components/HkScrollContainer.tsx +362 -0
  95. package/src/components/HkSearchInput.scss +132 -0
  96. package/src/components/HkSearchInput.tsx +117 -0
  97. package/src/components/HkSelect.scss +143 -0
  98. package/src/components/HkSelect.tsx +230 -0
  99. package/src/components/HkSelectionGrid.scss +177 -0
  100. package/src/components/HkSelectionGrid.tsx +99 -0
  101. package/src/components/HkSelectionWaterfall.scss +93 -0
  102. package/src/components/HkSelectionWaterfall.tsx +90 -0
  103. package/src/components/HkSidebar.scss +81 -0
  104. package/src/components/HkSidebar.tsx +45 -0
  105. package/src/components/HkSkeleton.scss +44 -0
  106. package/src/components/HkSkeleton.tsx +30 -0
  107. package/src/components/HkSkeletonList.tsx +35 -0
  108. package/src/components/HkSpinner.scss +67 -0
  109. package/src/components/HkSpinner.tsx +41 -0
  110. package/src/components/HkSplash.scss +60 -0
  111. package/src/components/HkSplash.tsx +40 -0
  112. package/src/components/HkSwitch.scss +157 -0
  113. package/src/components/HkSwitch.tsx +72 -0
  114. package/src/components/HkTable.scss +198 -0
  115. package/src/components/HkTable.tsx +246 -0
  116. package/src/components/HkTabs.scss +109 -0
  117. package/src/components/HkTabs.tsx +111 -0
  118. package/src/components/HkTag.scss +88 -0
  119. package/src/components/HkTag.tsx +44 -0
  120. package/src/components/HkTextarea.scss +72 -0
  121. package/src/components/HkTextarea.tsx +104 -0
  122. package/src/components/HkTimeline.scss +139 -0
  123. package/src/components/HkTimeline.tsx +95 -0
  124. package/src/components/HkToast.scss +168 -0
  125. package/src/components/HkToast.tsx +175 -0
  126. package/src/components/HkTooltip.scss +47 -0
  127. package/src/components/HkTooltip.tsx +108 -0
  128. package/src/components/HkTree.scss +188 -0
  129. package/src/components/HkTree.tsx +217 -0
  130. package/src/components/HkTrendChart.tsx +180 -0
  131. package/src/components/HkWindowedItem.scss +17 -0
  132. package/src/components/HkWindowedItem.tsx +107 -0
  133. package/src/components/HkZoomToolbar.scss +35 -0
  134. package/src/components/HkZoomToolbar.tsx +69 -0
  135. package/src/composables/useBreakpoint.ts +41 -0
  136. package/src/composables/useClipboard.ts +37 -0
  137. package/src/composables/useConfirm.ts +35 -0
  138. package/src/composables/useProgressDialog.ts +52 -0
  139. package/src/composables/useReportedTransition.ts +69 -0
  140. package/src/composables/useScrollWindow.ts +21 -0
  141. package/src/composables/useToast.ts +2 -0
  142. package/src/demo.scss +24 -0
  143. package/src/demo.ts +22 -0
  144. package/src/env.d.ts +6 -0
  145. package/src/i18n/context.ts +89 -0
  146. package/src/i18n/locales/ar/components.json +43 -0
  147. package/src/i18n/locales/ar/components.toml +40 -0
  148. package/src/i18n/locales/de/components.json +43 -0
  149. package/src/i18n/locales/de/components.toml +40 -0
  150. package/src/i18n/locales/en/components.json +43 -0
  151. package/src/i18n/locales/en/components.toml +40 -0
  152. package/src/i18n/locales/es/components.json +43 -0
  153. package/src/i18n/locales/es/components.toml +40 -0
  154. package/src/i18n/locales/fr/components.json +43 -0
  155. package/src/i18n/locales/fr/components.toml +40 -0
  156. package/src/i18n/locales/ja/components.json +43 -0
  157. package/src/i18n/locales/ja/components.toml +40 -0
  158. package/src/i18n/locales/ko/components.json +43 -0
  159. package/src/i18n/locales/ko/components.toml +40 -0
  160. package/src/i18n/locales/pt/components.json +43 -0
  161. package/src/i18n/locales/pt/components.toml +40 -0
  162. package/src/i18n/locales/ru/components.json +43 -0
  163. package/src/i18n/locales/ru/components.toml +40 -0
  164. package/src/i18n/locales/zh-Hans/components.json +43 -0
  165. package/src/i18n/locales/zh-Hans/components.toml +40 -0
  166. package/src/i18n/locales/zh-Hant/components.json +43 -0
  167. package/src/i18n/locales/zh-Hant/components.toml +40 -0
  168. package/src/index.ts +137 -0
  169. package/src/render.ts +18 -0
  170. package/src/runtime/animationBus.ts +278 -0
  171. package/src/runtime/cronBus.ts +25 -0
  172. package/src/runtime/index.ts +12 -0
  173. package/src/runtime/useAsyncData.ts +30 -0
  174. package/src/runtime/useBreakpoint.ts +62 -0
  175. package/src/runtime/useClipboard.ts +37 -0
  176. package/src/runtime/useConfirm.ts +35 -0
  177. package/src/runtime/useOverlay.ts +82 -0
  178. package/src/runtime/usePopupManager.ts +89 -0
  179. package/src/runtime/useToast.ts +140 -0
  180. package/src/styles/index.scss +10 -0
  181. package/src/theme/index.ts +5 -0
  182. package/src/theme/presets.ts +307 -0
  183. package/src/theme/theme.scss +17 -0
  184. package/src/theme/useBackgroundLuminance.ts +145 -0
  185. package/src/theme/useSolarTime.ts +101 -0
  186. package/src/theme/useTheme.ts +141 -0
  187. package/src/tokens.scss +26 -0
@@ -0,0 +1,75 @@
1
+ import { computed, defineComponent, type PropType } from "vue";
2
+ import "./HkRadio.scss";
3
+
4
+ export interface HkRadioOption {
5
+ value: string | number;
6
+ label: string;
7
+ disabled?: boolean;
8
+ }
9
+
10
+ export default defineComponent({
11
+ name: "HkRadio",
12
+ props: {
13
+ modelValue: {
14
+ type: [String, Number] as PropType<string | number | null>,
15
+ default: null,
16
+ },
17
+ options: {
18
+ type: Array as PropType<HkRadioOption[]>,
19
+ required: true,
20
+ },
21
+ disabled: { type: Boolean, default: false },
22
+ direction: {
23
+ type: String as PropType<"horizontal" | "vertical">,
24
+ default: "horizontal",
25
+ },
26
+ size: {
27
+ type: String as PropType<"sm" | "md" | "lg">,
28
+ default: "md",
29
+ },
30
+ },
31
+ emits: {
32
+ "update:modelValue": (_value: string | number) => true,
33
+ },
34
+ setup(props, { emit }) {
35
+ return () => (
36
+ <div
37
+ class={[
38
+ "hk-radio-group",
39
+ `hk-radio-group-${props.direction}`,
40
+ `hk-radio-group-${props.size}`,
41
+ ]}
42
+ >
43
+ {props.options.map((opt) => {
44
+ const isSelected = props.modelValue === opt.value;
45
+ const isDisabled = props.disabled || opt.disabled === true;
46
+
47
+ return (
48
+ <label
49
+ key={opt.value}
50
+ class="hk-radio"
51
+ data-disabled={isDisabled ? "" : undefined}
52
+ data-size={props.size}
53
+ >
54
+ <span
55
+ class="hk-radio-box"
56
+ data-checked={isSelected ? "" : undefined}
57
+ >
58
+ <input
59
+ class="hk-radio-input"
60
+ type="radio"
61
+ value={opt.value}
62
+ checked={isSelected}
63
+ disabled={isDisabled}
64
+ onChange={() => emit("update:modelValue", opt.value)}
65
+ />
66
+ {isSelected && <span class="hk-radio-dot" />}
67
+ </span>
68
+ <span class="hk-radio-label-text">{opt.label}</span>
69
+ </label>
70
+ );
71
+ })}
72
+ </div>
73
+ );
74
+ },
75
+ });
@@ -0,0 +1,37 @@
1
+ .hk-rolling-number {
2
+ display: inline-flex;
3
+ font-variant-numeric: tabular-nums;
4
+ line-height: 1;
5
+
6
+ [data-el="char"] {
7
+ display: inline-block;
8
+ }
9
+
10
+ [data-el="slot"] {
11
+ display: inline-block;
12
+ position: relative;
13
+ overflow: hidden;
14
+ vertical-align: baseline;
15
+ height: 1em;
16
+ }
17
+
18
+ [data-el="roll"] {
19
+ display: flex;
20
+ flex-direction: column;
21
+ animation: hk-rolling-number-up var(--hi-duration-instant, 0.1s) cubic-bezier(0.4, 0, 0.2, 1) forwards;
22
+ }
23
+
24
+ [data-el="digit"] {
25
+ display: block;
26
+ height: 1em;
27
+ }
28
+ }
29
+
30
+ @keyframes hk-rolling-number-up {
31
+ from {
32
+ transform: translateY(0);
33
+ }
34
+ to {
35
+ transform: translateY(-1em);
36
+ }
37
+ }
@@ -0,0 +1,97 @@
1
+ import { defineComponent, onBeforeUnmount, ref, watch, type PropType } from "vue";
2
+
3
+ import { useReportedTransition } from "../composables/useReportedTransition";
4
+ import "./HkRollingNumber.scss";
5
+
6
+ export default defineComponent({
7
+ name: "HkRollingNumber",
8
+ props: {
9
+ value: { type: [Number, String] as PropType<number | string>, required: true },
10
+ },
11
+ setup(props) {
12
+ interface CharState {
13
+ current: string;
14
+ prev: string;
15
+ animating: boolean;
16
+ }
17
+
18
+ const chars = ref<CharState[]>([]);
19
+
20
+ const ROLL_ANIM_MS = 100;
21
+ const rollAnim = useReportedTransition(ROLL_ANIM_MS);
22
+
23
+ function update(newStr: string): boolean {
24
+ const newChars = newStr.split("");
25
+ const oldLen = chars.value.length;
26
+
27
+ if (newChars.length !== oldLen) {
28
+ chars.value = newChars.map((ch) => ({
29
+ current: ch,
30
+ prev: ch,
31
+ animating: false,
32
+ }));
33
+ return false;
34
+ }
35
+
36
+ let anyRolled = false;
37
+ for (let i = 0; i < newChars.length; i++) {
38
+ const prev = chars.value[i].current;
39
+ const next = newChars[i];
40
+ if (prev !== next) {
41
+ chars.value[i] = {
42
+ current: next,
43
+ prev,
44
+ animating: true,
45
+ };
46
+ anyRolled = true;
47
+ }
48
+ }
49
+ return anyRolled;
50
+ }
51
+
52
+ const stopWatch = watch(
53
+ () => String(props.value),
54
+ (val) => {
55
+ if (update(val)) rollAnim.run();
56
+ },
57
+ { immediate: true },
58
+ );
59
+ onBeforeUnmount(stopWatch);
60
+
61
+ function onAnimEnd(i: number) {
62
+ if (chars.value[i]) {
63
+ chars.value[i].animating = false;
64
+ }
65
+ }
66
+
67
+ return () => (
68
+ <span class="hk-rolling-number">
69
+ {chars.value.map((ch, i) => {
70
+ const isDigit = /^[0-9]$/.test(ch.current);
71
+ if (!isDigit || !ch.animating) {
72
+ return (
73
+ <span key={i} data-el="char">
74
+ {ch.current}
75
+ </span>
76
+ );
77
+ }
78
+ return (
79
+ <span key={i} data-el="slot">
80
+ <span
81
+ data-el="roll"
82
+ onAnimationend={() => onAnimEnd(i)}
83
+ >
84
+ <span data-el="digit" data-variant="old">
85
+ {ch.prev}
86
+ </span>
87
+ <span data-el="digit" data-variant="new">
88
+ {ch.current}
89
+ </span>
90
+ </span>
91
+ </span>
92
+ );
93
+ })}
94
+ </span>
95
+ );
96
+ },
97
+ });
@@ -0,0 +1,101 @@
1
+ .hk-scroll-container {
2
+ position: relative;
3
+ overflow: hidden;
4
+ display: flex;
5
+ flex-direction: column;
6
+ }
7
+
8
+ .hk-scroll-container-viewport {
9
+ flex: 1;
10
+ min-height: 0;
11
+ overflow: auto;
12
+ scrollbar-width: none !important;
13
+
14
+ &::-webkit-scrollbar {
15
+ display: none !important;
16
+ }
17
+ }
18
+
19
+ .hk-scroll-container[data-axis="horizontal"] > .hk-scroll-container-viewport {
20
+ overflow-x: auto;
21
+ overflow-y: hidden;
22
+ }
23
+
24
+ .hk-scroll-container-autofollow-content {
25
+ display: block;
26
+ flex-shrink: 0;
27
+ }
28
+
29
+ .hk-scroll-container-autotag {
30
+ position: absolute;
31
+ right: 0.625rem;
32
+ bottom: 0.625rem;
33
+ z-index: 6;
34
+ pointer-events: none;
35
+ user-select: none;
36
+ -webkit-user-select: none;
37
+ padding: 2px 8px;
38
+ font-size: 0.625rem;
39
+ letter-spacing: 0.04em;
40
+ color: var(--hi-color-muted);
41
+ background: color-mix(in srgb, var(--hi-color-surface) 78%, transparent);
42
+ border: 1px solid color-mix(in srgb, var(--hi-color-border) 12%, transparent);
43
+ border-radius: var(--hi-radius-full, 9999px);
44
+ backdrop-filter: blur(4px);
45
+ opacity: 0.85;
46
+ transition: opacity var(--hi-duration-fast, 0.15s) ease;
47
+ }
48
+
49
+ .hk-scrollbar-track {
50
+ position: absolute;
51
+ z-index: 10;
52
+ background: transparent;
53
+ pointer-events: auto;
54
+ opacity: 0;
55
+ transition: opacity var(--hi-duration-fast, 0.15s) ease;
56
+
57
+ &[data-axis="horizontal"] {
58
+ left: 4px;
59
+ right: 4px;
60
+ bottom: 2px;
61
+ height: 6px;
62
+ }
63
+
64
+ &:not([data-axis="horizontal"]) {
65
+ top: 4px;
66
+ bottom: 4px;
67
+ right: 2px;
68
+ width: 6px;
69
+ }
70
+
71
+ &.is-scrolling,
72
+ &.is-hovering {
73
+ opacity: 1;
74
+ }
75
+ }
76
+
77
+ .hk-scrollbar-thumb {
78
+ position: absolute;
79
+ border-radius: 3px;
80
+ background: color-mix(in srgb, var(--hi-color-muted) 35%, transparent);
81
+ transition: background var(--hi-duration-fast, 0.15s) ease;
82
+
83
+ .is-hovering &,
84
+ &.is-dragging {
85
+ background: color-mix(in srgb, var(--hi-color-muted) 55%, transparent);
86
+ }
87
+ }
88
+
89
+ .hk-scrollbar-track[data-axis="horizontal"] .hk-scrollbar-thumb {
90
+ top: 0;
91
+ left: 0;
92
+ height: 100%;
93
+ min-width: 20px;
94
+ }
95
+
96
+ .hk-scrollbar-track:not([data-axis="horizontal"]) .hk-scrollbar-thumb {
97
+ top: 0;
98
+ left: 0;
99
+ width: 100%;
100
+ min-height: 20px;
101
+ }
@@ -0,0 +1,362 @@
1
+ import {
2
+ computed,
3
+ defineComponent,
4
+ onBeforeUnmount,
5
+ onMounted,
6
+ ref,
7
+ shallowRef,
8
+ type PropType,
9
+ } from "vue";
10
+
11
+ import { useI18n } from "../i18n/context";
12
+ import "./HkScrollContainer.scss";
13
+ import { provideScrollWindow } from "../composables/useScrollWindow";
14
+ import { scheduleCronAfter, type CronHandle } from "../runtime/cronBus";
15
+ import { notifyScrollStart, onceFrame, scheduleFrame, type AnimationHandle } from "../runtime/animationBus";
16
+
17
+ type ScrollAxis = "vertical" | "horizontal" | "both";
18
+ type ScrollMode = "traditional" | "windowed";
19
+
20
+ interface AxisState {
21
+ track: HTMLElement;
22
+ thumb: HTMLElement;
23
+ dragging: boolean;
24
+ dragStartClient: number;
25
+ dragStartScroll: number;
26
+ hideTimer: CronHandle;
27
+ }
28
+
29
+ interface DragHandlers {
30
+ onDown: (e: MouseEvent) => void;
31
+ onMove: (e: MouseEvent) => void;
32
+ onUp: () => void;
33
+ onTrackClick: (e: MouseEvent) => void;
34
+ onEnter: () => void;
35
+ onLeave: () => void;
36
+ }
37
+
38
+ export default defineComponent({
39
+ name: "HkScrollContainer",
40
+ props: {
41
+ as: { type: String, default: "div" },
42
+ axis: { type: String as PropType<ScrollAxis>, default: "vertical" },
43
+ mode: { type: String as PropType<ScrollMode>, default: "traditional" },
44
+ scrollbar: { type: Boolean, default: true },
45
+ autoFollow: { type: Boolean, default: false },
46
+ overscanScreens: { type: Number, default: 1 },
47
+ },
48
+ setup(props, { slots, expose }) {
49
+ const { t } = useI18n();
50
+ const viewportRef = ref<HTMLElement>();
51
+ let ro: ResizeObserver;
52
+ let scheduled: AnimationHandle | null = null;
53
+ let vState: AxisState | null = null;
54
+ let hState: AxisState | null = null;
55
+ let vDrag: DragHandlers | null = null;
56
+ let hDrag: DragHandlers | null = null;
57
+
58
+ const pinned = ref(true);
59
+ const FOLLOW_THRESHOLD = 24;
60
+ const autoFollowContent = shallowRef<HTMLElement | null>(null);
61
+ let followRO: ResizeObserver | null = null;
62
+ const showAutoTag = computed(() => props.autoFollow && props.scrollbar && pinned.value);
63
+
64
+ if (props.mode === "windowed") {
65
+ provideScrollWindow(viewportRef as unknown as import("vue").Ref<HTMLElement | null>, props.overscanScreens);
66
+ }
67
+
68
+ const hasV = () => props.axis !== "horizontal";
69
+ const hasH = () => props.axis !== "vertical";
70
+
71
+ function recomputePinned() {
72
+ const vp = viewportRef.value;
73
+ if (!vp) return;
74
+ pinned.value = vp.scrollHeight - vp.scrollTop - vp.clientHeight < FOLLOW_THRESHOLD;
75
+ }
76
+
77
+ function pinToBottomIfPinned() {
78
+ const vp = viewportRef.value;
79
+ if (!vp || !pinned.value) return;
80
+ vp.scrollTop = vp.scrollHeight;
81
+ }
82
+
83
+ const SETTLE_FRAMES = 4;
84
+ let settleRemaining = 0;
85
+ let settleHandle: AnimationHandle | null = null;
86
+
87
+ function runSettleFrame() {
88
+ settleHandle = null;
89
+ pinToBottomIfPinned();
90
+ if (settleRemaining > 0) {
91
+ settleRemaining--;
92
+ settleHandle = scheduleFrame(runSettleFrame);
93
+ }
94
+ }
95
+
96
+ function kickSettle() {
97
+ settleRemaining = SETTLE_FRAMES;
98
+ if (settleHandle) return;
99
+ settleHandle = scheduleFrame(runSettleFrame);
100
+ }
101
+
102
+ function makeState(horizontal: boolean): AxisState {
103
+ const track = document.createElement("div");
104
+ track.className = "hk-scrollbar-track";
105
+ if (horizontal) track.setAttribute("data-axis", "horizontal");
106
+ const thumb = document.createElement("div");
107
+ thumb.className = "hk-scrollbar-thumb";
108
+ track.appendChild(thumb);
109
+ return {
110
+ track,
111
+ thumb,
112
+ dragging: false,
113
+ dragStartClient: 0,
114
+ dragStartScroll: 0,
115
+ hideTimer: undefined as unknown as CronHandle,
116
+ };
117
+ }
118
+
119
+ function scheduleUpdate() {
120
+ scheduled?.disconnect();
121
+ scheduled = scheduleFrame(() => {
122
+ scheduled = null;
123
+ update();
124
+ });
125
+ }
126
+
127
+ function update() {
128
+ const vp = viewportRef.value;
129
+ if (!vp) return;
130
+ if (vState) updateAxis(vp, vState, false);
131
+ if (hState) updateAxis(vp, hState, true);
132
+ }
133
+
134
+ function updateAxis(vp: HTMLElement, s: AxisState, horizontal: boolean) {
135
+ const scrollSize = horizontal ? vp.scrollWidth : vp.scrollHeight;
136
+ const clientSize = horizontal ? vp.clientWidth : vp.clientHeight;
137
+ const scrollPos = horizontal ? vp.scrollLeft : vp.scrollTop;
138
+ if (scrollSize <= clientSize) {
139
+ s.track.style.display = "none";
140
+ return;
141
+ }
142
+ s.track.style.display = "";
143
+ const trackSize = horizontal ? s.track.clientWidth : s.track.clientHeight;
144
+ const ratio = clientSize / scrollSize;
145
+ const thumbSize = Math.max(ratio * trackSize, 20);
146
+ const maxScroll = scrollSize - clientSize;
147
+ const maxTrack = trackSize - thumbSize;
148
+ const offset = maxScroll > 0 ? (scrollPos / maxScroll) * maxTrack : 0;
149
+ if (horizontal) {
150
+ s.thumb.style.width = `${thumbSize}px`;
151
+ s.thumb.style.transform = `translateX(${offset}px)`;
152
+ } else {
153
+ s.thumb.style.height = `${thumbSize}px`;
154
+ s.thumb.style.transform = `translateY(${offset}px)`;
155
+ }
156
+ }
157
+
158
+ function flash(s: AxisState | null) {
159
+ if (!s) return;
160
+ s.track.classList.add("is-scrolling");
161
+ s.hideTimer?.disconnect();
162
+ s.hideTimer = scheduleCronAfter(() => s.track.classList.remove("is-scrolling"), 1200);
163
+ }
164
+
165
+ function onScroll() {
166
+ if (!viewportRef.value) return;
167
+ scheduleUpdate();
168
+ if (props.scrollbar) notifyScrollStart();
169
+ flash(vState);
170
+ flash(hState);
171
+ if (props.autoFollow) recomputePinned();
172
+ }
173
+
174
+ function onWheel(e: WheelEvent) {
175
+ const vp = viewportRef.value;
176
+ if (!vp || !hasH()) return;
177
+ if (vp.scrollWidth <= vp.clientWidth) return;
178
+ const delta = e.deltaX !== 0 ? e.deltaX : e.deltaY;
179
+ if (delta === 0) return;
180
+ const prev = vp.scrollLeft;
181
+ vp.scrollLeft += delta;
182
+ if (vp.scrollLeft !== prev) {
183
+ e.preventDefault();
184
+ }
185
+ }
186
+
187
+ function makeDragHandlers(s: AxisState, horizontal: boolean): DragHandlers {
188
+ const onDown = (e: MouseEvent) => {
189
+ e.preventDefault();
190
+ e.stopPropagation();
191
+ const vp = viewportRef.value;
192
+ if (!vp) return;
193
+ s.dragging = true;
194
+ s.dragStartClient = horizontal ? e.clientX : e.clientY;
195
+ s.dragStartScroll = horizontal ? vp.scrollLeft : vp.scrollTop;
196
+ s.thumb.classList.add("is-dragging");
197
+ document.addEventListener("mousemove", onMove);
198
+ document.addEventListener("mouseup", onUp);
199
+ };
200
+ const onMove = (e: MouseEvent) => {
201
+ const vp = viewportRef.value;
202
+ if (!vp || !s.dragging) return;
203
+ const scrollSize = horizontal ? vp.scrollWidth : vp.scrollHeight;
204
+ const clientSize = horizontal ? vp.clientWidth : vp.clientHeight;
205
+ const delta = (horizontal ? e.clientX : e.clientY) - s.dragStartClient;
206
+ const thumbSize = Math.max((clientSize / scrollSize) * clientSize, 20);
207
+ const trackRange = clientSize - thumbSize;
208
+ if (trackRange <= 0) return;
209
+ const target = s.dragStartScroll + (delta / trackRange) * (scrollSize - clientSize);
210
+ if (horizontal) vp.scrollLeft = target;
211
+ else vp.scrollTop = target;
212
+ };
213
+ const onUp = () => {
214
+ s.dragging = false;
215
+ s.thumb.classList.remove("is-dragging");
216
+ document.removeEventListener("mousemove", onMove);
217
+ document.removeEventListener("mouseup", onUp);
218
+ };
219
+ const onTrackClick = (e: MouseEvent) => {
220
+ if (e.target === s.thumb) return;
221
+ const vp = viewportRef.value;
222
+ if (!vp) return;
223
+ const rect = s.track.getBoundingClientRect();
224
+ const ratio = horizontal
225
+ ? (e.clientX - rect.left) / rect.width
226
+ : (e.clientY - rect.top) / rect.height;
227
+ const max = (horizontal ? vp.scrollWidth : vp.scrollHeight) - (horizontal ? vp.clientWidth : vp.clientHeight);
228
+ if (horizontal) vp.scrollLeft = ratio * max;
229
+ else vp.scrollTop = ratio * max;
230
+ };
231
+ const onEnter = () => s.track.classList.add("is-hovering");
232
+ const onLeave = () => s.track.classList.remove("is-hovering");
233
+ return { onDown, onMove, onUp, onTrackClick, onEnter, onLeave };
234
+ }
235
+
236
+ function attachAxis(s: AxisState, d: DragHandlers) {
237
+ s.thumb.addEventListener("mousedown", d.onDown);
238
+ s.track.addEventListener("click", d.onTrackClick);
239
+ s.track.addEventListener("mouseenter", d.onEnter);
240
+ s.track.addEventListener("mouseleave", d.onLeave);
241
+ }
242
+
243
+ function detachAxis(s: AxisState | null, d: DragHandlers | null) {
244
+ if (!s || !d) return;
245
+ s.thumb.removeEventListener("mousedown", d.onDown);
246
+ s.track.removeEventListener("click", d.onTrackClick);
247
+ s.track.removeEventListener("mouseenter", d.onEnter);
248
+ s.track.removeEventListener("mouseleave", d.onLeave);
249
+ document.removeEventListener("mousemove", d.onMove);
250
+ document.removeEventListener("mouseup", d.onUp);
251
+ s.hideTimer?.disconnect();
252
+ s.track.remove();
253
+ }
254
+
255
+ onMounted(() => {
256
+ const vp = viewportRef.value;
257
+ if (!vp) return;
258
+ const containerEl = vp.parentElement;
259
+ if (!containerEl) return;
260
+
261
+ if (props.scrollbar) {
262
+ if (hasV()) {
263
+ vState = makeState(false);
264
+ containerEl.appendChild(vState.track);
265
+ vDrag = makeDragHandlers(vState, false);
266
+ attachAxis(vState, vDrag);
267
+ }
268
+ if (hasH()) {
269
+ hState = makeState(true);
270
+ containerEl.appendChild(hState.track);
271
+ hDrag = makeDragHandlers(hState, true);
272
+ attachAxis(hState, hDrag);
273
+ }
274
+ }
275
+
276
+ vp.addEventListener("scroll", onScroll, { passive: true });
277
+ vp.addEventListener("wheel", onWheel, { passive: false });
278
+
279
+ ro = new ResizeObserver(scheduleUpdate);
280
+ ro.observe(vp);
281
+
282
+ if (props.autoFollow && autoFollowContent.value) {
283
+ pinned.value = true;
284
+ onceFrame(() => {
285
+ pinToBottomIfPinned();
286
+ recomputePinned();
287
+ });
288
+ followRO = new ResizeObserver(() => {
289
+ pinToBottomIfPinned();
290
+ kickSettle();
291
+ });
292
+ followRO.observe(autoFollowContent.value!);
293
+ }
294
+
295
+ update();
296
+ });
297
+
298
+ onBeforeUnmount(() => {
299
+ const vp = viewportRef.value;
300
+ if (vp) {
301
+ vp.removeEventListener("scroll", onScroll);
302
+ vp.removeEventListener("wheel", onWheel);
303
+ }
304
+ detachAxis(vState, vDrag);
305
+ detachAxis(hState, hDrag);
306
+ scheduled?.disconnect();
307
+ settleHandle?.disconnect();
308
+ settleHandle = null;
309
+ ro?.disconnect();
310
+ followRO?.disconnect();
311
+ followRO = null;
312
+ });
313
+
314
+ function scrollTo(top: number, behavior: ScrollBehavior = "auto") {
315
+ const vp = viewportRef.value;
316
+ if (!vp) return;
317
+ vp.scrollTo({ top, behavior });
318
+ }
319
+
320
+ function scrollToElement(el: HTMLElement | null, behavior: ScrollBehavior = "auto") {
321
+ if (!el) return;
322
+ const vp = viewportRef.value;
323
+ if (!vp) {
324
+ el.scrollIntoView({ behavior, block: "start" });
325
+ return;
326
+ }
327
+ const target = el.getBoundingClientRect().top - vp.getBoundingClientRect().top + vp.scrollTop;
328
+ vp.scrollTo({ top: target, behavior });
329
+ }
330
+
331
+ function getScrollElement(): HTMLElement | undefined {
332
+ return viewportRef.value;
333
+ }
334
+
335
+ function getScrollTop(): number {
336
+ return viewportRef.value?.scrollTop ?? 0;
337
+ }
338
+
339
+ expose({ scrollTo, scrollToElement, getScrollElement, getScrollTop });
340
+
341
+ return () => {
342
+ const Tag = props.as as "div" | "section" | "nav" | "main" | "aside";
343
+ const content = props.autoFollow ? (
344
+ <div ref={autoFollowContent} class="hk-scroll-container-autofollow-content">
345
+ {slots.default?.()}
346
+ </div>
347
+ ) : (
348
+ slots.default?.()
349
+ );
350
+ return (
351
+ <Tag class="hk-scroll-container" data-axis={props.axis}>
352
+ <div ref={viewportRef} class="hk-scroll-container-viewport">
353
+ {content}
354
+ </div>
355
+ {showAutoTag.value && (
356
+ <span class="hk-scroll-container-autotag" aria-hidden="true">{t("hk.scrollContainer.auto", "Auto")}</span>
357
+ )}
358
+ </Tag>
359
+ );
360
+ };
361
+ },
362
+ });