Skip to content

Migrate to Tailwind v4#7291

Merged
robertknight merged 1 commit intomainfrom
tailwind-v4
Sep 15, 2025
Merged

Migrate to Tailwind v4#7291
robertknight merged 1 commit intomainfrom
tailwind-v4

Conversation

@robertknight
Copy link
Contributor

@robertknight robertknight commented Sep 11, 2025

This follows similar changes in h and the LMS, but with a few additional changes needed:

  • Update dependencies to support Tailwind v4

  • Convert SASS source files to CSS where possible, as it is easier to
    use Tailwind v4 with CSS sources.

  • Consolidate the small number of styles defined in annotator and
    sidebar .scss files into the root .css file.

  • Remove autoprefixer, as it is no longer needed

  • Replace Tailwind @apply with SASS mixin in highlights.scss

    The highlights stylesheet is no longer compiled with Tailwind after updating to
    v4 so @apply does nothing. Use a mixin with the same rules.

  • Convert all-initial utility into a class in the @components layer so that
    it has lower precedence than utility classes applied to the same element.

  • Add an explicit margin to ModalDialog to avoid depending on a default
    in the user agent style sheet. The Tailwind v4 reset overrides the UA
    default and sets it to zero.

@codecov
Copy link

codecov bot commented Sep 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.47%. Comparing base (513413c) to head (a63fe25).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7291   +/-   ##
=======================================
  Coverage   99.47%   99.47%           
=======================================
  Files         271      271           
  Lines       10947    10947           
  Branches     2618     2618           
=======================================
  Hits        10889    10889           
  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
Copy link
Contributor Author

This is now mostly working. I need to fix an issue with some CSS @property declarations seemingly being ignored, which causes missing borders in the toolbar at the edge of the sidebar.

Sidebar border

@robertknight
Copy link
Contributor Author

robertknight commented Sep 12, 2025

I need to fix an issue with some CSS @property declarations seemingly being ignored, which causes missing borders in the toolbar at the edge of the sidebar.

This is a known issue: https://stackoverflow.com/questions/78717295, tailwindlabs/tailwindcss#15005.

This test case produces blank output when the template is appended to the shadow DOM, but works if appended to the container's light DOM.

<html>
  <body>
    <template id="template">
      <style>
        @property --border-style {
          syntax: '*';
          inherits: false;
          initial-value: solid;
        }

        @property --border-color {
          syntax: '<color>';
          inherits: false;
          initial-value: red;
        }

        .foo {
          border-style: var(--border-style);
          border-color: var(--border-color);
          border-width: 1px;
          width: 20px;
          height: 20px;
        }
      </style>
      <div class="foo"></div>
    </template>
    <div id="container"></div>
    <script>
      const c = document.querySelector('#container');
      const shadow = c.attachShadow({ mode: 'open' });

      const t = document.querySelector('#template');
      shadow.append(t.content.cloneNode(true));
    </script>
  </body>
</html>

@robertknight
Copy link
Contributor Author

The issues linked in the previous comment mention a workaround of attaching the @property declarations to base document. That will probably work, but will break if Hypothesis is loaded on a site which decides to define these properties in a different way. The whole point of us using Shadow DOM is to achieve isolation from the host page.

@@ -0,0 +1,113 @@
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is annotator.scss and content it imported via @use declarations but adjusted to work with Tailwind v4.

@@ -0,0 +1,12 @@
/* Hide annotation layer while selecting text.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just pdfjs-overrides.scss renamed.

@@ -0,0 +1,91 @@
@import 'tailwindcss' source(none);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sidebar.scss from before and modules that it imported via @use combined into one CSS file and adjusted to work with Tailwind v4.


@utility container {
@apply m-auto px-[9px] max-w-[768px];
}
Copy link
Contributor Author

@robertknight robertknight Sep 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a replacement for the container customizations that were defined in the JS Tailwind configuration previously.

@robertknight
Copy link
Contributor Author

robertknight commented Sep 12, 2025

Some more general discussion of CSS names and shadow DOM here: https://developer.chrome.com/docs/css-ui/css-names#how_names_and_the_shadow_dom_should_work_together

Unlike other CSS names, CSS properties are not encapsulated by shadow DOM. Rather, they are the common means to pass parameters across different shadow trees. This makes the @property descriptor special: it's supposed to behave like a document-global type declaration that defines how a particular named property acts. Because properties have to match across shadow trees, mismatch of property declarations would create unexpected results, so @property declarations are specified to be flattened and resolved according to document order.

@property
As defined in the specification, any declaration of @property will be flattened to the document scope. Today however, in all browsers you can only declare @property in the document scope and @property declarations within shadow roots are ignored.
See issue 10541.

@robertknight robertknight force-pushed the tailwind-v4 branch 2 times, most recently from 817e7f8 to a45adec Compare September 15, 2025 09:27
 - Update dependencies to support Tailwind v4

 - Convert SASS source files to CSS where possible, as it is easier to
   use Tailwind v4 with CSS sources.

 - Consolidate the small number of styles defined in annotator and
   sidebar .scss files into the root .css file.

 - Remove autoprefixer, as it is no longer needed

 - Replace Tailwind @apply with SASS mixin in highlights.scss

   The highlights stylesheet is no longer compiled with Tailwind after updating to
   v4 so `@apply` does nothing. Use a mixin with the same rules.

 - Convert `all-initial` utility into a class in the `@components` layer so that
   it has lower precedence than utility classes applied to the same element.

 - Add an explicit margin to ModalDialog to avoid depending on a default
   in the user agent style sheet. The Tailwind v4 reset overrides the UA
   default and sets it to zero.
@robertknight
Copy link
Contributor Author

For future reference, the issue with @property and shadow DOM was solved in #7293.

@robertknight robertknight marked this pull request as ready for review September 15, 2025 10:01
<dialog
ref={dialogRef}
className="relative w-full h-full backdrop:bg-black/50"
className="relative m-5 w-full h-full backdrop:bg-black/50"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This equals 20 pixels in my browser, compared to 19 for the user agent default before.

screens: {
lg: '768px',
},
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to a utility defined in CSS. I have simplified the definition there as well.

'.all-initial': {
all: 'initial',
},
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to a class in CSS.

@robertknight robertknight merged commit 6e86750 into main Sep 15, 2025
4 checks passed
@robertknight robertknight deleted the tailwind-v4 branch September 15, 2025 10:15
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