bits-lab 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/dist/Accordion.d.ts +52 -0
- package/dist/Accordion.js +192 -0
- package/dist/AchievementToast.d.ts +36 -0
- package/dist/AchievementToast.js +162 -0
- package/dist/AgentCard.d.ts +41 -0
- package/dist/AgentCard.js +168 -0
- package/dist/ArcadeButtons.d.ts +23 -0
- package/dist/ArcadeButtons.js +169 -0
- package/dist/AuroraBands.d.ts +19 -0
- package/dist/AuroraBands.js +126 -0
- package/dist/AvatarTooltips.d.ts +32 -0
- package/dist/AvatarTooltips.js +121 -0
- package/dist/BarRace.d.ts +39 -0
- package/dist/BarRace.js +227 -0
- package/dist/BentoGrid.d.ts +50 -0
- package/dist/BentoGrid.js +145 -0
- package/dist/BootSequence.d.ts +48 -0
- package/dist/BootSequence.js +234 -0
- package/dist/BreadcrumbTrail.d.ts +31 -0
- package/dist/BreadcrumbTrail.js +181 -0
- package/dist/CRTPower.d.ts +30 -0
- package/dist/CRTPower.js +142 -0
- package/dist/CardDeck.d.ts +29 -0
- package/dist/CardDeck.js +154 -0
- package/dist/ChangelogTimeline.d.ts +35 -0
- package/dist/ChangelogTimeline.js +137 -0
- package/dist/ChapterProgress.d.ts +23 -0
- package/dist/ChapterProgress.js +211 -0
- package/dist/ChatBubbles.d.ts +42 -0
- package/dist/ChatBubbles.js +198 -0
- package/dist/ChiptuneKit.d.ts +158 -0
- package/dist/ChiptuneKit.js +207 -0
- package/dist/CipherCard.d.ts +22 -0
- package/dist/CipherCard.js +91 -0
- package/dist/CloudSky.d.ts +28 -0
- package/dist/CloudSky.js +222 -0
- package/dist/CodeRain.d.ts +24 -0
- package/dist/CodeRain.js +158 -0
- package/dist/CodeShowcase.d.ts +50 -0
- package/dist/CodeShowcase.js +307 -0
- package/dist/ColorDial.d.ts +42 -0
- package/dist/ColorDial.js +288 -0
- package/dist/CommandPalette.d.ts +59 -0
- package/dist/CommandPalette.js +349 -0
- package/dist/CompareSlider.d.ts +33 -0
- package/dist/CompareSlider.js +152 -0
- package/dist/Confetti.d.ts +61 -0
- package/dist/Confetti.js +176 -0
- package/dist/ConnectBeams.d.ts +39 -0
- package/dist/ConnectBeams.js +132 -0
- package/dist/ContextOrb.d.ts +34 -0
- package/dist/ContextOrb.js +269 -0
- package/dist/ContourLines.d.ts +20 -0
- package/dist/ContourLines.js +159 -0
- package/dist/Coverflow.d.ts +27 -0
- package/dist/Coverflow.js +167 -0
- package/dist/CursorAttractor.d.ts +21 -0
- package/dist/CursorAttractor.js +136 -0
- package/dist/CutReveal.d.ts +21 -0
- package/dist/CutReveal.js +116 -0
- package/dist/DateRange.d.ts +45 -0
- package/dist/DateRange.js +327 -0
- package/dist/DeviceFrame.d.ts +30 -0
- package/dist/DeviceFrame.js +145 -0
- package/dist/DiffReveal.d.ts +36 -0
- package/dist/DiffReveal.js +247 -0
- package/dist/DirectionHover.d.ts +15 -0
- package/dist/DirectionHover.js +90 -0
- package/dist/Discard.d.ts +18 -0
- package/dist/Discard.js +61 -0
- package/dist/DitherField.d.ts +15 -0
- package/dist/DitherField.js +157 -0
- package/dist/DotGlobe.d.ts +48 -0
- package/dist/DotGlobe.js +312 -0
- package/dist/DotMap.d.ts +47 -0
- package/dist/DotMap.js +172 -0
- package/dist/DropZone.d.ts +57 -0
- package/dist/DropZone.js +304 -0
- package/dist/DynamicIsland.d.ts +32 -0
- package/dist/DynamicIsland.js +92 -0
- package/dist/EdgeFX.d.ts +27 -0
- package/dist/EdgeFX.js +98 -0
- package/dist/ElasticString.d.ts +22 -0
- package/dist/ElasticString.js +127 -0
- package/dist/ExpandCard.d.ts +44 -0
- package/dist/ExpandCard.js +182 -0
- package/dist/FeatureTabs.d.ts +34 -0
- package/dist/FeatureTabs.js +169 -0
- package/dist/FeedList.d.ts +37 -0
- package/dist/FeedList.js +163 -0
- package/dist/FlickerGrid.d.ts +29 -0
- package/dist/FlickerGrid.js +156 -0
- package/dist/FocusGrid.d.ts +20 -0
- package/dist/FocusGrid.js +48 -0
- package/dist/FocusPull.d.ts +26 -0
- package/dist/FocusPull.js +106 -0
- package/dist/Footprints.d.ts +24 -0
- package/dist/Footprints.js +126 -0
- package/dist/FunnelFlow.d.ts +34 -0
- package/dist/FunnelFlow.js +267 -0
- package/dist/GameOver.d.ts +46 -0
- package/dist/GameOver.js +191 -0
- package/dist/GaugeMeter.d.ts +38 -0
- package/dist/GaugeMeter.js +195 -0
- package/dist/GlassPanel.d.ts +27 -0
- package/dist/GlassPanel.js +80 -0
- package/dist/GrainFX.d.ts +34 -0
- package/dist/GrainFX.js +122 -0
- package/dist/GravityBox.d.ts +30 -0
- package/dist/GravityBox.js +220 -0
- package/dist/HealthBar.d.ts +33 -0
- package/dist/HealthBar.js +140 -0
- package/dist/Heatmap.d.ts +42 -0
- package/dist/Heatmap.js +232 -0
- package/dist/HexField.d.ts +29 -0
- package/dist/HexField.js +188 -0
- package/dist/HoldToConfirm.d.ts +29 -0
- package/dist/HoldToConfirm.js +163 -0
- package/dist/HorizontalTrack.d.ts +28 -0
- package/dist/HorizontalTrack.js +195 -0
- package/dist/HotbarDock.d.ts +40 -0
- package/dist/HotbarDock.js +148 -0
- package/dist/InkText.d.ts +27 -0
- package/dist/InkText.js +65 -0
- package/dist/InlineEdit.d.ts +30 -0
- package/dist/InlineEdit.js +198 -0
- package/dist/InteractiveGrid.d.ts +25 -0
- package/dist/InteractiveGrid.js +194 -0
- package/dist/InventoryGrid.d.ts +45 -0
- package/dist/InventoryGrid.js +272 -0
- package/dist/JellyBlob.d.ts +30 -0
- package/dist/JellyBlob.js +209 -0
- package/dist/KineticHeadline.d.ts +30 -0
- package/dist/KineticHeadline.js +133 -0
- package/dist/LavaLamp.d.ts +21 -0
- package/dist/LavaLamp.js +133 -0
- package/dist/LetterSwap.d.ts +20 -0
- package/dist/LetterSwap.js +78 -0
- package/dist/LevelMap.d.ts +35 -0
- package/dist/LevelMap.js +189 -0
- package/dist/LightShafts.d.ts +22 -0
- package/dist/LightShafts.js +170 -0
- package/dist/LightboxGallery.d.ts +26 -0
- package/dist/LightboxGallery.js +327 -0
- package/dist/LinkPeek.d.ts +36 -0
- package/dist/LinkPeek.js +194 -0
- package/dist/LogoOrbit.d.ts +37 -0
- package/dist/LogoOrbit.js +117 -0
- package/dist/Marquee.d.ts +33 -0
- package/dist/Marquee.js +148 -0
- package/dist/MarqueeLine.d.ts +33 -0
- package/dist/MarqueeLine.js +167 -0
- package/dist/MasonryWall.d.ts +13 -0
- package/dist/MasonryWall.js +90 -0
- package/dist/MegaMenu.d.ts +32 -0
- package/dist/MegaMenu.js +280 -0
- package/dist/MeteorShower.d.ts +24 -0
- package/dist/MeteorShower.js +155 -0
- package/dist/Minimap.d.ts +21 -0
- package/dist/Minimap.js +236 -0
- package/dist/MobileTabBar.d.ts +38 -0
- package/dist/MobileTabBar.js +148 -0
- package/dist/MorphDialog.d.ts +34 -0
- package/dist/MorphDialog.js +199 -0
- package/dist/MorphPopover.d.ts +33 -0
- package/dist/MorphPopover.js +168 -0
- package/dist/MorphWords.d.ts +18 -0
- package/dist/MorphWords.js +136 -0
- package/dist/OTPInput.d.ts +43 -0
- package/dist/OTPInput.js +174 -0
- package/dist/OverlayKit.d.ts +97 -0
- package/dist/OverlayKit.js +466 -0
- package/dist/OverlayMenu.d.ts +32 -0
- package/dist/OverlayMenu.js +154 -0
- package/dist/ParallaxCollage.d.ts +41 -0
- package/dist/ParallaxCollage.js +143 -0
- package/dist/PasswordMeter.d.ts +44 -0
- package/dist/PasswordMeter.js +165 -0
- package/dist/PegBoard.d.ts +26 -0
- package/dist/PegBoard.js +184 -0
- package/dist/PinCard.d.ts +21 -0
- package/dist/PinCard.js +84 -0
- package/dist/PixelAvatar.d.ts +33 -0
- package/dist/PixelAvatar.js +144 -0
- package/dist/PixelButton.d.ts +20 -0
- package/dist/PixelButton.js +128 -0
- package/dist/PixelExplode.d.ts +31 -0
- package/dist/PixelExplode.js +164 -0
- package/dist/PixelLoader.d.ts +32 -0
- package/dist/PixelLoader.js +142 -0
- package/dist/PixelParallax.d.ts +41 -0
- package/dist/PixelParallax.js +151 -0
- package/dist/PixelSpinners.d.ts +21 -0
- package/dist/PixelSpinners.js +110 -0
- package/dist/PixelType.d.ts +26 -0
- package/dist/PixelType.js +220 -0
- package/dist/PointerKit.d.ts +23 -0
- package/dist/PointerKit.js +222 -0
- package/dist/Polaroids.d.ts +41 -0
- package/dist/Polaroids.js +293 -0
- package/dist/PressStart.d.ts +28 -0
- package/dist/PressStart.js +99 -0
- package/dist/PricingFlip.d.ts +52 -0
- package/dist/PricingFlip.js +190 -0
- package/dist/ProximityType.d.ts +26 -0
- package/dist/ProximityType.js +135 -0
- package/dist/QuestDialog.d.ts +50 -0
- package/dist/QuestDialog.js +204 -0
- package/dist/QuestLog.d.ts +39 -0
- package/dist/QuestLog.js +214 -0
- package/dist/RangeDual.d.ts +34 -0
- package/dist/RangeDual.js +238 -0
- package/dist/RatingPips.d.ts +25 -0
- package/dist/RatingPips.js +129 -0
- package/dist/RetroHorizon.d.ts +27 -0
- package/dist/RetroHorizon.js +169 -0
- package/dist/Reveal.d.ts +30 -0
- package/dist/Reveal.js +92 -0
- package/dist/RingProgress.d.ts +45 -0
- package/dist/RingProgress.js +180 -0
- package/dist/RollingNumber.d.ts +28 -0
- package/dist/RollingNumber.js +108 -0
- package/dist/ScanScope.d.ts +24 -0
- package/dist/ScanScope.js +164 -0
- package/dist/ScoreTicker.d.ts +32 -0
- package/dist/ScoreTicker.js +176 -0
- package/dist/ScreenScroller.d.ts +24 -0
- package/dist/ScreenScroller.js +138 -0
- package/dist/Screensaver.d.ts +31 -0
- package/dist/Screensaver.js +221 -0
- package/dist/ScrollTilt.d.ts +27 -0
- package/dist/ScrollTilt.js +100 -0
- package/dist/ScrollTypography.d.ts +26 -0
- package/dist/ScrollTypography.js +124 -0
- package/dist/ScrollZoom.d.ts +31 -0
- package/dist/ScrollZoom.js +123 -0
- package/dist/ScrubNumber.d.ts +29 -0
- package/dist/ScrubNumber.js +152 -0
- package/dist/ShadowStack.d.ts +28 -0
- package/dist/ShadowStack.js +116 -0
- package/dist/SideRail.d.ts +43 -0
- package/dist/SideRail.js +253 -0
- package/dist/SignalText.d.ts +25 -0
- package/dist/SignalText.js +178 -0
- package/dist/SkeletonShimmer.d.ts +18 -0
- package/dist/SkeletonShimmer.js +111 -0
- package/dist/SketchHighlight.d.ts +27 -0
- package/dist/SketchHighlight.js +180 -0
- package/dist/SlideTabs.d.ts +40 -0
- package/dist/SlideTabs.js +190 -0
- package/dist/SmartNavbar.d.ts +48 -0
- package/dist/SmartNavbar.js +188 -0
- package/dist/SparkChart.d.ts +41 -0
- package/dist/SparkChart.js +188 -0
- package/dist/Spoiler.d.ts +22 -0
- package/dist/Spoiler.js +97 -0
- package/dist/SpotlightTour.d.ts +38 -0
- package/dist/SpotlightTour.js +231 -0
- package/dist/SpringDrawer.d.ts +39 -0
- package/dist/SpringDrawer.js +360 -0
- package/dist/SpriteAnimator.d.ts +41 -0
- package/dist/SpriteAnimator.js +124 -0
- package/dist/StatCard.d.ts +43 -0
- package/dist/StatCard.js +168 -0
- package/dist/StepLoader.d.ts +41 -0
- package/dist/StepLoader.js +191 -0
- package/dist/StepperForm.d.ts +41 -0
- package/dist/StepperForm.js +206 -0
- package/dist/StickerBoard.d.ts +39 -0
- package/dist/StickerBoard.js +209 -0
- package/dist/StickyReveal.d.ts +42 -0
- package/dist/StickyReveal.js +181 -0
- package/dist/StoryViewer.d.ts +36 -0
- package/dist/StoryViewer.js +175 -0
- package/dist/StreamingText.d.ts +29 -0
- package/dist/StreamingText.js +271 -0
- package/dist/SuggestionChips.d.ts +32 -0
- package/dist/SuggestionChips.js +96 -0
- package/dist/SwipeActions.d.ts +30 -0
- package/dist/SwipeActions.js +141 -0
- package/dist/TagInput.d.ts +30 -0
- package/dist/TagInput.js +284 -0
- package/dist/TallyUp.d.ts +38 -0
- package/dist/TallyUp.js +157 -0
- package/dist/TerminalDemo.d.ts +63 -0
- package/dist/TerminalDemo.js +214 -0
- package/dist/TestimonialWall.d.ts +35 -0
- package/dist/TestimonialWall.js +131 -0
- package/dist/TextOnPath.d.ts +36 -0
- package/dist/TextOnPath.js +130 -0
- package/dist/ThinkingShimmer.d.ts +36 -0
- package/dist/ThinkingShimmer.js +132 -0
- package/dist/ToastStack.d.ts +62 -0
- package/dist/ToastStack.js +289 -0
- package/dist/ToggleGroup.d.ts +39 -0
- package/dist/ToggleGroup.js +190 -0
- package/dist/ToggleKit.d.ts +70 -0
- package/dist/ToggleKit.js +229 -0
- package/dist/TracingLine.d.ts +24 -0
- package/dist/TracingLine.js +139 -0
- package/dist/TradingCard.d.ts +30 -0
- package/dist/TradingCard.js +147 -0
- package/dist/VanishInput.d.ts +34 -0
- package/dist/VanishInput.js +190 -0
- package/dist/VideoHero.d.ts +32 -0
- package/dist/VideoHero.js +207 -0
- package/dist/VoiceOrb.d.ts +23 -0
- package/dist/VoiceOrb.js +182 -0
- package/dist/VoxelTiles.d.ts +38 -0
- package/dist/VoxelTiles.js +201 -0
- package/dist/WarpField.d.ts +18 -0
- package/dist/WarpField.js +160 -0
- package/dist/WebcamPixels.d.ts +38 -0
- package/dist/WebcamPixels.js +292 -0
- package/dist/WheelPicker.d.ts +29 -0
- package/dist/WheelPicker.js +199 -0
- package/dist/WobbleCard.d.ts +23 -0
- package/dist/WobbleCard.js +112 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +159 -0
- package/package.json +34 -4
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { Children, useEffect, useInsertionEffect, useRef, useState } from "react";
|
|
4
|
+
import Matter from "matter-js";
|
|
5
|
+
const STYLE_ID = "bl-gravity";
|
|
6
|
+
const CSS = `
|
|
7
|
+
.bl-grav { position: relative; width: 100%; height: var(--bl-h); overflow: hidden; border-radius: var(--bl-radius); background: var(--bl-bg); }
|
|
8
|
+
.bl-grav__layout { display: flex; flex-wrap: wrap; align-content: flex-start; justify-content: center; gap: var(--bl-gap); padding: var(--bl-pad); }
|
|
9
|
+
.bl-grav__item { display: inline-block; }
|
|
10
|
+
.bl-grav[data-live] .bl-grav__item { position: absolute; left: 0; top: 0; margin: 0; touch-action: none; cursor: grab; user-select: none; will-change: transform; }
|
|
11
|
+
.bl-grav[data-live] .bl-grav__item[data-held] { cursor: grabbing; z-index: 2; }
|
|
12
|
+
`;
|
|
13
|
+
function useStyles() {
|
|
14
|
+
useInsertionEffect(() => {
|
|
15
|
+
if (typeof document === "undefined") return;
|
|
16
|
+
const style = document.getElementById(STYLE_ID) ?? Object.assign(document.createElement("style"), { id: STYLE_ID });
|
|
17
|
+
style.textContent = CSS;
|
|
18
|
+
if (!style.isConnected) document.head.appendChild(style);
|
|
19
|
+
}, []);
|
|
20
|
+
}
|
|
21
|
+
const { Engine, Bodies, Body, Composite, Constraint, Sleeping } = Matter;
|
|
22
|
+
function GravityBox({
|
|
23
|
+
children,
|
|
24
|
+
height = 420,
|
|
25
|
+
gravity = 1,
|
|
26
|
+
restitution = 0.35,
|
|
27
|
+
friction = 0.2,
|
|
28
|
+
startOn = "view",
|
|
29
|
+
spin = true,
|
|
30
|
+
draggable = true,
|
|
31
|
+
sleep = true,
|
|
32
|
+
gap = 10,
|
|
33
|
+
padding = 20,
|
|
34
|
+
radius = 16,
|
|
35
|
+
backgroundColor = "transparent",
|
|
36
|
+
force = false,
|
|
37
|
+
onSettle,
|
|
38
|
+
className = "",
|
|
39
|
+
style
|
|
40
|
+
}) {
|
|
41
|
+
useStyles();
|
|
42
|
+
const rootRef = useRef(null);
|
|
43
|
+
const itemRefs = useRef([]);
|
|
44
|
+
const [live, setLive] = useState(false);
|
|
45
|
+
const [armed, setArmed] = useState(startOn === "mount");
|
|
46
|
+
const onSettleRef = useRef(onSettle);
|
|
47
|
+
onSettleRef.current = onSettle;
|
|
48
|
+
const items = Children.toArray(children);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (armed || startOn !== "view") return void 0;
|
|
51
|
+
const observer = new IntersectionObserver(([entry]) => entry.isIntersecting && setArmed(true), { threshold: 0.4 });
|
|
52
|
+
observer.observe(rootRef.current);
|
|
53
|
+
return () => observer.disconnect();
|
|
54
|
+
}, [armed, startOn]);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (!armed) return void 0;
|
|
57
|
+
if (!force && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return void 0;
|
|
58
|
+
const root = rootRef.current;
|
|
59
|
+
const nodes = itemRefs.current.filter(Boolean);
|
|
60
|
+
const base = root.getBoundingClientRect();
|
|
61
|
+
const boxes = nodes.map((node) => {
|
|
62
|
+
const r = node.getBoundingClientRect();
|
|
63
|
+
const radius2 = parseFloat(getComputedStyle(node.firstElementChild ?? node).borderTopLeftRadius) || 0;
|
|
64
|
+
return { x: r.left - base.left, y: r.top - base.top, w: r.width, h: r.height, radius: radius2 };
|
|
65
|
+
});
|
|
66
|
+
root.setAttribute("data-live", "");
|
|
67
|
+
setLive(true);
|
|
68
|
+
const engine = Engine.create({ enableSleeping: sleep });
|
|
69
|
+
engine.gravity.y = gravity;
|
|
70
|
+
const bodies = boxes.map((b) => {
|
|
71
|
+
const body = Bodies.rectangle(b.x + b.w / 2, b.y + b.h / 2, b.w, b.h, {
|
|
72
|
+
restitution,
|
|
73
|
+
friction,
|
|
74
|
+
frictionAir: 0.012,
|
|
75
|
+
chamfer: { radius: Math.min(b.radius, b.w / 2, b.h / 2) }
|
|
76
|
+
});
|
|
77
|
+
if (spin) Body.setAngularVelocity(body, (Math.random() - 0.5) * 0.08);
|
|
78
|
+
return body;
|
|
79
|
+
});
|
|
80
|
+
Composite.add(engine.world, bodies);
|
|
81
|
+
let walls = [];
|
|
82
|
+
const buildWalls = () => {
|
|
83
|
+
Composite.remove(engine.world, walls);
|
|
84
|
+
const w = root.clientWidth;
|
|
85
|
+
const h = root.clientHeight;
|
|
86
|
+
const t = 200;
|
|
87
|
+
walls = [
|
|
88
|
+
Bodies.rectangle(w / 2, h + t / 2, w + t * 2, t, { isStatic: true }),
|
|
89
|
+
Bodies.rectangle(-t / 2, h / 2, t, h * 3, { isStatic: true }),
|
|
90
|
+
Bodies.rectangle(w + t / 2, h / 2, t, h * 3, { isStatic: true })
|
|
91
|
+
];
|
|
92
|
+
Composite.add(engine.world, walls);
|
|
93
|
+
};
|
|
94
|
+
buildWalls();
|
|
95
|
+
const paint = () => {
|
|
96
|
+
bodies.forEach((body, i) => {
|
|
97
|
+
const b = boxes[i];
|
|
98
|
+
nodes[i].style.transform = `translate(${(body.position.x - b.w / 2).toFixed(2)}px, ${(body.position.y - b.h / 2).toFixed(2)}px) rotate(${body.angle.toFixed(4)}rad)`;
|
|
99
|
+
nodes[i].style.width = `${b.w}px`;
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
paint();
|
|
103
|
+
let frame = 0;
|
|
104
|
+
let visible = true;
|
|
105
|
+
let last = performance.now();
|
|
106
|
+
let settledOnce = false;
|
|
107
|
+
let awakeFor = 0;
|
|
108
|
+
let grab = null;
|
|
109
|
+
const loop = (now) => {
|
|
110
|
+
frame = 0;
|
|
111
|
+
const dt = Math.min(1e3 / 30, now - last);
|
|
112
|
+
last = now;
|
|
113
|
+
Engine.update(engine, dt);
|
|
114
|
+
awakeFor += dt;
|
|
115
|
+
paint();
|
|
116
|
+
const resting = !grab && awakeFor > 500 && bodies.every((body) => body.isSleeping || body.speed < 0.05);
|
|
117
|
+
if (resting && sleep) {
|
|
118
|
+
if (!settledOnce) {
|
|
119
|
+
settledOnce = true;
|
|
120
|
+
onSettleRef.current?.();
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (visible) frame = requestAnimationFrame(loop);
|
|
125
|
+
};
|
|
126
|
+
const wake = () => {
|
|
127
|
+
bodies.forEach((body) => Sleeping.set(body, false));
|
|
128
|
+
awakeFor = 0;
|
|
129
|
+
if (!frame && visible) {
|
|
130
|
+
last = performance.now();
|
|
131
|
+
frame = requestAnimationFrame(loop);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const onDown = (event) => {
|
|
135
|
+
if (!draggable || event.button > 0) return;
|
|
136
|
+
const index = nodes.findIndex((node) => node.contains(event.target));
|
|
137
|
+
if (index < 0 || event.target.closest("a, button, input, textarea, select")) return;
|
|
138
|
+
const rect = root.getBoundingClientRect();
|
|
139
|
+
const point = { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
|
140
|
+
const body = bodies[index];
|
|
141
|
+
const local = Matter.Vector.rotate(Matter.Vector.sub(point, body.position), -body.angle);
|
|
142
|
+
const constraint = Constraint.create({ pointA: point, bodyB: body, pointB: local, stiffness: 0.08, damping: 0.08, length: 0 });
|
|
143
|
+
Composite.add(engine.world, constraint);
|
|
144
|
+
grab = { constraint, node: nodes[index], id: event.pointerId };
|
|
145
|
+
nodes[index].setAttribute("data-held", "");
|
|
146
|
+
try {
|
|
147
|
+
root.setPointerCapture(event.pointerId);
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
wake();
|
|
151
|
+
};
|
|
152
|
+
const onMove = (event) => {
|
|
153
|
+
if (!grab || event.pointerId !== grab.id) return;
|
|
154
|
+
const rect = root.getBoundingClientRect();
|
|
155
|
+
grab.constraint.pointA = { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
|
156
|
+
};
|
|
157
|
+
const onUp = (event) => {
|
|
158
|
+
if (!grab || event.pointerId !== grab.id) return;
|
|
159
|
+
Composite.remove(engine.world, grab.constraint);
|
|
160
|
+
grab.node.removeAttribute("data-held");
|
|
161
|
+
grab = null;
|
|
162
|
+
wake();
|
|
163
|
+
};
|
|
164
|
+
const resize = new ResizeObserver(() => {
|
|
165
|
+
buildWalls();
|
|
166
|
+
wake();
|
|
167
|
+
});
|
|
168
|
+
resize.observe(root);
|
|
169
|
+
const io = new IntersectionObserver(([entry]) => {
|
|
170
|
+
visible = entry.isIntersecting;
|
|
171
|
+
if (visible) wake();
|
|
172
|
+
});
|
|
173
|
+
io.observe(root);
|
|
174
|
+
root.addEventListener("pointerdown", onDown);
|
|
175
|
+
root.addEventListener("pointermove", onMove);
|
|
176
|
+
root.addEventListener("pointerup", onUp);
|
|
177
|
+
root.addEventListener("pointercancel", onUp);
|
|
178
|
+
frame = requestAnimationFrame(loop);
|
|
179
|
+
return () => {
|
|
180
|
+
cancelAnimationFrame(frame);
|
|
181
|
+
resize.disconnect();
|
|
182
|
+
io.disconnect();
|
|
183
|
+
root.removeEventListener("pointerdown", onDown);
|
|
184
|
+
root.removeEventListener("pointermove", onMove);
|
|
185
|
+
root.removeEventListener("pointerup", onUp);
|
|
186
|
+
root.removeEventListener("pointercancel", onUp);
|
|
187
|
+
Engine.clear(engine);
|
|
188
|
+
nodes.forEach((node) => {
|
|
189
|
+
node.style.transform = "";
|
|
190
|
+
node.style.width = "";
|
|
191
|
+
});
|
|
192
|
+
root.removeAttribute("data-live");
|
|
193
|
+
setLive(false);
|
|
194
|
+
};
|
|
195
|
+
}, [armed, gravity, restitution, friction, spin, draggable, sleep, force, items.length]);
|
|
196
|
+
return /* @__PURE__ */ jsx(
|
|
197
|
+
"div",
|
|
198
|
+
{
|
|
199
|
+
ref: rootRef,
|
|
200
|
+
className: `bl-grav ${className}`,
|
|
201
|
+
"data-live": live || void 0,
|
|
202
|
+
onClick: () => startOn === "click" && setArmed(true),
|
|
203
|
+
style: { "--bl-h": `${height}px`, "--bl-gap": `${gap}px`, "--bl-pad": `${padding}px`, "--bl-radius": `${radius}px`, "--bl-bg": backgroundColor, ...style },
|
|
204
|
+
children: /* @__PURE__ */ jsx("div", { className: "bl-grav__layout", children: items.map((child, i) => /* @__PURE__ */ jsx(
|
|
205
|
+
"div",
|
|
206
|
+
{
|
|
207
|
+
ref: (node) => {
|
|
208
|
+
itemRefs.current[i] = node;
|
|
209
|
+
},
|
|
210
|
+
className: "bl-grav__item",
|
|
211
|
+
children: child
|
|
212
|
+
},
|
|
213
|
+
child.key ?? i
|
|
214
|
+
)) })
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
export {
|
|
219
|
+
GravityBox as default
|
|
220
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type CSSProperties } from 'react';
|
|
2
|
+
export interface HealthBarProps {
|
|
3
|
+
/** Current value. Changes animate: damage drains, heals fill. */
|
|
4
|
+
value?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
/** Shown above the bar and used as the meter's accessible name. */
|
|
7
|
+
label?: string;
|
|
8
|
+
/** Number of cells; 0 or 1 for a smooth bar. */
|
|
9
|
+
segments?: number;
|
|
10
|
+
/** Bar height, in px. */
|
|
11
|
+
height?: number;
|
|
12
|
+
color?: string;
|
|
13
|
+
/** Fill color while critical. */
|
|
14
|
+
lowColor?: string;
|
|
15
|
+
/** Ghost bar color after damage. */
|
|
16
|
+
damageColor?: string;
|
|
17
|
+
/** Ghost bar color while healing. */
|
|
18
|
+
healColor?: string;
|
|
19
|
+
trackColor?: string;
|
|
20
|
+
labelColor?: string;
|
|
21
|
+
/** Fraction of max at which the bar turns critical and pulses. */
|
|
22
|
+
lowThreshold?: number;
|
|
23
|
+
/** How long the damage ghost waits before draining, in ms. */
|
|
24
|
+
delay?: number;
|
|
25
|
+
/** How long the ghost drains (or the heal fills), in ms. */
|
|
26
|
+
drainDuration?: number;
|
|
27
|
+
/** Show the "72/100" readout. */
|
|
28
|
+
showValue?: boolean;
|
|
29
|
+
fontFamily?: string;
|
|
30
|
+
className?: string;
|
|
31
|
+
style?: CSSProperties;
|
|
32
|
+
}
|
|
33
|
+
export default function HealthBar({ value, max, label, segments, height, color, lowColor, damageColor, healColor, trackColor, labelColor, lowThreshold, delay, drainDuration, showValue, fontFamily, className, style }: HealthBarProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useInsertionEffect, useRef, useState } from "react";
|
|
4
|
+
const STYLE_ID = "bl-health-bar";
|
|
5
|
+
const CSS = `
|
|
6
|
+
.bl-hp { display: grid; gap: 6px; font-family: var(--bl-font, "Press Start 2P", ui-monospace, monospace); color: var(--bl-label); }
|
|
7
|
+
.bl-hp__head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; font-size: 10px; text-transform: uppercase; }
|
|
8
|
+
.bl-hp__value { font-variant-numeric: tabular-nums; }
|
|
9
|
+
.bl-hp__track {
|
|
10
|
+
position: relative; height: var(--bl-height); margin: 3px; background: var(--bl-track); overflow: hidden;
|
|
11
|
+
box-shadow: 0 -3px 0 0 #0b0b14, 0 3px 0 0 #0b0b14, -3px 0 0 0 #0b0b14, 3px 0 0 0 #0b0b14;
|
|
12
|
+
}
|
|
13
|
+
.bl-hp__ghost, .bl-hp__fill { position: absolute; inset: 0 auto 0 0; }
|
|
14
|
+
.bl-hp__ghost { background: var(--bl-ghost); }
|
|
15
|
+
.bl-hp__fill { background: var(--bl-fill); box-shadow: inset 0 calc(var(--bl-height) / 4) 0 0 rgba(255, 255, 255, 0.22), inset 0 calc(var(--bl-height) / -4) 0 0 rgba(0, 0, 0, 0.18); }
|
|
16
|
+
.bl-hp[data-mode="damage"] .bl-hp__fill { transition: width 80ms steps(2); }
|
|
17
|
+
.bl-hp[data-mode="damage"] .bl-hp__ghost { transition: width var(--bl-drain) cubic-bezier(.4, 0, .2, 1) var(--bl-delay); }
|
|
18
|
+
.bl-hp[data-mode="heal"] .bl-hp__ghost { transition: width 80ms steps(2); }
|
|
19
|
+
.bl-hp[data-mode="heal"] .bl-hp__fill { transition: width var(--bl-drain) cubic-bezier(.2, .8, .2, 1) var(--bl-delay); }
|
|
20
|
+
.bl-hp[data-flash] .bl-hp__fill { filter: brightness(2.2) saturate(0.4); }
|
|
21
|
+
.bl-hp__segments {
|
|
22
|
+
position: absolute; inset: 0; pointer-events: none;
|
|
23
|
+
background: repeating-linear-gradient(to right, transparent 0 calc(100% / var(--bl-segments) - 3px), #0b0b14 calc(100% / var(--bl-segments) - 3px) calc(100% / var(--bl-segments)));
|
|
24
|
+
}
|
|
25
|
+
.bl-hp__shimmer { position: absolute; inset: 0; pointer-events: none; background: linear-gradient(100deg, transparent 30%, rgba(255,255,255,0.55) 50%, transparent 70%); transform: translateX(-100%); }
|
|
26
|
+
.bl-hp[data-shimmer] .bl-hp__shimmer { animation: bl-hp-shimmer 700ms steps(8) 1; }
|
|
27
|
+
.bl-hp[data-critical] .bl-hp__track { animation: bl-hp-pulse 0.8s steps(2) infinite; }
|
|
28
|
+
@keyframes bl-hp-shimmer { to { transform: translateX(100%); } }
|
|
29
|
+
@keyframes bl-hp-pulse { 50% { box-shadow: 0 -3px 0 0 var(--bl-low), 0 3px 0 0 var(--bl-low), -3px 0 0 0 var(--bl-low), 3px 0 0 0 var(--bl-low); } }
|
|
30
|
+
@media (prefers-reduced-motion: reduce) {
|
|
31
|
+
.bl-hp .bl-hp__fill, .bl-hp .bl-hp__ghost { transition: none !important; }
|
|
32
|
+
.bl-hp[data-critical] .bl-hp__track, .bl-hp[data-shimmer] .bl-hp__shimmer { animation: none; }
|
|
33
|
+
}
|
|
34
|
+
`;
|
|
35
|
+
function useStyles() {
|
|
36
|
+
useInsertionEffect(() => {
|
|
37
|
+
if (typeof document === "undefined") return;
|
|
38
|
+
const style = document.getElementById(STYLE_ID) ?? Object.assign(document.createElement("style"), { id: STYLE_ID });
|
|
39
|
+
style.textContent = CSS;
|
|
40
|
+
if (!style.isConnected) document.head.appendChild(style);
|
|
41
|
+
}, []);
|
|
42
|
+
}
|
|
43
|
+
const clamp = (value, max) => Math.min(max, Math.max(0, value));
|
|
44
|
+
function HealthBar({
|
|
45
|
+
value = 72,
|
|
46
|
+
max = 100,
|
|
47
|
+
label = "HP",
|
|
48
|
+
segments = 10,
|
|
49
|
+
height = 20,
|
|
50
|
+
color = "#38b764",
|
|
51
|
+
lowColor = "#b13e53",
|
|
52
|
+
damageColor = "#f4f4f4",
|
|
53
|
+
healColor = "#a7f070",
|
|
54
|
+
trackColor = "#1a1c2c",
|
|
55
|
+
labelColor = "#f4f4f4",
|
|
56
|
+
lowThreshold = 0.25,
|
|
57
|
+
delay = 350,
|
|
58
|
+
drainDuration = 600,
|
|
59
|
+
showValue = true,
|
|
60
|
+
fontFamily,
|
|
61
|
+
className = "",
|
|
62
|
+
style
|
|
63
|
+
}) {
|
|
64
|
+
useStyles();
|
|
65
|
+
const current = clamp(value, max);
|
|
66
|
+
const previous = useRef(current);
|
|
67
|
+
const [mode, setMode] = useState("damage");
|
|
68
|
+
const [flash, setFlash] = useState(false);
|
|
69
|
+
const [shimmer, setShimmer] = useState(0);
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (current === previous.current) return void 0;
|
|
72
|
+
const damage = current < previous.current;
|
|
73
|
+
previous.current = current;
|
|
74
|
+
setMode(damage ? "damage" : "heal");
|
|
75
|
+
if (damage) {
|
|
76
|
+
setFlash(true);
|
|
77
|
+
const timer = setTimeout(() => setFlash(false), 140);
|
|
78
|
+
return () => clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
setShimmer((count) => count + 1);
|
|
81
|
+
return void 0;
|
|
82
|
+
}, [current]);
|
|
83
|
+
const ratio = max > 0 ? current / max : 0;
|
|
84
|
+
const critical = ratio > 0 && ratio <= lowThreshold;
|
|
85
|
+
const fill = critical ? lowColor : color;
|
|
86
|
+
return /* @__PURE__ */ jsxs(
|
|
87
|
+
"div",
|
|
88
|
+
{
|
|
89
|
+
className: `bl-hp ${className}`,
|
|
90
|
+
"data-mode": mode,
|
|
91
|
+
"data-flash": flash || void 0,
|
|
92
|
+
"data-critical": critical || void 0,
|
|
93
|
+
"data-shimmer": shimmer ? "" : void 0,
|
|
94
|
+
style: {
|
|
95
|
+
"--bl-height": `${height}px`,
|
|
96
|
+
"--bl-fill": fill,
|
|
97
|
+
"--bl-ghost": mode === "damage" ? damageColor : healColor,
|
|
98
|
+
"--bl-track": trackColor,
|
|
99
|
+
"--bl-label": labelColor,
|
|
100
|
+
"--bl-low": lowColor,
|
|
101
|
+
"--bl-segments": Math.max(1, segments),
|
|
102
|
+
"--bl-delay": `${delay}ms`,
|
|
103
|
+
"--bl-drain": `${drainDuration}ms`,
|
|
104
|
+
...fontFamily ? { "--bl-font": fontFamily } : null,
|
|
105
|
+
...style
|
|
106
|
+
},
|
|
107
|
+
children: [
|
|
108
|
+
(label || showValue) && /* @__PURE__ */ jsxs("div", { className: "bl-hp__head", "aria-hidden": "true", children: [
|
|
109
|
+
/* @__PURE__ */ jsx("span", { children: label }),
|
|
110
|
+
showValue && /* @__PURE__ */ jsxs("span", { className: "bl-hp__value", children: [
|
|
111
|
+
Math.round(current),
|
|
112
|
+
"/",
|
|
113
|
+
max
|
|
114
|
+
] })
|
|
115
|
+
] }),
|
|
116
|
+
/* @__PURE__ */ jsxs(
|
|
117
|
+
"div",
|
|
118
|
+
{
|
|
119
|
+
className: "bl-hp__track",
|
|
120
|
+
role: "meter",
|
|
121
|
+
"aria-label": label || "Health",
|
|
122
|
+
"aria-valuemin": 0,
|
|
123
|
+
"aria-valuemax": max,
|
|
124
|
+
"aria-valuenow": Math.round(current),
|
|
125
|
+
"aria-valuetext": `${Math.round(current)} of ${max}${critical ? ", critical" : ""}`,
|
|
126
|
+
children: [
|
|
127
|
+
/* @__PURE__ */ jsx("div", { className: "bl-hp__ghost", style: { width: `${ratio * 100}%` } }),
|
|
128
|
+
/* @__PURE__ */ jsx("div", { className: "bl-hp__fill", style: { width: `${ratio * 100}%` } }),
|
|
129
|
+
/* @__PURE__ */ jsx("div", { className: "bl-hp__shimmer" }, shimmer),
|
|
130
|
+
segments > 1 && /* @__PURE__ */ jsx("div", { className: "bl-hp__segments" })
|
|
131
|
+
]
|
|
132
|
+
}
|
|
133
|
+
)
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
export {
|
|
139
|
+
HealthBar as default
|
|
140
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type CSSProperties } from 'react';
|
|
2
|
+
export interface HeatDatum {
|
|
3
|
+
date: Date | string | number;
|
|
4
|
+
value: number;
|
|
5
|
+
}
|
|
6
|
+
export interface HeatDay {
|
|
7
|
+
date: Date;
|
|
8
|
+
week: number;
|
|
9
|
+
weekday: number;
|
|
10
|
+
value: number;
|
|
11
|
+
future: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface HeatmapProps {
|
|
14
|
+
data?: HeatDatum[];
|
|
15
|
+
/** The last day shown (default today). */
|
|
16
|
+
end?: Date | string | number;
|
|
17
|
+
weeks?: number;
|
|
18
|
+
weekStartsOn?: number;
|
|
19
|
+
/** Three cut-offs between the four non-zero levels (default quartiles). */
|
|
20
|
+
thresholds?: number[];
|
|
21
|
+
/** Empty plus four steps of one hue. */
|
|
22
|
+
colors?: string[];
|
|
23
|
+
title?: string;
|
|
24
|
+
unit?: string;
|
|
25
|
+
unitPlural?: string;
|
|
26
|
+
locale?: string;
|
|
27
|
+
cellSize?: number;
|
|
28
|
+
gap?: number;
|
|
29
|
+
radius?: number;
|
|
30
|
+
onSelect?: (day: HeatDay) => void;
|
|
31
|
+
textColor?: string;
|
|
32
|
+
mutedColor?: string;
|
|
33
|
+
dimColor?: string;
|
|
34
|
+
accentColor?: string;
|
|
35
|
+
tooltipColor?: string;
|
|
36
|
+
tooltipTextColor?: string;
|
|
37
|
+
fontFamily?: string;
|
|
38
|
+
className?: string;
|
|
39
|
+
style?: CSSProperties;
|
|
40
|
+
}
|
|
41
|
+
export declare const HEAT_RAMP: string[];
|
|
42
|
+
export default function Heatmap({ data, end, weeks, weekStartsOn, thresholds, colors, title, unit, unitPlural, locale, cellSize, gap, radius, onSelect, textColor, mutedColor, dimColor, accentColor, tooltipColor, tooltipTextColor, fontFamily, className, style }: HeatmapProps): import("react").JSX.Element;
|
package/dist/Heatmap.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useId, useInsertionEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
const STYLE_ID = "bl-heatmap";
|
|
5
|
+
const CSS = `
|
|
6
|
+
.bl-heat { position: relative; width: fit-content; max-width: 100%; color: var(--bl-ink); font-family: var(--bl-font); }
|
|
7
|
+
.bl-heat__head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 10px; }
|
|
8
|
+
.bl-heat__title { margin: 0; font-size: 14px; font-weight: 600; }
|
|
9
|
+
.bl-heat__scroll { overflow-x: auto; padding: 4px 2px 6px; scrollbar-width: thin; }
|
|
10
|
+
.bl-heat__frame { display: grid; grid-template-columns: auto 1fr; column-gap: 6px; width: max-content; }
|
|
11
|
+
.bl-heat__months { grid-column: 2; position: relative; height: 16px; color: var(--bl-dim); font-size: 11px; }
|
|
12
|
+
.bl-heat__months span { position: absolute; top: 0; white-space: nowrap; }
|
|
13
|
+
.bl-heat__days { display: grid; grid-template-rows: repeat(7, var(--bl-cell)); row-gap: var(--bl-gap); color: var(--bl-dim); font-size: 10px; line-height: var(--bl-cell); }
|
|
14
|
+
.bl-heat__grid { display: grid; grid-template-rows: repeat(7, var(--bl-cell)); grid-auto-columns: var(--bl-cell); grid-auto-flow: column; gap: var(--bl-gap); outline: none; }
|
|
15
|
+
.bl-heat__grid:focus-visible { outline: 2px solid var(--bl-accent); outline-offset: 4px; border-radius: 4px; }
|
|
16
|
+
.bl-heat__row { display: contents; }
|
|
17
|
+
.bl-heat__cell { border-radius: var(--bl-radius); background: var(--c); box-shadow: inset 0 0 0 1px rgba(255,255,255,0.04); animation: bl-heat-in 420ms cubic-bezier(.3,1.4,.5,1) both; animation-delay: calc(var(--wk) * 14ms); }
|
|
18
|
+
.bl-heat__cell[data-active] { box-shadow: 0 0 0 2px var(--bl-ink); }
|
|
19
|
+
.bl-heat__cell[data-out] { visibility: hidden; }
|
|
20
|
+
.bl-heat__foot { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 10px; margin-top: 8px; color: var(--bl-muted); font-size: 12px; }
|
|
21
|
+
.bl-heat__legend { display: flex; align-items: center; gap: 4px; }
|
|
22
|
+
.bl-heat__legend i { width: 11px; height: 11px; border-radius: 2px; }
|
|
23
|
+
.bl-heat__tip { position: absolute; z-index: 5; padding: 6px 9px; border-radius: 8px; background: var(--bl-tip); color: var(--bl-tip-ink); box-shadow: 0 10px 24px -10px rgba(0,0,0,0.8); font-size: 12px; line-height: 1.35; white-space: nowrap; pointer-events: none; transform: translate(-50%, calc(-100% - 8px)); }
|
|
24
|
+
.bl-heat__tip b { display: block; font-size: 14px; }
|
|
25
|
+
|
|
26
|
+
@keyframes bl-heat-in { from { opacity: 0; transform: scale(0.3); } }
|
|
27
|
+
@media (prefers-reduced-motion: reduce) { .bl-heat__cell { animation: none; } }
|
|
28
|
+
`;
|
|
29
|
+
function useStyles() {
|
|
30
|
+
useInsertionEffect(() => {
|
|
31
|
+
if (typeof document === "undefined") return;
|
|
32
|
+
const style = document.getElementById(STYLE_ID) ?? Object.assign(document.createElement("style"), { id: STYLE_ID });
|
|
33
|
+
style.textContent = CSS;
|
|
34
|
+
if (!style.isConnected) document.head.appendChild(style);
|
|
35
|
+
}, []);
|
|
36
|
+
}
|
|
37
|
+
const HEAT_RAMP = ["#262b44", "#184f95", "#256abf", "#3987e5", "#86b6ef"];
|
|
38
|
+
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
39
|
+
const keyOf = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
40
|
+
function Heatmap({
|
|
41
|
+
data = [],
|
|
42
|
+
end,
|
|
43
|
+
weeks = 53,
|
|
44
|
+
weekStartsOn = 0,
|
|
45
|
+
thresholds,
|
|
46
|
+
colors = HEAT_RAMP,
|
|
47
|
+
title,
|
|
48
|
+
unit = "contribution",
|
|
49
|
+
unitPlural,
|
|
50
|
+
locale,
|
|
51
|
+
cellSize = 12,
|
|
52
|
+
gap = 3,
|
|
53
|
+
radius = 2,
|
|
54
|
+
onSelect,
|
|
55
|
+
textColor = "#f4f4f4",
|
|
56
|
+
mutedColor = "#94b0c2",
|
|
57
|
+
dimColor = "#566c86",
|
|
58
|
+
accentColor = "#73eff7",
|
|
59
|
+
tooltipColor = "#f4f4f4",
|
|
60
|
+
tooltipTextColor = "#1a1c2c",
|
|
61
|
+
fontFamily = "ui-sans-serif, system-ui, sans-serif",
|
|
62
|
+
className = "",
|
|
63
|
+
style
|
|
64
|
+
}) {
|
|
65
|
+
useStyles();
|
|
66
|
+
const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "");
|
|
67
|
+
const plural = unitPlural ?? `${unit}s`;
|
|
68
|
+
const last = startOfDay(end ? new Date(end) : /* @__PURE__ */ new Date());
|
|
69
|
+
const first = new Date(last.getFullYear(), last.getMonth(), last.getDate() - (last.getDay() - weekStartsOn + 7) % 7 - (weeks - 1) * 7);
|
|
70
|
+
const values = useMemo(() => {
|
|
71
|
+
const map = /* @__PURE__ */ new Map();
|
|
72
|
+
for (const entry of data) {
|
|
73
|
+
const d = startOfDay(new Date(entry.date));
|
|
74
|
+
map.set(keyOf(d), (map.get(keyOf(d)) ?? 0) + (Number(entry.value) || 0));
|
|
75
|
+
}
|
|
76
|
+
return map;
|
|
77
|
+
}, [data]);
|
|
78
|
+
const cuts = useMemo(() => {
|
|
79
|
+
if (thresholds) return thresholds;
|
|
80
|
+
const sorted = [...values.values()].filter((v) => v > 0).sort((a, b) => a - b);
|
|
81
|
+
if (!sorted.length) return [1, 2, 3];
|
|
82
|
+
const q = (p) => sorted[Math.min(sorted.length - 1, Math.floor(p * sorted.length))];
|
|
83
|
+
return [q(0.25), q(0.5), q(0.75)];
|
|
84
|
+
}, [values, thresholds]);
|
|
85
|
+
const levelOf = (v) => v <= 0 ? 0 : v <= cuts[0] ? 1 : v <= cuts[1] ? 2 : v <= cuts[2] ? 3 : 4;
|
|
86
|
+
const cells = useMemo(
|
|
87
|
+
() => Array.from({ length: weeks * 7 }, (_, i) => {
|
|
88
|
+
const date = new Date(first.getFullYear(), first.getMonth(), first.getDate() + i);
|
|
89
|
+
return { date, week: Math.floor(i / 7), weekday: i % 7, value: values.get(keyOf(date)) ?? 0, future: date > last };
|
|
90
|
+
}),
|
|
91
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
92
|
+
[values, weeks, weekStartsOn, last.getTime()]
|
|
93
|
+
);
|
|
94
|
+
const total = cells.reduce((sum, c) => sum + (c.future ? 0 : c.value), 0);
|
|
95
|
+
const [active, setActive] = useState(cells.length - 1 - cells.filter((c) => c.future).length);
|
|
96
|
+
const [tip, setTip] = useState(null);
|
|
97
|
+
const scrollRef = useRef(null);
|
|
98
|
+
const gridRef = useRef(null);
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
const node = scrollRef.current;
|
|
101
|
+
if (node) node.scrollLeft = node.scrollWidth;
|
|
102
|
+
}, [weeks]);
|
|
103
|
+
const fmtDate = (d) => new Intl.DateTimeFormat(locale, { weekday: "short", month: "short", day: "numeric", year: "numeric" }).format(d);
|
|
104
|
+
const describe = (c) => `${c.value.toLocaleString(locale)} ${c.value === 1 ? unit : plural}, ${fmtDate(c.date)}`;
|
|
105
|
+
const showTip = (index) => {
|
|
106
|
+
const cell = cells[index];
|
|
107
|
+
const node = gridRef.current?.querySelector(`[data-i="${index}"]`);
|
|
108
|
+
if (!cell || !node || cell.future) return setTip(null);
|
|
109
|
+
const box = node.getBoundingClientRect();
|
|
110
|
+
const base = gridRef.current.closest(".bl-heat").getBoundingClientRect();
|
|
111
|
+
setTip({ index, x: box.left - base.left + box.width / 2, y: box.top - base.top });
|
|
112
|
+
};
|
|
113
|
+
const onKeyDown = (event) => {
|
|
114
|
+
const moves = { ArrowUp: -1, ArrowDown: 1, ArrowLeft: -7, ArrowRight: 7, Home: -active % 7, End: 6 - active % 7 };
|
|
115
|
+
if (!(event.key in moves)) {
|
|
116
|
+
if ((event.key === "Enter" || event.key === " ") && onSelect) {
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
onSelect(cells[active]);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
let next = active + moves[event.key];
|
|
124
|
+
while (next >= 0 && next < cells.length && cells[next].future) next -= 7;
|
|
125
|
+
if (next < 0 || next >= cells.length) return;
|
|
126
|
+
setActive(next);
|
|
127
|
+
showTip(next);
|
|
128
|
+
gridRef.current?.querySelector(`[data-i="${next}"]`)?.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
129
|
+
};
|
|
130
|
+
const months = [];
|
|
131
|
+
for (let week = 0; week < weeks; week++) {
|
|
132
|
+
const date = cells[week * 7].date;
|
|
133
|
+
const previous = week > 0 ? cells[(week - 1) * 7].date : null;
|
|
134
|
+
if (previous && previous.getMonth() === date.getMonth()) continue;
|
|
135
|
+
if (months.length && week - months[months.length - 1].week < 3) {
|
|
136
|
+
if (months[months.length - 1].week === 0) months.pop();
|
|
137
|
+
else continue;
|
|
138
|
+
}
|
|
139
|
+
months.push({ week, text: new Intl.DateTimeFormat(locale, { month: "short" }).format(date) });
|
|
140
|
+
}
|
|
141
|
+
const dayNames = Array.from({ length: 7 }, (_, i) => new Intl.DateTimeFormat(locale, { weekday: "short" }).format(new Date(2024, 0, 7 + (i + weekStartsOn) % 7)));
|
|
142
|
+
const step = cellSize + gap;
|
|
143
|
+
return /* @__PURE__ */ jsxs(
|
|
144
|
+
"div",
|
|
145
|
+
{
|
|
146
|
+
className: `bl-heat ${className}`,
|
|
147
|
+
style: {
|
|
148
|
+
"--bl-cell": `${cellSize}px`,
|
|
149
|
+
"--bl-gap": `${gap}px`,
|
|
150
|
+
"--bl-radius": `${radius}px`,
|
|
151
|
+
"--bl-ink": textColor,
|
|
152
|
+
"--bl-muted": mutedColor,
|
|
153
|
+
"--bl-dim": dimColor,
|
|
154
|
+
"--bl-accent": accentColor,
|
|
155
|
+
"--bl-tip": tooltipColor,
|
|
156
|
+
"--bl-tip-ink": tooltipTextColor,
|
|
157
|
+
"--bl-font": fontFamily,
|
|
158
|
+
...style
|
|
159
|
+
},
|
|
160
|
+
children: [
|
|
161
|
+
title && /* @__PURE__ */ jsx("div", { className: "bl-heat__head", children: /* @__PURE__ */ jsx("p", { className: "bl-heat__title", id: `${uid}-title`, children: title }) }),
|
|
162
|
+
/* @__PURE__ */ jsx("div", { ref: scrollRef, className: "bl-heat__scroll", children: /* @__PURE__ */ jsxs("div", { className: "bl-heat__frame", children: [
|
|
163
|
+
/* @__PURE__ */ jsx("div", { className: "bl-heat__months", "aria-hidden": "true", style: { width: weeks * step - gap }, children: months.map((m) => /* @__PURE__ */ jsx("span", { style: { left: m.week * step }, children: m.text }, m.week)) }),
|
|
164
|
+
/* @__PURE__ */ jsx("div", { className: "bl-heat__days", "aria-hidden": "true", children: dayNames.map((name, i) => /* @__PURE__ */ jsx("span", { children: i % 2 === 1 ? name : "" }, name)) }),
|
|
165
|
+
/* @__PURE__ */ jsx(
|
|
166
|
+
"div",
|
|
167
|
+
{
|
|
168
|
+
ref: gridRef,
|
|
169
|
+
className: "bl-heat__grid",
|
|
170
|
+
role: "grid",
|
|
171
|
+
tabIndex: 0,
|
|
172
|
+
"aria-label": `${title ?? "Activity"}: ${total.toLocaleString(locale)} ${plural}. Use the arrow keys to read each day.`,
|
|
173
|
+
"aria-activedescendant": `${uid}-d${active}`,
|
|
174
|
+
onKeyDown,
|
|
175
|
+
onFocus: () => showTip(active),
|
|
176
|
+
onBlur: () => setTip(null),
|
|
177
|
+
onPointerLeave: () => setTip(null),
|
|
178
|
+
children: Array.from({ length: 7 }, (_, row) => /* @__PURE__ */ jsx("div", { className: "bl-heat__row", role: "row", children: cells.filter((c) => c.weekday === row).map((c) => {
|
|
179
|
+
const i = c.week * 7 + c.weekday;
|
|
180
|
+
return /* @__PURE__ */ jsx(
|
|
181
|
+
"span",
|
|
182
|
+
{
|
|
183
|
+
id: `${uid}-d${i}`,
|
|
184
|
+
"data-i": i,
|
|
185
|
+
role: "gridcell",
|
|
186
|
+
"aria-label": c.future ? void 0 : describe(c),
|
|
187
|
+
"aria-hidden": c.future || void 0,
|
|
188
|
+
"aria-selected": i === active,
|
|
189
|
+
className: "bl-heat__cell",
|
|
190
|
+
"data-out": c.future || void 0,
|
|
191
|
+
"data-active": tip && tip.index === i || void 0,
|
|
192
|
+
style: { gridColumn: c.week + 1, gridRow: row + 1, "--c": colors[levelOf(c.value)], "--wk": c.week },
|
|
193
|
+
onPointerEnter: () => showTip(i),
|
|
194
|
+
onClick: () => {
|
|
195
|
+
setActive(i);
|
|
196
|
+
if (!c.future) onSelect?.(c);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
i
|
|
200
|
+
);
|
|
201
|
+
}) }, row))
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
] }) }),
|
|
205
|
+
/* @__PURE__ */ jsxs("div", { className: "bl-heat__foot", children: [
|
|
206
|
+
/* @__PURE__ */ jsxs("span", { children: [
|
|
207
|
+
total.toLocaleString(locale),
|
|
208
|
+
" ",
|
|
209
|
+
plural
|
|
210
|
+
] }),
|
|
211
|
+
/* @__PURE__ */ jsxs("span", { className: "bl-heat__legend", "aria-hidden": "true", children: [
|
|
212
|
+
"Less",
|
|
213
|
+
colors.map((c) => /* @__PURE__ */ jsx("i", { style: { background: c } }, c)),
|
|
214
|
+
"More"
|
|
215
|
+
] })
|
|
216
|
+
] }),
|
|
217
|
+
tip && cells[tip.index] && /* @__PURE__ */ jsxs("div", { className: "bl-heat__tip", "aria-hidden": "true", style: { left: tip.x, top: tip.y }, children: [
|
|
218
|
+
/* @__PURE__ */ jsxs("b", { children: [
|
|
219
|
+
cells[tip.index].value.toLocaleString(locale),
|
|
220
|
+
" ",
|
|
221
|
+
cells[tip.index].value === 1 ? unit : plural
|
|
222
|
+
] }),
|
|
223
|
+
fmtDate(cells[tip.index].date)
|
|
224
|
+
] })
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
export {
|
|
230
|
+
HEAT_RAMP,
|
|
231
|
+
Heatmap as default
|
|
232
|
+
};
|