项目作者: questmetrics

项目描述 :
A custom attribute for managing common element size observation and changes notification
高级语言: TypeScript
项目地址: git://github.com/questmetrics/aurelia-content-size-attribute.git
创建时间: 2018-07-19T01:10:38Z
项目社区:https://github.com/questmetrics/aurelia-content-size-attribute

开源协议:MIT License

下载


aurelia-content-size-attribute

TravisCI

A custom attribute for managing common element size observation and changes notification

Installation

  1. npm install aurelia-content-size-attribute --save

Development

  • Standard process: pull, install and build
    1. npm install
    2. npm run build

Test

  1. npm run test

Plugin Configuration

Simplest form of plugin configuration:

  1. // main.js
  2. import { configure as configureContentSizeAttribute } from 'aurelia-content-size-attribute';
  3. export function main(aurelia) {
  4. aurelia
  5. .use
  6. .plugin(configureContentSizeAttribute)
  7. // ...
  8. }

The plugin uses ResizeObserver, which is quite a new feature of the web,
which mean you may want to supply your own polyfill. Recommended polyfill at https://github.com/que-etc/resize-observer-polyfill . By default, the plugin uses the default ResizeObserver in global of PLATFORM, but you can override this by specifying it in 2nd parameter of plugin configuration:

  1. // main.js
  2. import { configure as configureContentSizeAttribute } from 'aurelia-content-size-attribute';
  3. import ResizeObserver from 'resize-observer-polyfill';
  4. export function main(aurelia) {
  5. aurelia
  6. .use
  7. .plugin(configureContentSizeAttribute, { resizeObserver: ResizeObserver })
  8. // ...
  9. }

Usage

Annotate any element that has its content size that needs to be observed with attribute content-size and bind command. like the following:

  1. <!-- app.html -->
  2. <template>
  3. <div class='container' content-size.bind="containerSize">
  4. </div>
  5. </template>

By default, the binding direction is from-view, which means only the custom attribute will notify the view model it resides in. (And the other direction doesn’t make sense).

Problem & solution

Sometimes in an application, it is desirable to be notified after dimension of an element has changed. There are many valid reasons for this: to adjust display, to reform functionalities. The causes are from many places: it could be because new content has been added to the element or some css effect / pseudo elements like :before & :after.

There is standard feature at ResizeObserver for this purpose. This plugin (read content-size attribute) is a convenient way to use the observer in an Aurelia application.