ember-resize-observer-component
A tiny, ResizeObserver-based solution for building components responsive to their container size, rather than the viewport. The idea is to overcome the limitations of viewport-based media queries, and it's known as Container Queries
.
Here is how it can be done, using this addon (controversial example):
This addon focuses on simplicity. It only includes a single component and won't do any attributes binding for you. Instead, it provides element's dimensions and easy to use size-matchers
.
Highlights:
- a single component and nothing more
- only provides data (no attributes or class binding)
- supports
no-wrapper
mode - use your element directly - easy to use
size-matchers
- works with FastBoot/SSR
Installation
ember install ember-resize-observer-component
If you need a ResizeObserver polyfill (caniuse.com):
ember install ember-resize-observer-polyfill
Usage
Matcher query syntax
Matchers allow you to map size-related conditions, such as width > 100
, to properties on the object you get from <ResizeObserver />
(e.g. RO.is.large
). And whenever the element's size changes, the relevant properties will update.
In the example below, large
is a matcher name, and (hash width-gte=1200)
represents a condition query.
Available operators: eq
, gt
, gte
, lt
, lte
(they are similar to ember-truth-helpers)
Available conditions: width
, height
, aspectRatio
.
Note: The addon protects you from mistyping by validating all your matchers and telling you exactly what was wrong with a particular matcher.
Query | JavaScript |
---|---|
(hash width=10) |
width === 10 |
(hash width-gt=10) |
width > 10 |
(hash width-gte=10) |
width >= 10 |
(hash width-lt=10) |
width < 10 |
(hash width-lte=10) |
width <= 10 |
Examples with multiple values:
Query | JavaScript |
---|---|
(hash width=10 height-lt=20) |
width === 10 && height < 20 |
(hash width-gt=10 aspectRatio=1) |
width > 10 && aspectRatio === 1 |
In no-wrapper
mode, the component won't provide a wrapper element, so you will need to attach onResize
handler to a target element yourself:
You can set initial demensions that will be used if the component can't get element's size, e.g. in FastBoot environment:
For more info, please read the tests. The documentation will be improved soon.
Related addons
Alternative solutions worth checking out
Contributing
See the Contributing guide for details.
Compatibility
- Ember.js v3.20 or above
- Ember CLI v3.20 or above
- Node.js v12 or above
- Modern browsers (for IE 11 install polyfill)
License
This project is licensed under the MIT License.