{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fx-magnetic",
  "type": "registry:component",
  "title": "Magnetic",
  "description": "Content that leans toward the pointer over it",
  "author": "sudo-kw <https://github.com/sudo-kw>",
  "dependencies": [
    "@gpuix/native@0.11.0",
    "@gpuix/react@0.11.0",
    "effect@4.1.2",
    "react@^19.2.4"
  ],
  "registryDependencies": [
    "https://gpuixui.sudokw.com/gpuix-components.json",
    "https://gpuixui.sudokw.com/gpuix-motion.json",
    "https://gpuixui.sudokw.com/gpuix-tw.json"
  ],
  "files": [
    {
      "path": "packages/fx/src/magnetic.tsx",
      "target": "@ui/gpuixui/fx/magnetic.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport type { EventPayload, Props } from \"@gpuix/react\";\nimport {\n  mergeStyles,\n  resolveStyle,\n  type StateStyle,\n} from \"@gpuix/react/floating\";\nimport type { MotionLevel } from \"../motion/timing.js\";\nimport { cn } from \"../tw/index.js\";\nimport * as Option from \"effect/Option\";\nimport type { ReactNode } from \"react\";\nimport { useEffect } from \"react\";\nimport {\n  measuredPoint,\n  send,\n  useLocalState,\n  useLocalStore,\n  useMotionPreference,\n  useSurfaceTiming,\n  recipeStyle,\n  surfaceOriginStyle,\n  useSurfaceBox,\n  useTheme,\n} from \"../components/authoring.js\";\nimport {\n  magneticFrames,\n  magneticPull,\n  magneticRest,\n  samePull,\n} from \"./magnetic/logic.js\";\nimport { makeMagneticStore } from \"./magnetic/store.js\";\n\nexport interface MagneticState {\n  readonly pulled: boolean;\n  readonly reducedMotion: boolean;\n}\n\nexport interface MagneticProps extends Omit<Props, \"children\" | \"style\"> {\n  readonly children?: ReactNode;\n\n  readonly strength?: number;\n\n  readonly maxOffset?: number;\n\n  readonly level?: MotionLevel;\n\n  readonly className?: string;\n\n  readonly style?: StateStyle<MagneticState>;\n}\n\nexport const Magnetic = ({\n  children,\n  strength,\n  maxOffset,\n  level = \"fast\",\n  className,\n  style,\n  onMouseMove,\n  onMouseLeave,\n  onLayout,\n  ...rest\n}: MagneticProps) => {\n  const theme = useTheme();\n  const preference = useMotionPreference();\n  const timing = useSurfaceTiming(level).enter;\n  const surface = useSurfaceBox();\n  const store = useLocalStore(makeMagneticStore);\n  const pointer = useLocalState(store.local);\n  const reduced = preference === \"reduced\";\n\n  const pull = reduced\n    ? magneticRest\n    : Option.match(Option.all({ surface: surface.box, point: pointer }), {\n        onNone: () => magneticRest,\n        onSome: ({ surface, point }) =>\n          magneticPull(\n            point,\n            surface,\n            strength ?? undefined,\n            maxOffset ?? undefined,\n          ),\n      });\n\n  useEffect(() => {\n    if (reduced) send(store.release);\n  }, [reduced, store]);\n\n  const measure = (event: EventPayload) => {\n    surface.measure(event);\n    onLayout?.(event);\n  };\n\n  const follow = (event: EventPayload) => {\n    onMouseMove?.(event);\n    if (reduced) return;\n    surface.read();\n    send(store.follow(measuredPoint(event)));\n  };\n\n  const leave = (event: EventPayload) => {\n    onMouseLeave?.(event);\n    send(store.release);\n  };\n\n  const state: MagneticState = {\n    pulled: !samePull(pull, magneticRest),\n    reducedMotion: reduced,\n  };\n\n  return (\n    <div\n      {...rest}\n      onMouseMove={follow}\n      onMouseLeave={leave}\n      onLayout={measure}\n      style={mergeStyles(\n        mergeStyles(\n          { position: \"relative\" },\n          recipeStyle(cn(className), theme),\n        ),\n        resolveStyle(style, state),\n      )}\n    >\n      <div\n        style={surfaceOriginStyle(surface.nudge)}\n        onLayout={surface.noteOrigin}\n      />\n      <div\n        testId=\"magnetic-plate\"\n        motion={{\n          initial: false,\n          animate: magneticFrames(pull),\n          transition: timing,\n        }}\n        style={{ position: \"relative\" }}\n      >\n        {children}\n      </div>\n    </div>\n  );\n};\n\nexport const magneticMeta = {\n  name: \"Magnetic\",\n  tier: 5,\n  status: \"done\",\n  gpuixSupport: \"partial\",\n  limitations: [\n    \"the lean is a relative left/top offset on an inner plate, not a translate: the runtime has no transform (upstream issue #102)\",\n    \"the outer box deliberately stays still, so the pointer cannot leave the surface it is pulling; the content moves inside it\",\n    \"a reduced-motion reader and a keyboard reader get no lean; the hover fill and the focus ring are the feedback that remains\",\n    \"the pull follows the centre of the measured box; a 1px probe at the surface's origin re-reports it on every pointer read, because onLayout reports a size change and not a move\",\n    \"nothing moves until the surface and its probe have both reported a box\",\n  ],\n  docs: \"docs/magnetic.mdx\",\n  example: \"examples/magnetic.example.tsx\",\n  testFile: \"src/behaviour/magnetic.test.tsx\",\n} as const;\n"
    },
    {
      "path": "packages/fx/src/magnetic/logic.ts",
      "target": "@ui/gpuixui/fx/magnetic/logic.ts",
      "type": "registry:lib",
      "content": "import type { MotionStyle } from \"@gpuix/react\";\n\nexport interface MagneticPointer {\n  readonly x: number;\n  readonly y: number;\n}\n\nexport interface MagneticBox {\n  readonly x: number;\n  readonly y: number;\n  readonly width: number;\n  readonly height: number;\n}\n\nexport interface MagneticPull {\n  readonly left: number;\n  readonly top: number;\n}\n\nexport const magneticStrength = 0.35;\n\nexport const magneticReach = 12;\n\nexport const magneticRest: MagneticPull = { left: 0, top: 0 };\n\nconst usable = (value: number, low: number, fallback: number): number =>\n  Number.isFinite(value) && value >= low ? value : fallback;\n\nconst between = (value: number, low: number, high: number): number =>\n  Math.min(high, Math.max(low, value));\n\nconst tenth = (value: number): number => Math.round(value * 10) / 10;\n\nexport const magneticPull = (\n  pointer: MagneticPointer,\n  box: MagneticBox,\n  strength: number = magneticStrength,\n  maxOffset: number = magneticReach,\n): MagneticPull => {\n  const share = between(usable(strength, 0, magneticStrength), 0, 1);\n  const reach = usable(maxOffset, 0, magneticReach);\n  const left = between(\n    (pointer.x - (box.x + box.width / 2)) * share,\n    -reach,\n    reach,\n  );\n  const top = between(\n    (pointer.y - (box.y + box.height / 2)) * share,\n    -reach,\n    reach,\n  );\n  return { left: tenth(left), top: tenth(top) };\n};\n\nexport const magneticFrames = (pull: MagneticPull): MotionStyle => ({\n  left: pull.left,\n  top: pull.top,\n});\n\nexport const samePull = (a: MagneticPull, b: MagneticPull): boolean =>\n  a.left === b.left && a.top === b.top;\n\nexport const samePoint = (a: MagneticPointer, b: MagneticPointer): boolean =>\n  a.x === b.x && a.y === b.y;\n"
    },
    {
      "path": "packages/fx/src/magnetic/store.ts",
      "target": "@ui/gpuixui/fx/magnetic/store.ts",
      "type": "registry:lib",
      "content": "import { Effect } from \"effect\";\nimport * as Option from \"effect/Option\";\nimport * as SubscriptionRef from \"effect/SubscriptionRef\";\nimport {\n  type MeasuredPoint,\n  makeLocalState,\n  type LocalState,\n} from \"../../components/authoring.js\";\nimport { samePoint } from \"./logic.js\";\n\nexport type MagneticFollower = Option.Option<MeasuredPoint>;\n\nexport interface MagneticStore {\n  readonly local: LocalState<MagneticFollower>;\n\n  readonly follow: (point: Option.Option<MeasuredPoint>) => Effect.Effect<void>;\n\n  readonly release: Effect.Effect<void>;\n}\n\nexport const makeMagneticStore: Effect.Effect<MagneticStore> = Effect.gen(\n  function* () {\n    const local = yield* makeLocalState<MagneticFollower>(Option.none());\n\n    const follow = Effect.fnUntraced(function* (\n      point: Option.Option<MeasuredPoint>,\n    ) {\n      if (Option.isNone(point)) return;\n      const held = yield* SubscriptionRef.get(local.state);\n      if (Option.exists(held, (current) => samePoint(current, point.value)))\n        return;\n      yield* local.write(point);\n    });\n\n    return {\n      local,\n      follow,\n      release: local.write(Option.none()),\n    };\n  },\n);\n"
    }
  ],
  "docs": "[Magnetic](https://gpuixui.sudokw.com/effects/magnetic) — These are GPUix native components: the files render through GPUix's reconciler against `@gpuix/react`, not the DOM, and need the per-platform `@gpuix/native` binding. The CLI installs the `@gpuixui/*` runtime the components import as registry items, so the copied tree resolves on its own; wrap it in `ThemeProvider` before it renders.",
  "meta": {
    "url": "/effects/magnetic",
    "kind": "component",
    "runtime": "gpuix",
    "registry": "gpuixui"
  }
}