@molecule/app-annotation-pin-svelte
Feature · annotation-pin · App (browser) · v1.1.4 · Apache-2.0
Pin/annotation marker overlay with click-to-place pins and label/note popups for 3d-model-viewer, image annotations, map pins, hotspot tours
npm install @molecule/app-annotation-pin-svelteHow it works
@molecule/app-annotation-pin-svelte is a ready-made annotation-pin feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
<script lang="ts">
import { AnnotationLayer, type Pin } from '@molecule/app-annotation-pin-svelte'
let pins = $state<Pin[]>([])
let activeId = $state<string | null>(null)
function addPin({ x, y }: { x: number; y: number }): void {
const id = crypto.randomUUID()
pins = [...pins, { id, position: { x, y }, label: pins.length + 1 }]
activeId = id
}
</script>
<AnnotationLayer
{pins}
activePinId={activeId}
onPinClick={(id) => (activeId = id === activeId ? null : id)}
onSurfaceClick={addPin}
>
<img src="/floorplan.png" alt="" style="width: 100%; display: block" />
</AnnotationLayer>Works with: @molecule/app-svelte, @molecule/app-ui, @molecule/app-ui-svelte
Reference
Svelte annotation pin / hotspot overlay primitives — the Svelte counterpart
of @molecule/app-annotation-pin-react: same props (React names) and the
same DOM for the same input, so an app switches framework by changing only
the package name in its imports.
Exports AnnotationPin (single click-to-toggle marker) and
AnnotationLayer (parent wrapper that manages many pins on a surface).
Reusable for 3d-model viewers, image annotations, map pins, and hotspot
tours.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
<script lang="ts">
import { AnnotationLayer, type Pin } from '@molecule/app-annotation-pin-svelte'
let pins = $state<Pin[]>([])
let activeId = $state<string | null>(null)
function addPin({ x, y }: { x: number; y: number }): void {
const id = crypto.randomUUID()
pins = [...pins, { id, position: { x, y }, label: pins.length + 1 }]
activeId = id
}
</script>
<AnnotationLayer
{pins}
activePinId={activeId}
onPinClick={(id) => (activeId = id === activeId ? null : id)}
onSurfaceClick={addPin}
>
<img src="/floorplan.png" alt="" style="width: 100%; display: block" />
</AnnotationLayer>
Type
feature
Installation
npm install @molecule/app-annotation-pin-svelte @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte
API
Interfaces
Pin
A single pin to display on a parent surface. position is in the
parent's coordinate space — for image / 2D-canvas overlays this is
normalised 0..1 (so the pin re-anchors as the surface resizes); for
pixel-positioned overlays callers can pass raw px values and provide
their own style overrides via className.
The label/note are pure data — components handle render only. Surfaces
supply pin objects via <AnnotationLayer {pins}> (or render a single pin
directly if there is only one).
interface Pin {
/** Stable identifier (used as the list key). */
id: string
/** Position in the parent surface's coordinate space (typically normalised 0..1). */
position: { x: number; y: number }
/** Short label (e.g. an index or single-word tag) shown inside the pin marker. */
label?: Snippet | string | number | null | undefined
/** Longer note / description body, shown inside the popup when the pin is selected. */
note?: Snippet | string | number | null | undefined
}
AnnotationPinProps
<AnnotationPin> props.
interface AnnotationPinProps {
/** Position in the parent surface's coordinate space (typically normalised 0..1). */
position: { x: number; y: number }
/** Short label (e.g. an index or single-word tag) shown inside the marker. */
label?: Snippet | string | number | null | undefined
/** Longer note shown inside the popup when the pin is selected. */
note?: Snippet | string | number | null | undefined
/**
* Whether the pin is currently selected. When `true` the popup is
* rendered. Selection is controlled — callers manage state in
* `<AnnotationLayer>` (or themselves) and update via `onClick`.
*/
selected?: boolean
/** Click handler — fires when the pin marker is clicked. */
onClick?: () => void
/**
* Side the popup renders on, relative to the marker. Defaults to `'right'`.
*/
popupSide?: 'top' | 'bottom' | 'left' | 'right'
/** Extra classes merged onto the root wrapper. */
className?: string
/**
* Whether `position` is normalised (0..1) — when `true` the wrapper
* positions itself with `%` units so pins re-anchor on resize. When
* `false` the wrapper uses raw `px` values. Defaults to `true`.
*/
normalised?: boolean
}
AnnotationLayerProps
<AnnotationLayer> props.
interface AnnotationLayerProps {
/** Pins to render on the layer. */
pins: Pin[]
/** Optional layer content (the surface to annotate — image, canvas, map tile, etc.). */
children?: Snippet
/**
* The currently active (popup-open) pin id, or `null` for none. The
* layer is fully controlled — callers manage selection state.
*/
activePinId?: string | null
/** Called with the clicked pin id when a pin marker is clicked. */
onPinClick?: (pinId: string) => void
/**
* Called when the user clicks an empty area of the layer (i.e. not on
* a pin marker or a popup). Coordinates are in the same space as
* `Pin.position` — normalised 0..1 by default. Keyboard users get the
* same action: activating the layer's "add pin" button (Tab to it,
* then Enter / Space) places a pin at the surface centre.
*/
onSurfaceClick?: (position: { x: number; y: number }) => void
/**
* Whether positions are normalised (0..1) — when `true` the layer
* computes click coordinates as fractions of its bounding box; when
* `false` it returns raw pixel offsets. Defaults to `true`.
*/
normalised?: boolean
/**
* Side the popup renders on, relative to its marker. Defaults to `'right'`.
*/
popupSide?: 'top' | 'bottom' | 'left' | 'right'
/** Extra classes merged onto the layer root. */
className?: string
/** Inline style for the layer root, merged over the layer's own. */
style?: Record<string, string | number>
}
Constants
AnnotationLayer
const AnnotationLayer: LegacyComponentType
AnnotationPin
const AnnotationPin: LegacyComponentType
Injection Notes
Requirements
Peer dependencies:
@molecule/app-svelte^3.5.0 || ^4.0.0@molecule/app-ui^1.0.1@molecule/app-ui-svelte^1.6.0svelte^5.29.0
Runtime Dependencies
@molecule/app-svelte@molecule/app-ui@molecule/app-ui-sveltesvelte
Selection is fully controlled — the layer never stores the active pin;
manage activePinId yourself and toggle it in onPinClick. position
is normalised 0..1 by default (fractions of the layer box, so pins
re-anchor on resize); pass normalised={false} for raw pixel offsets — on
BOTH <AnnotationLayer> and any directly-rendered <AnnotationPin>, or
clicks and markers will disagree. The layer wraps the default content in a
position: relative box; a bare <AnnotationPin> needs its own positioned
ancestor. Node-valued props are snippets (or plain strings). Translations
come from the companion @molecule/app-locales-annotation-pin locale bond.
Translations
Translation strings are provided by @molecule/app-locales-annotation-pin.