Skip to content

Commit

Permalink
fix: use sorted breakpoints in useBreakpointValue (#5625)
Browse files Browse the repository at this point in the history
* fix: useBreakpointValue with custom breakpoints
Closes #5555

* fix: use ChakraProvider in Skeleton test
  • Loading branch information
TimKolberger authored Feb 20, 2022
1 parent 085bbcc commit 99c92df
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/wicked-turkeys-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@chakra-ui/media-query": patch
---

Fixed an issue where the `useBreakpointValue` hook did not work as expected with
custom breakpoints
4 changes: 2 additions & 2 deletions packages/media-query/src/use-breakpoint-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export function useBreakpointValue<T = any>(
if (!breakpoint) return undefined

/**
* Get the non-number breakpoint keys from the provided breakpoints
* Get the sorted breakpoint keys from the provided breakpoints
*/
const breakpoints = Object.keys(theme.breakpoints)
const breakpoints = Array.from(theme.__breakpoints?.keys || [])

const obj = isArray(values)
? fromEntries<Partial<Record<string, T>>>(
Expand Down
25 changes: 25 additions & 0 deletions packages/media-query/stories/media-query.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EnvironmentProvider } from "@chakra-ui/react-env"
import { chakra } from "@chakra-ui/system"
import * as React from "react"
import Frame from "react-frame-component"
import { ChakraProvider, extendTheme } from "@chakra-ui/react"
import { Hide, Show, useBreakpoint, useBreakpointValue } from "../src"

export default {
Expand Down Expand Up @@ -75,3 +76,27 @@ const BreakpointValue = () => {
})
return <p>Breakpoint: {breakpoint}</p>
}

const NestedBreakpointValueWithCustomBreakpoint = () => {
const bp = useBreakpoint()
const isMobile = useBreakpointValue({
base: true,
md: false,
})

return (
<>
<p>Breakpoint: {bp}</p>
<p>isMobile: {String(isMobile)}</p>
<i>Expect isMobile to be true util "md" breakpoint is hit</i>
</>
)
}

export const BreakpointValueWithCustomBreakpoint = () => (
<ChakraProvider
theme={extendTheme({ breakpoints: { preSm: "28em", postSm: "36em" } })}
>
<NestedBreakpointValueWithCustomBreakpoint />
</ChakraProvider>
)
12 changes: 7 additions & 5 deletions packages/skeleton/tests/skeleton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from "react"
import { ThemeProvider } from "@chakra-ui/system"
import { render } from "@chakra-ui/test-utils"
import MatchMediaMock from "jest-matchmedia-mock"
import { extendTheme, ChakraProvider } from "@chakra-ui/react"
import { Skeleton, SkeletonText } from "../src"
import { queries, theme } from "./test-data"
import { queries, theme as testTheme } from "./test-data"

const theme = extendTheme(testTheme)

let matchMedia: any

Expand Down Expand Up @@ -37,9 +39,9 @@ test("Change in parent state does not cause animation if already loaded", () =>
setState(true)
}, [])
return (
<ThemeProvider theme={theme}>
<ChakraProvider theme={theme}>
<Skeleton isLoaded />
</ThemeProvider>
</ChakraProvider>
)
}

Expand All @@ -53,5 +55,5 @@ test("Change in parent state does not cause animation if already loaded", () =>

function renderWithQuery(query: string, ui: React.ReactElement) {
matchMedia.useMediaQuery(query)
return render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>)
return render(<ChakraProvider theme={theme}>{ui}</ChakraProvider>)
}
1 change: 1 addition & 0 deletions packages/skeleton/tests/test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const breakpoints = createBreakpoints({
md: "200px",
lg: "300px",
xl: "400px",
"2xl": "600px",
customBreakpoint: "500px",
})

Expand Down

1 comment on commit 99c92df

@vercel
Copy link

@vercel vercel bot commented on 99c92df Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.