npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

container-media-observer

v0.1.4

Published

A lightweight JavaScript polyfill to support CSS @container queries in older browsers.

Readme

container-media-observer 🚀

Uma biblioteca leve, performática e agnóstica para simular CSS Container Queries em qualquer ambiente JavaScript.

Diferente das Media Queries tradicionais que dependem da largura da tela (viewport), esta biblioteca permite que seus componentes reajam ao tamanho do seu próprio container, garantindo modularidade real ao seu Design System.

NPM Version NPM Downloads License Build Status


✨ Destaques

  • Alta Performance: Utiliza ResizeObserver aliado ao requestAnimationFrame para evitar gargalos de renderização.
  • 🛠 Estratégias Flexíveis: Escolha entre aplicar Atributos de Dados (padrão CSS) ou Classes CSS.
  • 🧩 Escopável: Evite conflitos entre componentes distintos (ex: .Button--md vs .Box--md) usando prefixos customizados.
  • 📦 Agnóstico: Integração perfeita com qualquer framework moderno.

📦 Instalação

npm install container-media-observer

📖 Conceito e Tipagem (JSDoc)

A biblioteca fornece IntelliSense completo. Ao utilizar a função, seu editor de código exibirá automaticamente os tipos e descrições dos parâmetros:

/**
 * @param {HTMLElement} element - O elemento DOM a ser monitorado.
 * @param {Object.<string, number>} breakpoints - Ex: { sm: 300, md: 600 }
 * @param {Object} [options] - Configurações opcionais.
 * @param {string} [options.prefix='container-'] - Prefixo para a classe ou atributo.
 * @param {'class'|'attribute'} [options.strategy='attribute'] - Onde aplicar a marcação.
 * @returns {() => void} Função de limpeza (cleanup) para desconectar o observer.
 */

🛠 Exemplos por Framework

VueJs

Utilize ref para capturar o elemento e os hooks de ciclo de vida para gerenciar o observador.

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { containerJsQuery } from 'container-media-observer';

const containerRef = ref(null);
let stopQuery;

onMounted(() => {
  // Aplicando estratégia de CLASSE para um Drawer
  stopQuery = containerJsQuery(
    containerRef.value,
    {
      sm: 300,
      md: 700,
    },
    { strategy: 'class', prefix: 'Drawer--' }
  );
});

onBeforeUnmount(() => stopQuery?.());
</script>

<template>
  <div ref="containerRef" class="drawer-container">
    <slot />
  </div>
</template>

<style scoped>
/* A classe será aplicada conforme o tamanho do container: .Drawer--sm ou .Drawer--md */
.Drawer--md {
  display: flex;
  padding: 2rem;
}
</style>

Svelte

Em Svelte, a forma mais eficiente é utilizar uma Action, que lida automaticamente com a criação e destruição do componente.

<script>
  import { containerJsQuery } from 'container-media-observer';

  function containerAction(node) {
    const stop = containerJsQuery(
      node,
      {
        mobile: 320,
        tablet: 768,
      },
      { strategy: 'class', prefix: 'widget-' }
    );

    return {
      destroy() {
        stop();
      },
    };
  }
</script>

<div use:containerAction class="my-widget">
  O estilo desta div muda conforme o tamanho dela mesma.
</div>

<style>
  :global(.widget-mobile) {
    font-size: 0.8rem;
  }
  :global(.widget-tablet) {
    font-size: 1.2rem;
  }
</style>

React

Combine useRef e useEffect para inicializar a biblioteca e garantir que o cleanup seja executado ao desmontar.

import { useEffect, useRef } from 'react';
import { containerJsQuery } from 'container-media-observer';

export const Box = ({ children }) => {
  const boxRef = useRef(null);

  useEffect(() => {
    const cleanup = containerJsQuery(
      boxRef.current,
      {
        wide: 500,
      },
      { strategy: 'class', prefix: 'Box--' }
    );

    return () => cleanup();
  }, []);

  return (
    <div ref={boxRef} className="box-component">
      {children}
    </div>
  );
};

Angular

Acesse o DOM através de @ViewChild e utilize os hooks AfterViewInit e OnDestroy.

import {
  Component,
  ElementRef,
  ViewChild,
  AfterViewInit,
  OnDestroy,
} from '@angular/core';
import { containerJsQuery } from 'container-media-observer';

@Component({
  selector: 'app-card',
  template: `<div #cardElement class="card-container">...</div>`,
})
export class CardComponent implements AfterViewInit, OnDestroy {
  @ViewChild('cardElement') cardElement!: ElementRef;
  private stop?: () => void;

  ngAfterViewInit() {
    this.stop = containerJsQuery(
      this.cardElement.nativeElement,
      {
        full: 800,
      },
      { strategy: 'attribute', prefix: 'card-' }
    );
  }

  ngOnDestroy() {
    this.stop?.();
  }
}

Lit

Em componentes Lit (Web Components), o método firstUpdated é o local correto para iniciar observadores de DOM.

import { LitElement, html } from 'lit';
import { query } from 'lit/decorators.js';
import { containerJsQuery } from 'container-media-observer';

class MyButton extends LitElement {
  @query('.btn-wrapper') _btn;

  firstUpdated() {
    this._cleanup = containerJsQuery(
      this._btn,
      {
        large: 400,
      },
      { strategy: 'class', prefix: 'btn-' }
    );
  }

  disconnectedCallback() {
    super.disconnectedCallback();
    this._cleanup?.();
  }

  render() {
    return html`<div class="btn-wrapper"><button>Click Me</button></div>`;
  }
}
customElements.define('my-button', MyButton);

🎨 Exemplos de CSS

Usando Atributos (Padrão)

Ideal para manter uma estrutura baseada em dados:

.card[data-container-md] {
  grid-template-columns: 1fr 1fr;
}

Usando Classes (Estratégia de Escopo)

Ideal para Design Systems onde cada componente tem seu próprio namespace:

/* Botão reage apenas se o container dele for 'sm' */
.Button--sm {
  padding: 4px;
  font-size: 10px;
}

/* Box reage apenas se o container dela for 'lg' */
.Box--lg {
  padding: 40px;
  border-radius: 20px;
}