Class ClampedContent
Caps tall content at a maximum height and reveals the rest behind a Show more / Show less toggle.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class ClampedContent : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder
Remarks
Add any content to a ClampedContent and it measures it on every layout pass. While the content is shorter than maxHeight the element is completely transparent — no toggle, no fade, no clipping. Once it overflows, the body is clipped, a fade sits over the cut edge and the toggle appears.
This clamps by height, not by line count. That mirrors the registry component, which uses a
max-h-* cap rather than -webkit-line-clamp; UI Toolkit has no line-clamp either, so the two
implementations agree. To cap at roughly N lines, set maxHeight to N times the
content's line height.
The cap lives on the clipping viewport and the measurement reads the container inside it,
which keeps its natural height whether or not it is clipped. That is why measurement can run in
both states here, where the web component has to skip it while expanded: with the cap off, a DOM
measurement reads "not overflowing" and the toggle vanishes, stranding the user expanded.
The registry's contentKey prop exists for the same reason and is not needed either, since UI
Toolkit re-runs layout on any child change. Call Remeasure() only after replacing
the content outside a layout pass.
The fade is a vertical gradient from transparent to the colour set by the
--clamped-content-fade-color custom USS property on the fade element (the default surface
colour by default). USS has no linear-gradient, so it is drawn as a vertex-coloured mesh.
Override the property when the host surface is not the default background, or set
fadeVisible to false.
Examples
Clamping a long description in UXML.
<appui:ClampedContent max-height="120">
<appui:Text text="A very long description that runs well past the cap..." />
</appui:ClampedContent>
Clamping in C# with custom labels.
var clamped = new ClampedContent
{
maxHeight = 200f,
moreLabel = "Read more",
lessLabel = "Collapse",
};
clamped.Add(new Text { text = logOutput });
clamped.expandedChanged += expanded => Debug.Log($"expanded: {expanded}");
Constructors
ClampedContent()
Default constructor.
Declaration
public ClampedContent()
Fields
containerUssClassName
The styling class of the container the content actually lives in.
Declaration
public const string containerUssClassName = "appui-clamped-content__container"
Field Value
| Type | Description |
|---|---|
| string |
defaultMaxHeight
The default height cap, in pixels.
Declaration
public const float defaultMaxHeight = 320
Field Value
| Type | Description |
|---|---|
| float |
Remarks
Mirrors the registry's max-h-80 default. It is a literal because it is a per-use content
budget, not a design token — no App UI sizing step is anywhere near this tall.
expandedUssClassName
The styling class applied while the content is expanded.
Declaration
public const string expandedUssClassName = "appui-clamped-content--expanded"
Field Value
| Type | Description |
|---|---|
| string |
fadeUssClassName
The styling class of the fade drawn over the clipped edge.
Declaration
public const string fadeUssClassName = "appui-clamped-content__fade"
Field Value
| Type | Description |
|---|---|
| string |
overflowingUssClassName
The styling class applied while the content actually overflows.
Declaration
public const string overflowingUssClassName = "appui-clamped-content--overflowing"
Field Value
| Type | Description |
|---|---|
| string |
toggleUssClassName
The styling class of the Show more / Show less toggle.
Declaration
public const string toggleUssClassName = "appui-clamped-content__toggle"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The ClampedContent main styling class.
Declaration
public const string ussClassName = "appui-clamped-content"
Field Value
| Type | Description |
|---|---|
| string |
viewportUssClassName
The styling class of the clipping viewport.
Declaration
public const string viewportUssClassName = "appui-clamped-content__viewport"
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
Logical container where child elements are added. If a child is added to this element, the child is added to this element's content container instead.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
expanded
Whether the content is currently revealed in full.
Declaration
[Tooltip("Whether the content is currently revealed in full.")]
[CreateProperty]
[UxmlAttribute]
public bool expanded { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Setting this drives the same state the toggle does, so a host can expand the block from elsewhere — a "expand all" command, for instance.
fadeVisible
Whether the fade is drawn over the clipped edge while collapsed and overflowing.
Declaration
[Tooltip("Whether the fade is drawn over the clipped edge.")]
[CreateProperty]
[UxmlAttribute]
public bool fadeVisible { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
lessLabel
The toggle's label while expanded. Accepts a localization key.
Declaration
[Tooltip("The toggle's label while expanded.")]
[CreateProperty]
[UxmlAttribute]
public string lessLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
maxHeight
The height cap applied while collapsed, in pixels.
Declaration
[Tooltip("The height cap applied while collapsed, in pixels.")]
[CreateProperty]
[UxmlAttribute]
public float maxHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
moreLabel
The toggle's label while collapsed. Accepts a localization key.
Declaration
[Tooltip("The toggle's label while collapsed.")]
[CreateProperty]
[UxmlAttribute]
public string moreLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
overflowing
Whether the content is taller than maxHeight.
Declaration
public bool overflowing { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Read-only, and re-evaluated on every layout pass while collapsed. While expanded it keeps the value measured just before expanding.
Methods
Remeasure()
Re-measures the content against maxHeight and updates the toggle and the fade.
Declaration
public void Remeasure()
Remarks
This runs automatically on attach and on every layout change. Call it directly only after swapping the content outside a layout pass. It is correct in both states: what is measured is the uncapped container, not the clipping viewport.
Events
expandedChanged
Raised when expanded changes, by toggle or programmatically.
Declaration
public event Action<bool> expandedChanged
Event Type
| Type | Description |
|---|---|
| Action<bool> |