Viewport Module

GitHub 

Reactive viewport dimensions with a single shared resize listener. Components subscribe to specific properties and only re-render when those values change.

Why Use This 

  • Single listener - One resize event handler shared across all subscribers
  • Granular updates - Subscribe to width, height, or breakpoints independently
  • SSR-safe - Proper hydration handling for server-rendered apps

Installation 

npm
pnpm
yarn
npm install contection-viewport
pnpm add contection-viewport
yarn add contection-viewport

Usage 

import { createViewportStore } from "contection-viewport";
import { useStore } from "contection";

const ViewportStore = createViewportStore();

function App() {
return (
<ViewportStore>
<YourComponents />
</ViewportStore>
);
}

Subscribe to Dimensions 

function WindowSize() {
const { width, height } = useStore(ViewportStore, {
keys: ["width", "height"],
});
return <p>{width} x {height}</p>;
}

Subscribe to Breakpoints 

function ResponsiveComponent() {
const { isMobile } = useStore(ViewportStore, {
keys: ["isMobile"],
});
return isMobile ? <MobileLayout /> : <DesktopLayout />;
}

Available Store Keys 

KeyTypeDescription
widthnumberViewport width in pixels
heightnumberViewport height in pixels
isMobilebooleantrue if width < 768px
isTabletbooleantrue if width >= 768px and < 1024px
isDesktopbooleantrue if width >= 1024px
Return to navigation