Vue Screen Utilities

1.0.0-beta.13 · active · verified Sun Apr 19

vue-screen-utils is a dependency-free Vue 3 library providing a suite of reactive utility functions for managing screen breakpoints, media queries, DOM element resize observations, and dark mode detection. Currently in version `1.0.0-beta.13`, it is in active development with a stable `1.0.0` release anticipated soon, implying a potentially frequent update cadence until then. The library is fully written in TypeScript, offering robust type safety and excellent IDE support. It distinguishes itself by allowing developers to define custom, semantically named screen size keys (e.g., `sm`, `md`, `lg`) and map them to various media query formats, which are then reactively evaluated against the current viewport. It offers both composable functions like `useScreens`, `useMediaQuery`, `useResizeObserver`, and `useDarkMode`, as well as a plugin for application-wide integration. Key features include a reactive `matches` object indicating active breakpoints, a `current` computed property for the largest active screen key, and utility functions like `mapCurrent` and `mapList` for mapping custom values to the current screen state.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `vue-screen-utils` in a parent component using `useScreens` and then consume the reactive screen utilities in a child component via Vue's `inject` API. It shows how to access the current screen, check active breakpoints, and map custom values.

// App.vue (Parent component where screens are configured)
<script setup lang="ts">
import { useScreens } from 'vue-screen-utils';
import ChildComponent from './ChildComponent.vue'; // Assume this file exists and is imported

// Configure screen breakpoints. This call also automatically 'provides' the screen utilities
// under the key '$screens' (or a custom injectKey if specified).
useScreens({
  xs: '0px', // Example: (min-width: 0px)
  sm: '640px',
  md: '768px',
  lg: '1024px',
  xl: '1280px'
});
</script>

<template>
  <div style="font-family: sans-serif; padding: 20px;">
    <h1>Parent Component</h1>
    <p>This component sets up the global screen utilities.</p>
    <ChildComponent />
  </div>
</template>


// ChildComponent.vue (Child component accessing screen utilities)
<script setup lang="ts">
import { inject, computed } from 'vue';
import type { Screens } from 'vue-screen-utils';

// Inject the screens object that was provided by the parent via useScreens.
// Type assertion `!` can be used if you're certain it will be injected, or handle `null`.
const $screens = inject<Screens>('$screens');

// Defensive check for robustness, especially in complex applications
if (!$screens) {
  console.error("Screen utilities not injected. Ensure useScreens is called in an ancestor.");
  // In a real application, you might provide fallback values or throw an error.
}

// Access reactive screen properties and utility functions
const currentScreen = computed(() => $screens?.current.value || 'N/A');
const isLargeScreen = computed(() => $screens?.matches.lg || false);
const mappedValue = computed(() =>
  $screens?.mapCurrent({ xs: 0, sm: 1, md: 2, lg: 3, xl: 4 }, 0).value || 0
);
</script>

<template>
  <div style="border: 1px solid #ccc; padding: 15px; margin-top: 20px;">
    <h2>Child Component</h2>
    <p>Resize your browser window to see reactive updates:</p>
    <p>Current screen: <strong>{{ currentScreen }}</strong></p>
    <p>Is 'large' (>=1024px): <strong>{{ isLargeScreen }}</strong></p>
    <p>Mapped value for current screen: <strong>{{ mappedValue }}</strong></p>
    <p v-if="currentScreen === 'xl'" style="color: #007bff;">You're on an extra-large screen!</p>
  </div>
</template>

view raw JSON →