useBreakpointValue
useBreakpointValueは、提供されたオブジェクトから現在のブレイクポイントの値を返すカスタムフックです。このフックは、ウィンドウのサイズの変更を監視し、適切な値を返します。
The current breakpoint is "base"
const breakpoint = useBreakpoint()
const bg = useBreakpointValue({
base: "red.500",
sm: "purple.500",
md: "yellow.500",
lg: "green.500",
xl: "blue.500",
"2xl": "pink.500",
})
return (
<Box
bg={bg}
color="white"
p="md"
transitionDuration="moderate"
transitionProperty="all"
>
The current breakpoint is "{breakpoint}"
</Box>
)
こちらのコードを使用する場合は、
"use client"をファイルの上部に追加する必要があります。使い方
import { useBreakpointValue } from "@yamada-ui/react"
import { useBreakpointValue } from "@/components/ui"
import { useBreakpointValue } from "@workspaces/ui"
const bg = useBreakpointValue({
base: "red.500",
sm: "purple.500",
md: "yellow.500",
lg: "green.500",
xl: "blue.500",
"2xl": "pink.500",
})