--- title: styled description: "`styled`関数は、propsを使用してスタイリングを可能にするJSX要素を生成します。" --- ## 概要 `styled`は、[Style Props](https://yamada-ui.com/docs/styling/style-props.md)を有効にしたJSX要素のオブジェクトであり、カスタムコンポーネントが[Style Props](https://yamada-ui.com/docs/styling/style-props.md)を受け取るための関数としても使用できます。 ## 使い方 `styled.`の記法を使用して、[Style Props](https://yamada-ui.com/docs/styling/style-props.md)を備えたHTML要素を生成します。 例えば、[Style Props](https://yamada-ui.com/docs/styling/style-props.md)を備えたHTML要素の`button`を生成する場合は、``と記述します。 ```tsx ``` ### コンポーネントを作成する `styled`は、JSX要素の``とJSX要素を返す関数の`styled('div')`と2つの使い方ができます。 関数は、簡単なコンポーネントを生成するのに適しています。 ```tsx import { styled } from "@workspaces/ui" const Button = styled("button") const App = () => { return } ``` 引数にカスタムコンポーネントを渡し、[Style Props](https://yamada-ui.com/docs/styling/style-props.md)を有効にしたカスタムコンポーネントを生成することもできます。 ```tsx import { styled } from "@workspaces/ui" import { YourComponent } from "./your-component" const NewComponent = styled(YourComponent) const App = () => { return } ``` ### スタイリングする コンポーネントにデフォルトのスタイルを設定したり、`variant`や`size`などの条件に応じたスタイルを設定することができます。 #### ベーススタイル ベーススタイルは、コンポーネントのデフォルトのスタイルを設定します。 ```tsx const Button = styled("button", { base: { alignItems: "center", appearance: "none", cursor: "pointer", display: "inline-flex", fontWeight: "medium", justifyContent: "center", overflow: "hidden", position: "relative", rounded: "l2", transitionDuration: "moderate", transitionProperty: "common", userSelect: "none", verticalAlign: "middle", whiteSpace: "nowrap", _readOnly: { layerStyle: "readOnly" }, _disabled: { layerStyle: "disabled" }, }, }) ``` #### バリアントスタイル バリアントスタイルは、コンポーネントの`variant`に応じたスタイルを設定します。 ```tsx const Button = styled("button", { variants: { outline: { layerStyle: "outline", _hover: { layerStyle: "outline.hover" }, }, solid: { layerStyle: "solid", _hover: { layerStyle: "solid.hover" }, }, subtle: { layerStyle: "subtle", _hover: { layerStyle: "subtle.hover" }, }, }, }) ``` バリアントスタイルを適用する場合は、`variant`に値を設定します。 ```tsx