Breakpoints
Learn how to customize breakpoints.
Overview
Breakpoints are tokens used for responsive design, such as Responsive Design.
The breakpoints defined in the theme can be found in the source code.
export const breakpoints = defineTokens.breakpoints({
sm: "30em",
md: "48em",
lg: "61em",
xl: "80em",
"2xl": "90em",
})
Customize
- 1
Generate a Theme
Use the CLI to generate a theme.
Before running the following commands, you need to install@yamada-ui/cliand execute theinitcommand. For more details, please see the CLI.pnpm yamada-cli themenpm yamada-cli themeyarn yamada-cli themebun yamada-cli theme - 2
Change the Token
Change the
tokens/breakpoints.tsin the generated theme.theme/tokens/breakpoints.ts
import { defineTokens } from "@yamada-ui/react" export const breakpoints = defineTokens.breakpoints({ sm: "27em", md: "48em", lg: "61em", xl: "80em", "2xl": "90em", }) - 3
Update the Provider
Set the generated theme to the
UIProvider.import { UIProvider } from "@workspaces/ui" import { theme } from "@workspace/theme" const App = () => { return ( <UIProvider theme={theme}> <YourApplication /> </UIProvider> ) }
Media Queries
By default, the responsive design uses the @media(max-width) media query.
The breakpoint when using @media(max-width) is "base".
The breakpoint when using @media(min-width) is "base".
const config = defineConfig({ breakpoint: { direction: "up" } })
const Component: FC<{ query: string }> = ({ query }) => {
const breakpoint = useBreakpoint()
return (
<Text>
The breakpoint when using{" "}
<Text as="span" color="info">
{query}
</Text>{" "}
is "{breakpoint}".
</Text>
)
}
return (
<VStack gap="md">
<Component query="@media(max-width)" />
<UIProvider config={config}>
<Component query="@media(min-width)" />
</UIProvider>
</VStack>
)
If you want to use the @media(min-width) media query, set the breakpoint.direction to "up" in the config.
- 1
Change the Config
Change the
config.tsin the generated theme.theme/config.ts
import { defineConfig } from "@yamada-ui/react" export const config = defineConfig({ css: { varPrefix: "ui" }, breakpoint: { direction: "up", identifier: "@media screen" }, defaultColorMode: "light", defaultThemeScheme: "base", notice: { duration: 5000 }, theme: { responsive: true }, }) - 2
Update the Provider
Set the generated theme to
UIProvider.import { UIProvider } from "@workspaces/ui" import { theme, config } from "@workspace/theme" const App = () => { return ( <UIProvider theme={theme} config={config}> <YourApplication /> </UIProvider> ) }
Container Queries
The responsive design uses the media query. If you want to use the container query, set the breakpoint.identifier to "@container" in the config.
The breakpoint when using @media(max-width) is "base".
The breakpoint when using @container is "base".
const containerRef = useRef<HTMLDivElement>(null)
const config = defineConfig({
breakpoint: { containerRef, identifier: "@container" },
})
const Component: FC<{ query: string }> = ({ query }) => {
const breakpoint = useBreakpoint()
return (
<Text>
The breakpoint when using{" "}
<Text as="span" color="info">
{query}
</Text>{" "}
is "{breakpoint}".
</Text>
)
}
return (
<Box
ref={containerRef}
bg="bg.panel"
containerType="inline-size"
overflow="auto"
maxW="full"
p="md"
resize="inline"
w="full"
>
<Component query="@media(max-width)" />
<UIProvider config={config}>
<Component query="@container" />
</UIProvider>
</Box>
)