# Compass button

> The needle of a brass compass hunts for the cursor anywhere on the page and wobbles with inertia before it settles.

- Category: [Buttons](https://library.aimran.es/components/buttons)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/uv-btn-compass
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

A directions button on a forest green pill with faint contour lines, like a corner of a hiking map. Its brass compass has a real needle driven by a spring simulation: it turns toward the pointer anywhere on the page, overshoots a little and settles. When the pointer leaves the window it swings back to north. Ideal for maps, store locators and travel guides.

The main color is the pill and the secondary color is the brass bezel, so navy with gold or sand with copper also work. The needle snaps straight to its target when reduced motion is on, and the global listener is removed when the component unmounts.

## Code

### HTML

```html
<button class="uvq01-cmp" type="button"><span class="dl" aria-hidden="true"><b>N</b><span class="nd"></span></span><span class="t">Get directions</span><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg></button>
```

### CSS

```css
.uvq01-cmp {
  --c1: #1f5140;
  --c2: #c9a24a;
  --s: 16px;
  /* tamaño */
  --k: 1;
  /* velocidad */
  --fg: #1d1d1f;
  /* texto */
  --bg: #f5f5f7;
  /* fondo */
}

.uvq01-cmp {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .6em;
  font-weight: 650;
  line-height: 1;
  letter-spacing: -.005em;
  white-space: nowrap;
  user-select: none;
  gap: .9em;
  padding: .6em 1.4em .6em .65em;
  border-radius: 999px;
  color: #f4ecd6;
  font-weight: 700;
  background: repeating-radial-gradient(circle at 85% 130%,transparent 0 .55em,rgba(255,255,255,.07) .55em calc(.55em + 1px)),radial-gradient(120% 140% at 20% 0%,color-mix(in srgb,var(--c1) 78%,#fff),var(--c1) 55%);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18),0 12px 24px -12px color-mix(in srgb,var(--c1) 80%,#000);
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uvq01-cmp .dl {
  position: relative;
  display: block;
  width: 2.5em;
  height: 2.5em;
  border-radius: 50%;
  background: radial-gradient(circle,#f4ecd6 0 60%,transparent 61%),repeating-conic-gradient(from -1.5deg,#5a4a30 0 3deg,transparent 3deg 30deg),#f4ecd6;
  box-shadow: 0 0 0 .2em var(--c2),0 0 0 .26em color-mix(in srgb,var(--c2) 50%,#000),0 .2em .4em rgba(0,0,0,.35);
}

.uvq01-cmp .dl::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: linear-gradient(135deg,rgba(255,255,255,.6),transparent 45%);
  pointer-events: none;
}

.uvq01-cmp .dl b {
  position: absolute;
  top: .3em;
  left: 50%;
  translate: -50% 0;
  font-size: .45em;
  font-weight: 900;
  line-height: 1;
  color: #b3261e;
}

.uvq01-cmp .nd {
  position: absolute;
  left: 50%;
  top: 50%;
  width: .36em;
  height: 1.9em;
  margin: -.95em 0 0 -.18em;
  background: linear-gradient(180deg,#d6402b 0 50%,#2c2a28 50%);
  clip-path: polygon(50% 0,100% 50%,50% 100%,0 50%);
  transform: rotate(var(--a,0deg));
}

.uvq01-cmp .nd::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: .2em;
  height: .2em;
  margin: -.1em;
  border-radius: 50%;
  background: var(--c2);
}

.uvq01-cmp svg {
  width: 1.1em;
  height: 1.1em;
  transition: transform calc(0.35s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uvq01-cmp:hover svg {
  transform: translateX(.25em);
}

.uvq01-cmp:active {
  transform: scale(.97);
}

.uvq01-cmp:focus-visible {
  outline: 2px solid var(--c1);
  outline-offset: 3px;
}
```

### JavaScript

```js
document.querySelectorAll(".uvq01-cmp").forEach(el => {
  const nd=el.querySelector(".nd"),dl=el.querySelector(".dl");let a=-70,v=0,tg=0,raf=0,last=0;
const rm=matchMedia("(prefers-reduced-motion: reduce)");
const put=()=>nd.style.setProperty("--a",a.toFixed(2)+"deg");
const step=t=>{const dt=Math.min(32,last?t-last:16)/16;last=t;const d=((tg-a)%360+540)%360-180;v+=d*.05*dt;v*=Math.pow(.88,dt);a+=v*dt;put();if(Math.abs(d)<.15&&Math.abs(v)<.05){raf=0;last=0;return}raf=requestAnimationFrame(step)};
const go=()=>{if(rm.matches){a=tg;v=0;put();return}if(!raf){last=0;raf=requestAnimationFrame(step)}};
const aim=e=>{const r=dl.getBoundingClientRect();const x=e.clientX-(r.left+r.width/2),y=e.clientY-(r.top+r.height/2);if(Math.hypot(x,y)<4)return;tg=Math.atan2(x,-y)*180/Math.PI;go()};
const home=()=>{tg=0;go()};const east=()=>{tg=90;go()};
window.addEventListener("pointermove",aim,{passive:true});window.addEventListener("pointerdown",aim,{passive:true});
document.documentElement.addEventListener("pointerleave",home);el.addEventListener("focus",east);
put();go();
return()=>{window.removeEventListener("pointermove",aim);window.removeEventListener("pointerdown",aim);document.documentElement.removeEventListener("pointerleave",home);cancelAnimationFrame(raf)}
});
```

### React

```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uvq01-cmp {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .6em;
  font-weight: 650;
  line-height: 1;
  letter-spacing: -.005em;
  white-space: nowrap;
  user-select: none;
  gap: .9em;
  padding: .6em 1.4em .6em .65em;
  border-radius: 999px;
  color: #f4ecd6;
  font-weight: 700;
  background: repeating-radial-gradient(circle at 85% 130%,transparent 0 .55em,rgba(255,255,255,.07) .55em calc(.55em + 1px)),radial-gradient(120% 140% at 20% 0%,color-mix(in srgb,var(--c1) 78%,#fff),var(--c1) 55%);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18),0 12px 24px -12px color-mix(in srgb,var(--c1) 80%,#000);
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uvq01-cmp .dl {
  position: relative;
  display: block;
  width: 2.5em;
  height: 2.5em;
  border-radius: 50%;
  background: radial-gradient(circle,#f4ecd6 0 60%,transparent 61%),repeating-conic-gradient(from -1.5deg,#5a4a30 0 3deg,transparent 3deg 30deg),#f4ecd6;
  box-shadow: 0 0 0 .2em var(--c2),0 0 0 .26em color-mix(in srgb,var(--c2) 50%,#000),0 .2em .4em rgba(0,0,0,.35);
}

.uvq01-cmp .dl::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: linear-gradient(135deg,rgba(255,255,255,.6),transparent 45%);
  pointer-events: none;
}

.uvq01-cmp .dl b {
  position: absolute;
  top: .3em;
  left: 50%;
  translate: -50% 0;
  font-size: .45em;
  font-weight: 900;
  line-height: 1;
  color: #b3261e;
}

.uvq01-cmp .nd {
  position: absolute;
  left: 50%;
  top: 50%;
  width: .36em;
  height: 1.9em;
  margin: -.95em 0 0 -.18em;
  background: linear-gradient(180deg,#d6402b 0 50%,#2c2a28 50%);
  clip-path: polygon(50% 0,100% 50%,50% 100%,0 50%);
  transform: rotate(var(--a,0deg));
}

.uvq01-cmp .nd::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: .2em;
  height: .2em;
  margin: -.1em;
  border-radius: 50%;
  background: var(--c2);
}

.uvq01-cmp svg {
  width: 1.1em;
  height: 1.1em;
  transition: transform calc(0.35s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uvq01-cmp:hover svg {
  transform: translateX(.25em);
}

.uvq01-cmp:active {
  transform: scale(.97);
}

.uvq01-cmp:focus-visible {
  outline: 2px solid var(--c1);
  outline-offset: 3px;
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const nd=el.querySelector(".nd"),dl=el.querySelector(".dl");let a=-70,v=0,tg=0,raf=0,last=0;
const rm=matchMedia("(prefers-reduced-motion: reduce)");
const put=()=>nd.style.setProperty("--a",a.toFixed(2)+"deg");
const step=t=>{const dt=Math.min(32,last?t-last:16)/16;last=t;const d=((tg-a)%360+540)%360-180;v+=d*.05*dt;v*=Math.pow(.88,dt);a+=v*dt;put();if(Math.abs(d)<.15&&Math.abs(v)<.05){raf=0;last=0;return}raf=requestAnimationFrame(step)};
const go=()=>{if(rm.matches){a=tg;v=0;put();return}if(!raf){last=0;raf=requestAnimationFrame(step)}};
const aim=e=>{const r=dl.getBoundingClientRect();const x=e.clientX-(r.left+r.width/2),y=e.clientY-(r.top+r.height/2);if(Math.hypot(x,y)<4)return;tg=Math.atan2(x,-y)*180/Math.PI;go()};
const home=()=>{tg=0;go()};const east=()=>{tg=90;go()};
window.addEventListener("pointermove",aim,{passive:true});window.addEventListener("pointerdown",aim,{passive:true});
document.documentElement.addEventListener("pointerleave",home);el.addEventListener("focus",east);
put();go();
return()=>{window.removeEventListener("pointermove",aim);window.removeEventListener("pointerdown",aim);document.documentElement.removeEventListener("pointerleave",home);cancelAnimationFrame(raf)}
}

export default function BtnCompass() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#1f5140",
          "--c2": "#c9a24a",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <button className="uvq01-cmp" type="button"><span className="dl" aria-hidden="true"><b>N</b><span className="nd"></span></span><span className="t">Get directions</span><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6" /></svg></button>
      </div>
    </>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Compass button” block (Buttons) from AIMRAN Library to my project.

## What it is
The needle of a brass compass hunts for the cursor anywhere on the page and wobbles with inertia before it settles.

## Chosen settings
- Main color: #1f5140
- Secondary color: #c9a24a
- Size: 16px (from 12 to 24px)
- Speed: 1x (from 0.5 to 2x)
- Text: "Cómo llegar"

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with plain HTML and CSS (the reference wraps it in a React component with its CSS). If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uvq01-cmp {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .6em;
  font-weight: 650;
  line-height: 1;
  letter-spacing: -.005em;
  white-space: nowrap;
  user-select: none;
  gap: .9em;
  padding: .6em 1.4em .6em .65em;
  border-radius: 999px;
  color: #f4ecd6;
  font-weight: 700;
  background: repeating-radial-gradient(circle at 85% 130%,transparent 0 .55em,rgba(255,255,255,.07) .55em calc(.55em + 1px)),radial-gradient(120% 140% at 20% 0%,color-mix(in srgb,var(--c1) 78%,#fff),var(--c1) 55%);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18),0 12px 24px -12px color-mix(in srgb,var(--c1) 80%,#000);
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uvq01-cmp .dl {
  position: relative;
  display: block;
  width: 2.5em;
  height: 2.5em;
  border-radius: 50%;
  background: radial-gradient(circle,#f4ecd6 0 60%,transparent 61%),repeating-conic-gradient(from -1.5deg,#5a4a30 0 3deg,transparent 3deg 30deg),#f4ecd6;
  box-shadow: 0 0 0 .2em var(--c2),0 0 0 .26em color-mix(in srgb,var(--c2) 50%,#000),0 .2em .4em rgba(0,0,0,.35);
}

.uvq01-cmp .dl::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: linear-gradient(135deg,rgba(255,255,255,.6),transparent 45%);
  pointer-events: none;
}

.uvq01-cmp .dl b {
  position: absolute;
  top: .3em;
  left: 50%;
  translate: -50% 0;
  font-size: .45em;
  font-weight: 900;
  line-height: 1;
  color: #b3261e;
}

.uvq01-cmp .nd {
  position: absolute;
  left: 50%;
  top: 50%;
  width: .36em;
  height: 1.9em;
  margin: -.95em 0 0 -.18em;
  background: linear-gradient(180deg,#d6402b 0 50%,#2c2a28 50%);
  clip-path: polygon(50% 0,100% 50%,50% 100%,0 50%);
  transform: rotate(var(--a,0deg));
}

.uvq01-cmp .nd::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: .2em;
  height: .2em;
  margin: -.1em;
  border-radius: 50%;
  background: var(--c2);
}

.uvq01-cmp svg {
  width: 1.1em;
  height: 1.1em;
  transition: transform calc(0.35s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uvq01-cmp:hover svg {
  transform: translateX(.25em);
}

.uvq01-cmp:active {
  transform: scale(.97);
}

.uvq01-cmp:focus-visible {
  outline: 2px solid var(--c1);
  outline-offset: 3px;
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const nd=el.querySelector(".nd"),dl=el.querySelector(".dl");let a=-70,v=0,tg=0,raf=0,last=0;
const rm=matchMedia("(prefers-reduced-motion: reduce)");
const put=()=>nd.style.setProperty("--a",a.toFixed(2)+"deg");
const step=t=>{const dt=Math.min(32,last?t-last:16)/16;last=t;const d=((tg-a)%360+540)%360-180;v+=d*.05*dt;v*=Math.pow(.88,dt);a+=v*dt;put();if(Math.abs(d)<.15&&Math.abs(v)<.05){raf=0;last=0;return}raf=requestAnimationFrame(step)};
const go=()=>{if(rm.matches){a=tg;v=0;put();return}if(!raf){last=0;raf=requestAnimationFrame(step)}};
const aim=e=>{const r=dl.getBoundingClientRect();const x=e.clientX-(r.left+r.width/2),y=e.clientY-(r.top+r.height/2);if(Math.hypot(x,y)<4)return;tg=Math.atan2(x,-y)*180/Math.PI;go()};
const home=()=>{tg=0;go()};const east=()=>{tg=90;go()};
window.addEventListener("pointermove",aim,{passive:true});window.addEventListener("pointerdown",aim,{passive:true});
document.documentElement.addEventListener("pointerleave",home);el.addEventListener("focus",east);
put();go();
return()=>{window.removeEventListener("pointermove",aim);window.removeEventListener("pointerdown",aim);document.documentElement.removeEventListener("pointerleave",home);cancelAnimationFrame(raf)}
}

export default function BtnCompass() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#1f5140",
          "--c2": "#c9a24a",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <button className="uvq01-cmp" type="button"><span className="dl" aria-hidden="true"><b>N</b><span className="nd"></span></span><span className="t">Get directions</span><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6" /></svg></button>
      </div>
    </>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
