项目作者: gisu

项目描述 :
ContainerQueries in a smarter way
高级语言: JavaScript
项目地址: git://github.com/gisu/rcomps.git
创建时间: 2018-08-01T21:18:04Z
项目社区:https://github.com/gisu/rcomps

开源协议:MIT License

下载


ResponsiveComponents - rcomps

Based on the script by Philip Walton, here in form of an NPM plugin (https://philipwalton.com/articles/responsive-components-a-solution-to-the-container-queries-problem/). If you work with components you will often reach the limits with media queries. To increase reusability, however, you must be independent of the viewport. And this is exactly where container queries come into play.

Installation

  1. npm i rcomps --save
  2. yarn add rcomps --D

Usage

The simplest method is to mark the component with the tag data-observe-resizes.

  1. <div data-observe-resizes>
  2. <div class="component">...</div>
  3. </div>
  4. <!-- Result -->
  5. <div class="xs sm" data-observe-resizes>
  6. <div class="component">...</div>
  7. </div>

In this case, the predefined breakpoints are used (xs: 320, sm: 560, m: 768, l: 960, xl: 1200). You can overwrite the breakpoints with a new object - important here is to specify the breakpoints in px.

  1. rcomps({"break1": 500, "break2": 1000})

However, you are even more flexible if you can define breakpoints directly at the component. Nice side effect, you can also trigger utility classes (e.g. from tailwind) via the breakpoints.

  1. <div class="wrapper" data-observe-resizes
  2. data-breakpoints='{"break1": 300, "break2": 600, "break3": 800, "break4": 1000}'>
  3. <div class="component">...</div>
  4. </div>
  5. <!-- Result -->
  6. <div class="wrapper break1 break2 break3" data-observe-resizes
  7. data-breakpoints='{"break1": 300, "break2": 600, "break3": 800, "break4": 1000}'>
  8. <div class="component">...</div>
  9. </div>

If you use utility classes, you can trigger them individually, depending on the breakpoint. Adding the data attribute data-utility adds only one class at a time. This avoids the order around CSS when it comes to overwriting the declarations.

  1. <div class="wrapper"
  2. data-observe-resizes
  3. data-utility
  4. data-breakpoints='{"bg-green": 300, "bg-red": 600, "bg-blue": 800, "bg-yellow": 1000}'>
  5. <div class="component">...</div>
  6. </div>
  7. <!-- Result -->
  8. <div class="wrapper bg-blue"
  9. data-utility
  10. data-observe-resizes
  11. data-breakpoints='{"bg-green": 300, "bg-red": 600, "bg-blue": 800, "bg-yellow": 1000}'>
  12. <div class="component">...</div>
  13. </div>

Browser Support

  • Firefox 50+
  • Chrome
  • Internet Explorer 10+
  • Edge
  • Safari Desktop 10+
  • Safari Mobile

License

MIT

Thanks

To Philip Walton @philipwalton