contained-react
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

Contained React

Contained React is a library that mimics the logic of Container Queries for React.

This library serves as a placeholder until we get matchContainer support for HTMLElements, and this will be updated to support that natively.

Usage

Container Query

import { useContainerQuery } from 'contained-react';

export function ContainerQuery() {
  const [ref, matches] = useContainerQuery('min-width: 700px');

  // matches will be true if the container is at least 700px wide
  return <div ref={ref}>{matches ? 'MATCH' : 'DOES NOT MATCH'}</div>;
}

Container Queries

Only width

import { useContainerQueries } from 'contained-react';

export function ContainerQueries() {
  const [ref, matches] = useContainerQueries({
    sm: 'min-width: 550px',
    md: 'min-width: 768px',
  });

  return (
    <div ref={ref}>
      {matches.width === 'sm' && <div>Small size</div>}
      {matches.width === 'md' && <div>Medium size</div>}
    </div>
  );
}

Width and height

import { useContainerQueries } from 'contained-react';

export function ContainerQueries() {
  const [ref, matches] = useContainerQueries({
    widthSm: 'min-width: 550px',
    widthMd: 'min-width: 768px',
    heightSm: 'min-height: 300px',
  });

  return (
    <div ref={ref}>
      {matches.width === 'widthSm' && matches.height === 'heightSm' && <div>Width and height both small size</div>}
      {matches.width === 'widthMd' && <div>Medium size</div>}
    </div>
  );
}

See NPM stats

https://bundlephobia.com/package/contained-react

Readme

Keywords

none

Package Sidebar

Install

npm i contained-react

Weekly Downloads

3

Version

1.4.0

License

ISC

Unpacked Size

19 kB

Total Files

9

Last publish

Collaborators

  • mthines