mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 04:09:12 +00:00
* initial attempt at not auto defining * add files with - * continued work on removing auto-define * fix component definitions * update with new tag stuff * fix lots of things * fix improper scoped elements * working through side effects * continued react wrapper work * update changelog * formatting * fixes * update changelog * lint / formatting * fix version injection * fix version injection, work on test * fix version injection, work on test * fix merge conflicts * fix jsdoc null issue * fix templates * use exports * working on tests * working on registration mocking * fix customElements test * linting * fix some test stuff * clean up test * clean up comment * rename scopedElements to dependencies * linting / formatting * linting / formatting * mark all packages external and still bundle * set bundle false * set bundle true * dont minify * fix merge conflicts * use built shoelace-element * fix lint errors * prettier * appease eslint * appease eslint gods * appease eslint gods * appease eslint gods * appease eslint gods * add shoelace-autoloader * move it all into 1 function * add exportmaps note * prettier * add jsdelivr entrypoint * read as utf8 * update docs with .component.js importS * prettier
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { html } from 'lit';
|
|
import { property } from 'lit/decorators.js';
|
|
import { watch } from '../../internal/watch.js';
|
|
import ShoelaceElement from '../../internal/shoelace-element.js';
|
|
import styles from './resize-observer.styles.js';
|
|
import type { CSSResultGroup } from 'lit';
|
|
|
|
/**
|
|
* @summary The Resize Observer component offers a thin, declarative interface to the [`ResizeObserver API`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
|
|
* @documentation https://shoelace.style/components/resize-observer
|
|
* @status stable
|
|
* @since 2.0
|
|
*
|
|
* @slot - One or more elements to watch for resizing.
|
|
*
|
|
* @event {{ entries: ResizeObserverEntry[] }} sl-resize - Emitted when the element is resized.
|
|
*/
|
|
export default class SlResizeObserver extends ShoelaceElement {
|
|
static styles: CSSResultGroup = styles;
|
|
|
|
private resizeObserver: ResizeObserver;
|
|
private observedElements: HTMLElement[] = [];
|
|
|
|
/** Disables the observer. */
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
|
|
|
connectedCallback() {
|
|
super.connectedCallback();
|
|
this.resizeObserver = new ResizeObserver((entries: ResizeObserverEntry[]) => {
|
|
this.emit('sl-resize', { detail: { entries } });
|
|
});
|
|
|
|
if (!this.disabled) {
|
|
this.startObserver();
|
|
}
|
|
}
|
|
|
|
disconnectedCallback() {
|
|
super.disconnectedCallback();
|
|
this.stopObserver();
|
|
}
|
|
|
|
private handleSlotChange() {
|
|
if (!this.disabled) {
|
|
this.startObserver();
|
|
}
|
|
}
|
|
|
|
private startObserver() {
|
|
const slot = this.shadowRoot!.querySelector('slot');
|
|
|
|
if (slot !== null) {
|
|
const elements = slot.assignedElements({ flatten: true }) as HTMLElement[];
|
|
|
|
// Unwatch previous elements
|
|
this.observedElements.forEach(el => this.resizeObserver.unobserve(el));
|
|
this.observedElements = [];
|
|
|
|
// Watch new elements
|
|
elements.forEach(el => {
|
|
this.resizeObserver.observe(el);
|
|
this.observedElements.push(el);
|
|
});
|
|
}
|
|
}
|
|
|
|
private stopObserver() {
|
|
this.resizeObserver.disconnect();
|
|
}
|
|
|
|
@watch('disabled', { waitUntilFirstUpdate: true })
|
|
handleDisabledChange() {
|
|
if (this.disabled) {
|
|
this.stopObserver();
|
|
} else {
|
|
this.startObserver();
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return html` <slot @slotchange=${this.handleSlotChange}></slot> `;
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
'sl-resize-observer': SlResizeObserver;
|
|
}
|
|
}
|