Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move breakpointsComputed set to better place #1064

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/js/media-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ const MEDIA_UI_ATTRIBUTE_NAMES = Object.values(MediaUIAttributes);
const defaultBreakpoints = 'sm:384 md:576 lg:768 xl:960';

function resizeCallback(entry: ResizeObserverEntry) {
setBreakpoints(entry.target as HTMLElement, entry.contentRect.width);
setBreakpoints(entry.target as MediaContainer, entry.contentRect.width);
}

function setBreakpoints(container: HTMLElement, width: number) {
function setBreakpoints(container: MediaContainer, width: number) {
if (!container.isConnected) return;

const breakpoints =
Expand Down Expand Up @@ -259,6 +259,17 @@ function setBreakpoints(container: HTMLElement, width: number) {

container.dispatchEvent(evt);
}

if (!container.breakpointsComputed) {
container.breakpointsComputed = true;

container.dispatchEvent(
new CustomEvent(MediaStateChangeEvents.BREAKPOINTS_COMPUTED, {
bubbles: true,
composed: true,
})
);
}
}

function createBreakpointMap(breakpoints: string) {
Expand Down Expand Up @@ -524,16 +535,6 @@ class MediaContainer extends globalThis.HTMLElement {

// Once we've completed, reset the pending cb flag to false
this.#isResizePending = false;

if (!this.breakpointsComputed) {
this.breakpointsComputed = true;
this.dispatchEvent(
new CustomEvent(MediaStateChangeEvents.BREAKPOINTS_COMPUTED, {
bubbles: true,
composed: true,
})
);
}
}, 0);

this.#isResizePending = true;
Expand Down