{"version":3,"file":"ngwr-circular-text.mjs","sources":["../../../projects/lib/circular-text/circular-text.ts","../../../projects/lib/circular-text/circular-text.html","../../../projects/lib/circular-text/ngwr-circular-text.ts"],"sourcesContent":["/**\n * @license\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE\n *\n * Angular adaptation of the CircularText effect by David Haz / reactbits.dev.\n * Original: https://www.reactbits.dev/text-animations/circular-text\n *\n * Reactbits drives the rotation with `motion/react`, which lets the spin\n * angle persist across speed changes. CSS keyframes can't do that — when\n * `animation-duration` changes mid-flight, the browser recomputes the\n * progress as `(elapsed % newDuration) / newDuration`, which visibly\n * jumps the rotation. So this port drives the rotation through the\n * Web Animations API and preserves the current angle when hover swaps\n * the duration. Bonkers `scale` is still CSS-driven (no conflict — the\n * rotation lives on an inner wrapper).\n */\n\nimport { isPlatformBrowser } from '@angular/common';\nimport {\n  Component,\n  DestroyRef,\n  type ElementRef,\n  PLATFORM_ID,\n  ViewEncapsulation,\n  afterNextRender,\n  computed,\n  effect,\n  inject,\n  input,\n  viewChild,\n} from '@angular/core';\n\nimport { WrPlatform } from 'ngwr/platform';\nimport { numAttr } from 'ngwr/utils';\n\nimport type { WrCircularTextHover } from './interfaces';\n\ninterface Char {\n  readonly ch: string;\n  readonly transform: string;\n}\n\n/**\n * Text laid out around a circle, with the whole circle spinning. Hover\n * behaviour swaps the spin rate (or pauses it) — the current rotation\n * angle is preserved across the swap so there's no visible jump.\n *\n * @example\n * ```html\n * <wr-circular-text text=\"HELLO * NGWR * \" />\n * <wr-circular-text text=\"GO BONKERS\" onHover=\"goBonkers\" [spinDuration]=\"12\" />\n * ```\n *\n * @see https://www.reactbits.dev/text-animations/circular-text\n */\n@Component({\n  selector: 'wr-circular-text',\n  templateUrl: './circular-text.html',\n  styleUrl: './circular-text.scss',\n  encapsulation: ViewEncapsulation.None,\n  host: {\n    class: 'wr-circular-text',\n    '[class.wr-circular-text--bonkers]': \"onHover() === 'goBonkers'\",\n  },\n})\nexport class WrCircularText {\n  /** Text to lay out around the circle. */\n  readonly text = input.required<string>();\n\n  /** Seconds per full revolution at rest. @default 20 */\n  readonly spinDuration = input(20, { transform: numAttr(20) });\n\n  /** Hover behaviour. `null` disables hover reactivity. @default 'speedUp' */\n  readonly onHover = input<WrCircularTextHover>('speedUp');\n\n  protected readonly chars = computed<readonly Char[]>(() => {\n    const letters = Array.from(this.text());\n    const len = letters.length;\n    if (len === 0) return [];\n    return letters.map((ch, i) => {\n      const rotation = (360 / len) * i;\n      // Rotate around the spinner's centre, then push each char outward\n      // by the orbit radius (set via CSS var). All chars end up on a\n      // circle of constant radius — not a diagonal line.\n      const transform = `rotate(${rotation}deg) translateY(calc(-1 * var(--wr-circular-text-radius)))`;\n      return { ch, transform };\n    });\n  });\n\n  private readonly spinEl = viewChild.required<ElementRef<HTMLElement>>('spin');\n\n  private readonly destroyRef = inject(DestroyRef);\n  private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));\n  private readonly platform = inject(WrPlatform);\n  private rotation: Animation | null = null;\n  private hovered = false;\n\n  /** Active spin duration in seconds — base, or hover-modified. */\n  private effectiveDuration(): number | 'pause' {\n    const base = this.spinDuration();\n    if (!this.hovered) return base;\n    switch (this.onHover()) {\n      case 'speedUp':\n        return base / 4;\n      case 'slowDown':\n        return base * 2;\n      case 'pause':\n        return 'pause';\n      case 'goBonkers':\n        return base / 20;\n      default:\n        return base;\n    }\n  }\n\n  constructor() {\n    if (!this.isBrowser) return;\n\n    afterNextRender(() => {\n      this.startOrSwap();\n\n      const host = this.spinEl().nativeElement.parentElement!;\n      const onEnter = (): void => {\n        this.hovered = true;\n        this.startOrSwap();\n      };\n      const onLeave = (): void => {\n        this.hovered = false;\n        this.startOrSwap();\n      };\n      host.addEventListener('mouseenter', onEnter);\n      host.addEventListener('mouseleave', onLeave);\n      this.destroyRef.onDestroy(() => {\n        host.removeEventListener('mouseenter', onEnter);\n        host.removeEventListener('mouseleave', onLeave);\n        this.rotation?.cancel();\n      });\n    });\n\n    // React to input changes (spinDuration, onHover) — and the OS\n    // reduced-motion setting — without losing angle.\n    effect(() => {\n      this.spinDuration();\n      this.onHover();\n      this.platform.prefersReducedMotion();\n      queueMicrotask(() => this.startOrSwap());\n    });\n  }\n\n  /**\n   * Start the rotation, or swap its duration while preserving the\n   * current angle. If the new mode is `pause`, just pause the animation.\n   */\n  private startOrSwap(): void {\n    if (!this.spinEl()) return;\n\n    // Reduced motion: hold the ring still — the circular layout is the\n    // point of the component; only the revolution is decorative.\n    if (this.platform.prefersReducedMotion()) {\n      this.rotation?.pause();\n      return;\n    }\n\n    const target = this.effectiveDuration();\n\n    if (target === 'pause') {\n      this.rotation?.pause();\n      return;\n    }\n\n    const newDurMs = Math.max(1, target * 1000);\n\n    // First start — no previous animation to preserve.\n    if (!this.rotation) {\n      this.rotation = this.spinEl().nativeElement.animate(\n        [{ transform: 'rotate(0deg)' }, { transform: 'rotate(360deg)' }],\n        { duration: newDurMs, iterations: Infinity, easing: 'linear' }\n      );\n      return;\n    }\n\n    // Compute the current angle (0..1 of a full turn) under the old\n    // timing, then restart with the new duration seeked to the same\n    // angle — keeps the visual continuous across speed changes.\n    const oldEffect = this.rotation.effect as KeyframeEffect | null;\n    const oldDurMs = (oldEffect?.getTiming().duration as number) || newDurMs || newDurMs;\n    const oldTime = (this.rotation.currentTime as number) ?? 0;\n    const ratio = (((oldTime % oldDurMs) + oldDurMs) % oldDurMs) / oldDurMs;\n\n    this.rotation.cancel();\n    this.rotation = this.spinEl().nativeElement.animate(\n      [{ transform: 'rotate(0deg)' }, { transform: 'rotate(360deg)' }],\n      { duration: newDurMs, iterations: Infinity, easing: 'linear' }\n    );\n    this.rotation.currentTime = ratio * newDurMs;\n  }\n}\n\nexport type { WrCircularTextHover } from './interfaces';\n","<span class=\"wr-circular-text__spin\" #spin>\n  @for (c of chars(); track $index) {\n    <span class=\"wr-circular-text__char\" [style.transform]=\"c.transform\">{{ c.ch }}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