Skip to content

Commit

Permalink
fix(OnyxNavBar): support SSR (#1321)
Browse files Browse the repository at this point in the history
Relates to #1073 

Fix error when using the nav bar with Nuxt / server side rendering
  • Loading branch information
larsrickert authored Jun 14, 2024
1 parent d928d62 commit f1aad40
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .changeset/wise-owls-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"sit-onyx": patch
---

fix(OnyxNavBar): support SSR

prevent "ResizeObserver not defined" error
2 changes: 1 addition & 1 deletion packages/sit-onyx/src/components/OnyxNavBar/OnyxNavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const isMobile = computed(() => {
typeof props.mobileBreakpoint === "number"
? props.mobileBreakpoint
: ONYX_BREAKPOINTS[props.mobileBreakpoint];
return width.value <= mobileWidth;
return width.value !== 0 && width.value <= mobileWidth;
});
const { t } = injectI18n();
Expand Down
8 changes: 8 additions & 0 deletions packages/sit-onyx/src/composables/useResizeObserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { describe, expect, test, vi } from "vitest";
import { ref } from "vue";
import { useResizeObserver } from "./useResizeObserver";

vi.mock("vue", async (importOriginal) => {
return {
...(await importOriginal()),
// this will only affect "foo" outside of the original module
onBeforeMount: (callback) => callback(),
} satisfies typeof import("vue");
});

describe("useResizeObserver", () => {
test("should get component size", () => {
let callback: ((entries: Partial<ResizeObserverEntry>[]) => void) | undefined;
Expand Down
29 changes: 16 additions & 13 deletions packages/sit-onyx/src/composables/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onBeforeUnmount, ref, watch, type Ref } from "vue";
import { onBeforeMount, onBeforeUnmount, ref, watch, type Ref } from "vue";

export type UseResizeObserverOptions = {
/**
Expand Down Expand Up @@ -29,18 +29,21 @@ export const useResizeObserver = (
height.value = boxSize.reduce((acc, { blockSize }) => acc + blockSize, 0);
};

const observer = new ResizeObserver(callback);

watch(
target,
(newTarget, oldTarget) => {
if (oldTarget) observer?.unobserve(oldTarget);
if (newTarget) observer?.observe(newTarget, { box });
},
{ immediate: true },
);

onBeforeUnmount(() => observer.disconnect());
// ensure ResizeObserver is only called before/on mount to support server side rendering
onBeforeMount(() => {
const observer = new ResizeObserver(callback);

watch(
target,
(newTarget, oldTarget) => {
if (oldTarget) observer?.unobserve(oldTarget);
if (newTarget) observer?.observe(newTarget, { box });
},
{ immediate: true },
);

onBeforeUnmount(() => observer.disconnect());
});

return { width, height };
};

0 comments on commit f1aad40

Please sign in to comment.