|
| 1 | +--- |
| 2 | +title: Signature Strokes Are Offset in PanelBar ContentTemplate |
| 3 | +description: Learn how to render the Telerik Signature in a PanelBar ContentTemplate after the expand animation completes, so the Signature canvas initializes with correct dimensions. |
| 4 | +type: troubleshooting |
| 5 | +page_title: Signature Strokes Are Offset in PanelBar ContentTemplate |
| 6 | +slug: signature-kb-panelbar-contenttemplate-offset-strokes |
| 7 | +tags: telerik, blazor, signature, panelbar, contenttemplate |
| 8 | +ticketid: 1712712 |
| 9 | +res_type: kb |
| 10 | +components: ["signature","panelbar"] |
| 11 | +--- |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>UI for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This article answers the following questions: |
| 26 | + |
| 27 | +* Why does the Signature line look zoomed in when the component is inside a PanelBar item? |
| 28 | +* Why is the Signature stroke offset from the pointer in a PanelBar `ContentTemplate`? |
| 29 | +* How can you initialize Signature after PanelBar expand animation completes? |
| 30 | + |
| 31 | +## Cause |
| 32 | + |
| 33 | +When you place a `TelerikSignature` inside a PanelBar `ContentTemplate`, the Signature canvas can initialize while the PanelBar expand animation is still running. At that time, the container can have zero or partial height. The Signature JavaScript reads incorrect dimensions and the drawn line appears zoomed and offset from the cursor. |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +Render the Signature only after the PanelBar animation ends: |
| 38 | + |
| 39 | +1. Keep a `ShowSignature` flag set to `false` only when the user starts expanding the Signature item. |
| 40 | +1. In `OnAfterRenderAsync`, wait for the animation duration (`Task.Delay(100)` in this example), then set `ShowSignature` to `true`. |
| 41 | +1. Use a version counter to ignore stale delayed completions if the user expands or collapses again before the delay finishes. |
| 42 | + |
| 43 | +>caption Delay Signature rendering until PanelBar animation completes |
| 44 | +
|
| 45 | +````RAZOR |
| 46 | +<div class="panelbar-template"> |
| 47 | + <TelerikPanelBar Data="@PanelBarData" |
| 48 | + ExpandedItems="@ExpandedItems" |
| 49 | + ExpandedItemsChanged="@OnExpandedItemsChanged"> |
| 50 | + <PanelBarBindings> |
| 51 | + <PanelBarBinding ItemsField="@nameof(PanelBarItem.Items)"> |
| 52 | + <ContentTemplate> |
| 53 | + @{ |
| 54 | + var item = (PanelBarItem)context; |
| 55 | + } |
| 56 | +
|
| 57 | + @if (item.Text == "Sign Here") |
| 58 | + { |
| 59 | + if (ShowSignature) |
| 60 | + { |
| 61 | + <TelerikSignature @bind-Value="@SignatureValue" |
| 62 | + Width="320px" |
| 63 | + Height="180px" /> |
| 64 | + } |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + <div>@item.Text</div> |
| 69 | + } |
| 70 | + </ContentTemplate> |
| 71 | + </PanelBarBinding> |
| 72 | + </PanelBarBindings> |
| 73 | + </TelerikPanelBar> |
| 74 | +</div> |
| 75 | +
|
| 76 | +<style> |
| 77 | + .panelbar-template { |
| 78 | + margin: 0 auto; |
| 79 | + width: 400px; |
| 80 | + } |
| 81 | +</style> |
| 82 | +
|
| 83 | +@code { |
| 84 | + private List<PanelBarItem> PanelBarData { get; set; } = new(); |
| 85 | + private IEnumerable<object> ExpandedItems { get; set; } = new List<object>(); |
| 86 | + private string SignatureValue { get; set; } = string.Empty; |
| 87 | +
|
| 88 | + private bool ShowSignature { get; set; } |
| 89 | + private bool ShowSignaturePending { get; set; } |
| 90 | + private int SignatureVersion { get; set; } |
| 91 | +
|
| 92 | + private PanelBarItem? SignatureItem { get; set; } |
| 93 | +
|
| 94 | + private void OnExpandedItemsChanged(IEnumerable<object> items) |
| 95 | + { |
| 96 | + var expandedList = items.ToList(); |
| 97 | +
|
| 98 | + bool signatureWasExpanded = ExpandedItems.Contains(SignatureItem); |
| 99 | + bool signatureIsExpanding = SignatureItem != null && expandedList.Contains(SignatureItem); |
| 100 | +
|
| 101 | + ExpandedItems = expandedList; |
| 102 | +
|
| 103 | + if (!signatureWasExpanded && signatureIsExpanding) |
| 104 | + { |
| 105 | + ShowSignature = false; |
| 106 | + ShowSignaturePending = true; |
| 107 | + } |
| 108 | + } |
| 109 | +
|
| 110 | + private void LoadData() |
| 111 | + { |
| 112 | + SignatureItem = new PanelBarItem { Text = "Sign Here" }; |
| 113 | +
|
| 114 | + PanelBarData = new List<PanelBarItem> |
| 115 | + { |
| 116 | + new() { Text = "Documents" }, |
| 117 | + new() { Text = "Reports" }, |
| 118 | + SignatureItem, |
| 119 | + new() { Text = "Settings" } |
| 120 | + }; |
| 121 | +
|
| 122 | + ExpandedItems = new List<object> { SignatureItem }; |
| 123 | + } |
| 124 | +
|
| 125 | + protected override void OnInitialized() |
| 126 | + { |
| 127 | + LoadData(); |
| 128 | + ShowSignature = false; |
| 129 | + ShowSignaturePending = true; |
| 130 | + } |
| 131 | +
|
| 132 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 133 | + { |
| 134 | + if (firstRender || ShowSignaturePending) |
| 135 | + { |
| 136 | + ShowSignaturePending = false; |
| 137 | +
|
| 138 | + var version = ++SignatureVersion; |
| 139 | + await Task.Delay(100); |
| 140 | +
|
| 141 | + if (SignatureVersion == version) |
| 142 | + { |
| 143 | + ShowSignature = true; |
| 144 | + StateHasChanged(); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + public class PanelBarItem |
| 150 | + { |
| 151 | + public string Text { get; set; } = string.Empty; |
| 152 | + public List<PanelBarItem>? Items { get; set; } |
| 153 | + } |
| 154 | +} |
| 155 | +```` |
| 156 | + |
| 157 | +The `Task.Delay` value must match your PanelBar animation duration. If you use a different animation setup, increase or decrease the delay accordingly. |
| 158 | + |
| 159 | +## See Also |
| 160 | + |
| 161 | +* [Signature Overview](slug:signature-overview) |
| 162 | +* [PanelBar Content Template](slug:panelbar-templates-content) |
| 163 | +* [PanelBar Events](slug:panelbar-events) |
| 164 | +* [Resize Signature with the Browser](slug:signature-kb-relative-width-height) |
0 commit comments