| 60 | * The controller's `value` is usable during the host's update cycle. |
| 61 | */ |
| 62 | export class ResizeController<T = unknown> implements ReactiveController { |
| 63 | private _host: ReactiveControllerHost; |
| 64 | private _targets = new Set<Element>(); |
| 65 | private _config?: ResizeObserverOptions; |
| 66 | private _observer!: ResizeObserver; |
| 67 | private _skipInitial = false; |
| 68 | /** |
| 69 | * Flag used to help manage calling the `callback` when observe is called |
| 70 | * in addition to when a mutation occurs. This is done to help setup initial |
| 71 | * state and is performed async by requesting a host update and calling |
| 72 | * `handleChanges` once by checking and then resetting this flag. |
| 73 | */ |
| 74 | private _unobservedUpdate = false; |
| 75 | /** |
| 76 | * The result of processing the observer's changes via the `callback` |
| 77 | * function. |
| 78 | */ |
| 79 | value?: T; |
| 80 | /** |
| 81 | * Function that returns a value processed from the observer's changes. |
| 82 | * The result is stored in the `value` property. |
| 83 | */ |
| 84 | callback?: ResizeValueCallback<T>; |
| 85 | constructor( |
| 86 | host: ReactiveControllerHost & Element, |
| 87 | {target, config, callback, skipInitial}: ResizeControllerConfig<T> |
| 88 | ) { |
| 89 | this._host = host; |
| 90 | // Target defaults to `host` unless explicitly `null`. |
| 91 | if (target !== null) { |
| 92 | this._targets.add(target ?? host); |
| 93 | } |
| 94 | this._config = config; |
| 95 | this._skipInitial = skipInitial ?? this._skipInitial; |
| 96 | this.callback = callback; |
| 97 | if (isServer) { |
| 98 | return; |
| 99 | } |
| 100 | // Check browser support. |
| 101 | if (!window.ResizeObserver) { |
| 102 | console.warn( |
| 103 | `ResizeController error: browser does not support ResizeObserver.` |
| 104 | ); |
| 105 | return; |
| 106 | } |
| 107 | this._observer = new ResizeObserver((entries: ResizeObserverEntry[]) => { |
| 108 | this.handleChanges(entries); |
| 109 | this._host.requestUpdate(); |
| 110 | }); |
| 111 | host.addController(this); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Process the observer's changes with the controller's `callback` |
| 116 | * function to produce a result stored in the `value` property. |
| 117 | */ |
| 118 | protected handleChanges(entries: ResizeObserverEntry[]) { |
| 119 | this.value = this.callback?.(entries, this._observer); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…