@yewdiel21/motion-kit 0.0.0-stage → 0.9.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/NOTICE +38 -0
- package/README.md +80 -2
- package/bin/motion-kit.mjs +242 -0
- package/css.d.ts +2 -0
- package/dist/auto/index.d.ts +63 -0
- package/dist/auto/index.js +207 -0
- package/dist/auto/index.js.map +1 -0
- package/dist/chunk-2BQGFOA7.js +177 -0
- package/dist/chunk-2BQGFOA7.js.map +1 -0
- package/dist/chunk-34XYOXHG.js +2374 -0
- package/dist/chunk-34XYOXHG.js.map +1 -0
- package/dist/chunk-6OJAP6ZL.js +832 -0
- package/dist/chunk-6OJAP6ZL.js.map +1 -0
- package/dist/chunk-ABNRDDO7.js +28 -0
- package/dist/chunk-ABNRDDO7.js.map +1 -0
- package/dist/chunk-FS2HJVVY.js +3320 -0
- package/dist/chunk-FS2HJVVY.js.map +1 -0
- package/dist/chunk-J27KH2A2.js +157 -0
- package/dist/chunk-J27KH2A2.js.map +1 -0
- package/dist/chunk-KLY5BRRI.js +18 -0
- package/dist/chunk-KLY5BRRI.js.map +1 -0
- package/dist/chunk-Q4LVFNLN.js +1617 -0
- package/dist/chunk-Q4LVFNLN.js.map +1 -0
- package/dist/chunk-RFDIZYFX.js +304 -0
- package/dist/chunk-RFDIZYFX.js.map +1 -0
- package/dist/chunk-TGV27ZWM.js +17 -0
- package/dist/chunk-TGV27ZWM.js.map +1 -0
- package/dist/confetti-BSX1CLLV.d.ts +41 -0
- package/dist/css/components/chat-background.css +87 -0
- package/dist/css/components/chat.css +237 -0
- package/dist/css/components/cursor.css +178 -0
- package/dist/css/components/dialog.css +259 -0
- package/dist/css/components/island.css +301 -0
- package/dist/css/components/menu.css +105 -0
- package/dist/css/components/nav.css +1054 -0
- package/dist/css/components/steps.css +50 -0
- package/dist/css/components/theme-picker.css +115 -0
- package/dist/css/components/toast.css +115 -0
- package/dist/css/controls.css +950 -0
- package/dist/css/effects.css +587 -0
- package/dist/css/fonts.css +3 -0
- package/dist/css/themes/black-green.css +126 -0
- package/dist/css/themes/black.css +126 -0
- package/dist/css/themes/blue.css +126 -0
- package/dist/css/themes/brown.css +126 -0
- package/dist/css/themes/cold-blue.css +126 -0
- package/dist/css/themes/funny.css +126 -0
- package/dist/css/themes/gray.css +126 -0
- package/dist/css/themes/green.css +126 -0
- package/dist/css/themes/modern-1.css +126 -0
- package/dist/css/themes/neogreen.css +126 -0
- package/dist/css/themes/new.css +126 -0
- package/dist/css/themes/new2.css +126 -0
- package/dist/css/themes/oled.css +126 -0
- package/dist/css/themes/orange.css +126 -0
- package/dist/css/themes/pastel-pink.css +126 -0
- package/dist/css/themes/pink.css +126 -0
- package/dist/css/themes/purple.css +126 -0
- package/dist/css/themes/red.css +126 -0
- package/dist/css/themes/super-blue.css +126 -0
- package/dist/css/themes.css +2431 -0
- package/dist/css/tokens.css +452 -0
- package/dist/elements/index.d.ts +183 -0
- package/dist/elements/index.js +472 -0
- package/dist/elements/index.js.map +1 -0
- package/dist/engine-CUr9QijM.d.ts +46 -0
- package/dist/gl/index.d.ts +61 -0
- package/dist/gl/index.js +18 -0
- package/dist/gl/index.js.map +1 -0
- package/dist/index.css +5296 -0
- package/dist/index.d.ts +440 -0
- package/dist/index.js +390 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +476 -0
- package/dist/react/index.js +969 -0
- package/dist/react/index.js.map +1 -0
- package/dist/reduced-BM4Ls0g5.d.ts +566 -0
- package/dist/reveal-WFUWDrei.d.ts +416 -0
- package/dist/store-Bqy_obnD.d.ts +195 -0
- package/dist/theme/generate.d.ts +18 -0
- package/dist/theme/generate.js +64 -0
- package/dist/theme/generate.js.map +1 -0
- package/dist/theme/index.d.ts +17 -0
- package/dist/theme/index.js +32 -0
- package/dist/theme/index.js.map +1 -0
- package/dist/theme-BFMoCzi6.d.ts +158 -0
- package/dist/types-BkS_raL8.d.ts +7 -0
- package/package.json +114 -3
- package/registry.json +469 -0
- package/skills/motion-kit/SKILL.md +189 -0
- package/skills/motion-kit/reference/components.md +86 -0
- package/skills/motion-kit/reference/design.md +92 -0
- package/skills/motion-kit/reference/recipes.md +172 -0
- package/src/auto/auto-init.ts +151 -0
- package/src/auto/index.ts +3 -0
- package/src/auto/parse-options.ts +68 -0
- package/src/components/chat/README.md +90 -0
- package/src/components/chat/chat.css +236 -0
- package/src/components/chat/element.ts +98 -0
- package/src/components/chat/engine.ts +295 -0
- package/src/components/chat/react.tsx +196 -0
- package/src/components/chat-background/README.md +50 -0
- package/src/components/chat-background/audio.ts +56 -0
- package/src/components/chat-background/chat-background.css +86 -0
- package/src/components/chat-background/element.ts +31 -0
- package/src/components/chat-background/engine.ts +108 -0
- package/src/components/chat-background/react.tsx +36 -0
- package/src/components/cursor/README.md +53 -0
- package/src/components/cursor/cursor.css +177 -0
- package/src/components/cursor/element.ts +23 -0
- package/src/components/cursor/engine.ts +254 -0
- package/src/components/cursor/react.tsx +26 -0
- package/src/components/dialog/README.md +106 -0
- package/src/components/dialog/dialog.css +258 -0
- package/src/components/dialog/element.ts +118 -0
- package/src/components/dialog/engine.ts +432 -0
- package/src/components/dialog/react.tsx +200 -0
- package/src/components/icon.tsx +10 -0
- package/src/components/icons.ts +24 -0
- package/src/components/island/README.md +91 -0
- package/src/components/island/element.ts +45 -0
- package/src/components/island/engine.ts +200 -0
- package/src/components/island/island.css +300 -0
- package/src/components/island/react.tsx +115 -0
- package/src/components/menu/README.md +67 -0
- package/src/components/menu/element.ts +55 -0
- package/src/components/menu/engine.ts +142 -0
- package/src/components/menu/menu.css +104 -0
- package/src/components/menu/react.tsx +82 -0
- package/src/components/nav/README.md +85 -0
- package/src/components/nav/element.ts +39 -0
- package/src/components/nav/engine.ts +164 -0
- package/src/components/nav/nav.css +1053 -0
- package/src/components/nav/react.tsx +90 -0
- package/src/components/steps/README.md +70 -0
- package/src/components/steps/element.ts +43 -0
- package/src/components/steps/engine.ts +185 -0
- package/src/components/steps/react.tsx +91 -0
- package/src/components/steps/steps.css +49 -0
- package/src/components/theme-picker/README.md +57 -0
- package/src/components/theme-picker/element.ts +20 -0
- package/src/components/theme-picker/engine.ts +91 -0
- package/src/components/theme-picker/react.tsx +31 -0
- package/src/components/theme-picker/theme-picker.css +114 -0
- package/src/components/toast/README.md +60 -0
- package/src/components/toast/element.ts +25 -0
- package/src/components/toast/engine.ts +483 -0
- package/src/components/toast/react.tsx +22 -0
- package/src/components/toast/store.ts +122 -0
- package/src/components/toast/toast.css +114 -0
- package/src/core/channels.ts +165 -0
- package/src/core/curves.ts +35 -0
- package/src/core/frame.ts +12 -0
- package/src/core/gsap.ts +65 -0
- package/src/core/loop.ts +44 -0
- package/src/core/math.ts +9 -0
- package/src/core/physics-spring.ts +59 -0
- package/src/core/pointer.ts +63 -0
- package/src/core/reduced.ts +21 -0
- package/src/core/scope.ts +70 -0
- package/src/core/scroll-track.ts +106 -0
- package/src/core/split.ts +68 -0
- package/src/core/spring.ts +22 -0
- package/src/core/styles.ts +31 -0
- package/src/core/timing.ts +204 -0
- package/src/core/types.ts +20 -0
- package/src/core/visible.ts +19 -0
- package/src/css/base.css +142 -0
- package/src/css/controls/README.md +122 -0
- package/src/css/controls/burger.css +67 -0
- package/src/css/controls/button.css +226 -0
- package/src/css/controls/checkbox.css +53 -0
- package/src/css/controls/chip.css +85 -0
- package/src/css/controls/field.css +124 -0
- package/src/css/controls/glass.css +220 -0
- package/src/css/controls/icon.css +26 -0
- package/src/css/controls/segmented.css +60 -0
- package/src/css/controls/switch.css +72 -0
- package/src/css/default-theme.css +123 -0
- package/src/css/effects/README.md +134 -0
- package/src/css/effects/anim.css +224 -0
- package/src/css/effects/fade.css +15 -0
- package/src/css/effects/float.css +64 -0
- package/src/css/effects/frost.css +62 -0
- package/src/css/effects/grain.css +58 -0
- package/src/css/effects/loading.css +60 -0
- package/src/css/effects/success.css +22 -0
- package/src/css/effects/text.css +67 -0
- package/src/css/fonts.css +3 -0
- package/src/css/states.css +51 -0
- package/src/css/themes/black-green.css +126 -0
- package/src/css/themes/black.css +126 -0
- package/src/css/themes/blue.css +126 -0
- package/src/css/themes/brown.css +126 -0
- package/src/css/themes/cold-blue.css +126 -0
- package/src/css/themes/funny.css +126 -0
- package/src/css/themes/gray.css +126 -0
- package/src/css/themes/green.css +126 -0
- package/src/css/themes/modern-1.css +126 -0
- package/src/css/themes/neogreen.css +126 -0
- package/src/css/themes/new.css +126 -0
- package/src/css/themes/new2.css +126 -0
- package/src/css/themes/oled.css +126 -0
- package/src/css/themes/orange.css +126 -0
- package/src/css/themes/pastel-pink.css +126 -0
- package/src/css/themes/pink.css +126 -0
- package/src/css/themes/purple.css +126 -0
- package/src/css/themes/red.css +126 -0
- package/src/css/themes/super-blue.css +126 -0
- package/src/css/tokens.css +182 -0
- package/src/elements/base.ts +76 -0
- package/src/elements/index.ts +48 -0
- package/src/gl/index.ts +15 -0
- package/src/gl/liquid-blend/README.md +23 -0
- package/src/gl/liquid-blend/create-liquid-blend.ts +7 -0
- package/src/gl/liquid-blend/shader.ts +9 -0
- package/src/gl/matcap/README.md +23 -0
- package/src/gl/matcap/create-matcap.ts +7 -0
- package/src/gl/matcap/shader.ts +16 -0
- package/src/gl/particle-hero/README.md +41 -0
- package/src/gl/particle-hero/create-particle-hero.ts +544 -0
- package/src/gl/particle-hero/particle-hero.css +19 -0
- package/src/gl/particle-hero/shaders.ts +84 -0
- package/src/gl/shader-blur/README.md +23 -0
- package/src/gl/shader-blur/create-shader-blur.ts +7 -0
- package/src/gl/shader-blur/shader.ts +11 -0
- package/src/gl/shader-grain/README.md +23 -0
- package/src/gl/shader-grain/create-shader-grain.ts +7 -0
- package/src/gl/shader-grain/shader.ts +6 -0
- package/src/gl/shared/create-shader-effect.ts +186 -0
- package/src/gl/shared/shader.css +3 -0
- package/src/gl/shared/shaders.ts +30 -0
- package/src/index.ts +177 -0
- package/src/motion/anchor.ts +45 -0
- package/src/motion/chat.ts +97 -0
- package/src/motion/confetti.ts +130 -0
- package/src/motion/count.ts +95 -0
- package/src/motion/details.ts +70 -0
- package/src/motion/drag-dismiss.ts +231 -0
- package/src/motion/effects.ts +81 -0
- package/src/motion/entrances.ts +27 -0
- package/src/motion/floating.ts +389 -0
- package/src/motion/flow.ts +115 -0
- package/src/motion/indicator.ts +44 -0
- package/src/motion/layout.ts +39 -0
- package/src/motion/liquid.ts +227 -0
- package/src/motion/morph.ts +540 -0
- package/src/motion/reveal.ts +112 -0
- package/src/motion/sheet.ts +65 -0
- package/src/motion/surface.ts +108 -0
- package/src/react/animated-number.tsx +30 -0
- package/src/react/flip-group.tsx +44 -0
- package/src/react/frost.tsx +33 -0
- package/src/react/index.ts +80 -0
- package/src/react/slot-text.tsx +44 -0
- package/src/react/theme-provider.tsx +42 -0
- package/src/react/use-card-dock.ts +9 -0
- package/src/react/use-cascade.ts +9 -0
- package/src/react/use-contrast.ts +9 -0
- package/src/react/use-counter.ts +8 -0
- package/src/react/use-curved-carousel.ts +10 -0
- package/src/react/use-depth-grid.ts +9 -0
- package/src/react/use-details.ts +7 -0
- package/src/react/use-dock-pill.ts +10 -0
- package/src/react/use-drag-ruler.ts +10 -0
- package/src/react/use-fill-text.ts +9 -0
- package/src/react/use-fit-text.ts +8 -0
- package/src/react/use-flow.ts +8 -0
- package/src/react/use-frame-zoom.ts +9 -0
- package/src/react/use-infinite-reel.ts +8 -0
- package/src/react/use-letters.ts +9 -0
- package/src/react/use-liquid-blend.ts +8 -0
- package/src/react/use-marquee.ts +9 -0
- package/src/react/use-mask-text.ts +9 -0
- package/src/react/use-matcap.ts +8 -0
- package/src/react/use-parallax.ts +9 -0
- package/src/react/use-particle-hero.ts +9 -0
- package/src/react/use-pile.ts +8 -0
- package/src/react/use-pop.ts +8 -0
- package/src/react/use-radial-menu.ts +10 -0
- package/src/react/use-recipe.ts +38 -0
- package/src/react/use-reflow.ts +9 -0
- package/src/react/use-repel-bento.ts +8 -0
- package/src/react/use-reveal.ts +8 -0
- package/src/react/use-rotator.ts +10 -0
- package/src/react/use-shader-blur.ts +8 -0
- package/src/react/use-shader-grain.ts +8 -0
- package/src/react/use-slot-text.ts +17 -0
- package/src/react/use-snap-strip.ts +9 -0
- package/src/react/use-sortable.ts +14 -0
- package/src/react/use-spring-cards.ts +8 -0
- package/src/react/use-stretch-footer.ts +20 -0
- package/src/react/use-tabs.ts +10 -0
- package/src/react/use-text-roll.ts +9 -0
- package/src/react/use-ticket-menu.ts +15 -0
- package/src/react/use-words.ts +8 -0
- package/src/recipes/card-dock/README.md +36 -0
- package/src/recipes/card-dock/card-dock.css +178 -0
- package/src/recipes/card-dock/create-card-dock.ts +97 -0
- package/src/recipes/cascade/README.md +33 -0
- package/src/recipes/cascade/create-cascade.ts +111 -0
- package/src/recipes/contrast/README.md +29 -0
- package/src/recipes/contrast/create-contrast.ts +68 -0
- package/src/recipes/counter/README.md +42 -0
- package/src/recipes/counter/create-counter.ts +84 -0
- package/src/recipes/curved-carousel/README.md +50 -0
- package/src/recipes/curved-carousel/carousel-math.ts +20 -0
- package/src/recipes/curved-carousel/create-curved-carousel.ts +257 -0
- package/src/recipes/curved-carousel/curved-carousel.css +50 -0
- package/src/recipes/depth-grid/README.md +30 -0
- package/src/recipes/depth-grid/create-depth-grid.ts +120 -0
- package/src/recipes/details/README.md +31 -0
- package/src/recipes/details/create-details.ts +27 -0
- package/src/recipes/dock-pill/README.md +23 -0
- package/src/recipes/dock-pill/create-dock-pill.ts +68 -0
- package/src/recipes/dock-pill/dock-pill.css +5 -0
- package/src/recipes/drag-ruler/README.md +21 -0
- package/src/recipes/drag-ruler/create-drag-ruler.ts +95 -0
- package/src/recipes/drag-ruler/drag-ruler.css +16 -0
- package/src/recipes/fill-text/README.md +27 -0
- package/src/recipes/fill-text/create-fill-text.ts +93 -0
- package/src/recipes/fit-text/README.md +37 -0
- package/src/recipes/fit-text/create-fit-text.ts +55 -0
- package/src/recipes/fit-text/fit-text.css +6 -0
- package/src/recipes/flow/README.md +46 -0
- package/src/recipes/flow/create-flow.ts +55 -0
- package/src/recipes/frame-zoom/README.md +29 -0
- package/src/recipes/frame-zoom/create-frame-zoom.ts +91 -0
- package/src/recipes/infinite-reel/README.md +23 -0
- package/src/recipes/infinite-reel/create-infinite-reel.ts +140 -0
- package/src/recipes/infinite-reel/infinite-reel.css +8 -0
- package/src/recipes/letters/README.md +30 -0
- package/src/recipes/letters/create-letters.ts +113 -0
- package/src/recipes/marquee/README.md +32 -0
- package/src/recipes/marquee/create-marquee.ts +74 -0
- package/src/recipes/marquee/marquee.css +41 -0
- package/src/recipes/mask-text/README.md +39 -0
- package/src/recipes/mask-text/create-mask-text.ts +134 -0
- package/src/recipes/parallax/README.md +31 -0
- package/src/recipes/parallax/create-parallax.ts +74 -0
- package/src/recipes/pile/README.md +48 -0
- package/src/recipes/pile/create-pile.ts +50 -0
- package/src/recipes/pile/pile-math.ts +27 -0
- package/src/recipes/pile/pile.css +63 -0
- package/src/recipes/pill/README.md +30 -0
- package/src/recipes/pill/pill.css +48 -0
- package/src/recipes/pop/README.md +36 -0
- package/src/recipes/pop/create-pop.ts +52 -0
- package/src/recipes/radial-menu/README.md +44 -0
- package/src/recipes/radial-menu/create-radial-menu.ts +96 -0
- package/src/recipes/radial-menu/radial-menu.css +45 -0
- package/src/recipes/reflow/README.md +47 -0
- package/src/recipes/reflow/create-reflow.ts +49 -0
- package/src/recipes/repel-bento/README.md +21 -0
- package/src/recipes/repel-bento/create-repel-bento.ts +65 -0
- package/src/recipes/repel-bento/repel-bento.css +3 -0
- package/src/recipes/reveal/README.md +36 -0
- package/src/recipes/reveal/create-reveal.ts +48 -0
- package/src/recipes/rotator/README.md +21 -0
- package/src/recipes/rotator/create-rotator.ts +66 -0
- package/src/recipes/rotator/rotator.css +3 -0
- package/src/recipes/slot-text/README.md +44 -0
- package/src/recipes/slot-text/create-slot-text.ts +244 -0
- package/src/recipes/slot-text/slot-text.css +42 -0
- package/src/recipes/snap-strip/README.md +31 -0
- package/src/recipes/snap-strip/create-snap-strip.ts +133 -0
- package/src/recipes/snap-strip/snap-strip.css +23 -0
- package/src/recipes/sortable/README.md +88 -0
- package/src/recipes/sortable/create-sortable.ts +863 -0
- package/src/recipes/sortable/sortable.css +40 -0
- package/src/recipes/spring-cards/README.md +48 -0
- package/src/recipes/spring-cards/create-spring-cards.ts +140 -0
- package/src/recipes/spring-cards/spring-cards.css +23 -0
- package/src/recipes/stretch-footer/README.md +44 -0
- package/src/recipes/stretch-footer/create-stretch-footer.ts +69 -0
- package/src/recipes/stretch-footer/stretch-footer.css +28 -0
- package/src/recipes/tabs/README.md +39 -0
- package/src/recipes/tabs/create-tabs.ts +91 -0
- package/src/recipes/tabs/tabs.css +19 -0
- package/src/recipes/text-roll/README.md +35 -0
- package/src/recipes/text-roll/create-text-roll.ts +137 -0
- package/src/recipes/text-roll/text-roll.css +21 -0
- package/src/recipes/ticket-menu/README.md +24 -0
- package/src/recipes/ticket-menu/create-ticket-menu.ts +6 -0
- package/src/recipes/words/README.md +43 -0
- package/src/recipes/words/create-words.ts +114 -0
- package/src/theme/css-format.ts +32 -0
- package/src/theme/generate.ts +72 -0
- package/src/theme/index.ts +15 -0
- package/src/theme/presets.ts +79 -0
- package/src/theme/theme.ts +119 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { gsap, registerMotion } from '../../core/gsap';
|
|
2
|
+
import { bezier, channels, easeInCubic, easeOut, easeOutCubic, eased, spring, springEase } from '../../core/channels';
|
|
3
|
+
import { reduced } from '../../core/reduced';
|
|
4
|
+
import { createScope } from '../../core/scope';
|
|
5
|
+
import { splitGraphemes } from '../../core/split';
|
|
6
|
+
import { TIMING } from '../../core/timing';
|
|
7
|
+
import { inert, warnOnce, type Instance } from '../../core/types';
|
|
8
|
+
import { iconSvg, type IconName } from '../icons';
|
|
9
|
+
import { dismissToast, getToasts, removeToast, subscribeToasts, toastTimers, type ToastItem, type ToastTone } from './store';
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* Stepbro's toast deck (ported from Kayro's ToastHost):
|
|
13
|
+
* - toasts stack at the top; older ones peek out 16 px below, 5 % smaller each,
|
|
14
|
+
* three drawn; hovering opens the deck (spring .55, 60 ms cascade) and pauses
|
|
15
|
+
* the timers, leaving closes it after 150 ms;
|
|
16
|
+
* - a toast with an origin grows out of that control (spring travel 144/14 with
|
|
17
|
+
* a 2400 px/s kick, size 0.32 s, content zooming in from twice its size);
|
|
18
|
+
* otherwise it slides in 12 px out of a 6 px blur; its letters rise one by one;
|
|
19
|
+
* - it leaves 24 px up into a 4 px blur (0.3 s) and can be swiped up (48 px) or
|
|
20
|
+
* wheeled away (80 px).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const STACK = { peek: 16, scaleStep: 0.05, minScale: 0.7, maxVisible: 3, gap: 8, baseZ: 1000 };
|
|
24
|
+
const DISTANCE = { enter: 12, enterBlur: 6, exit: 24, exitBlur: 4, fade: 120 };
|
|
25
|
+
const GESTURE = { swipe: 48, wheel: 80, expandDrag: 24, resistance: 0.15, wheelEndMs: 140 };
|
|
26
|
+
const TONE_ICONS: Record<ToastTone, IconName | null> = { success: 'check', error: 'alert', info: 'info', neutral: null };
|
|
27
|
+
|
|
28
|
+
export interface ToasterLabels {
|
|
29
|
+
dismiss?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ToasterOptions {
|
|
33
|
+
labels?: ToasterLabels;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type ToasterInstance = Instance;
|
|
37
|
+
|
|
38
|
+
interface Card {
|
|
39
|
+
card: HTMLElement;
|
|
40
|
+
shell: HTMLElement;
|
|
41
|
+
body: HTMLElement;
|
|
42
|
+
message: HTMLElement;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Where each live toast rests: index 0 is the newest, on top. */
|
|
46
|
+
function layout(count: number, expanded: boolean, heights: number[]) {
|
|
47
|
+
let offset = 0;
|
|
48
|
+
return Array.from({ length: count }, (_, index) => {
|
|
49
|
+
if (expanded) {
|
|
50
|
+
const slot = { y: offset, scale: 1, opacity: 1, visible: true, zIndex: STACK.baseZ - index };
|
|
51
|
+
offset += (heights[index] ?? 0) + STACK.gap;
|
|
52
|
+
return slot;
|
|
53
|
+
}
|
|
54
|
+
const visible = index < STACK.maxVisible;
|
|
55
|
+
return {
|
|
56
|
+
y: index * STACK.peek,
|
|
57
|
+
scale: Math.max(STACK.minScale, 1 - index * STACK.scaleStep),
|
|
58
|
+
opacity: visible ? 1 : 0,
|
|
59
|
+
visible,
|
|
60
|
+
zIndex: STACK.baseZ - index,
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A message's letters rise into place (graphemes, words kept together, at most 64). */
|
|
66
|
+
function revealLetters(element: HTMLElement) {
|
|
67
|
+
const text = element.textContent ?? '';
|
|
68
|
+
const T = TIMING.toast;
|
|
69
|
+
if (!text || text.length > 64 || reduced()) return;
|
|
70
|
+
const letters = splitGraphemes(text, 64);
|
|
71
|
+
const original = element.innerHTML;
|
|
72
|
+
const width = element.getBoundingClientRect().width;
|
|
73
|
+
const fragment = document.createDocumentFragment();
|
|
74
|
+
const chars: HTMLSpanElement[] = [];
|
|
75
|
+
let word: HTMLSpanElement | null = null;
|
|
76
|
+
for (const letter of letters) {
|
|
77
|
+
if (!letter.trim()) {
|
|
78
|
+
word = null;
|
|
79
|
+
fragment.append(document.createTextNode(letter));
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (!word) {
|
|
83
|
+
word = document.createElement('span');
|
|
84
|
+
word.style.display = 'inline-block';
|
|
85
|
+
word.style.whiteSpace = 'nowrap';
|
|
86
|
+
fragment.append(word);
|
|
87
|
+
}
|
|
88
|
+
const char = document.createElement('span');
|
|
89
|
+
char.textContent = letter;
|
|
90
|
+
char.style.display = 'inline-block';
|
|
91
|
+
word.append(char);
|
|
92
|
+
chars.push(char);
|
|
93
|
+
}
|
|
94
|
+
if (width) element.style.width = `${width}px`;
|
|
95
|
+
element.textContent = '';
|
|
96
|
+
element.append(fragment);
|
|
97
|
+
gsap.fromTo(
|
|
98
|
+
chars,
|
|
99
|
+
{ opacity: 0, yPercent: 40, filter: 'blur(4px)' },
|
|
100
|
+
{
|
|
101
|
+
opacity: 1,
|
|
102
|
+
yPercent: 0,
|
|
103
|
+
filter: 'blur(0px)',
|
|
104
|
+
duration: T.chars,
|
|
105
|
+
ease: easeOutCubic,
|
|
106
|
+
delay: (index: number) => T.charsDelay + index * T.charsStagger,
|
|
107
|
+
onComplete: () => {
|
|
108
|
+
element.innerHTML = original;
|
|
109
|
+
element.style.width = '';
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Grow a toast's shell out of `origin` (Stepbro's toast morph). Returns a function that settles it now. */
|
|
116
|
+
function morphFrom(card: HTMLElement, shell: HTMLElement, body: HTMLElement, origin: HTMLElement) {
|
|
117
|
+
const T = TIMING.toast;
|
|
118
|
+
const box = shell.getBoundingClientRect();
|
|
119
|
+
const start = origin.getBoundingClientRect();
|
|
120
|
+
const look = getComputedStyle(origin);
|
|
121
|
+
const own = getComputedStyle(shell);
|
|
122
|
+
const endRadius = parseFloat(own.borderTopLeftRadius) || 20;
|
|
123
|
+
const startRadius = Math.min(parseFloat(look.borderTopLeftRadius) || 0, start.height / 2);
|
|
124
|
+
card.style.width = `${box.width}px`;
|
|
125
|
+
card.style.height = `${box.height}px`;
|
|
126
|
+
body.style.width = `${body.getBoundingClientRect().width}px`;
|
|
127
|
+
const towardX = start.left + start.width / 2 < innerWidth / 2 ? 1 : -1;
|
|
128
|
+
const towardY = start.top + start.height / 2 < innerHeight / 2 ? 1 : -1;
|
|
129
|
+
const offset = { x: start.left - box.left, y: start.top - box.top };
|
|
130
|
+
Object.assign(shell.style, {
|
|
131
|
+
position: 'absolute',
|
|
132
|
+
top: '0px',
|
|
133
|
+
left: '0px',
|
|
134
|
+
width: `${start.width}px`,
|
|
135
|
+
height: `${start.height}px`,
|
|
136
|
+
borderRadius: `${startRadius}px`,
|
|
137
|
+
background: look.backgroundColor,
|
|
138
|
+
transform: `translate(${offset.x}px, ${offset.y}px)`,
|
|
139
|
+
transition: 'none',
|
|
140
|
+
});
|
|
141
|
+
Object.assign(body.style, { transform: 'scale(2)', transformOrigin: 'center center', filter: 'blur(8px)', opacity: '0' });
|
|
142
|
+
// The control steps aside while its toast leaves it.
|
|
143
|
+
origin.style.transition = 'opacity 160ms ease';
|
|
144
|
+
origin.style.opacity = '0';
|
|
145
|
+
void shell.offsetWidth;
|
|
146
|
+
shell.style.transition = `background ${T.morphColor}s ease-out ${T.morphColorDelay}s, box-shadow ${T.morphShadow}s ease-out ${T.morphShadowDelay}s`;
|
|
147
|
+
shell.style.background = '';
|
|
148
|
+
body.style.transition = `opacity ${T.morphColor}s ease-out ${T.morphColorDelay}s`;
|
|
149
|
+
body.style.opacity = '1';
|
|
150
|
+
const motion = channels(
|
|
151
|
+
{ x: offset.x, y: offset.y, width: start.width, height: start.height, radius: startRadius, scale: 2, blur: 8 },
|
|
152
|
+
(name, value) => {
|
|
153
|
+
if (name === 'x') offset.x = value;
|
|
154
|
+
else if (name === 'y') offset.y = value;
|
|
155
|
+
else if (name === 'width') shell.style.width = `${value}px`;
|
|
156
|
+
else if (name === 'height') shell.style.height = `${value}px`;
|
|
157
|
+
else if (name === 'radius') shell.style.borderRadius = `${value}px`;
|
|
158
|
+
else if (name === 'scale') body.style.transform = `scale(${value})`;
|
|
159
|
+
else if (name === 'blur') body.style.filter = `blur(${value}px)`;
|
|
160
|
+
if (name === 'x' || name === 'y') shell.style.transform = `translate(${offset.x}px, ${offset.y}px)`;
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
void motion.animate(
|
|
164
|
+
{ x: 0, y: 0, width: box.width, height: box.height, radius: endRadius, scale: 1, blur: 0 },
|
|
165
|
+
{
|
|
166
|
+
x: spring(144, 14, { velocity: 2400, direction: towardX }),
|
|
167
|
+
y: spring(144, 14, { velocity: 2400, direction: towardY }),
|
|
168
|
+
width: eased(T.morphSize, bezier(0.8, 0.3, 0.5, 0.8)),
|
|
169
|
+
height: eased(T.morphSize, bezier(0.8, 0.3, 0.5, 0.8)),
|
|
170
|
+
radius: eased(T.morphRadius, easeOut),
|
|
171
|
+
scale: eased(T.morphContent, easeOut),
|
|
172
|
+
blur: eased(T.morphContent, easeOut),
|
|
173
|
+
},
|
|
174
|
+
);
|
|
175
|
+
let settled = false;
|
|
176
|
+
const settle = () => {
|
|
177
|
+
if (settled) return;
|
|
178
|
+
settled = true;
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
motion.stop();
|
|
181
|
+
for (const name of ['position', 'top', 'left', 'width', 'height', 'border-radius', 'background', 'transform', 'transition']) shell.style.removeProperty(name);
|
|
182
|
+
card.style.removeProperty('width');
|
|
183
|
+
card.style.removeProperty('height');
|
|
184
|
+
for (const name of ['transform', 'transform-origin', 'filter', 'opacity', 'width', 'transition']) body.style.removeProperty(name);
|
|
185
|
+
origin.style.transition = 'opacity 300ms ease';
|
|
186
|
+
origin.style.opacity = '';
|
|
187
|
+
setTimeout(() => {
|
|
188
|
+
origin.style.transition = '';
|
|
189
|
+
}, 320);
|
|
190
|
+
};
|
|
191
|
+
const timer = setTimeout(settle, T.safetyMs);
|
|
192
|
+
return settle;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function buildCard(item: ToastItem, dismissLabel: string): Card {
|
|
196
|
+
const card = document.createElement('div');
|
|
197
|
+
card.className = 'mk-toast';
|
|
198
|
+
card.dataset.mkToast = String(item.id);
|
|
199
|
+
card.dataset.mkTone = item.tone;
|
|
200
|
+
const shell = document.createElement('div');
|
|
201
|
+
shell.className = 'mk-toast__shell';
|
|
202
|
+
const body = document.createElement('div');
|
|
203
|
+
body.className = 'mk-toast__body';
|
|
204
|
+
const icon = item.icon ? TONE_ICONS[item.tone] : null;
|
|
205
|
+
if (icon) {
|
|
206
|
+
const holder = document.createElement('span');
|
|
207
|
+
holder.className = 'mk-toast__icon';
|
|
208
|
+
holder.innerHTML = iconSvg(icon, 18);
|
|
209
|
+
body.append(holder);
|
|
210
|
+
}
|
|
211
|
+
const message = document.createElement('span');
|
|
212
|
+
message.className = 'mk-toast__message';
|
|
213
|
+
message.textContent = item.message;
|
|
214
|
+
body.append(message);
|
|
215
|
+
if (item.action) {
|
|
216
|
+
const action = document.createElement('button');
|
|
217
|
+
action.type = 'button';
|
|
218
|
+
action.className = 'mk-toast__action';
|
|
219
|
+
action.dataset.mkToastAction = '';
|
|
220
|
+
action.textContent = item.action.label;
|
|
221
|
+
body.append(action);
|
|
222
|
+
}
|
|
223
|
+
const dismiss = document.createElement('button');
|
|
224
|
+
dismiss.type = 'button';
|
|
225
|
+
dismiss.className = 'mk-toast__dismiss';
|
|
226
|
+
dismiss.dataset.mkToastDismiss = '';
|
|
227
|
+
dismiss.setAttribute('aria-label', dismissLabel);
|
|
228
|
+
dismiss.innerHTML = iconSvg('close', 14);
|
|
229
|
+
body.append(dismiss);
|
|
230
|
+
shell.append(body);
|
|
231
|
+
card.append(shell);
|
|
232
|
+
return { card, shell, body, message };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Render the toast list into `host` (a fixed layer at the top of the screen). */
|
|
236
|
+
export function createToaster(host: HTMLElement | null, options: ToasterOptions = {}): ToasterInstance {
|
|
237
|
+
if (!host) {
|
|
238
|
+
warnOnce('toaster: missing host');
|
|
239
|
+
return inert();
|
|
240
|
+
}
|
|
241
|
+
registerMotion();
|
|
242
|
+
const dismissLabel = options.labels?.dismiss ?? 'Cerrar';
|
|
243
|
+
const scope = createScope();
|
|
244
|
+
const cards = new Map<number, Card>();
|
|
245
|
+
const entered = new Set<number>();
|
|
246
|
+
const leaving = new Set<number>();
|
|
247
|
+
const dragging = new Set<number>();
|
|
248
|
+
const settles = new Map<number, () => void>();
|
|
249
|
+
let expanded = false;
|
|
250
|
+
let collapse: ReturnType<typeof setTimeout> | undefined;
|
|
251
|
+
const popover = typeof host.showPopover === 'function';
|
|
252
|
+
|
|
253
|
+
scope.attr(host, 'role', 'status');
|
|
254
|
+
scope.attr(host, 'aria-live', 'polite');
|
|
255
|
+
if (popover) scope.attr(host, 'popover', 'manual');
|
|
256
|
+
|
|
257
|
+
const live = () =>
|
|
258
|
+
getToasts()
|
|
259
|
+
.filter((item) => !item.closing)
|
|
260
|
+
.reverse()
|
|
261
|
+
.map((item) => ({ item, entry: cards.get(item.id) }))
|
|
262
|
+
.filter((pair): pair is { item: ToastItem; entry: Card } => pair.entry !== undefined);
|
|
263
|
+
|
|
264
|
+
const slotsFor = () => {
|
|
265
|
+
const entries = live();
|
|
266
|
+
return { entries, slots: layout(entries.length, expanded, entries.map(({ entry }) => entry.card.offsetHeight)) };
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const relayout = (duration: number = TIMING.toast.relayout, ease: gsap.EaseFunction = easeOutCubic, stagger = 0) => {
|
|
270
|
+
const { entries, slots } = slotsFor();
|
|
271
|
+
entries.forEach(({ item, entry }, index) => {
|
|
272
|
+
const slot = slots[index];
|
|
273
|
+
if (!slot) return;
|
|
274
|
+
entry.card.style.zIndex = String(slot.zIndex);
|
|
275
|
+
entry.card.style.pointerEvents = slot.visible ? 'auto' : 'none';
|
|
276
|
+
entry.card.setAttribute('aria-hidden', slot.visible ? 'false' : 'true');
|
|
277
|
+
if (dragging.has(item.id) || !entered.has(item.id)) return;
|
|
278
|
+
gsap.to(entry.card, {
|
|
279
|
+
xPercent: -50,
|
|
280
|
+
y: slot.y,
|
|
281
|
+
scale: slot.scale,
|
|
282
|
+
opacity: slot.opacity,
|
|
283
|
+
duration: reduced() ? 0 : duration,
|
|
284
|
+
ease,
|
|
285
|
+
delay: stagger * index,
|
|
286
|
+
overwrite: 'auto',
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
const restingY = (id: number) => {
|
|
291
|
+
const { entries, slots } = slotsFor();
|
|
292
|
+
return slots[entries.findIndex(({ item }) => item.id === id)]?.y ?? 0;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const openDeck = () => {
|
|
296
|
+
clearTimeout(collapse);
|
|
297
|
+
if (expanded) return;
|
|
298
|
+
expanded = true;
|
|
299
|
+
for (const { item } of live()) toastTimers.pause(item.id);
|
|
300
|
+
relayout(TIMING.toast.expand, springEase(0.55), TIMING.toast.expandStagger);
|
|
301
|
+
};
|
|
302
|
+
const closeDeck = () => {
|
|
303
|
+
clearTimeout(collapse);
|
|
304
|
+
collapse = setTimeout(() => {
|
|
305
|
+
if (!expanded) return;
|
|
306
|
+
expanded = false;
|
|
307
|
+
for (const { item } of live()) toastTimers.resume(item.id);
|
|
308
|
+
relayout(TIMING.toast.expand, springEase(0.55), TIMING.toast.expandStagger);
|
|
309
|
+
}, TIMING.toast.collapseDelayMs);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const showLayer = (visible: boolean) => {
|
|
313
|
+
if (!popover) return;
|
|
314
|
+
try {
|
|
315
|
+
if (host.matches(':popover-open')) host.hidePopover();
|
|
316
|
+
if (visible) host.showPopover();
|
|
317
|
+
} catch {
|
|
318
|
+
// :popover-open unsupported (no popover API): nothing to toggle.
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const attachGestures = (item: ToastItem, card: HTMLElement) => {
|
|
323
|
+
let start: { y: number; type: string } | null = null;
|
|
324
|
+
let wheel: { offset: number; end: ReturnType<typeof setTimeout> | undefined } | null = null;
|
|
325
|
+
const move = (offset: number) => gsap.set(card, { y: restingY(item.id) + offset, opacity: Math.max(0, 1 + Math.min(0, offset) / DISTANCE.fade) });
|
|
326
|
+
const cancel = () => {
|
|
327
|
+
dragging.delete(item.id);
|
|
328
|
+
gsap.to(card, { y: restingY(item.id), opacity: 1, duration: 0.25, ease: easeOutCubic });
|
|
329
|
+
if (!expanded) toastTimers.resume(item.id);
|
|
330
|
+
};
|
|
331
|
+
card.addEventListener('mouseenter', openDeck);
|
|
332
|
+
card.addEventListener('mouseleave', closeDeck);
|
|
333
|
+
card.addEventListener('pointerdown', (event) => {
|
|
334
|
+
if (event.button !== 0 || (event.target as Element).closest('button, a, input, textarea, select, [role="button"]')) return;
|
|
335
|
+
start = { y: event.clientY, type: event.pointerType };
|
|
336
|
+
card.setPointerCapture?.(event.pointerId);
|
|
337
|
+
dragging.add(item.id);
|
|
338
|
+
gsap.killTweensOf(card);
|
|
339
|
+
toastTimers.pause(item.id);
|
|
340
|
+
});
|
|
341
|
+
card.addEventListener('pointermove', (event) => {
|
|
342
|
+
if (!start) return;
|
|
343
|
+
const dy = event.clientY - start.y;
|
|
344
|
+
move(dy < 0 ? dy : start.type === 'touch' ? dy * GESTURE.resistance : 0);
|
|
345
|
+
});
|
|
346
|
+
const up = (event: PointerEvent) => {
|
|
347
|
+
if (!start) return;
|
|
348
|
+
const dy = event.clientY - start.y;
|
|
349
|
+
const type = start.type;
|
|
350
|
+
start = null;
|
|
351
|
+
if (dy < -GESTURE.swipe) {
|
|
352
|
+
dragging.delete(item.id);
|
|
353
|
+
dismissToast(item.id);
|
|
354
|
+
} else if (type === 'touch' && dy > GESTURE.expandDrag) {
|
|
355
|
+
cancel();
|
|
356
|
+
if (expanded) closeDeck();
|
|
357
|
+
else openDeck();
|
|
358
|
+
} else cancel();
|
|
359
|
+
};
|
|
360
|
+
card.addEventListener('pointerup', up);
|
|
361
|
+
card.addEventListener('pointercancel', up);
|
|
362
|
+
card.addEventListener(
|
|
363
|
+
'wheel',
|
|
364
|
+
(event) => {
|
|
365
|
+
const delta = event.deltaMode === 1 ? event.deltaY * 16 : event.deltaMode === 2 ? event.deltaY * innerHeight : event.deltaY;
|
|
366
|
+
if (!wheel) {
|
|
367
|
+
wheel = { offset: 0, end: undefined };
|
|
368
|
+
dragging.add(item.id);
|
|
369
|
+
toastTimers.pause(item.id);
|
|
370
|
+
}
|
|
371
|
+
wheel.offset = Math.min(0, wheel.offset - delta);
|
|
372
|
+
move(wheel.offset);
|
|
373
|
+
clearTimeout(wheel.end);
|
|
374
|
+
wheel.end = setTimeout(() => {
|
|
375
|
+
const offset = wheel?.offset ?? 0;
|
|
376
|
+
wheel = null;
|
|
377
|
+
if (offset < -GESTURE.wheel) {
|
|
378
|
+
dragging.delete(item.id);
|
|
379
|
+
dismissToast(item.id);
|
|
380
|
+
} else cancel();
|
|
381
|
+
}, GESTURE.wheelEndMs);
|
|
382
|
+
},
|
|
383
|
+
{ passive: true },
|
|
384
|
+
);
|
|
385
|
+
card.addEventListener('click', (event) => {
|
|
386
|
+
const target = event.target as Element;
|
|
387
|
+
if (target.closest('[data-mk-toast-dismiss]')) dismissToast(item.id);
|
|
388
|
+
else if (target.closest('[data-mk-toast-action]')) {
|
|
389
|
+
item.action?.onClick();
|
|
390
|
+
dismissToast(item.id);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const finishCard = (id: number) => {
|
|
396
|
+
settles.get(id)?.();
|
|
397
|
+
settles.delete(id);
|
|
398
|
+
const entry = cards.get(id);
|
|
399
|
+
if (entry) {
|
|
400
|
+
gsap.killTweensOf(entry.card);
|
|
401
|
+
entry.card.remove();
|
|
402
|
+
}
|
|
403
|
+
cards.delete(id);
|
|
404
|
+
entered.delete(id);
|
|
405
|
+
leaving.delete(id);
|
|
406
|
+
dragging.delete(id);
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const render = () => {
|
|
410
|
+
const list = getToasts();
|
|
411
|
+
const ids = new Set(list.map((item) => item.id));
|
|
412
|
+
for (const id of [...cards.keys()]) if (!ids.has(id)) finishCard(id);
|
|
413
|
+
for (const item of list) {
|
|
414
|
+
if (!cards.has(item.id) && !item.closing) {
|
|
415
|
+
const entry = buildCard(item, dismissLabel);
|
|
416
|
+
cards.set(item.id, entry);
|
|
417
|
+
host.append(entry.card);
|
|
418
|
+
gsap.set(entry.card, { xPercent: -50 });
|
|
419
|
+
attachGestures(item, entry.card);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
showLayer(list.some((item) => !item.closing) || leaving.size > 0);
|
|
423
|
+
for (const item of list) {
|
|
424
|
+
const entry = cards.get(item.id);
|
|
425
|
+
if (!entry) continue;
|
|
426
|
+
if (!entered.has(item.id) && !item.closing) {
|
|
427
|
+
entered.add(item.id);
|
|
428
|
+
toastTimers.start(item.id, item.duration);
|
|
429
|
+
relayout();
|
|
430
|
+
gsap.killTweensOf(entry.card);
|
|
431
|
+
gsap.set(entry.card, { xPercent: -50, y: restingY(item.id), scale: 1, opacity: 1 });
|
|
432
|
+
if (expanded) toastTimers.pause(item.id);
|
|
433
|
+
revealLetters(entry.message);
|
|
434
|
+
if (reduced()) continue;
|
|
435
|
+
if (item.origin?.isConnected) settles.set(item.id, morphFrom(entry.card, entry.shell, entry.body, item.origin));
|
|
436
|
+
else {
|
|
437
|
+
const y = restingY(item.id);
|
|
438
|
+
gsap.fromTo(
|
|
439
|
+
entry.card,
|
|
440
|
+
{ y: y - DISTANCE.enter, opacity: 0, filter: `blur(${DISTANCE.enterBlur}px)` },
|
|
441
|
+
{ y, opacity: 1, filter: 'blur(0px)', duration: TIMING.toast.enter, ease: springEase(0.58, -3.5), clearProps: 'filter' },
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (item.closing && !leaving.has(item.id)) {
|
|
446
|
+
leaving.add(item.id);
|
|
447
|
+
toastTimers.clear(item.id);
|
|
448
|
+
entry.card.style.zIndex = String(STACK.baseZ + 1);
|
|
449
|
+
const done = () => {
|
|
450
|
+
finishCard(item.id);
|
|
451
|
+
removeToast(item.id);
|
|
452
|
+
};
|
|
453
|
+
if (reduced()) done();
|
|
454
|
+
else {
|
|
455
|
+
gsap.to(entry.card, {
|
|
456
|
+
y: Number(gsap.getProperty(entry.card, 'y')) - DISTANCE.exit,
|
|
457
|
+
opacity: 0,
|
|
458
|
+
filter: `blur(${DISTANCE.exitBlur}px)`,
|
|
459
|
+
duration: TIMING.toast.exit,
|
|
460
|
+
ease: easeInCubic,
|
|
461
|
+
overwrite: 'auto',
|
|
462
|
+
onComplete: done,
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
relayout();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
scope.add(subscribeToasts(render));
|
|
471
|
+
render();
|
|
472
|
+
host.dataset.mkReady = '';
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
destroy() {
|
|
476
|
+
clearTimeout(collapse);
|
|
477
|
+
for (const id of [...cards.keys()]) finishCard(id);
|
|
478
|
+
showLayer(false);
|
|
479
|
+
scope.dispose();
|
|
480
|
+
delete host.dataset.mkReady;
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useRef } from 'react';
|
|
4
|
+
import { useIsomorphicLayoutEffect } from '../../react/use-recipe';
|
|
5
|
+
import { createToaster, type ToasterLabels } from './engine';
|
|
6
|
+
|
|
7
|
+
export interface ToasterProps {
|
|
8
|
+
labels?: ToasterLabels;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Stepbro's toast deck. Mount once near the root; show toasts with `toast()`. */
|
|
12
|
+
export function Toaster({ labels }: ToasterProps) {
|
|
13
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
14
|
+
const dismiss = labels?.dismiss;
|
|
15
|
+
useIsomorphicLayoutEffect(() => {
|
|
16
|
+
const toaster = createToaster(ref.current, { labels: { dismiss } });
|
|
17
|
+
return () => toaster.destroy();
|
|
18
|
+
}, [dismiss]);
|
|
19
|
+
return <div ref={ref} className="mk-toaster" />;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { toast, dismissToast, clearToasts, type ToastOptions, type ToastTone, type ToastAction } from './store';
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The toasts on screen and their timers (Stepbro's toast queue, ported from
|
|
3
|
+
* Kayro). Framework-agnostic: any number of toasters (vanilla, element, React)
|
|
4
|
+
* render the same list. A timer pauses while the deck is open and resumes with
|
|
5
|
+
* what was left. Never put a secret in a message: toasts confirm an action.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type ToastTone = 'success' | 'error' | 'info' | 'neutral';
|
|
9
|
+
|
|
10
|
+
export interface ToastAction {
|
|
11
|
+
label: string;
|
|
12
|
+
onClick: () => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ToastOptions {
|
|
16
|
+
/** The control the toast grows out of. */
|
|
17
|
+
origin?: HTMLElement | null;
|
|
18
|
+
/** ms on screen; 0 keeps it until dismissed. */
|
|
19
|
+
duration?: number;
|
|
20
|
+
tone?: ToastTone;
|
|
21
|
+
action?: ToastAction;
|
|
22
|
+
/** Show the tone's icon (default). */
|
|
23
|
+
icon?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ToastItem {
|
|
27
|
+
id: number;
|
|
28
|
+
message: string;
|
|
29
|
+
origin: HTMLElement | null;
|
|
30
|
+
duration: number;
|
|
31
|
+
tone: ToastTone;
|
|
32
|
+
action: ToastAction | null;
|
|
33
|
+
icon: boolean;
|
|
34
|
+
closing: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** At most this many live toasts; older ones leave to make room. */
|
|
38
|
+
export const TOAST_LIMIT = 5;
|
|
39
|
+
|
|
40
|
+
let items: ToastItem[] = [];
|
|
41
|
+
let nextId = 1;
|
|
42
|
+
const listeners = new Set<() => void>();
|
|
43
|
+
const emit = () => {
|
|
44
|
+
for (const listener of listeners) listener();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const getToasts = () => items;
|
|
48
|
+
|
|
49
|
+
export function subscribeToasts(listener: () => void) {
|
|
50
|
+
listeners.add(listener);
|
|
51
|
+
return () => {
|
|
52
|
+
listeners.delete(listener);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function show(message: string, { origin = null, duration = 4000, tone = 'success', action, icon = true }: ToastOptions = {}) {
|
|
57
|
+
const id = nextId++;
|
|
58
|
+
const live = items.filter((item) => !item.closing);
|
|
59
|
+
const extra = new Set(live.slice(0, Math.max(0, live.length - TOAST_LIMIT + 1)).map((item) => item.id));
|
|
60
|
+
items = [
|
|
61
|
+
...items.map((item) => (extra.has(item.id) ? { ...item, closing: true } : item)),
|
|
62
|
+
{ id, message, origin, duration, tone, action: action ?? null, icon, closing: false },
|
|
63
|
+
];
|
|
64
|
+
emit();
|
|
65
|
+
return id;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Start the closing animation of a toast. */
|
|
69
|
+
export function dismissToast(id: number) {
|
|
70
|
+
const item = items.find((entry) => entry.id === id);
|
|
71
|
+
if (!item || item.closing) return;
|
|
72
|
+
items = items.map((entry) => (entry.id === id ? { ...entry, closing: true } : entry));
|
|
73
|
+
emit();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Take a toast out of the list (after its exit animation). */
|
|
77
|
+
export function removeToast(id: number) {
|
|
78
|
+
toastTimers.clear(id);
|
|
79
|
+
items = items.filter((entry) => entry.id !== id);
|
|
80
|
+
emit();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Remove every toast at once (no animation). */
|
|
84
|
+
export function clearToasts() {
|
|
85
|
+
for (const item of items) toastTimers.clear(item.id);
|
|
86
|
+
items = [];
|
|
87
|
+
emit();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Show a toast: `toast('Copiado', { origin: button })`. Returns its id. */
|
|
91
|
+
export const toast = Object.assign(show, { dismiss: dismissToast, clear: clearToasts });
|
|
92
|
+
|
|
93
|
+
const running = new Map<number, { handle: ReturnType<typeof setTimeout> | null; remaining: number; started: number }>();
|
|
94
|
+
|
|
95
|
+
export const toastTimers = {
|
|
96
|
+
start(id: number, duration: number) {
|
|
97
|
+
if (duration <= 0 || running.has(id)) return;
|
|
98
|
+
running.set(id, { handle: setTimeout(() => dismissToast(id), duration), remaining: duration, started: Date.now() });
|
|
99
|
+
},
|
|
100
|
+
pause(id: number) {
|
|
101
|
+
const timer = running.get(id);
|
|
102
|
+
if (!timer?.handle) return;
|
|
103
|
+
clearTimeout(timer.handle);
|
|
104
|
+
timer.remaining = Math.max(0, timer.remaining - (Date.now() - timer.started));
|
|
105
|
+
timer.handle = null;
|
|
106
|
+
},
|
|
107
|
+
resume(id: number) {
|
|
108
|
+
const timer = running.get(id);
|
|
109
|
+
if (!timer || timer.handle) return;
|
|
110
|
+
if (timer.remaining <= 0) {
|
|
111
|
+
dismissToast(id);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
timer.started = Date.now();
|
|
115
|
+
timer.handle = setTimeout(() => dismissToast(id), timer.remaining);
|
|
116
|
+
},
|
|
117
|
+
clear(id: number) {
|
|
118
|
+
const timer = running.get(id);
|
|
119
|
+
if (timer?.handle) clearTimeout(timer.handle);
|
|
120
|
+
running.delete(id);
|
|
121
|
+
},
|
|
122
|
+
};
|