@micropasts/scale-card
v0.1.0
Published
Zero-dependency Scale & Bounding-Box Web Components (Shadow DOM + Tailwind Light DOM) for collection records.
Maintainers
Readme
@micropasts/scale-card
@micropasts/scale-card is a framework-free library of web components for displaying the physical size and bounding box of museum, archaeological, and other collection objects.
It includes two renderers:
<scale-card>: encapsulated styles and markup using Shadow DOM.<scale-card-tw>: light-DOM markup intended for Tailwind CSS projects.
The package has no runtime dependencies. Dimension extraction, unit conversion, reference-object scaling, and event handling are shared by both components.
Installation
npm install @micropasts/scale-cardIn a browser module, import the package:
import '@micropasts/scale-card';When using the distribution file directly:
<script type="module">
import './node_modules/@micropasts/scale-card/dist/scale-card.esm.js';
</script>Quick start
<scale-card></scale-card>
<script type="module">
import '@micropasts/scale-card';
const card = document.querySelector('scale-card');
card.record = {
title: 'Terracotta Amphora',
heightCm: 48,
widthCm: 26.5,
depthCm: 26.5,
weight_kg: 6.4,
};
</script>The card can also load a record with the src attribute:
<scale-card src="/records/amphora.json"></scale-card>For Tailwind projects:
<scale-card-tw unit="cm" ref-id="can"></scale-card-tw>
<script type="module">
import '@micropasts/scale-card';
document.querySelector('scale-card-tw').record = {
title: 'Terracotta Amphora',
heightCm: 48,
widthCm: 26.5,
depthCm: 26.5,
weight_kg: 6.4,
};
</script>Tailwind must include classes used by the component in its build configuration. The light-DOM component is the better choice when an application already controls Tailwind and global dark-mode behavior.
Both components follow the system color preference by default and let you choose a fixed theme with the theme attribute or property. The Shadow DOM component has Light/Dark buttons; the Tailwind component has a sun/moon toggle:
<scale-card-tw theme="light"></scale-card-tw>
<scale-card theme="dark"></scale-card>card.theme = 'dark'; // 'light', 'dark', or 'system'The Tailwind component applies dark utility variants using a local .dark wrapper. Configure Tailwind's class-based dark mode. With the CDN build, load Tailwind first, then set tailwind.config = { darkMode: 'class' } before the components render. For independent per-card themes, do not place the component under a page-wide .dark ancestor.
Component attributes
| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| src | URL | unset | Fetches a JSON record. |
| unit | mm, cm, in | mm | Display unit for dimensions. |
| ref-id | Reference-object ID | battery | Initially selected physical reference. |
| lod | compact, standard, full | standard | Controls the level of detail. |
| title-key | Dot-separated key | title | Field used as the record title. |
| theme | system, light, dark | system | Initial color theme; the in-card selector can change it. |
Example:
<scale-card
unit="cm"
ref-id="a4"
lod="full"
title-key="object.name"
></scale-card>Properties
Both components expose the following properties:
card.record = recordObject;
card.config = dimensionConfig;
card.references = referenceObjects;
card.template = customTemplate;<scale-card-tw> additionally exposes classes.
card.classes = {
card: 'max-w-lg rounded-xl border bg-white p-4',
};Events
The unit selector emits unit-change:
card.addEventListener('unit-change', ({ detail }) => {
console.log(detail.unit);
});The reference selector emits reference-change:
card.addEventListener('reference-change', ({ detail }) => {
console.log(detail.refId, detail.ref);
});Selecting a reference in one component synchronizes every mounted component through scaleBus.
A failed src request emits load-error:
card.addEventListener('load-error', ({ detail }) => {
console.error(detail.src, detail.error);
});Default reference objects
| ID | Label | Dimensions |
| --- | --- | --- |
| battery | AA Cell | 50.5 × 14.5 mm |
| ball | Tennis Ball | Ø 67 mm |
| can | 330ml Can | 66 × 115 mm |
| a4 | A4 Sheet | 210 × 297 mm |
Custom references can be supplied by setting the references property. Each reference requires id, widthMm, and heightMm; label and detail are optional.
card.references = [
{
id: 'card',
label: 'Credit Card',
widthMm: 85.6,
heightMm: 53.98,
detail: '85.6 × 53.98 mm',
},
];
card.record = {
title: 'Small Badge',
width_mm: 70,
height_mm: 45,
};Dimension mapping
The default configuration supports common museum and collection-record field names:
import { DEFAULT_DIMENSION_CONFIG } from '@micropasts/scale-card';
console.log(DEFAULT_DIMENSION_CONFIG);Each mapping rule declares an axis and an ordered list of source paths and units:
import { buildScaleViewModel } from '@micropasts/scale-card';
const config = [
{
axis: 'height',
sources: [
{ path: 'height_mm', unit: 'mm' },
{ path: 'height_cm', unit: 'cm' },
{ path: 'dimensions.height', unit: 'cm' },
],
},
{
axis: 'width',
sources: [
{ path: 'width_mm', unit: 'mm' },
{ path: 'width_cm', unit: 'cm' },
],
},
];
card.config = config;Supported conversion units are mm, cm, m, in, g, kg, oz, and lb. The mapping stops at the first positive, finite numeric value.
Custom templates
Set template to replace the default markup while retaining the shared view model.
import { escapeHtml } from '@micropasts/scale-card';
card.template = (vm, stageHtml) => `
<article>
<h2>${escapeHtml(vm.title)}</h2>
${stageHtml}
${vm.axes.map((axis) => `
<p>${axis.label}: ${axis.objFormatted}</p>
`).join('')}
</article>
`;The Shadow DOM signature is (viewModel, stageHtml). The Tailwind component signature is (viewModel, stageHtml, classes).
Shared APIs
The package exports these utilities:
extractDimensions(record, config)formatLength(millimeters, unit)formatWeight(grams, system)getByPath(object, dotSeparatedPath)escapeHtml(value)computeZebraScaleBar(pixelsPerMm, unit, targetWidthPx)buildScaleViewModel(options)renderReferenceSvg(referenceId, widthPx, heightPx, mode)renderComparisonStage(viewModel, mode)positionPopover(anchor, popover, options)RecordStoredefaultRecordStoreEventBusscaleBusREFERENCE_OBJECTSDEFAULT_DIMENSION_CONFIGLOD_ADAPTERS
Level of detail
| Lod | Silhouette | Weight | Axes |
| --- | --- | --- | --- |
| compact | Hidden | Hidden | 2 |
| standard | Visible | Visible | 3 |
| full | Visible | Visible | 3 |
The default axis order is height, width, and depth.
Styling
Shadow DOM
The <scale-card> component exposes CSS Shadow Parts:
cardheadereyebrowtitleunit-groupunit-buttontheme-grouptheme-buttonreferencesreference-buttonaxesaxisfootersilhouette
The component also provides several CSS custom properties:
scale-card {
--sc-bg: #ffffff;
--sc-surface: #f8fafc;
--sc-border: #e2e8f0;
--sc-text: #0f172a;
--sc-muted: #64748b;
--sc-accent: #4f46e5;
--sc-ref: #d97706;
--sc-ref-text: #b45309;
--sc-radius: 12px;
}Example Shadow Parts styling:
scale-card::part(card) {
max-width: 38rem;
}
scale-card::part(reference-button) {
text-transform: uppercase;
}Tailwind light DOM
Pass a partial or complete class map to override Tailwind classes:
import { DEFAULT_TW_CLASSES } from '@micropasts/scale-card';
twCard.classes = {
...DEFAULT_TW_CLASSES,
card: 'max-w-xl rounded-xl border border-slate-300 bg-white p-5 shadow',
};Development
Requirements:
- Node.js 18 or newer
- npm
Install dependencies:
npm installBuild dist/ and the demo under docs/:
npm run buildWatch source files and rebuild:
npm run devThe demo entry is src/demo.html. The build script:
- Bundles
src/index.jsintodist/scale-card.esm.js. - Creates a minified demo bundle at
docs/scale-card.esm.js. - Generates
docs/index.htmlfromsrc/demo.html.
Serve the repository over HTTP (for example, npx serve .) rather than opening docs/index.html through file://; ES modules and src record fetching require a browser HTTP origin.
Browser support
The package uses modern browser APIs and targets ECMAScript 2022:
- ES modules
- Custom Elements
- Shadow DOM
MapandSetglobalThis.fetch
It is intended for current evergreen browsers.
License
MIT. See LICENSE.
