-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components/dimension): provide signal to observe element size
```ts import {fromDimension} from '@scion/components/dimension'; const element: HTMLElement = ...; const dimension = fromDimension(element); ```
- Loading branch information
1 parent
31c5dd5
commit a160a1e
Showing
8 changed files
with
314 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
projects/scion/components/dimension/src/dimension.signal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2018-2024 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
import {assertInInjectionContext, assertNotInReactiveContext, DestroyRef, ElementRef, inject, signal, Signal} from '@angular/core'; | ||
import {coerceElement} from '@angular/cdk/coercion'; | ||
import {SciDimension} from './dimension'; | ||
|
||
/** | ||
* Creates a signal to observe the size of an element. | ||
* | ||
* The signal subscribes to the native {@link ResizeObserver} to monitor element size changes. | ||
* The subscription is automatically disposed of when the current context is destroyed. | ||
* | ||
* The function: | ||
* - must be called within an injection context or with a `DestroyRef` passed for automatic unsubscription. | ||
* - must NOT be called within a reactive context to avoid repeated subscriptions to the native {@link ResizeObserver}. | ||
*/ | ||
export function fromDimension(elementRef: HTMLElement | ElementRef<HTMLElement>, options?: {destroyRef?: DestroyRef}): Signal<SciDimension> { | ||
assertNotInReactiveContext(fromDimension, 'Invoking `fromDimension` causes new subscriptions every time. Move `fromDimension` outside of the reactive context and read the signal value where needed.'); | ||
if (!options?.destroyRef) { | ||
assertInInjectionContext(fromDimension); | ||
} | ||
|
||
const element = coerceElement(elementRef); | ||
const dimension = signal(getDimension(element)); | ||
// Update signal in animation frame to not block the resize callback (ResizeObserver loop completed with undelivered notifications). | ||
const resizeObserver = new ResizeObserver(() => requestAnimationFrame(() => dimension.set(getDimension(element)))); | ||
resizeObserver.observe(element); | ||
|
||
const destroyRef = options?.destroyRef ?? inject(DestroyRef); | ||
destroyRef.onDestroy(() => resizeObserver.disconnect()); | ||
return dimension; | ||
} | ||
|
||
function getDimension(element: HTMLElement): SciDimension { | ||
return { | ||
clientWidth: element.clientWidth, | ||
offsetWidth: element.offsetWidth, | ||
clientHeight: element.clientHeight, | ||
offsetHeight: element.offsetHeight, | ||
element: element, | ||
}; | ||
} |
Oops, something went wrong.