All Projects β†’ react-container-query β†’ React Container Query

react-container-query / React Container Query

πŸ“¦ Modular responsive component

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Container Query

React Native Responsive Grid
Bringing the Web's Responsive Design to React Native
Stars: ✭ 369 (-53.17%)
Mutual labels:  responsive, responsive-layout
react-timeline
The easiest way to add a responsive and customizable timeline to React apps
Stars: ✭ 68 (-91.37%)
Mutual labels:  responsive, responsive-layout
Container Query
A PostCSS plugin and Javascript runtime combination, which allows you to write container queries in your CSS the same way you would write media queries.
Stars: ✭ 119 (-84.9%)
Mutual labels:  browser, responsive
react-context-responsive
A package that provides a responsive context to your application, using React Context API.
Stars: ✭ 25 (-96.83%)
Mutual labels:  responsive, responsive-layout
Layout
Flutter | Create responsive layouts for mobile, web and desktop
Stars: ✭ 312 (-60.41%)
Mutual labels:  responsive, responsive-layout
science-fiction-magazines-blog
Blog template (concept) is inspired by stylish science fiction magazines of the 80-90s.
Stars: ✭ 24 (-96.95%)
Mutual labels:  responsive, responsive-layout
bootstrap-grid-ms
Missing grid range in Bootstrap 3, micro-small from 480-767px.
Stars: ✭ 34 (-95.69%)
Mutual labels:  responsive, responsive-layout
Responsive Bootstrap Toolkit
Responsive Bootstrap Toolkit allows for easy breakpoint detection in JavaScript
Stars: ✭ 364 (-53.81%)
Mutual labels:  responsive, responsive-layout
Responsiveframework
Easily make Flutter apps responsive. Automatically adapt UI to different screen sizes. Responsiveness made simple. Demo: https://gallery.codelessly.com/flutterwebsites/minimal/
Stars: ✭ 476 (-39.59%)
Mutual labels:  responsive, responsive-layout
Launchy
A helper for launching cross-platform applications in a fire and forget manner.
Stars: ✭ 704 (-10.66%)
Mutual labels:  browser
Javascript Obfuscator
A powerful obfuscator for JavaScript and Node.js
Stars: ✭ 8,204 (+941.12%)
Mutual labels:  browser
Hugo Tranquilpeak Theme
A gorgeous responsive theme for Hugo blog framework
Stars: ✭ 686 (-12.94%)
Mutual labels:  responsive
Vue Ctk Date Time Picker
VueJS component to select dates & time, including a range mode
Stars: ✭ 707 (-10.28%)
Mutual labels:  responsive
Priority Navigation
Javascript implementation for Priority+ Navigation β€” no dependencies
Stars: ✭ 739 (-6.22%)
Mutual labels:  responsive
Monaco Editor
A browser based code editor
Stars: ✭ 27,382 (+3374.87%)
Mutual labels:  browser
Gitbeaker
πŸ€– GitLab API NodeJS library with full support of all the Gitlab API services.
Stars: ✭ 755 (-4.19%)
Mutual labels:  browser
Kick Off
UIkit 3 Starter Layout / Templates - Quick start for your UIkit 3 project!
Stars: ✭ 672 (-14.72%)
Mutual labels:  responsive-layout
Image Map Resizer
Responsive HTML Image Maps
Stars: ✭ 661 (-16.12%)
Mutual labels:  responsive
Bootstrap Email
Bootstrap 4 (and soon 5) stylesheet, compiler, and inliner for responsive and consistent emails with the Bootstrap syntax you know and love.
Stars: ✭ 781 (-0.89%)
Mutual labels:  responsive
Beaker
An experimental peer-to-peer Web browser
Stars: ✭ 6,411 (+713.58%)
Mutual labels:  browser

React Container Query

True modularity in styling responsive component.

npm version Circle CI Build Status codecov

Build Status

Installation

npm i -D react-container-query

Disclaimer

I am providing code in this repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not from my employer (Facebook).

API

useContainerQuery(query, initialSize?)

Compare the hook style code with the original example from https://github.com/react-container-query/react-container-query#containerquery-queryquery-initialsizewidth-height

Hook Example 1 - Queries against a native DOM element as the container

  • Native DOM element refers to div, span, etc.
import React from 'react';
import { useContainerQuery } from 'react-container-query';

const query = {
  'width-between-400-and-599': {
    minWidth: 400,
    maxWidth: 599,
  },
  'width-larger-than-600': {
    minWidth: 600,
  },
};

const MyComponent = () => {
  const [params, containerRef] = useContainerQuery(query);
  return <div ref={containerRef} className={classnames(params)}>the box</div>;
};

Hook Example 2 - Usage for a React component as the container - React.forwardRef

  • If the container element we want to measure is a React component, and since we can't measure the size of React component itself, we can use React.forwardRef.
  • The container React component must then forward the ref and set it on the actual native DOM element it renders (e.g, div, span, etc) - as seen in th example below
import React from 'react';
import { useContainerQuery } from 'react-container-query';

const query = {
  'width-between-400-and-599': {
    minWidth: 400,
    maxWidth: 599,
  },
  'width-larger-than-600': {
    minWidth: 600,
  },
};

const MyCustomWrapper = React.forwardRef((props, ref) => {
  // MyCustomWrapper really renders a div which wraps the children. 
  // Setting the ref on it allows container query to measure its size.
  return <div ref={ref}>{props.children}</div>
});

const MyComponent = () => {
  const [params, containerRef] = useContainerQuery(query);
  return <MyCustomWrapper ref={containerRef} className={classnames(params)}>the box</div>;
};
  • In this example, <MyCustomWrapper /> would forward the containerRef and set it on the div element it is using to wrap the children.

<ContainerQuery query={query} initialSize?={{width?, height?}}>

import React, {Component} from 'react';
import {render} from 'react-dom';
import {ContainerQuery} from 'react-container-query';
import classnames from 'classnames';

const query = {
  'width-between-400-and-599': {
    minWidth: 400,
    maxWidth: 599
  },
  'width-larger-than-600': {
    minWidth: 600,
  }
};

function MyComponent() {
  /**
   * `params` in the children function will look like
   * {
   *   'width-between-400-and-599': true,
   *   'width-larger-than-600': false
   * }
   */
  return (
    <ContainerQuery query={query}>
      {(params) => (
        <div className={classnames(params)}>the box</div>
      )}
    </ContainerQuery>
  );
};

/**
 * This will generate following HTML:
 * <div class="width-between-400-and-599"></div>
 */

render(<MyComponent/>, document.getElementById('app'));

properties

  • props.children

    Must be a function to return a single or an array of React elements. The function will be invoked with params, which is a key-value pair where keys are class names, values are booleans to indicate if that class name's constraints are all satisfied.

  • props.query

    "query" is key-value pairs where keys are the class names that will be applied to container element when all constraints are met. The values are the constraints.

  • props.initialSize? (optional)

    initialSize is an object with optional width or height property. Because the limitation on how size is computed based on underlying element, in the initial rendering pass, we don't have the size info (because element must be in the DOM have a valid size). At this time initialSize will be used as the size of the element.

applyContainerQuery(Component, query, initialSize?) -> ReactComponent

import React, {Component} from 'react';
import {render} from 'react-dom';
import {applyContainerQuery} from 'react-container-query';
import classnames from 'classnames';

const query = {
  'width-between-400-and-599': {
    minWidth: 400,
    maxWidth: 599
  },
  'width-larger-than-600': {
    minWidth: 600,
  }
};

class Container extends Component {
  render() {
    /**
     * `this.props.containerQuery` will look like
     * {
     *   'width-between-400-and-599': true,
     *   'width-larger-than-600': false
     * }
     */
    return <div className={classnames(this.props.containerQuery)}>the box</div>;
  }
}

const App = applyContainerQuery(Container, query)

/**
 * This will generate following HTML:
 * <div class="width-between-400-and-599"></div>
 */

render(<App/>, document.getElementById('app'));

This is a very similar to <ContainerQuery/>, except it's higher order component style. You don't have to use them together.

Why

Modularity is the heart of component based UI. With most JavaScript modularized, CSS failed to catch up. When developing a responsive web page, we use media queries to toggle styles based on the size of the viewport. This creates problems when creating component level styles. The same component will behave differently when it is placed in different locations on a page. It seriously breaks the modularity of a component. We need components to be responsive and independent of viewport sizes.

What is container query

Container query is a work in process CSS feature. "Container queries allow an author to control styling based on the size of a containing element rather than the size of the user’s viewport." (from Container Query). Container Queries: Once More Unto the Breach is the inspiration of this repo.

With below CSS, .box will be blue when .container is wider than 600px, green when width between 400px and 599px, and red for the rest of time.

.box {
  background-color: red;
}

.container:media(min-width: 400px) {
  .box {
    background-color: green;
  }
}

.container:media(min-width: 600px) {
  .box {
    background-color: blue;
  }
}

Note: This library does not provide these CSS features.

Demo

Checkout CodePen

You can also check out examples directory.

Performance

react-container-query is using element-resize-detector in mainstream browsers and ResizeObserver in cutting edge browsers. It's completely event based, so no excessive code runs if no changes on element sizes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].