Socket
Book a DemoInstallSign in
Socket

@zeecoder/react-container-query

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zeecoder/react-container-query

React higher order component to manage a single Container instance.

npmnpm
Version
3.0.0-alpha.2
Version published
Weekly downloads
239
-20.33%
Maintainers
1
Weekly downloads
 
Created
Source

react-container-query

This module is part of a monorepo.

Detailed documentation can be found there.

Install

yarn add @zeecoder/react-container-query
# or
npm install --save @zeecoder/react-container-query

Set up PostCSS with webpack

Here is a documentation on how to do just that, in order to get the same syntax I've been using.

Usage

Assuming the following CSS:

.App {
  background: red;
  font-size: 20px;
  color: white;
  padding: 10px;
  border: none;

  @container (width > 900px) {
    background: green;
  }
}

And assuming that meta is the object obtained by running the above CSS through the postcss plugin.

<ContainerQuery>

A render-prop approach.

import React, { Component } from "react";
import { ContainerQuery } from "@zeecoder/react-container-query";
import { meta } from "./App.pcss";

const App = () => (
  <ContainerQuery meta={meta}>
    <div className="App">My App</div>
  </ContainerQuery>
);

export default App;

If you're also interested in the component's size:

import React, { Component } from "react";
import { ContainerQuery } from "@zeecoder/react-container-query";
import { meta } from "./App.pcss";

const App = () => (
  <ContainerQuery meta={meta}>
    {size => (
      <div className="App">
        My size is: {size.width}x{size.height}
      </div>
    )}
  </ContainerQuery>
);

export default App;

As you can see the ContainerQuery component automatically picks up the child element as the Container.

To do this, the component internally calls ReactDOM.findDOMNode(this). If you want to avoid that, you can also pass in the element prop:

import React, { Component } from "react";
import { ContainerQuery } from "@zeecoder/react-container-query";
import { meta } from "./App.pcss";

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      element: null
    };

    this.ref = React.createRef();
  }

  updateElementFromRef() {
    if (this.state.element !== this.ref.current) {
      this.setState({ element: this.ref.current });
    }
  }

  componentDidMount() {
    this.updateElementFromRef();
  }

  componentDidUpdate() {
    this.updateElementFromRef();
  }

  render() {
    return (
      <ContainerQuery meta={meta} element={this.state.element}>
        <div className="App" ref={this.ref}>
          My App
        </div>
      </ContainerQuery>
    );
  }
}

export default App;

withContainerQuery

If you prefer Higher-Order Components:

import { withContainerQuery } from "@zeecoder/react-container-query";
import { meta } from "./App.pcss";

const App = () => <div className="App">My App</div>;

export default withContainerQuery(App, meta);

License

MIT

Keywords

react

FAQs

Package last updated on 09 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts