# 6 - Parent

Introduction

The Components tutorial shows how Text and ColoredRectangle instances can be sized and positioned according to the Button that they are a part of. Instances can use other instances as their parents. Gum does not place a limit to the depth of the parent/child hierarchy, enabling flexible and responsive layouts through.

Parent/child relationships are useful for

* Automatically adjusting margins and backgrounds
* Word-wrapping text relative to a parent
* Stacking objects on top of each other or side by side
* Placing objects next to other objects which are dynamically sized
* Creating tables and other complicated layout objects

## Creating a Component

For this example we'll create a Component for displaying distance. This component has two Text instances:

* ValueText - the Text instance responsible for displaying the value for the distance. For example "100".
* UnitsDisplay - the Text instance responsible for displaying the units for the distance. For example "km" for kilometers.

The two Text instances will have different colors so that ValueText stands out.

{% hint style="info" %}
A single Text instance can support inline styling. We use two text instances here to show how to work with the Parent variable, but for more info see the [Text property](https://docs.flatredball.com/gum/gum-elements/text/text#using-bbcode-for-inline-styling) page.
{% endhint %}

To create our component:

1. Create a new component named **MeasurementDisplay**
2. Drop two Text objects in the newly-created component
3. Name the first **ValueText**
4. Name the second **UnitsDisplay**

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-c26ae2ab1128e1699b0cc0eba1e7e3e94532bd24%2F15_05%2038%2009.png?alt=media" alt=""><figcaption><p>MeasurementDisplay component</p></figcaption></figure>

## Positioning according to a parent

Next we'll make the **UnitsDisplay** use the **ValueText** as its parent:

1. Select the **UnitsDisplay** Text instance
2. Change its `Parent` to `ValueText`
3. Change its `X Units` to `Pixels From Right`. This makes the text object positioned according to its parent's right edge
4. Change its `X` to `10` . This means the **UnitsDisplay** text is 10 units offset from the right edge of its parent **ValueText**
5. Verify its `Y` is `0`

![UnitsDisplay positioned relative to the right-side of ValueText](https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-5c336d05c76a92fc76fff90bfa9546622918004e%2F15_05%2042%2039.png?alt=media)

The ValueText actual width should be based on its contents, so we'll do the following:

1. Select `ValueText`
2. Change `Width Units` to `Relative to Children`
3. Change `Width` to `0`

By setting these values, ValueText is now sized according to its children, which in this case are its letters.

![Width and Width Units set to size ValueText according to its contained Text variable](https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-056865e2c92e0f2061f3dde8370ed5c1307941e8%2F15_05%2048%2040.png?alt=media)

You may have noticed that UnitsDisplay is also a child of the ValueText. However, since UnitsDisplay is explicitly positioned outside of the bounds of its parent ValueText, then ValueText ignores this child when calculating its own Width. For a detailed discussion of Width Units and whether children are ignored, see the [Width Units](https://docs.flatredball.com/gum/gum-elements/general-properties/width-units#ignored-width-values) page.

## Adjusting Colors

Now that we have adjusted the position, size, and parent values on our Text instances, let's modify the color of the UnitsDisplay:

1. Select the **UnitsDisplay**
2. Change `Red` to `200`
3. Change `Green` to `150`
4. Change `Blue` to `0`

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-bccc5b9b9e14645e832c0ba9d2a84e132760d4a9%2F15_05%2051%2036.png?alt=media" alt=""><figcaption><p>Text with changed color values</p></figcaption></figure>

## Changing ValueText

Now that we have set up a parent/child relationship between ValueText and UnitsDisplay, UnitsDisplay automatically adjusts its position in response to changes in ValueText. Any change on ValueText resulting in the right-side of the parent changing automatically adjusts the position of UnitsDisplay.

For example, if we change the Text property on ValueText, it grows or shrinks in response.

![Changing the Text property results in ValueText changing its effective width](https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-62d58f3c68fa23b56648278602b418a13a3c1dd2%2F15_05%2055%2004.gif?alt=media)

## Width and Effective Width

As mentioned above, changing the Text property causes ValueText to grow or shrink. However, regardless of its size, the `Width` property is still set to 0.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-b5224f3863842f0606b1d7769744dce97478703d%2Fimage.png?alt=media" alt=""><figcaption><p>Width is 0 despite the size being larger than 0</p></figcaption></figure>

The `Width` variable is used in combination with its `Width Units` to calculate an *effective width.* In this case, the effective width is determined by the Text property on ValueText. It's important to note that all Gum objects have effective values for x, y, width, and height, all of which are determined by their respective *units* values.

**Children always depend on their parents' effective values rather than their explicitly set values**. Gum helps us visualize the effective values when we mouse over one of the *resize handles* on the selected object. For example, the following image shows the `Width`, `Width Units`, and effective width values of our **ValueText**.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-5d3f55311aa96162b9c579154914f022c63a306f%2F15_06%2002%2039.png?alt=media" alt=""><figcaption><p><code>Width</code> is 0, <code>Width Units</code> is <code>Relative To Children</code>, effective width is 65</p></figcaption></figure>
