Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AF-390 Add detection for / utilities to work around ResizeSensors that are detached from the DOM #187

Merged
merged 5 commits into from
Oct 3, 2018

Conversation

aaronlademann-wf
Copy link
Contributor

@aaronlademann-wf aaronlademann-wf commented Sep 27, 2018

Ultimate problem:

When a ResizeSensor is mounted into a node that is detached from the DOM, the offsetWidth / offsetHeight values of the expand / collapse nodes rendered by the ResizeSensor are 0, causing an invalid state within the ResizeSensor that will result in props.onResize events to not fire as expected.

How it was fixed:

  1. Add a props.onDetachedMountCheck callback, which will return true if the sensor was mounted into a detached node. That true value gives consumers the knowledge that they will most likely need to make use of a call to forceResetDetachedSensor() to get their onResize prop callbacks working again once they are confident that the node is now attached to the DOM.

Testing suggestions:

Passing tests.

Potential areas of regression:

None expected - new functionality only.


FYA: @greglittlefield-wf

@aviary-wf
Copy link

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on HipChat: InfoSec Forum.

ResizeSensorHandler onInitialize;

/// A function invoked when the parent element is resized.
/// A function invoked with a [ResizeSensorEvent] argument when the `children` of the
/// sensor causes the root node rendered by the [ResizeSensor] to resize.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the children

should be:

when the ResizeSensor resizes, either due to its parent or children resizing

@@ -44,10 +73,20 @@ abstract class ResizeSensorPropsMixin {

Map get props;

/// A function invoked when the resize sensor is initialized.
/// A function invoked with a [ResizeSensorEvent] argument when the resize sensor is initialized.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit ResizeSensorEvent is a redundant type reference; it's implied in the ResizeSensorHandler typing.

ResizeSensorHandler onInitialize;

/// A function invoked when the parent element is resized.
/// A function invoked with a [ResizeSensorEvent] argument when the `children` of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit ResizeSensorEvent is a redundant type reference; it's implied in the ResizeSensorHandler typing.

// Only perform this check if the consumer sets the callback.
if (props.onDidMountDetached == null) return;

var wasMountedDetachedFromDom = _needsReset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we glean this information from the DOM hierachy instead, so that we avoid accessing scrollTop/scrollLeft and causing reflows when quickMount is true?

bool _isAttachedToDocument(Node node) {
  Node current = node;
  while (current != null) {
    if (current == document) return true;
    current = node.parentNode;
  }
  return false;
}

var wasMountedDetachedFromDom = !_isAttachedToDocument(findDomNode(this));

/// > __NOTE:__ If this happens - you most likely do not need to set this callback. If for some reason the callback
/// sometimes returns `true`, and sometimes returns `false` _(unexpected)_,
/// you may have other underlying issues in your implementation that should be addressed separately.
BoolCallback onDidMountDetached;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this callback is a little misleading, and might imply that it's only called when the node was mounted detached from the DOM.

What about something like onDetachedMountCheck

/// repair the resize behavior via a call to [ResizeSensorComponent.forceResetDetachedSensor] at a time
/// when you are sure that the sensor has become attached to the DOM.
///
/// ### What does the bool return value indicate? ###
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/return value/argument

///
/// ### What does the bool return value indicate? ###
///
/// * A `true` return value indicates that __the [ResizeSensor] was mounted detached from the DOM__,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/return value/argument

/// values of the expand / collapse sensor nodes to the maximum possible value - which is what forces the
/// reflow / paint that makes the [onResize] callbacks begin firing when expected again.
///
/// * A `false` return value indicates that __the [ResizeSensor] was mounted attached to the DOM__.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/return value/argument

@codecov-io
Copy link

Codecov Report

Merging #187 into master will increase coverage by 0.05%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #187      +/-   ##
==========================================
+ Coverage   94.44%   94.48%   +0.05%     
==========================================
  Files          34       34              
  Lines        1617     1630      +13     
==========================================
+ Hits         1527     1540      +13     
  Misses         90       90

@aaronlademann-wf aaronlademann-wf changed the title Add detection for / utilities to work around ResizeSensors that are detached from the DOM AF-390 Add detection for / utilities to work around ResizeSensors that are detached from the DOM Oct 3, 2018
@aaronlademann-wf
Copy link
Contributor Author

QA +1

@Workiva/release-management-pp

@rmconsole2-wf rmconsole2-wf merged commit 45ff73a into master Oct 3, 2018
@rmconsole2-wf rmconsole2-wf deleted the fix-detached-resize-sensors branch October 3, 2018 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants