initRemChangeSensor function

  1. @visibleForTesting
Future<Null> initRemChangeSensor()

Implementation

@visibleForTesting
Future<Null> initRemChangeSensor() {
  _shouldStillMountRemChangeSensor = true;

  // Mount this asynchronously in case this initialization was triggered by
  // a `toRem` call inside a component's `render`.
  // (React emits a warning and sometimes gets in a bad state
  // when mounting component from inside `render`).
  return Future(() {
    // Short-circuit if destroyed during the async gap.
    if (!_shouldStillMountRemChangeSensor) {
      return;
    }

    // Short-circuit if already initialized (needs a check after async gap
    // to handle race conditions when this was called multiple times).
    if (changeSensorMountNode != null) {
      return;
    }

    // Force lazy-initialization of this variable if it hasn't happened already.
    _rootFontSize; // ignore: unnecessary_statements

    _changeSensorMountNode = DivElement()
      ..id = 'rem_change_sensor';

    // Ensure the sensor doesn't interfere with the rest of the page.
    _changeSensorMountNode!.style
      ..width = '0'
      ..height = '0'
      ..overflow = 'hidden'
      ..position = 'absolute'
      ..zIndex = '-1';

    document.body!.append(_changeSensorMountNode!);

    _changeSensor = react_dom.render((Dom.div()
      ..style = const {
        'position': 'absolute',
        'visibility': 'hidden',
        // ResizeSensor doesn't pick up sub-pixel changes due to its use of offsetWidth/Height,
        // so use 100rem for greater precision.
        'width': '100rem',
        'height': '100rem',
      }
    )(
      (v2.ResizeSensor()..onResize = (_) {
        recomputeRootFontSize();
      })()
    ), _changeSensorMountNode!);
  });
}