# ネストされたコンポーネント (Nested Components)

![.article-linkにネストされた.vote-boxコンポーネント](/files/-LzIM-_jxhlngMl87qIm)

```markup
<div class='article-link'>
  <div class='vote-box'>
    ...
  </div>
  <h3 class='title'>...</h3>
  <p class='meta'>...</p>
</div>
```

このようにコンポーネントをネストする必要が出てくる場合があります. ここでいくつかの方法を紹介します.&#x20;

## バリアント (Variants)

コンポーネントは別のコンポーネントでネストされた時に特定の方法で表現する必要がある場合があります. ネストされたコンポーネントを修正しないようにするにはそれを含むコンポーネントからアクセスします.

```css
.article-header {
  > .vote-box > .up { /* ✗ これは避ける */ }
}
```

代わりに, ネストされたコンポーネントにバリアントを加えてそれを含むコンポーネントから適用した方が良いです.

```markup
<div class='article-header'>
  <div class='vote-box -highlight'>
    ...
  </div>
  ...
</div>
```

```css
.vote-box {
  &.-highlight > .up { /* ... */ }
}
```

## ネストされたコンポーネントをシンプルにする

コンポーネントをネストしたとき、マークアップが美しくなくなる時があります.

```markup
<div class='search-form'>
  <input class='input' type='text'>
  <button class='search-button -red -large'></button>
</div>
```

こういった場合はCSSプリプロセッサの`@extend`を使ってシンプルにすることが出来ます.

```markup
<div class='search-form'>
  <input class='input' type='text'>
  <button class='submit'></button>
</div>
```

```css
.search-form {
  > .submit {
    @extend .search-button;
    @extend .search-button.-red;
    @extend .search-button.-large;
  }
}
```

リストのようにエレメントを繰り返したい場合はどうしたらよいでしょう。次のレイアウトで見てみます [Continue →](https://app.gitbook.com/@shufo/s/rscss/~/drafts/-LzJ1Y5OzltmnbwgEVZx/konpnentocomponents/reiautolayouts)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shufo.gitbook.io/rscss/components/nested-components.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
