# ui/autolayout/layoutstrength (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/autolayout/layoutstrength/

Layout constraint strength.

## LayoutStrength

Layout constraint strength.
- Cassowary / Kiwi standard strength system.
- Strength decides which side to satisfy first when constraints conflict.
- required must be satisfied, and an exception is thrown if the solver cannot solve it.
- strong / medium / weak are satisfied as much as possible, but yield in order of strength on conflict.
- Custom strengths can be composed with createStrength(a, b, c, weight).

Example: A strong constraint beats a weak constraint

```js
import { LayoutSolver, LayoutVariable, LayoutExpression, LayoutStrength } from "./build/vanilla.js";

const solver = new LayoutSolver();
const width = new LayoutVariable("width");
const widthExpression = LayoutExpression.fromVariable(width);
const preferredConstraint = widthExpression.equalTo(200);
const strongConstraint = preferredConstraint.withStrength(LayoutStrength.strong);
const fallbackConstraint = widthExpression.equalTo(100);
const weakConstraint = fallbackConstraint.withStrength(LayoutStrength.weak);
solver.addConstraint(strongConstraint);
solver.addConstraint(weakConstraint);
solver.updateVariables();
const widthValue = width.getValue();
console.log(widthValue);
```

- `static required = 1001001000` (static, since 0.2.1): Standard strength constants.
  - returns number
- `static strong = 1000000` (static, since 0.2.1): Strong strength. (1000000, same as createStrength(1, 0, 0))
  - returns number
- `static medium = 1000` (static, since 0.2.1): Medium strength. (1000, same as createStrength(0, 1, 0))
  - returns number
- `static weak = 1` (static, since 0.2.1): Weak strength. (1, same as createStrength(0, 0, 1))
  - returns number
- `static createStrength(a, b, c, weight)` (static, since 0.2.1): Composes a custom strength.
  - Composed from three strength components (a, b, c) and a weight (weight).
  - Each component is multiplied by weight and then clamped to the 0 ~ 1000 range.
  - The composed result is in the 0 ~ required (1001001000) range.
  - `a` number: Strong place component. (multiplied by weight, clamped to 0 ~ 1000, then times 1000000)
  - `b` number: Medium place component. (multiplied by weight, clamped to 0 ~ 1000, then times 1000)
  - `c` number: Weak place component. (multiplied by weight and clamped to 0 ~ 1000)
  - `weight` number: Weight multiplied into the three components. (1 if omitted or null)
  - returns number: Combined strength. (0 ~ 1001001000)

  Example: Make a strength between medium and strong

  ```js
  import { LayoutStrength } from "./build/vanilla.js";
  
  const betweenMediumAndStrong = LayoutStrength.createStrength(0, 500, 0);
  const halfStrong = LayoutStrength.createStrength(1, 0, 0, 0.5);
  const medium = LayoutStrength.medium;
  const strong = LayoutStrength.strong;
  console.log(betweenMediumAndStrong, halfStrong, medium, strong);
  ```

- `static clipStrength(strength)` (static, since 0.2.1): Clamps a strength. (0 ~ required)
  - `strength` number: Strength to clamp.
  - returns number: Strength clamped to 0 ~ required.
- `static fromPriority(priority)` (static, since 0.2.1): Converts UIKit's UILayoutPriority (1 ~ 1000) to LayoutStrength.
  - priority >= 1000 (required) maps to LayoutStrength.required.
  - 1 ~ 999 maps to the weight of the medium tier (priority 750 → 750000).
  - 0 or below maps to 0.
  - `priority` number: UIKit style priority. (1 ~ 1000, a LayoutPriority value)
  - returns number: Strength. (required at 1000 or above, 0 at 0 or below, priority * 1000 in between)

  Example: Convert priorities to strengths

  ```js
  import { LayoutStrength, LayoutPriority } from "./build/vanilla.js";
  
  const requiredStrength = LayoutStrength.fromPriority(LayoutPriority.required);
  const highStrength = LayoutStrength.fromPriority(LayoutPriority.defaultHigh);
  const zeroStrength = LayoutStrength.fromPriority(0);
  console.log(requiredStrength === LayoutStrength.required, highStrength, zeroStrength);
  ```

