Giter Club home page Giter Club logo

brightspaceui / resize-aware Goto Github PK

View Code? Open in Web Editor NEW
2.0 16.0 1.0 1.62 MB

Deprecated!!! A Polymer 3 compatible solution to reacting to changes in an element's size and/or position

License: Apache License 2.0

JavaScript 100.00%
polyfill team-mango lit eslint dependabot license-checker npm playwright web-dev-server web-test-runner resize-observer resize-observer-polyfill github-actions package-lock-updater semantic-release lit-analyzer stylelint

resize-aware's Introduction

@brightspace-ui/resize-aware

Deprecated: use native ResizeObserver construct available in most modern browsers along with the resize-observer-polyfill if needed.

A lit and polymer compatible solution to reacting to changes in an element's size and/or position. Contains a polyfill for ResizeObserver that is able to detect changes inside of webcomponents on all supported browsers (Firefox, Chrome/Chromium, Edge, IE11, and Safari).

Polyfill

Global Polyfill

To use the polyfill, simply import resize-observer-polyfill.js into the page:

import '@brightspace-ui/resize-aware/resize-observer-polyfill.js';

or

<script type="module" src="@brightspace-ui/resize-aware/resize-observer-polyfill.js"></script>

Module Import

Alternatively, if you do not wish to alter or define window.ResizeObserver, you can instead import the polyfill as an es6 module:

import { ResizeObserver } from '@brightspace-ui/resize-aware/resize-observer-module.js';

Additionally, a separate class is provided that is capable to detecting changes in position as well as size, and checks the client bounding box (as returned by getBoundingClientRect()) rather than its content box.

import { BoundingBoxObserver } from '@brightspace-ui/resize-aware/resize-observer-module.js';

Web Component

This repo also includes a webcomponent implementing the polyfill for convenience:

<d2l-resize-aware>
  ...
</d2l-resize-aware>

Properties

  • position-aware (flag) - Also fires a d2l-resize-aware-resized event when the component's position changes (by default this event is only fired on a size change)

Events

  • d2l-resize-aware-resized Fired when the component's size (or position if the position-aware flag is specified) changes. This event is also fired once when the component is attached to the DOM.

    Bubbles: no Properties:

    • target (Node) - The <d2l-resize-aware> element that fired the event
    • detail.previous (DomRect) - The previous bounding box of the element before the size changed
    • detail.current (DomRect) - The new bounding box of the element (equal to the result of target.getBoundingClientRect())

Browser Support

The latest version of all major browsers (including Internet Explorer) are supported.

There are 3 different implemenations that will be used depending on the browser, plus an additional workaround for Safari:

  • Browser natively supports ResizeObserver (Chrome, Chromium) Simply use the native ResizeObserver, unless position-aware is specified, in which case use the 3rd implementation
  • Browser supports neither ResizeObserver nor native Shadow DOM, using the Shady DOM polyfill instead (Edge, IE11) Uses a single MutationObserver plus a resize and transitionend event handler on the window, and relies on the shady DOM polyfill to detect changes in the shady DOM of webcomponents
  • Browser supports native Shadow DOM, but not ResizeObserver (Firefox, Safari) Like above, but also recursively adds a MutationObserver and transitionend event listener to the shadow root of all webcomponents so that changes within the shadow DOM of webcomponents can be detected. As webcomponents are added and removed, the tree of mutation observers is automatically updated.

Safari has a browser bug that prevents the resizing of a textarea using the native resize handle from being detected by mutation observers. To work around this bug, if the browser is detected as Safari, and a textarea element whose resize styling does not resolve to none (that is, it has a drag handle) is found on the page, this component will poll for changes in size whenever the mouse is moving or a moving touch event occurs.

Limitations

The polyfill and component are unable to detect changes in position or size that occur as a result of CSS pseudo-class styling (eg. :hover) unless said syling has a transition.

If the browser natively supports ResizeObserver, the BoundingBoxObserver class will use both the native ResizeObserver and the polyfill together to cover each other's limitations. A CSS pseudo-class that causes another element to move but not change size will still not be detected, however, as neither the native implementation nor the polyfill can handle this case.

Versioning and Releasing

This repo is configured to use semantic-release. Commits prefixed with fix: and feat: will trigger patch and minor releases when merged to main.

To learn how to create major releases and release from maintenance branches, refer to the semantic-release GitHub Action documentation.

resize-aware's People

Contributors

bearfriend avatar d2l-github-action-tokens[bot] avatar dependabot[bot] avatar devpow112 avatar dlockhart avatar github-actions[bot] avatar mpharoah-d2l avatar semantic-release-bot avatar svanherk avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

bearfriend

resize-aware's Issues

Publish to npm?

Hi, I'm using your very useful library because it seems to be the only ResizeObserver polyfill that is aware of shadow DOM (nolanlawson/emoji-picker-element#101). This is very useful in my case for Jest tests running on top of JSDom.

By any chance, would you consider publishing the library to npm? It would be convenient to be able to npm install d2l-resize-aware, and the package name is not taken.

Thanks again for creating this library! Super useful, and even without it being on npm, it's possible to use by installing from GitHub. Cheers.

Chrome's native ResizeObserver is unreliable

Chrome's implementation of ResizeObserver fails to detect resizes when the resize is a result of an element being removed. May have to use the workaround even if the browser has a native ResizeObserver implementation

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.