
Chakra UI の導入方法については、以下の記事にまとめています。
参考:Next.js (JavaScript/TypeScript) に Chakra UI を導入する方法 | fwywd(フュード)
基本的に Next.js の PJ をベースとして、解説しています。もし、掲載しているコードが React で動作しない場合は、ディレクトリ構造やコードを適宜、確認してください。
div タグ → Box コンポーネントh1 ~ h6 タグ → Heading コンポーネントp タグ → Text コンポーネントbutton タグ → Button コンポーネント// pages/index.tsx import { Heading, Box, Button, Text } from '@chakra-ui/react'; export default function Index(): JSX.Element { return ( <Box> <Text>テキスト</Text> <Heading>見出し</Heading> <Button>ボタン</Button> </Box> ); }


// pages/index.tsx import { Heading, Box, Button, Text } from '@chakra-ui/react'; export default function Index(): JSX.Element { return ( <Box width="200px" background="gray"> <Text color="blue" fontWeight="bold"> テキスト </Text> <Heading>見出し</Heading> <Button>ボタン</Button> </Box> ); }


2xl や 3xl など、デフォルトの変数が用意されていますが、10px など適切な値を代入することも可能です。
width → wheight → hbackground → bg
// pages/index.tsx import { Heading, Box, Button, Text } from '@chakra-ui/react'; export default function Index(): JSX.Element { return ( // 追記:横幅をレスポンシブ対応 <Box w={{ base: '150px', sm: '300px', md: '500px', lg: '700px', xl: '1000px' }} bg="gray"> <Text color="blue" fontWeight="bold"> テキスト </Text> <Heading>見出し</Heading> <Button>ボタン</Button> </Box> ); }

base:From 0em より大きい場合sm:From 30em(デフォルト値)より大きい場合md:From 48em(デフォルト値)より大きい場合lg:From 62em(デフォルト値)より大きい場合xl:From 80em(デフォルト値)より大きい場合// 配列 ["150px", "300px", "500px", "700px","1000px"] // オブジェクト { base: '150px', sm: '300px', md: '500px', lg: '700px', xl: '1000px' }
_ を先頭に付けて、表現します。以下、例になります。_before / _after_hover_focusimport { Heading, Box, Button, Text } from '@chakra-ui/react'; export default function Index(): JSX.Element { return ( <Box> <Text color="blue" fontWeight="bold" _before={{ content: '"before の要素"', color: 'white' }}> テキスト </Text> <Heading>見出し</Heading> <Button>ボタン</Button> </Box> ); }

as という props を使用します。Heading コンポーネントの html をタグを h1 ~ h6 に割り振る方法は以下の通りです。<Heading as="h1">h1</Heading> <Heading as="h2">h2</Heading> <Heading as="h3">h3</Heading> <Heading as="h4">h4</Heading> <Heading as="h5">h5</Heading> <Heading as="h6">h6</Heading>
sx という props を使用します。import { Heading, Box, Button, Text } from '@chakra-ui/react'; export default function Index(): JSX.Element { return ( <Box sx={{ // セレクタ '.vertical-mode': { // プロパティと値 writingMode: 'vertical-rl', }, }} > {/* sx で定義したクラス名 ".vertical-mode" を className に代入 */} <Heading className="vertical-mode" color="green" fontSize="7xl"> 縦文字 </Heading> </Box> ); }

sx の props を使用することで、独自 CSS も簡単に適用できます!Box コンポーネントを再利用したいとします。import { Box, Text } from '@chakra-ui/react'; export default function Index(): JSX.Element { return ( <Box> <Text>グラデーション1</Text> <Box bgGradient="linear(to-r, red,blue)" w="300px" h="200px" /> <Text>グラデーション2</Text> <Box bgGradient="linear(to-r, red,blue)" w="500px" h="100px" /> <Text>グラデーション3</Text> <Box bgGradient="linear(to-r, red,blue)" w="200px" h="100px" /> </Box> ); }

Box コンポーネントをコンポーネント化すると、以下のようになります。// components/GradientBox.tsx import { Box, BoxProps } from '@chakra-ui/react'; import { ReactNode } from 'react'; // BoxProps を継承 interface Props extends BoxProps { children?: ReactNode; } export default function GradientBox({ children, ...rest }: Props): JSX.Element { return ( <Box bgGradient="linear(to-r, red,blue)" {...rest}> {children} </Box> ); }
GradientBox コンポーネントで共通化すると、以下のようになります。import { Box, Text } from '@chakra-ui/react'; import GradientBox from 'components/GradientBox'; export default function Index(): JSX.Element { return ( <Box> <Text>グラデーション1</Text> <GradientBox w="300px" h="200px" /> <Text>グラデーション2</Text> <GradientBox w="500px" h="100px" /> <Text>グラデーション3</Text> <GradientBox w="200px" h="100px" /> </Box> ); }
bgGradient に別のグラデーション値を代入すると、以下のように背景色が上書きされます。import { Box, Text } from '@chakra-ui/react'; import GradientBox from 'components/GradientBox'; export default function Index(): JSX.Element { return ( <Box> <Text>グラデーション1</Text> {/* 別のグラデーション */} <GradientBox bgGradient="linear(to-r, green,blue)" w="300px" h="200px" /> <Text>グラデーション2</Text> <GradientBox w="500px" h="100px" /> <Text>グラデーション3</Text> <GradientBox w="200px" h="100px" /> </Box> ); }

bgGradient の値を固定し、props から除外したい場合は、継承する BoxProps から bgGradient を取り除きましょう。コードは以下のようになります。import { Box, BoxProps } from '@chakra-ui/react'; import { ReactNode } from 'react'; // Omit で bgGradient だけ除外 interface Props extends Omit<BoxProps, 'bgGradient'> { children?: ReactNode; } export default function GradientBox({ children, ...rest }: Props): JSX.Element { return ( <Box bgGradient="linear(to-r, red,blue)" {...rest}> {children} </Box> ); }
width(ショートハンド w)と height(ショートハンド h)だけ、継承することも可能です。import { Box, BoxProps } from '@chakra-ui/react'; import { ReactNode } from 'react'; // Pick で 横幅と縦幅のみ継承 interface Props extends Pick<BoxProps, 'width' | 'height' | 'w' | 'h'> { children?: ReactNode; } export default function GradientBox({ children, ...rest }: Props): JSX.Element { return ( <Box bgGradient="linear(to-r, red,blue)" {...rest}> {children} </Box> ); }
div)Spacer コンポーネントも使うことで、space-between と同様の効果が得られる新着記事
関連記事
著者