ref.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license
  3. * Copyright 2020 Google LLC
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. import { nothing } from '../lit-html.js';
  7. import { directive, AsyncDirective } from '../async-directive.js';
  8. /**
  9. * Creates a new Ref object, which is container for a reference to an element.
  10. */
  11. export const createRef = () => new Ref();
  12. /**
  13. * An object that holds a ref value.
  14. */
  15. class Ref {
  16. }
  17. // When callbacks are used for refs, this map tracks the last value the callback
  18. // was called with, for ensuring a directive doesn't clear the ref if the ref
  19. // has already been rendered to a new spot
  20. const lastElementForCallback = new WeakMap();
  21. class RefDirective extends AsyncDirective {
  22. render(_ref) {
  23. return nothing;
  24. }
  25. update(part, [ref]) {
  26. var _a;
  27. const refChanged = ref !== this._ref;
  28. if (refChanged && this._ref !== undefined) {
  29. // The ref passed to the directive has changed;
  30. // unset the previous ref's value
  31. this._updateRefValue(undefined);
  32. }
  33. if (refChanged || this._lastElementForRef !== this._element) {
  34. // We either got a new ref or this is the first render;
  35. // store the ref/element & update the ref value
  36. this._ref = ref;
  37. this._context = (_a = part.options) === null || _a === void 0 ? void 0 : _a.host;
  38. this._updateRefValue((this._element = part.element));
  39. }
  40. return nothing;
  41. }
  42. _updateRefValue(element) {
  43. if (typeof this._ref === 'function') {
  44. // If the current ref was called with a previous value, call with
  45. // `undefined`; We do this to ensure callbacks are called in a consistent
  46. // way regardless of whether a ref might be moving up in the tree (in
  47. // which case it would otherwise be called with the new value before the
  48. // previous one unsets it) and down in the tree (where it would be unset
  49. // before being set)
  50. if (lastElementForCallback.get(this._ref) !== undefined) {
  51. this._ref.call(this._context, undefined);
  52. }
  53. lastElementForCallback.set(this._ref, element);
  54. // Call the ref with the new element value
  55. if (element !== undefined) {
  56. this._ref.call(this._context, element);
  57. }
  58. }
  59. else {
  60. this._ref.value = element;
  61. }
  62. }
  63. get _lastElementForRef() {
  64. var _a;
  65. return typeof this._ref === 'function'
  66. ? lastElementForCallback.get(this._ref)
  67. : (_a = this._ref) === null || _a === void 0 ? void 0 : _a.value;
  68. }
  69. disconnected() {
  70. // Only clear the box if our element is still the one in it (i.e. another
  71. // directive instance hasn't rendered its element to it before us); that
  72. // only happens in the event of the directive being cleared (not via manual
  73. // disconnection)
  74. if (this._lastElementForRef === this._element) {
  75. this._updateRefValue(undefined);
  76. }
  77. }
  78. reconnected() {
  79. // If we were manually disconnected, we can safely put our element back in
  80. // the box, since no rendering could have occurred to change its state
  81. this._updateRefValue(this._element);
  82. }
  83. }
  84. /**
  85. * Sets the value of a Ref object or calls a ref callback with the element it's
  86. * bound to.
  87. *
  88. * A Ref object acts as a container for a reference to an element. A ref
  89. * callback is a function that takes an element as its only argument.
  90. *
  91. * The ref directive sets the value of the Ref object or calls the ref callback
  92. * during rendering, if the referenced element changed.
  93. *
  94. * Note: If a ref callback is rendered to a different element position or is
  95. * removed in a subsequent render, it will first be called with `undefined`,
  96. * followed by another call with the new element it was rendered to (if any).
  97. *
  98. * ```js
  99. * // Using Ref object
  100. * const inputRef = createRef();
  101. * render(html`<input ${ref(inputRef)}>`, container);
  102. * inputRef.value.focus();
  103. *
  104. * // Using callback
  105. * const callback = (inputElement) => inputElement.focus();
  106. * render(html`<input ${ref(callback)}>`, container);
  107. * ```
  108. */
  109. export const ref = directive(RefDirective);
  110. //# sourceMappingURL=ref.js.map