Skip to content

Recalculate sidebar position when annotator styles load#7309

Merged
robertknight merged 1 commit intomainfrom
fix-sidebar-margin-left
Sep 16, 2025
Merged

Recalculate sidebar position when annotator styles load#7309
robertknight merged 1 commit intomainfrom
fix-sidebar-margin-left

Conversation

@robertknight
Copy link
Contributor

@robertknight robertknight commented Sep 16, 2025

If the annotator.css bundle loads after the sidebar is opened, the margin-left property on the .sidebar-container element can be set incorrectly. Fix this problem by invoking Sidebar._onResize when styles finish loading. This is the same way we handle "resize" events on the window.

Part of #7307.

Testing:

  1. Apply this diff to simulate slow style bundle loading:
diff --git a/src/annotator/util/shadow-root.ts b/src/annotator/util/shadow-root.ts
index 18ef86a01..5a6218888 100644
--- a/src/annotator/util/shadow-root.ts
+++ b/src/annotator/util/shadow-root.ts
@@ -36,10 +36,13 @@ function loadStyles(shadowRoot: ShadowRoot): Promise<void> {
 
   const linkEl = document.createElement('link');
   linkEl.rel = 'stylesheet';
-  linkEl.href = url;
   linkEl.crossOrigin = 'anonymous';
   shadowRoot.appendChild(linkEl);
 
+  setTimeout(() => {
+    linkEl.href = url;
+  }, 1000);
+
   // When styles are loaded for the first time, wait for the stylesheet to load,
   // then extract `@property` declarations and append them to a stylesheet in
   // the top-level document.

The minimum timeout delay needed to trigger the problem varies. Locally I need it to be at least 100.

  1. Visit http://localhost:3000.

On main, the sidebar ends up positioned on the left side. On this branch it will initially appear there, but then update its position once styles loading.

There is further improvement needed here, as ideally we want to prevent the sidebar from being displayed until styles have loaded. That will need more refactoring, and I think it is valuable to get this partial fix out first.

If the annotator.css bundle loads after the sidebar is opened, the `margin-left`
property on the `.sidebar-container` element can be set incorrectly. Fix this
problem by invoking `Sidebar._onResize` when styles finish loading. This is the
same way we handle "resize" events on the window.

Part of #7307.
@codecov
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.47%. Comparing base (8a7d7a4) to head (b256726).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7309   +/-   ##
=======================================
  Coverage   99.47%   99.47%           
=======================================
  Files         271      271           
  Lines       10949    10954    +5     
  Branches     2616     2616           
=======================================
+ Hits        10891    10896    +5     
  Misses         58       58           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@robertknight robertknight merged commit e42f1dc into main Sep 16, 2025
4 checks passed
@robertknight robertknight deleted the fix-sidebar-margin-left branch September 16, 2025 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants