Commit 6f8851f
authored
perf(arrows): use reactive hooks for arrow handle display and editing state (tldraw#8167)
Previously, any change to any shape would cause all arrows to render. We
should be careful with any reactive call to getOnlySelectedShape,
because it will call when the selected shape is changing. If we need to
react to a change in which shape is selected, we should use
getOnlySelectedShapeId.
In order to reduce unnecessary re-renders of arrow components, this PR
changes a call to `getOnlySelectedShape()?.id` to
`getOnlySelectedShapeId`.
To avoid an extra render when editing shape changes, it also wraps
`shouldDisplayHandles` and `isEditing` checks in `useValue` hooks for
finer-grained reactivity. It also refactors `getArrowLabelPosition` to
accept `isEditing` as a parameter rather than computing it internally,
avoiding redundant reactive lookups.
### Change type
- [x] `improvement`
### Test plan
1. Create arrows between shapes
2. Select an arrow — handles should display correctly
3. Double-click an arrow label to edit — label positioning should work
as before
4. Deselect and verify handles disappear
- [ ] Unit tests
- [ ] End to end tests
### Release notes
- Improve arrow component rendering performance with finer-grained
reactivity
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches arrow rendering reactivity (selection/editing state and label
positioning), so regressions could show up as missing handles or
mispositioned labels, but the changes are localized to UI/perf code.
>
> **Overview**
> Reduces unnecessary arrow re-renders by switching arrow component
state derivations to fine-grained `useValue` subscriptions (e.g.
`shouldDisplayHandles`, selection, and editing state) and by preferring
`getOnlySelectedShapeId()` over `getOnlySelectedShape()` where only the
id is needed.
>
> Refactors `getArrowLabelPosition` to accept an explicit `isEditing`
flag and updates all call sites (geometry, component render, and SVG
export) to avoid redundant reactive lookups when computing label bounds.
Also updates touch-end handling in `useCanvasEvents` to use
`editor.getEditingShapeId()`.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
e04558e. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 535ee94 commit 6f8851f
3 files changed
Lines changed: 44 additions & 27 deletions
File tree
- packages
- editor/src/lib/hooks
- tldraw/src/lib/shapes/arrow
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
| |||
220 | 222 | | |
221 | 223 | | |
222 | 224 | | |
223 | | - | |
| 225 | + | |
224 | 226 | | |
225 | 227 | | |
226 | 228 | | |
| |||
755 | 757 | | |
756 | 758 | | |
757 | 759 | | |
758 | | - | |
| 760 | + | |
| 761 | + | |
759 | 762 | | |
760 | | - | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | 763 | | |
770 | | - | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
771 | 795 | | |
772 | 796 | | |
773 | | - | |
774 | | - | |
775 | | - | |
| 797 | + | |
776 | 798 | | |
777 | 799 | | |
778 | 800 | | |
779 | 801 | | |
780 | 802 | | |
781 | | - | |
782 | | - | |
783 | | - | |
784 | | - | |
| 803 | + | |
785 | 804 | | |
786 | 805 | | |
787 | 806 | | |
| |||
798 | 817 | | |
799 | 818 | | |
800 | 819 | | |
801 | | - | |
| 820 | + | |
802 | 821 | | |
803 | 822 | | |
804 | 823 | | |
| |||
811 | 830 | | |
812 | 831 | | |
813 | 832 | | |
814 | | - | |
815 | 833 | | |
816 | | - | |
| 834 | + | |
817 | 835 | | |
818 | 836 | | |
819 | 837 | | |
| |||
1072 | 1090 | | |
1073 | 1091 | | |
1074 | 1092 | | |
1075 | | - | |
| 1093 | + | |
1076 | 1094 | | |
1077 | 1095 | | |
1078 | 1096 | | |
| |||
1185 | 1203 | | |
1186 | 1204 | | |
1187 | 1205 | | |
1188 | | - | |
| 1206 | + | |
1189 | 1207 | | |
1190 | 1208 | | |
1191 | 1209 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
229 | | - | |
230 | | - | |
| 229 | + | |
231 | 230 | | |
232 | 231 | | |
233 | 232 | | |
| |||
0 commit comments