Code Monkey home page Code Monkey logo

wc-responsive-container's Introduction

wc-responsive-container

Web component to allow an author to control styling based on the size of a containing element rather than the size of the user’s viewport.

An implementation based on Philip Walton's Responsive Components: a Solution to the Container Queries Problem using custom elements and resize observer.


Demo gif

Why?

If a container is within a dynamic app layout that can affect the container size, e.g. layout has closable drawers, it becomes difficult to encapsulate and localize the responsive logic within the container. Media queries as they exist today fall short. Container queries are a long desired CSS feature that could help, but unfortunately progress has stalled.

Install

npm i wc-responsive-container

Example

<style>
  * {
    box-sizing: border-box;
  }
  body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  article {
    width: 76%;
    height: 500px;
  }
  section {
    background-color: lightgray;
    outline: 3px dotted tomato;
    padding: 2rem;
  }
  wc-responsive-container {
    min-height: 100%;
    display: grid;
    grid-gap: 2rem;
    padding: 2rem;
    outline: 3px dotted skyblue;
  }
  wc-responsive-container.md {
    grid-template-columns: repeat(3, 1fr);
  }
  wc-responsive-container.md > [md-colspan="2"] {
    grid-column: span 2;
  }
  wc-responsive-container.md > [md-colspan="3"] {
    grid-column: span 3;
  }
</style>
<script type="module">
  // Using @web/dev-server or your favorite bundler...
  import 'wc-responsive-container';
</script>
<article>
  <wc-responsive-container breaks='{"sm": 300, "md": 500, "lg": 700, "xl": 900}'>
    <section md-colspan="1">
      1
    </section>
    <section md-colspan="2">
      2
    </section>
    <section md-colspan="3">
      3
    </section>
  </wc-responsive-container>
</article>

wc-responsive-container's People

Contributors

dependabot[bot] avatar pinkhominid avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nkabrown

wc-responsive-container's Issues

Flash of unstyled content

I'm really enjoying this responsive container. One thing I can't figure is how to make my screen not flicker if my container starts out with 768px of width, I temporarily have this flash from the smaller view to the larger view. Any quick css tricks that I might be able to apply here to mitigate this transition from one break-point to the other?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.