near-field 0.3: A Vue Adapter
near-field 0.3 adds a near-field/vue entry: a useNearField composable, v-magnetic, v-tilt, and v-glow directives, and Magnetic, Tilt, and Glow components, all thin layers over the same framework-free core. Plus the component typing bug that only showed up on a different Vue version.
The 0.2 post ended with a roadmap: "a Vue adapter, scroll proximity, and gyroscope tilt on mobile." Then two months went by without a release. Version 0.3.0 crosses off the first item. There is now a near-field/vue entry for Vue 3.3 and up, and it's on npm today.
If you haven't used it: near-field measures how close the pointer is to an element and writes it as CSS custom properties, --nf from 0 to 1 plus a direction in --nf-x and --nf-y. What that number does is up to your stylesheet. Move your cursor around the demo site (opens in a new tab) to see it.
Three ways in
The Vue entry comes in the three shapes Vue developers already reach for: a composable for the primitive, directives for elements you already have, and components when you want a wrapper.
The composable takes a template ref and writes the custom properties onto it:
<script setup>
import { ref } from "vue";
import { useNearField } from "near-field/vue";
const card = ref(null);
useNearField(card, { radius: 140 });
</script>
<template>
<div ref="card" class="card">...</div>
</template>The CSS from the first post works unchanged:
.card {
transform: translate(
calc(var(--nf-x) * var(--nf) * 8px),
calc(var(--nf-y) * var(--nf) * 8px)
);
}Options can be plain values, refs, or getters, and they're read at frame time, so changing radius doesn't tear anything down. useNearField follows the ref through a v-if swap, stops when the component unmounts, and returns a stop if you want out sooner. The ref can point at an element or a component; for a component it uses the root element.
The directives are the shortest path. In <script setup>, importing them is enough to register them:
<script setup>
import { vMagnetic, vTilt, vGlow } from "near-field/vue";
</script>
<template>
<button v-magnetic="{ strength: 0.3 }">Get started</button>
<img v-tilt="{ max: 6 }" src="/cover.png" alt="" />
<div v-glow="{ color: 'rgba(120, 170, 255, 0.3)' }" class="card">...</div>
</template>There's also v-near-field for the bare primitive. And if you prefer components, Magnetic, Tilt, and Glow have the same names, props, and defaults as the React ones, as included:
<script setup>
import { Magnetic } from "near-field/vue";
</script>
<template>
<Magnetic :strength="0.3"><button>Get started</button></Magnetic>
</template>Not reactive on purpose
The obvious Vue design would be a composable that returns a reactive proximity ref. I didn't build that. The field reports on every pointer move, even for elements far away, so a reactive value would re-render every subscribed component on every mouse move. The whole point of near-field is that the framework never hears about the pointer.
So the composable writes styles directly and the component never re-renders. If you really do want a reactive number, you opt in through the callback:
<script setup>
import { ref } from "vue";
import { useNearField } from "near-field/vue";
const el = ref(null);
const proximity = ref(0);
useNearField(el, {
onFrame: (frame) => (proximity.value = frame.proximity),
});
</script>
<template>
<div ref="el">{{ proximity.toFixed(2) }}</div>
</template>Directives meet re-renders
Directives had two traps that only show up once a parent component re-renders.
The first is inline objects. v-magnetic="{ strength: 0.3 }" creates a new object on every render of the parent. If the directive re-attached whenever its binding changed identity, every unrelated state change upstream would reset the spring mid-motion. So the effect directives compare their options value by value and only restart when something actually changed.
The second is Glow. It needs an overlay element inside yours for the gradient, and when Vue updates an element's text it replaces the children, which deletes an overlay Vue didn't create. v-glow notices the overlay is gone and puts it back. The Glow component avoids the problem by rendering its overlay as part of its own template, which also puts the overlay in the server-rendered HTML.
Both fixes have tests. For each one I broke the fix on purpose, watched its test fail, and put it back.
Why it was cheap to add
When I wrote the first version in July, I kept the pointer field and the spring integrator free of React, guessing that other adapters would follow. The vanilla entry was the first test of that, and Vue is the second. The core (the single pointer listener, the shared animation frame loop, the springs, the reduced-motion detection) didn't change at all for this release.
The only refactor was on the effects side. The vanilla effect code moved into a shared internal file, and Glow was split into the part that styles an overlay and the part that drives it. The vanilla glow and the Vue Glow now share it. The React entry didn't change.
Because the Vue code calls the same core, it behaves the same way as the other two entries under reduced motion and on touch screens, with no Vue-specific code for either. It also renders on the server: nothing touches window at import time, and one of the tests runs renderToString in Node with no window defined.
The bug that only appeared on another Vue
If you publish Vue components with TypeScript, this one is worth knowing about.
I first left the component types inferred, the way most defineComponent code does. The build tool then writes out the exact generic signature of DefineComponent from the Vue version it compiled against, which was 3.5.46. That signature has 20 type arguments, written out in exactly the shape that one release expects. Every test passed. But a consumer on any other Vue version, even 3.5.0, would have got type errors from near-field's declarations, as long as they had skipLibCheck: false. I caught it before publishing by typechecking a scratch app against the built package.
The fix is to stop shipping inferred types. Each component is built as a private implementation and exported with an explicit type:
export interface MagneticProps extends MagneticOptions {
as?: string | Component;
}
export const Magnetic = MagneticImpl as DefineComponent<MagneticProps>;DefineComponent<Props> is a short form that every Vue in the supported range understands, so the declarations no longer carry the build machine's Vue around with them. The same scratch app now typechecks on Vue 3.3.13, 3.4.38, 3.5.0, and 3.5.46, and passing a wrong prop type is still an error. The prop interfaces are exported too, matching the React side.
The test suite and typecheck also run against Vue 3.3.13, the bottom of the supported range, and I tried the packed 0.3.0 in a real Vite and Vue 3.5 app before publishing.
Sizes
All min+gzip, still no dependencies:
near-field/vue, with the composable, directives, and components: about 4.2KBnear-field/vanilla: about 3.1KB (up a few dozen bytes from the Glow split)near-field(React): about 3.5KB, unchanged
Vue is an optional peer dependency, same as React, so installing near-field for one framework doesn't pull in the other.
Try it
$ npm install near-fieldThe docs (opens in a new tab) now have a Vue section, the lab (opens in a new tab) is still the best place to feel the presets, and the package is on npm (opens in a new tab). Scroll proximity and gyroscope tilt are next.
If you put it in a Vue app, I'd like to see what you make.
Have a wonderful day.