observekit is a typed, zero-dependency wrapper over five native browser observer APIs:
MutationObserverResizeObserverIntersectionObserverPerformanceObserverReportingObserver
It brings them under one consistent API:
observekit.elementobservekit.selectorobservekit.attributeobservekit.childrenobservekit.textobservekit.resizeobservekit.visibleobservekit.performanceobservekit.reports
Every method returns a Disposer ({ disconnect(): void }) and shares the same base options:
signaloncedebouncetimeoutonConnectonDisconnect
It's a typed replacement for arrive.js-style polling hacks, ad-hoc lazy-loading code, and hand-rolled resize/visibility listeners.
Install:
npm i observekit
Zero dependencies ยท ESM + CJS ยท Fully typed
Install
npm
npm install observekit
Bun
bun add observekit
pnpm
pnpm install observekit
CDN
No build step required:
<script type="module">
import { observekit } from 'https://cdn.jsdelivr.net/npm/observekit/dist/observekit.esm.min.js';
</script>
Note: A CDN URL without a version serves whatever is currently tagged as
lateston jsDelivr/unpkg. Pin a version such asobservekit@0.0.4in production.
The Methods, in Daily Life
observekit.element / observekit.selector
Show a "welcome back" toast the moment it lands in the DOM, even if it's injected later by another script.
observekit.element('.toast', (els) => {
els.forEach((el) => el.classList.add('in'));
});
Read more about observekit.element
observekit.attribute
Flip the page to dark mode the instant your toggle button's aria-pressed attribute changes.
observekit.attribute(toggleBtn, ['aria-pressed'], (els) => {
document.body.classList.toggle(
'dark',
els[0].getAttribute('aria-pressed') === 'true'
);
});
Read more about observekit.attribute
observekit.children
Show or hide an "your cart is empty" message as items get added or removed.
observekit.children(cartList, () => {
emptyMsg.hidden = cartList.children.length > 0;
});
Read more about observekit.children
observekit.text
Update the browser tab title whenever your unread-message counter's text changes.
observekit.text(unreadCounter, () => {
document.title = `(${unreadCounter.textContent}) Inbox`;
});
Read more about observekit.text
observekit.resize
Collapse a sidebar's text labels to icons once it's been dragged narrower than 80px.
observekit.resize(sidebar, (entries) => {
sidebar.classList.toggle('compact', entries[0].contentRect.width < 80);
});
Read more about observekit.resize
observekit.visible
Lazy-load a product image only once it scrolls near the viewport.
observekit.visible(
img,
(entries) => {
if (entries[0].isIntersecting) {
img.src = img.dataset.src;
}
},
{ offset: 200, once: true }
);
Read more about observekit.visible
observekit.performance
Log Largest Contentful Paint (LCP) so you know if your homepage actually feels fast.
observekit.performance(['largest-contentful-paint'], (entries) => {
console.log('LCP:', entries.at(-1).startTime);
});
Read more about observekit.performance
observekit.reports
Catch deprecated API usage in production before it breaks on the next browser update.
observekit.reports(['deprecation'], (reports) => {
reports.forEach((r) => sendToLogging(r));
});
Top comments (0)