Zero-dependency JavaScript Web Components for British National Grid (OSGB36 / EPSG:27700) references, WGS84 conversion, grid square precision/sensitivity indicators, and SVG grid-square previews.
Includes two custom elements built from the same core library:
<grid-ref>— Encapsulated Shadow DOM Web Component with CSS custom properties,::part()selectors, and custom HTML template hooks.<grid-ref-tw>— Light-DOM Web Component styled with Tailwind CSS utility classes anddark:variants.
- Pure ES2022 JavaScript: Zero runtime dependencies, no TypeScript build step, and works in vanilla HTML, static sites, or modern bundlers.
- OSGB36 ↔ WGS84 Conversion: Parses 2-letter 100km grid tiles plus 2- to 10-digit references (
TL10,TL 135 072,TL 1354 0728) and computes easting/northing, square precision (100kmdown to1m), WGS84 centroids, bounding boxes, and GeoJSON polygons. - Findspot Sensitivity & Precision Context: Highlights square size and rounding level so coarse public findspots (
10kmor1km) are visually distinct from site or trench coordinates. - Inline SVG Grid Preview: Renders a compact preview showing where the square sits within its parent 10km grid tile.
- Cross-Component Event Sync: Emits and listens for
heritage:selectCustomEvents so maps, timelines, and panels stay coordinated. - Clipboard Copy with Fallback: Copies formatted grid and coordinate strings with a fallback when restricted clipboard contexts block
navigator.clipboard.writeText.
npm install @micropasts/grid-ref
Or build from source with zero external dependencies:
npm run build
npm run dev
npm run build generates:
dist/grid-ref.esm.js— ES Module bundledist/grid-ref.iife.js— Global<script>bundle (window.GridRefLib)docs/index.htmlanddocs/grid-ref.esm.js— Self-contained GitHub Pages demo
<script type="module" src="./dist/grid-ref.esm.js"></script>
<p>
Findspot recorded at
<grid-ref-tw ref="TL 135 072" prevent-navigation>
TL 135 072
</grid-ref-tw>
or rounded publicly to
<grid-ref-tw ref="TL10" mode="inline"></grid-ref-tw>.
</p>Works anywhere without Tailwind CSS:
<grid-ref ref="SP910090">SP 910 090</grid-ref><grid-ref-tw mode="panel" listen="heritage:select" ref="TL 135 072"></grid-ref-tw>import {
parseGridRef,
osgbToWgs84,
buildGridSvg,
emitGridSelect,
} from '@micropasts/grid-ref';
const parsed = parseGridRef('TL 135 072');
console.log(parsed.center, parsed.precisionLabel, parsed.geojson);Pass trigger-class or card-class on individual elements:
<grid-ref-tw
ref="TL 135 072"
trigger-class="text-amber-700 dark:text-amber-400"
card-class="border-amber-500 ring-2 ring-amber-500/20">
TL 135 072
</grid-ref-tw>Or update GridRefTailwind.classes globally at startup.
grid-ref.custom-grid {
--gr-accent: #d97706;
--gr-border: #b45309;
--gr-radius: 0.375rem;
--gr-width: 360px;
}
grid-ref.custom-grid::part(title) {
color: #f59e0b;
}import { GridRef } from '@micropasts/grid-ref';
GridRef.setTemplate((parsed, esc) => `
<div>
<strong>${esc(parsed.formatted)}</strong> (${esc(parsed.precisionLabel)})
<div>${parsed.center.lat}, ${parsed.center.lon}</div>
</div>
`);- Push to
main(the included.github/workflows/pages.ymlworkflow runsnpm run buildand publishes./docs). - In GitHub repository Settings → Pages, set Source to GitHub Actions (or Deploy from a branch →
main//docsafter runningnpm run build).
MIT