Skip to content

Commit

Permalink
fix: type error when breakpoint is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
SofiaMargariti committed Sep 5, 2024
1 parent dfa83dd commit 64d0aa8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/utils/breakpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ describe('getValueForBreakpoint', () => {
expect(value).toEqual(desktopValue);
});
});

describe('when rendering a view without a matching breakpoint', () => {
it('falls back to the desktop-specific value', () => {
const value = getValueForBreakpoint(
valuesByBreakpointWithoutTabletAndMobile,
breakpoints,
3,
variableName,
);
expect(value).toEqual(desktopValue);
});
});
});

describe('getActiveBreakpointIndex', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/utils/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,18 @@ export const getValueForBreakpoint = (
if (valuesByBreakpoint instanceof Object) {
// Assume that the values are sorted by media query to apply the cascading CSS logic
for (let index = activeBreakpointIndex; index >= 0; index--) {
const breakpointId = breakpoints[index].id;
const breakpointId = breakpoints[index]?.id;
if (valuesByBreakpoint[breakpointId]) {
// If the value is defined, we use it and stop the breakpoints cascade
return eventuallyResolveDesignTokens(valuesByBreakpoint[breakpointId]);
}
}
// If no breakpoint matched, we search and apply the fallback breakpoint
const fallbackBreakpointIndex = getFallbackBreakpointIndex(breakpoints);
const fallbackBreakpointId = breakpoints[fallbackBreakpointIndex].id;
return eventuallyResolveDesignTokens(valuesByBreakpoint[fallbackBreakpointId]);
const fallbackBreakpointId = breakpoints[fallbackBreakpointIndex]?.id;
if (valuesByBreakpoint[fallbackBreakpointId]) {
return eventuallyResolveDesignTokens(valuesByBreakpoint[fallbackBreakpointId]);
}
} else {
// Old design properties did not support breakpoints, keep for backward compatibility
return valuesByBreakpoint;
Expand Down

0 comments on commit 64d0aa8

Please sign in to comment.