@kumix/ui 0.4.3 → 0.5.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/README.md +15 -13
- package/dist/components/agents/agent-activity/index.js +1 -1
- package/dist/components/agents/approval-card/index.js +2 -3
- package/dist/components/agents/approval-card/index.js.map +1 -1
- package/dist/components/agents/prompt-input.js +1 -2
- package/dist/components/agents/prompt-input.js.map +1 -1
- package/dist/components/agents/todo-list.js +1 -1
- package/dist/components/agents/tool-result.js +1 -1
- package/dist/components/charts/bar-chart/context.d.ts +114 -0
- package/dist/components/charts/bar-chart/context.d.ts.map +1 -0
- package/dist/components/charts/bar-chart/context.js +60 -0
- package/dist/components/charts/bar-chart/context.js.map +1 -0
- package/dist/components/charts/bar-chart/legend.d.ts +6 -0
- package/dist/components/charts/bar-chart/legend.d.ts.map +1 -0
- package/dist/components/charts/bar-chart/legend.js +21 -0
- package/dist/components/charts/bar-chart/legend.js.map +1 -0
- package/dist/components/charts/bar-chart/plot.d.ts +10 -0
- package/dist/components/charts/bar-chart/plot.d.ts.map +1 -0
- package/dist/components/charts/bar-chart/plot.js +290 -0
- package/dist/components/charts/bar-chart/plot.js.map +1 -0
- package/dist/components/charts/bar-chart/tooltip.d.ts +20 -0
- package/dist/components/charts/bar-chart/tooltip.d.ts.map +1 -0
- package/dist/components/charts/bar-chart/tooltip.js +57 -0
- package/dist/components/charts/bar-chart/tooltip.js.map +1 -0
- package/dist/components/charts/bar-chart.d.ts +9 -0
- package/dist/components/charts/bar-chart.d.ts.map +1 -0
- package/dist/components/charts/bar-chart.js +51 -0
- package/dist/components/charts/bar-chart.js.map +1 -0
- package/dist/components/charts/bump-chart/plot.js +1 -1
- package/dist/components/charts/bump-chart/point.js +1 -1
- package/dist/components/charts/bump-chart.js +1 -1
- package/dist/components/charts/composition-chart/plot.js +5 -1
- package/dist/components/charts/composition-chart/plot.js.map +1 -1
- package/dist/components/charts/composition-chart/tooltip-content.js +1 -1
- package/dist/components/charts/donut-chart.d.ts +1 -1
- package/dist/components/charts/donut-chart.js +1 -1
- package/dist/components/charts/funnel-chart.js +1 -1
- package/dist/components/charts/heat-calendar/tooltip.js +1 -1
- package/dist/components/charts/line-chart/context.d.ts +154 -0
- package/dist/components/charts/line-chart/context.d.ts.map +1 -0
- package/dist/components/charts/line-chart/context.js +67 -0
- package/dist/components/charts/line-chart/context.js.map +1 -0
- package/dist/components/charts/line-chart/legend.d.ts +6 -0
- package/dist/components/charts/line-chart/legend.d.ts.map +1 -0
- package/dist/components/charts/line-chart/legend.js +21 -0
- package/dist/components/charts/line-chart/legend.js.map +1 -0
- package/dist/components/charts/line-chart/plot.d.ts +12 -0
- package/dist/components/charts/line-chart/plot.d.ts.map +1 -0
- package/dist/components/charts/line-chart/plot.js +604 -0
- package/dist/components/charts/line-chart/plot.js.map +1 -0
- package/dist/components/charts/line-chart/tooltip.d.ts +22 -0
- package/dist/components/charts/line-chart/tooltip.d.ts.map +1 -0
- package/dist/components/charts/line-chart/tooltip.js +108 -0
- package/dist/components/charts/line-chart/tooltip.js.map +1 -0
- package/dist/components/charts/line-chart.d.ts +10 -0
- package/dist/components/charts/line-chart.d.ts.map +1 -0
- package/dist/components/charts/line-chart.js +53 -0
- package/dist/components/charts/line-chart.js.map +1 -0
- package/dist/components/charts/liquidity-heatmap.js +1 -1
- package/dist/components/charts/price-target-fan.js +1 -1
- package/dist/components/charts/returns-calendar/tooltip.js +1 -1
- package/dist/components/charts/returns-calendar.d.ts.map +1 -1
- package/dist/components/charts/sankey-chart/context.d.ts +2 -0
- package/dist/components/charts/sankey-chart/context.js +2 -0
- package/dist/components/charts/sankey-chart/plot.d.ts +8 -0
- package/dist/components/charts/sankey-chart/plot.d.ts.map +1 -0
- package/dist/components/charts/sankey-chart/plot.js +342 -0
- package/dist/components/charts/sankey-chart/plot.js.map +1 -0
- package/dist/components/charts/sankey-chart/tooltip.d.ts +10 -0
- package/dist/components/charts/sankey-chart/tooltip.d.ts.map +1 -0
- package/dist/components/charts/sankey-chart/tooltip.js +147 -0
- package/dist/components/charts/sankey-chart/tooltip.js.map +1 -0
- package/dist/components/charts/sankey-chart.d.ts +17 -0
- package/dist/components/charts/sankey-chart.d.ts.map +1 -0
- package/dist/components/charts/sankey-chart.js +76 -0
- package/dist/components/charts/sankey-chart.js.map +1 -0
- package/dist/components/charts/sparkline.d.ts +26 -0
- package/dist/components/charts/sparkline.d.ts.map +1 -0
- package/dist/components/charts/sparkline.js +197 -0
- package/dist/components/charts/sparkline.js.map +1 -0
- package/dist/components/charts/treemap/tooltip.js +1 -1
- package/dist/components/charts/treemap.d.ts.map +1 -1
- package/dist/components/charts/volume-profile/tooltip.js +1 -1
- package/dist/components/charts/xy/axis.d.ts +24 -0
- package/dist/components/charts/xy/axis.d.ts.map +1 -0
- package/dist/components/charts/xy/axis.js +90 -0
- package/dist/components/charts/xy/axis.js.map +1 -0
- package/dist/components/charts/xy/data-table.d.ts +9 -0
- package/dist/components/charts/xy/data-table.d.ts.map +1 -0
- package/dist/components/charts/xy/data-table.js +29 -0
- package/dist/components/charts/xy/data-table.js.map +1 -0
- package/dist/components/charts/xy/inspector.d.ts +27 -0
- package/dist/components/charts/xy/inspector.d.ts.map +1 -0
- package/dist/components/charts/xy/inspector.js +110 -0
- package/dist/components/charts/xy/inspector.js.map +1 -0
- package/dist/components/charts/xy/legend.d.ts +13 -0
- package/dist/components/charts/xy/legend.d.ts.map +1 -0
- package/dist/components/charts/xy/legend.js +42 -0
- package/dist/components/charts/xy/legend.js.map +1 -0
- package/dist/components/charts/xy/tooltip-content.d.ts +2 -0
- package/dist/components/charts/xy/tooltip-content.js +66 -0
- package/dist/components/charts/xy/tooltip-content.js.map +1 -0
- package/dist/components/motion/attachment-upload.js +1 -1
- package/dist/components/motion/availability-scheduler/copy-menu.js +1 -1
- package/dist/components/motion/availability-scheduler/day-row.js +2 -2
- package/dist/components/motion/availability-scheduler/index.js +1 -1
- package/dist/components/motion/avatar-group.d.ts +48 -0
- package/dist/components/motion/avatar-group.d.ts.map +1 -0
- package/dist/components/motion/avatar-group.js +196 -0
- package/dist/components/motion/avatar-group.js.map +1 -0
- package/dist/components/motion/bottom-sheet.js +1 -1
- package/dist/components/motion/bouncy-accordion.d.ts.map +1 -1
- package/dist/components/motion/bouncy-accordion.js +20 -34
- package/dist/components/motion/bouncy-accordion.js.map +1 -1
- package/dist/components/motion/breadcrumb.js +0 -3
- package/dist/components/motion/breadcrumb.js.map +1 -1
- package/dist/components/motion/button/magnetic.js +1 -1
- package/dist/components/motion/card-dealer.d.ts +36 -0
- package/dist/components/motion/card-dealer.d.ts.map +1 -0
- package/dist/components/motion/card-dealer.js +232 -0
- package/dist/components/motion/card-dealer.js.map +1 -0
- package/dist/components/motion/color-picker.js +1 -1
- package/dist/components/motion/combobox.js +1 -1
- package/dist/components/motion/context-menu.js +1 -1
- package/dist/components/motion/crash/crash-balloon.d.ts +9 -0
- package/dist/components/motion/crash/crash-balloon.d.ts.map +1 -0
- package/dist/components/motion/crash/crash-balloon.js +829 -0
- package/dist/components/motion/crash/crash-balloon.js.map +1 -0
- package/dist/components/motion/crash/crash-parts.d.ts +59 -0
- package/dist/components/motion/crash/crash-parts.d.ts.map +1 -0
- package/dist/components/motion/crash/crash-parts.js +2 -0
- package/dist/components/motion/crash/crash-tower.d.ts +9 -0
- package/dist/components/motion/crash/crash-tower.d.ts.map +1 -0
- package/dist/components/motion/crash/crash-tower.js +370 -0
- package/dist/components/motion/crash/crash-tower.js.map +1 -0
- package/dist/components/motion/crash.d.ts +9 -0
- package/dist/components/motion/crash.d.ts.map +1 -0
- package/dist/components/motion/crash.js +590 -0
- package/dist/components/motion/crash.js.map +1 -0
- package/dist/components/motion/date-range-picker.js +1 -1
- package/dist/components/motion/feedback-widget.js +0 -1
- package/dist/components/motion/feedback-widget.js.map +1 -1
- package/dist/components/motion/hold-action-button.js +1 -1
- package/dist/components/motion/liquid.js +2 -2
- package/dist/components/motion/liquid.js.map +1 -1
- package/dist/components/motion/loader.js +5 -4
- package/dist/components/motion/loader.js.map +1 -1
- package/dist/components/motion/mines.d.ts +41 -0
- package/dist/components/motion/mines.d.ts.map +1 -0
- package/dist/components/motion/mines.js +762 -0
- package/dist/components/motion/mines.js.map +1 -0
- package/dist/components/motion/morphing-modal.js +6 -4
- package/dist/components/motion/morphing-modal.js.map +1 -1
- package/dist/components/motion/morphing-tabs.js +1 -1
- package/dist/components/motion/multi-select/index.js +1 -1
- package/dist/components/motion/pagination.d.ts +31 -0
- package/dist/components/motion/pagination.d.ts.map +1 -0
- package/dist/components/motion/pagination.js +193 -0
- package/dist/components/motion/pagination.js.map +1 -0
- package/dist/components/motion/plinko.d.ts +59 -0
- package/dist/components/motion/plinko.d.ts.map +1 -0
- package/dist/components/motion/plinko.js +385 -0
- package/dist/components/motion/plinko.js.map +1 -0
- package/dist/components/motion/popover-morph.d.ts +11 -2
- package/dist/components/motion/popover-morph.d.ts.map +1 -1
- package/dist/components/motion/popover-morph.js +98 -4
- package/dist/components/motion/popover-morph.js.map +1 -1
- package/dist/components/motion/pull-to-refresh.js +1 -1
- package/dist/components/motion/range-slider-bubble.js +1 -1
- package/dist/components/motion/range-slider-fluid.js +1 -1
- package/dist/components/motion/range-slider-inline.js +1 -1
- package/dist/components/motion/range-slider-ruler.js +1 -1
- package/dist/components/motion/range-slider-wave.js +1 -1
- package/dist/components/motion/range-slider.js +1 -1
- package/dist/components/motion/signup-form.js +0 -1
- package/dist/components/motion/signup-form.js.map +1 -1
- package/dist/components/motion/slide-action-button.js +1 -1
- package/dist/components/motion/swap/field.js +1 -1
- package/dist/components/motion/swap/token-picker.js +1 -1
- package/dist/components/motion/swap.d.ts +1 -1
- package/dist/components/motion/swap.js +2 -2
- package/dist/components/motion/table/index.js +2 -2
- package/dist/components/motion/table/table-header.js +1 -1
- package/dist/components/motion/tooltip.d.ts +2 -2
- package/dist/components/motion/tooltip.d.ts.map +1 -1
- package/dist/components/motion/wallet-card/index.js +1 -2
- package/dist/components/motion/wallet-card/index.js.map +1 -1
- package/dist/components/motion/wheel-picker.js +1 -1
- package/dist/components/reui/badge.d.ts +1 -1
- package/dist/components/reui/cascader/cascader-columns.js +1 -1
- package/dist/components/reui/cascader/cascader-footer.js +1 -1
- package/dist/components/reui/cascader/cascader-nav.js +1 -1
- package/dist/components/reui/cascader/cascader-virtual.js +1 -1
- package/dist/components/reui/cascader/cascader.d.ts +1 -1
- package/dist/components/reui/cascader/cascader.js +2 -2
- package/dist/components/reui/code-block/code-block.d.ts +2 -1
- package/dist/components/reui/code-block/code-block.d.ts.map +1 -1
- package/dist/components/reui/data-grid/data-grid-column-filter.js +1 -1
- package/dist/components/reui/data-grid/data-grid-pagination.js +1 -1
- package/dist/components/reui/data-grid/data-grid-table.d.ts +1 -1
- package/dist/components/reui/data-grid/data-grid-table.d.ts.map +1 -1
- package/dist/components/reui/data-grid/data-grid-table.js +1 -1
- package/dist/components/reui/date-selector.js +2 -2
- package/dist/components/reui/emoji-picker.js +2 -2
- package/dist/components/reui/event-calendar/event-calendar-agenda-view.js +1 -1
- package/dist/components/reui/event-calendar/event-calendar-content.js +1 -1
- package/dist/components/reui/event-calendar/event-calendar-dnd.js +1 -1
- package/dist/components/reui/event-calendar/event-calendar-event.js +1 -1
- package/dist/components/reui/event-calendar/event-calendar-month-view.js +2 -2
- package/dist/components/reui/event-calendar/event-calendar-nav.js +2 -2
- package/dist/components/reui/event-calendar/event-calendar-resource-view.js +1 -1
- package/dist/components/reui/event-calendar/event-calendar-time-grid.js +1 -1
- package/dist/components/reui/event-calendar/event-calendar.d.ts +1 -1
- package/dist/components/reui/filters/filters-advanced.js +4 -4
- package/dist/components/reui/filters/filters-builder.js +1 -1
- package/dist/components/reui/filters/filters-chip.js +2 -2
- package/dist/components/reui/filters/filters-context.d.ts +1 -1
- package/dist/components/reui/filters/filters-editors.js +3 -3
- package/dist/components/reui/filters/filters.d.ts +3 -3
- package/dist/components/reui/filters/filters.js +6 -6
- package/dist/components/reui/gantt/gantt-bar.js +1 -1
- package/dist/components/reui/gantt/gantt-dnd.js +1 -1
- package/dist/components/reui/gantt/gantt-nav.js +2 -2
- package/dist/components/reui/gantt/gantt-view.js +3 -3
- package/dist/components/reui/gantt/gantt.d.ts +1 -1
- package/dist/components/reui/icon-tile.d.ts +1 -1
- package/dist/components/reui/phone-input.js +2 -2
- package/dist/components/reui/tree.d.ts +1 -0
- package/dist/components/reui/tree.d.ts.map +1 -1
- package/dist/components/ui/attachment.d.ts +1 -1
- package/dist/components/ui/badge.d.ts +1 -1
- package/dist/components/ui/bubble.d.ts +1 -1
- package/dist/components/ui/button-group.d.ts +1 -1
- package/dist/components/ui/button.d.ts +2 -2
- package/dist/components/ui/command.js +1 -1
- package/dist/components/ui/field.d.ts +1 -1
- package/dist/components/ui/field.js +1 -1
- package/dist/components/ui/item.d.ts +1 -1
- package/dist/components/ui/marker.d.ts +1 -1
- package/dist/components/ui/sidebar.d.ts +1 -1
- package/dist/components/ui/sidebar.js +1 -1
- package/dist/components/ui/tabs.d.ts +1 -1
- package/dist/context-CQbvWVnM.js +393 -0
- package/dist/context-CQbvWVnM.js.map +1 -0
- package/dist/context-tv-kneJV.d.ts +204 -0
- package/dist/context-tv-kneJV.d.ts.map +1 -0
- package/dist/{copy-menu-CKW3jM1J.js → copy-menu-LU54Aruj.js} +2 -2
- package/dist/{copy-menu-CKW3jM1J.js.map → copy-menu-LU54Aruj.js.map} +1 -1
- package/dist/crash-parts-CUv5gFYQ.js +1053 -0
- package/dist/crash-parts-CUv5gFYQ.js.map +1 -0
- package/dist/{ease-CHhXKlG3.d.ts → ease-DjhlGGZO.d.ts} +4 -2
- package/dist/ease-DjhlGGZO.d.ts.map +1 -0
- package/dist/hooks/use-highlighted-code.d.ts +1 -1
- package/dist/hooks/use-highlighted-code.d.ts.map +1 -1
- package/dist/hooks/use-modal-scope.js +2 -2
- package/dist/hooks/use-modal-scope.js.map +1 -1
- package/dist/lib/audio-context.d.ts +11 -0
- package/dist/lib/audio-context.d.ts.map +1 -0
- package/dist/lib/audio-context.js +37 -0
- package/dist/lib/audio-context.js.map +1 -0
- package/dist/lib/ease.d.ts +2 -2
- package/dist/lib/ease.js +8 -1
- package/dist/lib/ease.js.map +1 -1
- package/dist/lib/focusable.d.ts +2 -0
- package/dist/lib/focusable.d.ts.map +1 -0
- package/dist/lib/focusable.js +12 -0
- package/dist/lib/focusable.js.map +1 -0
- package/dist/lib/game-sounds.d.ts +18 -0
- package/dist/lib/game-sounds.d.ts.map +1 -0
- package/dist/lib/game-sounds.js +142 -0
- package/dist/lib/game-sounds.js.map +1 -0
- package/dist/lib/tick-sound.d.ts +4 -3
- package/dist/lib/tick-sound.d.ts.map +1 -1
- package/dist/lib/tick-sound.js +30 -37
- package/dist/lib/tick-sound.js.map +1 -1
- package/dist/model-CvWolJdo.d.ts +14 -0
- package/dist/model-CvWolJdo.d.ts.map +1 -0
- package/dist/path-VEfFUowr.d.ts +3 -0
- package/dist/path-VEfFUowr.d.ts.map +1 -0
- package/dist/{plot-DAKl-Lu1.js → plot-CTQFrQKi.js} +2 -2
- package/dist/{plot-DAKl-Lu1.js.map → plot-CTQFrQKi.js.map} +1 -1
- package/dist/scale-DSsaupNc.d.ts +7 -0
- package/dist/scale-DSsaupNc.d.ts.map +1 -0
- package/dist/tooltip-CNEYdIX3.d.ts.map +1 -1
- package/dist/tooltip-content-K0vPsEtI.d.ts +17 -0
- package/dist/tooltip-content-K0vPsEtI.d.ts.map +1 -0
- package/dist/use-crash-game-B3RHxFkm.d.ts +108 -0
- package/dist/use-crash-game-B3RHxFkm.d.ts.map +1 -0
- package/dist/use-inspection-CZhdpH8J.js +127 -0
- package/dist/use-inspection-CZhdpH8J.js.map +1 -0
- package/dist/use-points-C2jpElHh.js +118 -0
- package/dist/use-points-C2jpElHh.js.map +1 -0
- package/dist/use-width-CkKCh1b3.js +23 -0
- package/dist/use-width-CkKCh1b3.js.map +1 -0
- package/package.json +7 -7
- package/dist/ease-CHhXKlG3.d.ts.map +0 -1
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { EASE_IN, EASE_OUT } from "../../lib/ease.js";
|
|
3
|
+
import { createTickPlayer } from "../../lib/tick-sound.js";
|
|
4
|
+
import { ActionSwapCascadeIcon } from "./action-swap-cascade.js";
|
|
5
|
+
import { Button } from "./button/base.js";
|
|
6
|
+
import { t as Tooltip } from "../../tooltip-CuHpilPN.js";
|
|
7
|
+
import { useEffect, useRef, useState } from "react";
|
|
8
|
+
import { cn } from "@kumix/utils";
|
|
9
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
import { AnimatePresence, motion, useAnimate, useReducedMotion } from "motion/react";
|
|
11
|
+
import { Volume2, VolumeX, Zap, ZapOff } from "lucide-react";
|
|
12
|
+
// src/components/motion/plinko.tsx
|
|
13
|
+
const GAP = 32;
|
|
14
|
+
const ROW = 30;
|
|
15
|
+
const PEG = 3.5;
|
|
16
|
+
const BALL = 7;
|
|
17
|
+
const TOP = 48;
|
|
18
|
+
const SLOT = 34;
|
|
19
|
+
const RISE = [
|
|
20
|
+
0,
|
|
21
|
+
.55,
|
|
22
|
+
.45,
|
|
23
|
+
1
|
|
24
|
+
];
|
|
25
|
+
const HOP_TIME = .09;
|
|
26
|
+
const DROP_TIME = .16;
|
|
27
|
+
const HOP_HEIGHT = 7;
|
|
28
|
+
const LANDING_RATE = .55;
|
|
29
|
+
const LANDING_SPREAD = .4;
|
|
30
|
+
let nextBall = 0;
|
|
31
|
+
/** Height of the ball's centre where it touches a peg on `row`. */
|
|
32
|
+
const contactY = (row) => TOP + row * ROW - PEG - BALL;
|
|
33
|
+
/** Random left/right steps from the top peg to a slot, as keyframes. */
|
|
34
|
+
function plan(rows, width) {
|
|
35
|
+
const center = width / 2;
|
|
36
|
+
const points = [{
|
|
37
|
+
x: center,
|
|
38
|
+
y: BALL
|
|
39
|
+
}];
|
|
40
|
+
const steps = [.22];
|
|
41
|
+
const pegs = [];
|
|
42
|
+
let k = 0;
|
|
43
|
+
for (let row = 0; row < rows; row++) {
|
|
44
|
+
const x = center + (k - row / 2) * GAP;
|
|
45
|
+
pegs.push(`${row}-${k}`);
|
|
46
|
+
points.push({
|
|
47
|
+
x,
|
|
48
|
+
y: contactY(row)
|
|
49
|
+
});
|
|
50
|
+
const right = Math.random() < .5;
|
|
51
|
+
points.push({
|
|
52
|
+
x: x + (right ? GAP / 4 : -8),
|
|
53
|
+
y: contactY(row) - HOP_HEIGHT
|
|
54
|
+
});
|
|
55
|
+
steps.push(HOP_TIME, DROP_TIME);
|
|
56
|
+
if (right) k++;
|
|
57
|
+
}
|
|
58
|
+
const slotY = TOP + rows * ROW - BALL + 3;
|
|
59
|
+
points.push({
|
|
60
|
+
x: center + (k - rows / 2) * GAP,
|
|
61
|
+
y: slotY
|
|
62
|
+
});
|
|
63
|
+
const duration = steps.reduce((sum, step) => sum + step, 0);
|
|
64
|
+
const times = [0];
|
|
65
|
+
for (const step of steps) times.push(times[times.length - 1] + step / duration);
|
|
66
|
+
return {
|
|
67
|
+
path: points,
|
|
68
|
+
times,
|
|
69
|
+
slot: k,
|
|
70
|
+
duration,
|
|
71
|
+
pegs
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A Plinko board: balls drop through rows of pegs, bounce left or right off
|
|
76
|
+
* each one and land in a slot. Pegs flash when hit and the slot pulses on
|
|
77
|
+
* landing. Optional sound clicks on every peg and lands with a thunk that
|
|
78
|
+
* brightens toward the rarer edge slots. With reduced motion a ball lands at
|
|
79
|
+
* once.
|
|
80
|
+
*/
|
|
81
|
+
function Plinko({ rows: rowsProp = 12, slots, onLand, maxBalls = 12, dropLabel = "Drop ball", pegFlash, defaultPegFlash = true, onPegFlashChange, pegFlashLabel = "Peg flash", sound, defaultSound = false, onSoundChange, soundLabel = "Sound", "aria-label": ariaLabel = "Plinko board", className }) {
|
|
82
|
+
const reduce = useReducedMotion() ?? false;
|
|
83
|
+
const [scope, animate] = useAnimate();
|
|
84
|
+
const [balls, setBalls] = useState([]);
|
|
85
|
+
const [announcement, setAnnouncement] = useState("");
|
|
86
|
+
const [lastSlot, setLastSlot] = useState(null);
|
|
87
|
+
const [internalSound, setInternalSound] = useState(defaultSound);
|
|
88
|
+
const soundOn = sound ?? internalSound;
|
|
89
|
+
const [internalFlash, setInternalFlash] = useState(defaultPegFlash);
|
|
90
|
+
const flashOn = pegFlash ?? internalFlash;
|
|
91
|
+
const landings = useRef(0);
|
|
92
|
+
const players = useRef(null);
|
|
93
|
+
const getPlayers = () => {
|
|
94
|
+
if (!players.current) players.current = {
|
|
95
|
+
peg: createTickPlayer(),
|
|
96
|
+
landing: createTickPlayer()
|
|
97
|
+
};
|
|
98
|
+
return players.current;
|
|
99
|
+
};
|
|
100
|
+
useEffect(() => () => {
|
|
101
|
+
players.current?.peg.dispose();
|
|
102
|
+
players.current?.landing.dispose();
|
|
103
|
+
players.current = null;
|
|
104
|
+
}, []);
|
|
105
|
+
const togglePegFlash = () => {
|
|
106
|
+
const next = !flashOn;
|
|
107
|
+
if (pegFlash === void 0) setInternalFlash(next);
|
|
108
|
+
onPegFlashChange?.(next);
|
|
109
|
+
};
|
|
110
|
+
const toggleSound = () => {
|
|
111
|
+
const next = !soundOn;
|
|
112
|
+
if (next) getPlayers().peg.prepare();
|
|
113
|
+
if (sound === void 0) setInternalSound(next);
|
|
114
|
+
onSoundChange?.(next);
|
|
115
|
+
};
|
|
116
|
+
const labels = slots ?? plinkoSlots(rowsProp);
|
|
117
|
+
const rows = labels.length - 1;
|
|
118
|
+
const invalid = labels.length < 2;
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (process.env.NODE_ENV !== "production" && invalid) console.warn("Plinko needs at least 2 slots; it renders nothing until it has them.");
|
|
121
|
+
}, [invalid]);
|
|
122
|
+
const [boardRows, setBoardRows] = useState(rows);
|
|
123
|
+
if (boardRows !== rows) {
|
|
124
|
+
setBoardRows(rows);
|
|
125
|
+
setBalls([]);
|
|
126
|
+
setLastSlot(null);
|
|
127
|
+
}
|
|
128
|
+
const width = (rows + 2) * GAP;
|
|
129
|
+
const height = TOP + rows * ROW + SLOT;
|
|
130
|
+
const pegs = Array.from({ length: rows }, (_, row) => Array.from({ length: row + 3 }, (_, i) => ({
|
|
131
|
+
id: `${row}-${i - 1}`,
|
|
132
|
+
x: width / 2 + (i - 1 - row / 2) * GAP,
|
|
133
|
+
y: TOP + row * ROW
|
|
134
|
+
}))).flat();
|
|
135
|
+
const land = (slot) => {
|
|
136
|
+
animate(`[data-slot="${slot}"]`, { opacity: [.9, 0] }, {
|
|
137
|
+
duration: .3,
|
|
138
|
+
ease: EASE_OUT
|
|
139
|
+
});
|
|
140
|
+
if (soundOn) getPlayers().landing.play(LANDING_RATE + LANDING_SPREAD * (Math.abs(slot - rows / 2) / (rows / 2)));
|
|
141
|
+
setLastSlot(slot);
|
|
142
|
+
landings.current++;
|
|
143
|
+
setAnnouncement(`Ball ${landings.current} landed in slot ${slot + 1}: ${labels[slot]}`);
|
|
144
|
+
onLand?.(slot, labels[slot]);
|
|
145
|
+
};
|
|
146
|
+
const hitPegs = (ball, y) => {
|
|
147
|
+
const rising = y < ball.y;
|
|
148
|
+
const row = Math.round((Math.max(ball.y, y) - contactY(0)) / ROW);
|
|
149
|
+
if (rising && !ball.rising && row > ball.lastRow && row < ball.pegs.length) {
|
|
150
|
+
ball.lastRow = row;
|
|
151
|
+
const peg = ball.pegs[row];
|
|
152
|
+
if (flashOn) animate(`[data-peg="${peg}"]`, {
|
|
153
|
+
scale: [
|
|
154
|
+
1,
|
|
155
|
+
1.8,
|
|
156
|
+
1
|
|
157
|
+
],
|
|
158
|
+
opacity: [
|
|
159
|
+
.4,
|
|
160
|
+
1,
|
|
161
|
+
.4
|
|
162
|
+
]
|
|
163
|
+
}, {
|
|
164
|
+
duration: .3,
|
|
165
|
+
times: [
|
|
166
|
+
0,
|
|
167
|
+
.3,
|
|
168
|
+
1
|
|
169
|
+
],
|
|
170
|
+
ease: EASE_OUT
|
|
171
|
+
});
|
|
172
|
+
if (soundOn) getPlayers().peg.play();
|
|
173
|
+
}
|
|
174
|
+
ball.y = y;
|
|
175
|
+
ball.rising = rising;
|
|
176
|
+
};
|
|
177
|
+
const full = balls.length >= maxBalls;
|
|
178
|
+
const drop = () => {
|
|
179
|
+
if (full) return;
|
|
180
|
+
const ball = {
|
|
181
|
+
id: nextBall++,
|
|
182
|
+
lastRow: -1,
|
|
183
|
+
y: 0,
|
|
184
|
+
rising: false,
|
|
185
|
+
...plan(rows, width)
|
|
186
|
+
};
|
|
187
|
+
if (soundOn) getPlayers().peg.prepare();
|
|
188
|
+
if (reduce) {
|
|
189
|
+
land(ball.slot);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
setBalls((current) => [...current, ball]);
|
|
193
|
+
};
|
|
194
|
+
if (invalid) return null;
|
|
195
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
196
|
+
"aria-label": ariaLabel,
|
|
197
|
+
className: cn("flex w-full flex-col items-center gap-4", className),
|
|
198
|
+
children: [
|
|
199
|
+
/* @__PURE__ */ jsxs("svg", {
|
|
200
|
+
ref: scope,
|
|
201
|
+
viewBox: `0 0 ${width} ${height}`,
|
|
202
|
+
style: { maxWidth: width * 1.4 },
|
|
203
|
+
className: "w-full",
|
|
204
|
+
"aria-hidden": "true",
|
|
205
|
+
children: [
|
|
206
|
+
pegs.map((peg) => /* @__PURE__ */ jsx("circle", {
|
|
207
|
+
"data-peg": peg.id,
|
|
208
|
+
cx: peg.x,
|
|
209
|
+
cy: peg.y,
|
|
210
|
+
r: PEG,
|
|
211
|
+
className: "fill-foreground origin-center opacity-40 transform-fill"
|
|
212
|
+
}, peg.id)),
|
|
213
|
+
labels.map((label, slot) => {
|
|
214
|
+
const x = width / 2 + (slot - rows / 2) * GAP - GAP / 2;
|
|
215
|
+
const y = TOP + rows * ROW;
|
|
216
|
+
return /* @__PURE__ */ jsxs("g", { children: [
|
|
217
|
+
/* @__PURE__ */ jsx("rect", {
|
|
218
|
+
x: x + 2,
|
|
219
|
+
y,
|
|
220
|
+
width: 28,
|
|
221
|
+
height: 30,
|
|
222
|
+
rx: 6,
|
|
223
|
+
className: slot === lastSlot ? "fill-primary/20" : "fill-muted"
|
|
224
|
+
}),
|
|
225
|
+
/* @__PURE__ */ jsx("rect", {
|
|
226
|
+
"data-slot": slot,
|
|
227
|
+
x: x + 2,
|
|
228
|
+
y,
|
|
229
|
+
width: 28,
|
|
230
|
+
height: 30,
|
|
231
|
+
rx: 6,
|
|
232
|
+
className: "fill-primary opacity-0"
|
|
233
|
+
}),
|
|
234
|
+
/* @__PURE__ */ jsx("text", {
|
|
235
|
+
x: x + GAP / 2,
|
|
236
|
+
y: y + 15,
|
|
237
|
+
textAnchor: "middle",
|
|
238
|
+
dominantBaseline: "central",
|
|
239
|
+
fontSize: Math.min(9, 24 / (String(label).length * .6)),
|
|
240
|
+
className: cn("fill-foreground tabular-nums", slot === lastSlot ? "font-semibold" : "font-medium"),
|
|
241
|
+
children: label
|
|
242
|
+
})
|
|
243
|
+
] }, slot);
|
|
244
|
+
}),
|
|
245
|
+
/* @__PURE__ */ jsx(AnimatePresence, { children: balls.map((ball) => /* @__PURE__ */ jsx(motion.circle, {
|
|
246
|
+
r: BALL,
|
|
247
|
+
cx: 0,
|
|
248
|
+
cy: 0,
|
|
249
|
+
className: "fill-primary",
|
|
250
|
+
initial: {
|
|
251
|
+
x: ball.path[0].x,
|
|
252
|
+
y: ball.path[0].y,
|
|
253
|
+
opacity: 0,
|
|
254
|
+
scale: .9
|
|
255
|
+
},
|
|
256
|
+
animate: {
|
|
257
|
+
x: ball.path.map((point) => point.x),
|
|
258
|
+
y: ball.path.map((point) => point.y),
|
|
259
|
+
opacity: 1,
|
|
260
|
+
scale: 1
|
|
261
|
+
},
|
|
262
|
+
exit: {
|
|
263
|
+
opacity: 0,
|
|
264
|
+
y: (ball.path.at(-1)?.y ?? 0) + 4,
|
|
265
|
+
transition: {
|
|
266
|
+
duration: .12,
|
|
267
|
+
ease: EASE_OUT
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
transition: {
|
|
271
|
+
opacity: {
|
|
272
|
+
duration: .15,
|
|
273
|
+
ease: EASE_OUT
|
|
274
|
+
},
|
|
275
|
+
scale: {
|
|
276
|
+
duration: .15,
|
|
277
|
+
ease: EASE_OUT
|
|
278
|
+
},
|
|
279
|
+
x: {
|
|
280
|
+
duration: ball.duration,
|
|
281
|
+
times: ball.times,
|
|
282
|
+
ease: "linear"
|
|
283
|
+
},
|
|
284
|
+
y: {
|
|
285
|
+
duration: ball.duration,
|
|
286
|
+
times: ball.times,
|
|
287
|
+
ease: ball.times.slice(1).map((_, i) => i % 2 === 1 ? RISE : EASE_IN)
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
onUpdate: (latest) => {
|
|
291
|
+
if (typeof latest.y === "number") hitPegs(ball, latest.y);
|
|
292
|
+
},
|
|
293
|
+
onAnimationComplete: () => {
|
|
294
|
+
if (ball.landed) return;
|
|
295
|
+
ball.landed = true;
|
|
296
|
+
land(ball.slot);
|
|
297
|
+
setBalls((current) => current.filter((other) => other.id !== ball.id));
|
|
298
|
+
}
|
|
299
|
+
}, ball.id)) }, rows)
|
|
300
|
+
]
|
|
301
|
+
}),
|
|
302
|
+
/* @__PURE__ */ jsxs("div", {
|
|
303
|
+
className: "flex items-center gap-2",
|
|
304
|
+
children: [
|
|
305
|
+
/* @__PURE__ */ jsx(Button, {
|
|
306
|
+
onClick: drop,
|
|
307
|
+
"aria-disabled": full || void 0,
|
|
308
|
+
pressScale: .96,
|
|
309
|
+
className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-50",
|
|
310
|
+
children: dropLabel
|
|
311
|
+
}),
|
|
312
|
+
reduce ? null : /* @__PURE__ */ jsx(ToggleButton, {
|
|
313
|
+
on: flashOn,
|
|
314
|
+
onToggle: togglePegFlash,
|
|
315
|
+
label: pegFlashLabel,
|
|
316
|
+
onIcon: Zap,
|
|
317
|
+
offIcon: ZapOff
|
|
318
|
+
}),
|
|
319
|
+
/* @__PURE__ */ jsx(ToggleButton, {
|
|
320
|
+
on: soundOn,
|
|
321
|
+
onToggle: toggleSound,
|
|
322
|
+
label: soundLabel,
|
|
323
|
+
onIcon: Volume2,
|
|
324
|
+
offIcon: VolumeX
|
|
325
|
+
})
|
|
326
|
+
]
|
|
327
|
+
}),
|
|
328
|
+
/* @__PURE__ */ jsx("p", {
|
|
329
|
+
"aria-live": "polite",
|
|
330
|
+
className: "sr-only",
|
|
331
|
+
children: announcement
|
|
332
|
+
})
|
|
333
|
+
]
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Slot values for a board of `rows`: `center` in the middle, growing evenly
|
|
338
|
+
* (geometrically) to `edge` at both ends, where landing is rarest. Rounded to
|
|
339
|
+
* readable numbers: 12 rows give 100, 50, 20, 10, 5, 2, 1, 2, 5, 10, 20, 50, 100.
|
|
340
|
+
* Values from 1 to 10 round to whole numbers, the rest to one significant
|
|
341
|
+
* figure, so fractional curves work: 8 rows with `center: 0.5, edge: 10`
|
|
342
|
+
* give 10, 5, 2, 1, 0.5, 1, 2, 5, 10.
|
|
343
|
+
* `rows` is rounded to at least 1, `center` raised to at least 0.01 and
|
|
344
|
+
* `edge` to at least `center`.
|
|
345
|
+
*/
|
|
346
|
+
function plinkoSlots(rowCount, options = {}) {
|
|
347
|
+
const rows = Math.max(1, Math.round(rowCount) || 1);
|
|
348
|
+
const center = Math.max(.01, options.center ?? 1);
|
|
349
|
+
const edge = Math.max(center, options.edge ?? 100);
|
|
350
|
+
const half = rows / 2;
|
|
351
|
+
return Array.from({ length: rows + 1 }, (_, slot) => {
|
|
352
|
+
const value = center * (edge / center) ** (Math.abs(slot - half) / half);
|
|
353
|
+
if (value >= 1 && value < 10) return Math.round(value);
|
|
354
|
+
return Number(value.toPrecision(1));
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/** An icon button that shows and toggles one on/off setting. */
|
|
358
|
+
function ToggleButton({ on, onToggle, label, onIcon: OnIcon, offIcon: OffIcon }) {
|
|
359
|
+
return /* @__PURE__ */ jsx(Tooltip, {
|
|
360
|
+
content: label,
|
|
361
|
+
side: "top",
|
|
362
|
+
children: /* @__PURE__ */ jsx(Button, {
|
|
363
|
+
variant: "secondary",
|
|
364
|
+
onClick: onToggle,
|
|
365
|
+
"aria-label": label,
|
|
366
|
+
"aria-pressed": on,
|
|
367
|
+
pressScale: .96,
|
|
368
|
+
className: cn("w-10 px-0", on ? "text-foreground" : "text-muted-foreground"),
|
|
369
|
+
children: /* @__PURE__ */ jsx(ActionSwapCascadeIcon, {
|
|
370
|
+
value: on ? "on" : "off",
|
|
371
|
+
className: "size-4",
|
|
372
|
+
children: on ? /* @__PURE__ */ jsx(OnIcon, {
|
|
373
|
+
"aria-hidden": "true",
|
|
374
|
+
className: "size-4"
|
|
375
|
+
}) : /* @__PURE__ */ jsx(OffIcon, {
|
|
376
|
+
"aria-hidden": "true",
|
|
377
|
+
className: "size-4"
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
export { Plinko, plinkoSlots };
|
|
384
|
+
|
|
385
|
+
//# sourceMappingURL=plinko.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plinko.js","names":[],"sources":["../../../src/components/motion/plinko.tsx"],"sourcesContent":["\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { type LucideIcon, Volume2, VolumeX, Zap, ZapOff } from \"lucide-react\";\nimport { AnimatePresence, motion, useAnimate, useReducedMotion } from \"motion/react\";\n\nimport { cn } from \"@kumix/utils\";\nimport { EASE_IN, EASE_OUT } from \"../../lib/ease\";\nimport { createTickPlayer } from \"../../lib/tick-sound\";\nimport { ActionSwapCascadeIcon } from \"./action-swap-cascade\";\nimport { Button } from \"./button\";\nimport { Tooltip } from \"./tooltip\";\n\nexport type PlinkoValue = string | number;\n\nexport interface PlinkoProps {\n /** Rows of pegs when `slots` isn't passed. Default 12. */\n rows?: number;\n /**\n * Slot values, left to right. Their count sets the board: n slots get\n * n - 1 rows of pegs, so `rows` is ignored when this is passed. Without it\n * the slots come from `plinkoSlots(rows)`. Fewer than 2 slots render nothing.\n */\n slots?: PlinkoValue[];\n /** Called with the slot index and its value when a ball lands. */\n onLand?: (slot: number, value: PlinkoValue) => void;\n /** Balls allowed in flight at once. Default 12. */\n maxBalls?: number;\n /** Label of the drop button. Default \"Drop ball\". */\n dropLabel?: string;\n /** Flash each peg as the ball hits it (controlled). Its button is hidden with reduced motion, where no pegs are hit. */\n pegFlash?: boolean;\n /** Initial peg flash state when uncontrolled. Default true. */\n defaultPegFlash?: boolean;\n /** Called when the peg flash button is toggled. */\n onPegFlashChange?: (pegFlash: boolean) => void;\n /** Tooltip and accessible name of the peg flash button. Default \"Peg flash\". */\n pegFlashLabel?: string;\n /** Click on every peg hit and landing (controlled). */\n sound?: boolean;\n /** Initial sound state when uncontrolled. Default false. */\n defaultSound?: boolean;\n /** Called when the sound button is toggled. */\n onSoundChange?: (sound: boolean) => void;\n /** Tooltip and accessible name of the sound button. Default \"Sound\". */\n soundLabel?: string;\n /** Accessible name for the board. Default \"Plinko board\". */\n \"aria-label\"?: string;\n className?: string;\n}\n\n// Board geometry in SVG units; the board scales to its container's width.\nconst GAP = 32; // horizontal space between pegs\nconst ROW = 30; // vertical space between peg rows\nconst PEG = 3.5;\nconst BALL = 7;\nconst TOP = 48; // first peg row\nconst SLOT = 34; // slot height\n\n// A ball falls between pegs under gravity and rises a little after each hit.\n// A drop speeds up (EASE_IN); RISE, its mirror, slows down like a hop and is\n// local because only a bounce needs it.\nconst RISE = [0, 0.55, 0.45, 1] as const;\nconst HOP_TIME = 0.09; // seconds rising after a peg\nconst DROP_TIME = 0.16; // seconds falling to the next peg\nconst HOP_HEIGHT = 7;\n// Landing tick playback rates: 0.55 in the centre slot, up to 0.95 at the edges.\nconst LANDING_RATE = 0.55;\nconst LANDING_SPREAD = 0.4;\n\ntype Ball = {\n id: number;\n path: { x: number; y: number }[];\n times: number[];\n slot: number;\n duration: number;\n /** Id of the peg hit on each row, top to bottom. */\n pegs: string[];\n /** Row of the last peg hit, -1 before the first. */\n lastRow: number;\n /** Last rendered y, to catch the frame the ball turns back up off a peg. */\n y: number;\n /** Moving up on the last frame: still on the hop from the last peg. */\n rising: boolean;\n /** Set once the ball reaches its slot; its exit fade must not land again. */\n landed?: boolean;\n};\n\nlet nextBall = 0;\n\n/** Height of the ball's centre where it touches a peg on `row`. */\nconst contactY = (row: number) => TOP + row * ROW - PEG - BALL;\n\n/** Random left/right steps from the top peg to a slot, as keyframes. */\nfunction plan(rows: number, width: number): Omit<Ball, \"id\" | \"lastRow\" | \"y\" | \"rising\"> {\n const center = width / 2;\n const points = [{ x: center, y: BALL }]; // enters fully inside the board\n const steps: number[] = [0.22]; // first fall from the chute to the top peg\n const pegs: string[] = [];\n let k = 0; // rightward steps so far\n for (let row = 0; row < rows; row++) {\n const x = center + (k - row / 2) * GAP;\n pegs.push(`${row}-${k}`);\n points.push({ x, y: contactY(row) });\n const right = Math.random() < 0.5;\n points.push({ x: x + (right ? GAP / 4 : -GAP / 4), y: contactY(row) - HOP_HEIGHT });\n steps.push(HOP_TIME, DROP_TIME);\n if (right) k++;\n }\n // Come to rest just inside the slot's top edge, clear of its label.\n const slotY = TOP + rows * ROW - BALL + 3;\n points.push({ x: center + (k - rows / 2) * GAP, y: slotY });\n const duration = steps.reduce((sum, step) => sum + step, 0);\n const times = [0];\n for (const step of steps) times.push(times[times.length - 1] + step / duration);\n return { path: points, times, slot: k, duration, pegs };\n}\n\n/**\n * A Plinko board: balls drop through rows of pegs, bounce left or right off\n * each one and land in a slot. Pegs flash when hit and the slot pulses on\n * landing. Optional sound clicks on every peg and lands with a thunk that\n * brightens toward the rarer edge slots. With reduced motion a ball lands at\n * once.\n */\nexport function Plinko({\n rows: rowsProp = 12,\n slots,\n onLand,\n maxBalls = 12,\n dropLabel = \"Drop ball\",\n pegFlash,\n defaultPegFlash = true,\n onPegFlashChange,\n pegFlashLabel = \"Peg flash\",\n sound,\n defaultSound = false,\n onSoundChange,\n soundLabel = \"Sound\",\n \"aria-label\": ariaLabel = \"Plinko board\",\n className,\n}: PlinkoProps) {\n const reduce = useReducedMotion() ?? false;\n const [scope, animate] = useAnimate();\n const [balls, setBalls] = useState<Ball[]>([]);\n const [announcement, setAnnouncement] = useState(\"\");\n // The last slot hit stays marked after its flash fades.\n const [lastSlot, setLastSlot] = useState<number | null>(null);\n const [internalSound, setInternalSound] = useState(defaultSound);\n const soundOn = sound ?? internalSound;\n const [internalFlash, setInternalFlash] = useState(defaultPegFlash);\n const flashOn = pegFlash ?? internalFlash;\n\n // Landings are numbered so a second ball in the same slot still changes\n // the live region's text and is announced again.\n const landings = useRef(0);\n // Each player cuts its own previous tick, so landings get their own player:\n // another ball's peg click would otherwise cut the longer landing thunk.\n const players = useRef<Record<\"peg\" | \"landing\", ReturnType<typeof createTickPlayer>> | null>(\n null,\n );\n const getPlayers = () => {\n if (!players.current)\n players.current = { peg: createTickPlayer(), landing: createTickPlayer() };\n return players.current;\n };\n useEffect(\n () => () => {\n players.current?.peg.dispose();\n players.current?.landing.dispose();\n players.current = null;\n },\n [],\n );\n\n const togglePegFlash = () => {\n const next = !flashOn;\n if (pegFlash === undefined) setInternalFlash(next);\n onPegFlashChange?.(next);\n };\n\n const toggleSound = () => {\n const next = !soundOn;\n // Unlock audio inside the click, where browsers allow it.\n if (next) getPlayers().peg.prepare();\n if (sound === undefined) setInternalSound(next);\n onSoundChange?.(next);\n };\n\n const labels = slots ?? plinkoSlots(rowsProp);\n const rows = labels.length - 1;\n const invalid = labels.length < 2;\n useEffect(() => {\n if (process.env.NODE_ENV !== \"production\" && invalid)\n console.warn(\"Plinko needs at least 2 slots; it renders nothing until it has them.\");\n }, [invalid]);\n\n // A new board size makes every ball in flight point at pegs that moved, so\n // drop them in the same render. Clearing them in an effect would mount them\n // in the new board first, and their exit would land in a slot that may not\n // exist any more.\n const [boardRows, setBoardRows] = useState(rows);\n if (boardRows !== rows) {\n setBoardRows(rows);\n setBalls([]);\n setLastSlot(null);\n }\n\n const width = (rows + 2) * GAP;\n const height = TOP + rows * ROW + SLOT;\n // Each row runs one peg past the ball's reach on both sides, so the board\n // reads as a triangle that widens down to the slots.\n const pegs = Array.from({ length: rows }, (_, row) =>\n Array.from({ length: row + 3 }, (_, i) => ({\n id: `${row}-${i - 1}`,\n x: width / 2 + (i - 1 - row / 2) * GAP,\n y: TOP + row * ROW,\n })),\n ).flat();\n\n const land = (slot: number) => {\n animate(`[data-slot=\"${slot}\"]`, { opacity: [0.9, 0] }, { duration: 0.3, ease: EASE_OUT });\n // A lower thunk than a peg hit marks the landing. It brightens toward\n // the edges, where landing is rarest, so a good drop is heard.\n if (soundOn)\n getPlayers().landing.play(\n LANDING_RATE + LANDING_SPREAD * (Math.abs(slot - rows / 2) / (rows / 2)),\n );\n setLastSlot(slot);\n landings.current++;\n setAnnouncement(`Ball ${landings.current} landed in slot ${slot + 1}: ${labels[slot]}`);\n onLand?.(slot, labels[slot]);\n };\n\n // Flash and click a peg on the frame the ball turns back up off it. Reading\n // the ball's own frames keeps both on its clock: a background tab pauses\n // them together. motion rebinds onUpdate every render, so the settings\n // read here are always current.\n const hitPegs = (ball: Ball, y: number) => {\n const rising = y < ball.y;\n // The row comes from where the ball turned, not from counting turns, so\n // a slow frame that skips a whole hop misses only that peg. Both frames\n // sit within a hop height above the contact, which rounds to its row.\n const row = Math.round((Math.max(ball.y, y) - contactY(0)) / ROW);\n if (rising && !ball.rising && row > ball.lastRow && row < ball.pegs.length) {\n ball.lastRow = row;\n const peg = ball.pegs[row];\n if (flashOn) {\n // Fast attack, slower decay: the pop peaks a third of the way in.\n animate(\n `[data-peg=\"${peg}\"]`,\n { scale: [1, 1.8, 1], opacity: [0.4, 1, 0.4] },\n { duration: 0.3, times: [0, 0.3, 1], ease: EASE_OUT },\n );\n }\n if (soundOn) getPlayers().peg.play();\n }\n ball.y = y;\n ball.rising = rising;\n };\n\n const full = balls.length >= maxBalls;\n const drop = () => {\n if (full) return;\n const ball: Ball = { id: nextBall++, lastRow: -1, y: 0, rising: false, ...plan(rows, width) };\n if (soundOn) getPlayers().peg.prepare();\n if (reduce) {\n land(ball.slot);\n return;\n }\n setBalls((current) => [...current, ball]);\n };\n\n if (invalid) return null;\n\n return (\n <section\n aria-label={ariaLabel}\n className={cn(\"flex w-full flex-col items-center gap-4\", className)}\n >\n <svg\n ref={scope}\n viewBox={`0 0 ${width} ${height}`}\n // Grow with the board so pegs keep the same on-screen spacing at any\n // row count, until the container's width runs out.\n style={{ maxWidth: width * 1.4 }}\n className=\"w-full\"\n aria-hidden=\"true\"\n >\n {pegs.map((peg) => (\n <circle\n key={peg.id}\n data-peg={peg.id}\n cx={peg.x}\n cy={peg.y}\n r={PEG}\n className=\"fill-foreground origin-center opacity-40 transform-fill\"\n />\n ))}\n\n {labels.map((label, slot) => {\n const x = width / 2 + (slot - rows / 2) * GAP - GAP / 2;\n const y = TOP + rows * ROW;\n return (\n <g key={slot}>\n <rect\n x={x + 2}\n y={y}\n width={GAP - 4}\n height={SLOT - 4}\n rx={6}\n className={slot === lastSlot ? \"fill-primary/20\" : \"fill-muted\"}\n />\n <rect\n data-slot={slot}\n x={x + 2}\n y={y}\n width={GAP - 4}\n height={SLOT - 4}\n rx={6}\n className=\"fill-primary opacity-0\"\n />\n <text\n x={x + GAP / 2}\n y={y + (SLOT - 4) / 2}\n textAnchor=\"middle\"\n dominantBaseline=\"central\"\n // Long values shrink to fit their slot.\n fontSize={Math.min(9, (GAP - 8) / (String(label).length * 0.6))}\n className={cn(\n \"fill-foreground tabular-nums\",\n slot === lastSlot ? \"font-semibold\" : \"font-medium\",\n )}\n >\n {label}\n </text>\n </g>\n );\n })}\n\n {/* Keyed by size: balls from a board that changed size leave at once. */}\n <AnimatePresence key={rows}>\n {balls.map((ball) => (\n <motion.circle\n key={ball.id}\n r={BALL}\n cx={0}\n cy={0}\n className=\"fill-primary\"\n // Enters from 90% scale and transparent, never from nothing. The\n // circle is drawn at 0,0 and placed by translate, so it scales\n // about its own centre.\n initial={{ x: ball.path[0].x, y: ball.path[0].y, opacity: 0, scale: 0.9 }}\n animate={{\n x: ball.path.map((point) => point.x),\n y: ball.path.map((point) => point.y),\n opacity: 1,\n scale: 1,\n }}\n // Settle into the slot: a short fade with a small drop.\n exit={{\n opacity: 0,\n y: (ball.path.at(-1)?.y ?? 0) + 4,\n transition: { duration: 0.12, ease: EASE_OUT },\n }}\n transition={{\n opacity: { duration: 0.15, ease: EASE_OUT },\n scale: { duration: 0.15, ease: EASE_OUT },\n x: { duration: ball.duration, times: ball.times, ease: \"linear\" },\n y: {\n duration: ball.duration,\n times: ball.times,\n // Fall into the first peg, then hop up and fall down per row.\n ease: ball.times.slice(1).map((_, i) => (i % 2 === 1 ? RISE : EASE_IN)),\n },\n }}\n onUpdate={(latest) => {\n if (typeof latest.y === \"number\") hitPegs(ball, latest.y);\n }}\n onAnimationComplete={() => {\n if (ball.landed) return;\n ball.landed = true;\n land(ball.slot);\n setBalls((current) => current.filter((other) => other.id !== ball.id));\n }}\n />\n ))}\n </AnimatePresence>\n </svg>\n\n <div className=\"flex items-center gap-2\">\n {/* aria-disabled, not disabled: a disabled button drops keyboard focus. */}\n <Button\n onClick={drop}\n aria-disabled={full || undefined}\n pressScale={0.96}\n className=\"aria-disabled:cursor-not-allowed aria-disabled:opacity-50\"\n >\n {dropLabel}\n </Button>\n {/* With reduced motion balls land at once and hit no pegs. */}\n {reduce ? null : (\n <ToggleButton\n on={flashOn}\n onToggle={togglePegFlash}\n label={pegFlashLabel}\n onIcon={Zap}\n offIcon={ZapOff}\n />\n )}\n <ToggleButton\n on={soundOn}\n onToggle={toggleSound}\n label={soundLabel}\n onIcon={Volume2}\n offIcon={VolumeX}\n />\n </div>\n <p aria-live=\"polite\" className=\"sr-only\">\n {announcement}\n </p>\n </section>\n );\n}\n\n/**\n * Slot values for a board of `rows`: `center` in the middle, growing evenly\n * (geometrically) to `edge` at both ends, where landing is rarest. Rounded to\n * readable numbers: 12 rows give 100, 50, 20, 10, 5, 2, 1, 2, 5, 10, 20, 50, 100.\n * Values from 1 to 10 round to whole numbers, the rest to one significant\n * figure, so fractional curves work: 8 rows with `center: 0.5, edge: 10`\n * give 10, 5, 2, 1, 0.5, 1, 2, 5, 10.\n * `rows` is rounded to at least 1, `center` raised to at least 0.01 and\n * `edge` to at least `center`.\n */\nexport function plinkoSlots(\n rowCount: number,\n options: { center?: number; edge?: number } = {},\n): number[] {\n const rows = Math.max(1, Math.round(rowCount) || 1);\n // Any positive center works; zero or below would divide by zero.\n const center = Math.max(0.01, options.center ?? 1);\n const edge = Math.max(center, options.edge ?? 100);\n const half = rows / 2;\n return Array.from({ length: rows + 1 }, (_, slot) => {\n const value = center * (edge / center) ** (Math.abs(slot - half) / half);\n if (value >= 1 && value < 10) return Math.round(value);\n // One significant figure: 46 reads as 50, 315 as 300, 0.46 as 0.5.\n return Number(value.toPrecision(1));\n });\n}\n\n/** An icon button that shows and toggles one on/off setting. */\nfunction ToggleButton({\n on,\n onToggle,\n label,\n onIcon: OnIcon,\n offIcon: OffIcon,\n}: {\n on: boolean;\n onToggle: () => void;\n label: string;\n onIcon: LucideIcon;\n offIcon: LucideIcon;\n}) {\n return (\n // aria-label names the button; the tooltip shows the same label on hover\n // and focus. Tooltip's asLabel would hydrate differently from the server.\n <Tooltip content={label} side=\"top\">\n <Button\n variant=\"secondary\"\n onClick={onToggle}\n aria-label={label}\n aria-pressed={on}\n pressScale={0.96}\n // Full strength when on, muted when off: the slash alone is a thin cue.\n className={cn(\"w-10 px-0\", on ? \"text-foreground\" : \"text-muted-foreground\")}\n >\n <ActionSwapCascadeIcon value={on ? \"on\" : \"off\"} className=\"size-4\">\n {on ? (\n <OnIcon aria-hidden=\"true\" className=\"size-4\" />\n ) : (\n <OffIcon aria-hidden=\"true\" className=\"size-4\" />\n )}\n </ActionSwapCascadeIc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AAK,MAAM,KAAK;EAErD,OAAO,OAAO,MAAM,YAAY,CAAC,CAAC;CACpC,CAAC;AACH;;AAGA,SAAS,aAAa,EACpB,IACA,UACA,OACA,QAAQ,QACR,SAAS,WAOR;CACD,OAGE,oBAAC,SAAD;EAAS,SAAS;EAAO,MAAK;EAC5B,UAAA,oBAAC,QAAD;GACE,SAAQ;GACR,SAAS;GACT,cAAY;GACZ,gBAAc;GACd,YAAY;GAEZ,WAAW,GAAG,aAAa,KAAK,oBAAoB,uBAAuB;GAE3E,UAAA,oBAAC,uBAAD;IAAuB,OAAO,KAAK,OAAO;IAAO,WAAU;IACxD,UAAA,KACC,oBAAC,QAAD;KAAQ,eAAY;KAAO,WAAU;IAAU,CAAA,IAE/C,oBAAC,SAAD;KAAS,eAAY;KAAO,WAAU;IAAU,CAAA;GAE7B,CAAA;EACjB,CAAA;CACD,CAAA;AAEb"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReactElement, ReactNode } from "react";
|
|
1
|
+
import { ReactElement, ReactNode, RefObject } from "react";
|
|
2
2
|
type Side = "top" | "bottom";
|
|
3
3
|
type Align = "start" | "end";
|
|
4
4
|
export interface MorphPopoverProps {
|
|
@@ -32,8 +32,17 @@ export interface MorphPopoverContentProps {
|
|
|
32
32
|
radius?: number;
|
|
33
33
|
/** Draw the surface shadow. Default true. */
|
|
34
34
|
shadow?: boolean;
|
|
35
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* Runs after the portalled surface is positioned and visible. Passing it
|
|
37
|
+
* hands focus to the caller: the panel then moves no focus on its own.
|
|
38
|
+
*/
|
|
36
39
|
onOpenAutoFocus?: (content: HTMLDivElement) => void;
|
|
40
|
+
/**
|
|
41
|
+
* Element to focus when the trigger opens the panel by keyboard. Defaults
|
|
42
|
+
* to the first focusable element in the panel, or the panel itself. A
|
|
43
|
+
* pointer open does not move focus. Ignored with `onOpenAutoFocus`.
|
|
44
|
+
*/
|
|
45
|
+
initialFocus?: RefObject<HTMLElement | null>;
|
|
37
46
|
className?: string;
|
|
38
47
|
}
|
|
39
48
|
export declare function MorphPopoverContent(props: MorphPopoverContentProps): import("react").ReactPortal | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"popover-morph.d.ts","names":[],"sources":["../../../src/components/motion/popover-morph.tsx"],"mappings":";
|
|
1
|
+
{"version":3,"file":"popover-morph.d.ts","names":[],"sources":["../../../src/components/motion/popover-morph.tsx"],"mappings":";KAmCK;KACA;iBA0BY;EACf,UAAU;;EAEV;;EAEA;EACA,gBAAgB;EAChB;;;;;;;;wBASc,eACd,UACA,MAAM,gBACN,aACA,cACA,aACC,oCAAiB,IAAA;iBAiHH;EACf,UAAU;;;wBAcI,sBAAsB,YAAY,2BAAwB,aAAA,kDAAA;iBA4DzD;EACf,UAAU;EACV,OAAO;EACP,QAAQ;;EAER;;EAEA;;EAEA;;;;;EAKA,mBAAmB,SAAS;;;;;;EAM5B,eAAe,UAAU;EACzB;;wBAGc,oBAAoB,OAAO,2CAAwB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { EASE_OUT } from "../../lib/ease.js";
|
|
3
|
+
import { FOCUSABLE_SELECTOR } from "../../lib/focusable.js";
|
|
3
4
|
import { t as usePopoverPortalPosition } from "../../popover-position-BaDNBZ-D.js";
|
|
4
5
|
import { cloneElement, createContext, isValidElement, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
5
6
|
import { cn } from "@kumix/utils";
|
|
@@ -32,6 +33,32 @@ function MorphPopover({ children, open: controlledOpen, defaultOpen = false, onO
|
|
|
32
33
|
onOpenChange?.(next);
|
|
33
34
|
}, [controlled, onOpenChange]);
|
|
34
35
|
const toggle = useCallback(() => setOpen(!open), [setOpen, open]);
|
|
36
|
+
const focusOnOpenRef = useRef(false);
|
|
37
|
+
const disarmRef = useRef(null);
|
|
38
|
+
const armFocusOnOpen = useCallback((armed) => {
|
|
39
|
+
disarmRef.current?.abort();
|
|
40
|
+
disarmRef.current = null;
|
|
41
|
+
focusOnOpenRef.current = armed;
|
|
42
|
+
if (!armed) return;
|
|
43
|
+
const controller = new AbortController();
|
|
44
|
+
const disarm = () => {
|
|
45
|
+
focusOnOpenRef.current = false;
|
|
46
|
+
controller.abort();
|
|
47
|
+
};
|
|
48
|
+
const options = {
|
|
49
|
+
capture: true,
|
|
50
|
+
signal: controller.signal
|
|
51
|
+
};
|
|
52
|
+
document.addEventListener("keydown", disarm, options);
|
|
53
|
+
document.addEventListener("pointerdown", disarm, options);
|
|
54
|
+
disarmRef.current = controller;
|
|
55
|
+
}, []);
|
|
56
|
+
const takeFocusOnOpen = useCallback(() => {
|
|
57
|
+
const armed = focusOnOpenRef.current;
|
|
58
|
+
armFocusOnOpen(false);
|
|
59
|
+
return armed;
|
|
60
|
+
}, [armFocusOnOpen]);
|
|
61
|
+
useEffect(() => () => disarmRef.current?.abort(), []);
|
|
35
62
|
const anchorRef = useMemo(() => ({ current: trigger ?? root }), [root, trigger]);
|
|
36
63
|
const close = useCallback(() => {
|
|
37
64
|
setOpen(false);
|
|
@@ -69,13 +96,17 @@ function MorphPopover({ children, open: controlledOpen, defaultOpen = false, onO
|
|
|
69
96
|
contentId: `${baseId}-content`,
|
|
70
97
|
triggerRef: anchorRef,
|
|
71
98
|
registerTrigger: setTrigger,
|
|
72
|
-
contentRef
|
|
99
|
+
contentRef,
|
|
100
|
+
armFocusOnOpen,
|
|
101
|
+
takeFocusOnOpen
|
|
73
102
|
}), [
|
|
74
103
|
open,
|
|
75
104
|
setOpen,
|
|
76
105
|
toggle,
|
|
77
106
|
baseId,
|
|
78
|
-
anchorRef
|
|
107
|
+
anchorRef,
|
|
108
|
+
armFocusOnOpen,
|
|
109
|
+
takeFocusOnOpen
|
|
79
110
|
]);
|
|
80
111
|
return /* @__PURE__ */ jsx(MorphContext.Provider, {
|
|
81
112
|
value: ctx,
|
|
@@ -97,14 +128,29 @@ function MorphPopoverTrigger({ children }) {
|
|
|
97
128
|
const ctx = useMorphContext("MorphPopoverTrigger");
|
|
98
129
|
const child = children;
|
|
99
130
|
const childOnClick = child?.props?.onClick;
|
|
131
|
+
const childOnKeyDown = child?.props?.onKeyDown;
|
|
132
|
+
const childOnPointerDown = child?.props?.onPointerDown;
|
|
133
|
+
const keyboardRef = useRef(false);
|
|
100
134
|
const childRef = (child?.props)?.ref;
|
|
101
135
|
const mergedRef = useMemo(() => mergeRefs(childRef, ctx.registerTrigger), [childRef, ctx.registerTrigger]);
|
|
102
136
|
if (!isValidElement(children)) return children;
|
|
103
137
|
return cloneElement(child, {
|
|
104
138
|
id: ctx.triggerId,
|
|
105
139
|
ref: mergedRef,
|
|
140
|
+
onKeyDown: (e) => {
|
|
141
|
+
childOnKeyDown?.(e);
|
|
142
|
+
const key = e?.key;
|
|
143
|
+
keyboardRef.current = key === "Enter" || key === " ";
|
|
144
|
+
},
|
|
145
|
+
onPointerDown: (e) => {
|
|
146
|
+
childOnPointerDown?.(e);
|
|
147
|
+
keyboardRef.current = false;
|
|
148
|
+
},
|
|
106
149
|
onClick: (e) => {
|
|
107
150
|
childOnClick?.(e);
|
|
151
|
+
const keyboard = keyboardRef.current && e?.detail === 0;
|
|
152
|
+
keyboardRef.current = false;
|
|
153
|
+
ctx.armFocusOnOpen(!ctx.open && keyboard);
|
|
108
154
|
ctx.toggle();
|
|
109
155
|
},
|
|
110
156
|
"aria-haspopup": "dialog",
|
|
@@ -131,7 +177,7 @@ function MorphPopoverContent(props) {
|
|
|
131
177
|
if (!portalReady) return null;
|
|
132
178
|
return createPortal(/* @__PURE__ */ jsx(AnimatePresence, { children: ctx.open && /* @__PURE__ */ jsx(MorphPopoverSurface, { ...props }) }), document.body);
|
|
133
179
|
}
|
|
134
|
-
function MorphPopoverSurface({ children, side = "bottom", align = "end", sideOffset = 8, radius = 16, shadow = true, onOpenAutoFocus, className }) {
|
|
180
|
+
function MorphPopoverSurface({ children, side = "bottom", align = "end", sideOffset = 8, radius = 16, shadow = true, onOpenAutoFocus, initialFocus, className }) {
|
|
135
181
|
const ctx = useMorphContext("MorphPopoverContent");
|
|
136
182
|
const reduce = useReducedMotion() ?? false;
|
|
137
183
|
const [isPresent, safeToRemove] = usePresence();
|
|
@@ -194,6 +240,53 @@ function MorphPopoverSurface({ children, side = "bottom", align = "end", sideOff
|
|
|
194
240
|
isPresent,
|
|
195
241
|
reduce
|
|
196
242
|
]);
|
|
243
|
+
const initialFocusRef = useRef(initialFocus);
|
|
244
|
+
const callerFocusesRef = useRef(onOpenAutoFocus !== void 0);
|
|
245
|
+
useLayoutEffect(() => {
|
|
246
|
+
initialFocusRef.current = initialFocus;
|
|
247
|
+
callerFocusesRef.current = onOpenAutoFocus !== void 0;
|
|
248
|
+
});
|
|
249
|
+
const { contentRef, takeFocusOnOpen } = ctx;
|
|
250
|
+
useLayoutEffect(() => {
|
|
251
|
+
if (!ready || !isPresent) return;
|
|
252
|
+
if (!takeFocusOnOpen() || callerFocusesRef.current) return;
|
|
253
|
+
const panel = contentRef.current;
|
|
254
|
+
const wrapper = panel?.parentElement;
|
|
255
|
+
if (!panel || !wrapper) return;
|
|
256
|
+
const from = document.activeElement;
|
|
257
|
+
const unmoved = () => document.activeElement === from || document.activeElement === panel;
|
|
258
|
+
const focusFirst = () => {
|
|
259
|
+
const candidates = [initialFocusRef.current?.current, ...panel.querySelectorAll(FOCUSABLE_SELECTOR)];
|
|
260
|
+
for (const candidate of candidates) {
|
|
261
|
+
if (!candidate) continue;
|
|
262
|
+
candidate.focus({ preventScroll: true });
|
|
263
|
+
if (unmoved()) continue;
|
|
264
|
+
candidate.scrollIntoView?.({ block: "nearest" });
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
panel.focus({ preventScroll: true });
|
|
268
|
+
return false;
|
|
269
|
+
};
|
|
270
|
+
if (focusFirst()) return;
|
|
271
|
+
const retry = (event) => {
|
|
272
|
+
if (event.propertyName !== "visibility") return;
|
|
273
|
+
if (!unmoved() || focusFirst()) stop();
|
|
274
|
+
};
|
|
275
|
+
const stop = () => {
|
|
276
|
+
wrapper.removeEventListener("transitionend", retry);
|
|
277
|
+
document.removeEventListener("keydown", stop, true);
|
|
278
|
+
document.removeEventListener("pointerdown", stop, true);
|
|
279
|
+
};
|
|
280
|
+
wrapper.addEventListener("transitionend", retry);
|
|
281
|
+
document.addEventListener("keydown", stop, true);
|
|
282
|
+
document.addEventListener("pointerdown", stop, true);
|
|
283
|
+
return stop;
|
|
284
|
+
}, [
|
|
285
|
+
ready,
|
|
286
|
+
isPresent,
|
|
287
|
+
contentRef,
|
|
288
|
+
takeFocusOnOpen
|
|
289
|
+
]);
|
|
197
290
|
return /* @__PURE__ */ jsx(motion.div, {
|
|
198
291
|
"data-morph-popover-portal": "",
|
|
199
292
|
inert: !isPresent,
|
|
@@ -210,6 +303,7 @@ function MorphPopoverSurface({ children, side = "bottom", align = "end", sideOff
|
|
|
210
303
|
id: ctx.contentId,
|
|
211
304
|
role: "dialog",
|
|
212
305
|
"aria-labelledby": ctx.triggerId,
|
|
306
|
+
tabIndex: -1,
|
|
213
307
|
style: {
|
|
214
308
|
clipPath: reduce ? void 0 : clipPath,
|
|
215
309
|
borderRadius: radius,
|
|
@@ -217,7 +311,7 @@ function MorphPopoverSurface({ children, side = "bottom", align = "end", sideOff
|
|
|
217
311
|
overflowX: "hidden",
|
|
218
312
|
overflowY: "auto"
|
|
219
313
|
},
|
|
220
|
-
className: cn("border-border bg-background overflow-hidden border", className),
|
|
314
|
+
className: cn("border-border bg-background focus-visible:ring-ring overflow-hidden border outline-none focus-visible:ring-2 focus-visible:ring-inset", className),
|
|
221
315
|
children
|
|
222
316
|
})
|
|
223
317
|
});
|