Skip to content

Prevent text from overflowing in various cases#756

Merged
delucis merged 3 commits intowithastro:mainfrom
julien-deramond:main-jd-add-some-wrapping
Oct 6, 2023
Merged

Prevent text from overflowing in various cases#756
delucis merged 3 commits intowithastro:mainfrom
julien-deramond:main-jd-add-some-wrapping

Conversation

@julien-deramond
Copy link
Copy Markdown
Contributor

@julien-deramond julien-deramond commented Sep 25, 2023

What kind of changes does this PR include?

  • Changes to Starlight code

Before the final merge if/when approved

Description

Closes #754

This PR tackles the main issue from #754 but also tries to tackle #754 (comment). Changes are opinionated and can of course be discussed so that it fits well the global Starlight design and rendering.

The main idea here is to use overflow-wrap: anywhere.

I originally suggested using overflow-wrap: break-word, but overflow-wrap: anywhere is a better fit since it doesn't force folks to sometimes force the width of the components when they use it (more info in #756 (comment)).

word-break: break-word is already used in the project but I'd suggest replacing it with overflow-wrap: anywhere since break-word is deprecated (source: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#values).

This was the main direction. Let's dive into the use cases.


Search and reset

I did not manage to reproduce any issues with or without the original word-break: break-word source code. I've changed it to overflow-wrap: anywhere as mentioned in the main description for consistency.


Badge

overflow-wrap: anywhere is defined within the Badge component to define the overflow rule whatever the context.

Old rendering New rendering
Screenshot 2023-09-25 at 18 26 19 Screenshot 2023-09-25 at 18 24 42

PrevNextLink
This use case will be tackled in a separate PR (https://github.com//pull/814) given its complexity This one was a bit more complex; unless there's a simpler solution.
  1. Added a flex-shrink: 0 to the SVG so that its size remains the same
  2. From here, text content still overflows so overflow-wrap: anywhere is added to the <a>s.
  3. Then, in order to have both items displayed on the same row, or in full-width if there's no next/previous page, I used a combination of width: 50% and flex-basis: calc(50% - 1rem) to calculate the right size (and with 100% on mobile so that each item takes the full width).

No before/after rendering screenshots for this one. You'll find instead the rendering of the different use cases (which have the same layout in responsive):

Screenshot 2023-09-25 at 21 42 31
Screenshot 2023-09-25 at 21 42 23
Screenshot 2023-09-25 at 21 42 11
Screenshot 2023-09-25 at 21 44 27


RightSidebarPanel

word-break: break-word is replaced with overflow-wrap: anywhere.

Old rendering New rendering
Screenshot 2023-10-03 at 12 54 59 Screenshot 2023-10-03 at 12 55 09

SidebarSublist

Added a flex-shrink: 0 to the SVG so that its size remains the same and set up a new rule for li (mainly needed for .top-level > li) to use overflow-wrap: anywhere so that the scope is large enough to cover various use cases within this list.

Old rendering New rendering
Screenshot 2023-09-25 at 19 15 45 Screenshot 2023-09-25 at 19 15 26
Old rendering New rendering
Screenshot 2023-09-25 at 21 18 41 Screenshot 2023-09-25 at 21 18 58

Another use case

While testing everything, if the "On this page" wording is too long, here's the rendering:

Screenshot 2023-09-25 at 19 09 06

I'm not completely sure a fix is needed for this use case. Even with languages containing a lot of characters, the space should be sufficient for normal usage. I don't want to over-engineer it so I'll wait for your thoughts on this one.


Final note

The rendering has been tested on macOS (Firefox, Chrome, and Safari) in responsive mode, with or without long words, and also tableOfContents: false. There are a lot of use cases so I've probably missed some of them not knowing well Starlight's features.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Sep 25, 2023

🦋 Changeset detected

Latest commit: 7982b0c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link
Copy Markdown

netlify bot commented Sep 25, 2023

👷 Deploy Preview for astro-starlight processing.

Name Link
🔨 Latest commit 7982b0c
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/652018bf24a9aa0008e9722c

@github-actions github-actions bot added 📚 docs Documentation website changes 🌟 core Changes to Starlight’s main package labels Sep 25, 2023
@julien-deramond julien-deramond force-pushed the main-jd-add-some-wrapping branch 4 times, most recently from 4714016 to 4981cc1 Compare September 25, 2023 20:01
@julien-deramond julien-deramond marked this pull request as ready for review September 25, 2023 20:08
Copy link
Copy Markdown
Member

@HiDeoo HiDeoo left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution 🙌

I noticed a few missing edge cases and left a few comments/questions but the changes seems to work relatively well overall and be a real improvement so far.

margin-inline-start: var(--sl-sidebar-item-padding-inline);
border-inline-start: 1px solid var(--sl-color-hairline-light);
padding-inline-start: var(--sl-sidebar-item-padding-inline);
overflow-wrap: break-word;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This selector seems to be a bit too specific and does not cover top-level links:

SCR-20230926-jczb

To test this use-case, you can locally add a new entry to the sidebar configuration:, e.g.:

sidebar: [
  {
    label: "ThisIsAVeryLongLabelToTestTheSidebarIssues",
    link: "test",
  },
  // Existing sidebar configuration
];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ohhh good catch! I've added this use case in the commit so that we can test it.
So I've dropped this rule and added instead the following which is less specific:

.top-level > li {
  overflow-wrap: anywhere;
}

Comment on lines 63 to 64
ul :global(.sl-badge) {
max-width: 100%;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think an alternative solution could be to not do that and use overflow-wrap: anywhere; instead of overflow-wrap: break-word; in the <Badge/> component. This would mean that we would not have to do that every time we use a badge in a similar context.

Any thoughts?

Copy link
Copy Markdown
Contributor Author

@julien-deramond julien-deramond Sep 26, 2023

Choose a reason for hiding this comment

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

Neat, it works pretty well! So obviously, this is way better in terms of usage since there's nothing to do while using the component.

I was a bit worried about overflow-wrap: anywhere browser support for Safari which is only from 15.4.

But the team confirmed that other features used in Astro and/or Starlight are only working from Safari 15.4.
I ran some tests with BrowserStack with Safari 15.3 just to see how's the fallback in our specific use case, but it's impossible to say what's happening:

Screenshot 2023-09-26 at 18 43 49

I don't have access to any Safari 15.4, but from 16, everything's rendered as what you can see in your browser right now deployed by Netlify. Double-checked with various versions of Chrome and Firefox and seems to be good to go with this solution 🚀

Comment on lines +90 to +93
summary > h2 {
width: calc(100% - 1.25rem);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tried some flex-shriking on the caret without success.

What issues did you encounter? Disabling shrinking using flex-shrink: 0; on the caret icon and using overflow-wrap: anywhere; on the title seems to do the trick for me but I may be missing something.

Copy link
Copy Markdown
Contributor Author

@julien-deramond julien-deramond Sep 26, 2023

Choose a reason for hiding this comment

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

Tried it in combination with overflow-wrap: break-word and not anywhere. It works way better now, thanks for the trick.

So I've dropped this rule and used overflow-wrap: anywhere here + used flex-shrink :0 on the caret.
More globally I've made this same exact change everywhere (overflow-wrap: break-word -> overflow-wrap: anywhere)

Comment on lines +86 to +88
svg + span {
width: calc(100% - 1.5rem);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I am not missing something, I guess like in the <SidebarSublist/> component, this could be avoided by using overflow-wrap: anywhere; instead of overflow-wrap: break-word; for the link.

Any thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Based on the previous comments and fixes, this has been removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This one is a bit trickier indeed.

The current approach will break the existing wrapping behavior on small width screens:

Before After
SCR-20230926-jkeq SCR-20230926-jkcm

It also looks like this would not work when you are on a page where both the previous and next links are fairly long:

SCR-20230926-jkph

Not sure yet what would be the best approach here, might need to think about it a bit more 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I didn't test at the right breakpoints for mobile. So I've tried something with a @media rule to handle the 2 types of rendering: 1 row, 2 rows:

a {
  /* ... */
  width: 100%;
  flex-basis: calc(100% - 0.5rem);
  /* ... */
}

@media (min-width: 29.6em) {
  a {
    width: 50%;
      flex-basis: calc(50% - 1rem);
    }
}

Note that 29.6em might be a lil' bit magical here. I've found this value by comparing the rendering between this branch and the main branch. The switch between 1 row to 2 rows was between 473px and 474px which led me to that 29.6em. Obviously, can be changed to something like 30em or other values.

Being still a bit tricky to test, I hope that I've covered most of the use cases 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Currently inside Starlight we only use (min-width: 50rem) and (min-width: 72rem). Would love to keep us to that consistency if possible.

This is definitely one of the trickier ones. I also think it’s one of the less crucial ones, so we could also decide to split out fixing this one into a follow-up PR if figuring it out will otherwise block this PR.

Copy link
Copy Markdown
Contributor Author

@julien-deramond julien-deramond Oct 6, 2023

Choose a reason for hiding this comment

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

I definitely can understand that we prefer to stick with only (min-width: 50rem) and (min-width: 72rem). I've made some tests with container queries that could help but they are not available before Safari 16.
So I dropped this whole modification and I'll try to tackle it in a separate PR: #814

@julien-deramond julien-deramond marked this pull request as draft September 26, 2023 16:06
@julien-deramond julien-deramond marked this pull request as ready for review September 26, 2023 17:53
@julien-deramond
Copy link
Copy Markdown
Contributor Author

Please note that I haven't yet updated the description of the PR with the new modifications. I'm waiting for an agreement on the final solution with the first code review since the description is a bit heavy to update. Hope you don't mind.

Copy link
Copy Markdown
Member

@HiDeoo HiDeoo left a comment

Choose a reason for hiding this comment

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

From all the cases/pages I tested, the new changes look great. Let's see if some other people find more edge cases or not.

I'm not particularly a huge fan of the changes for the next/prev links but I have yet to find a more elegant solution not breaking anything.

Btw, did you investigate the CI / Check for accessibility issues failure or do you want me to take a look at it? (note: to run them locally you can NO_GRADIENTS=true pnpm build && pnpm test)

@julien-deramond
Copy link
Copy Markdown
Contributor Author

I'm not particularly a huge fan of the changes for the next/prev links but I have yet to find a more elegant solution not breaking anything.

Neither do I.

Btw, did you investigate the CI / Check for accessibility issues failure or do you want me to take a look at it? (note: to run them locally you can NO_GRADIENTS=true pnpm build && pnpm test)

Not yet, I'll check this out ASAP.

@julien-deramond
Copy link
Copy Markdown
Contributor Author

julien-deramond commented Sep 29, 2023

I haven't the precise answer yet but there are two errors about color contrast ratio thresholds located at:

<span class="astro-sn2en3ud">Configuration Reference</span>
<span class="astro-sn2en3ud">Frontmatter Reference</span>

Those elements are both at the end of the sidemenu and their rendering has not been changed.
When I remove my extra-long wordings, pa11y-ci is happy again.
The only difference is that with the long wording, they are hidden when one opens the page with a regular screen.
So I'd say that the errors start to happen when there's too much content in the sidebar and that it's not especially related to this PR (where the extra-long labels will be removed before merging).


Edit: I tried the following:

  • git checkout main
  • I've cloned the frontmatter.md several times so that the sidebar is really full of elements
Screenshot 2023-09-29 at 11 30 46
  • Start running pa11y-ci locally and:
Running Pa11y on 68 URLs:
 > http://localhost:4321/ - 0 errors
 > http://localhost:4321/environmental-impact/ - 3 errors
 > http://localhost:4321/getting-started/ - 3 errors
 > http://localhost:4321/guides/authoring-content/ - 3 errors
 > http://localhost:4321/guides/components/ - 3 errors
 > http://localhost:4321/guides/css-and-tailwind/ - 3 errors
 > http://localhost:4321/guides/customization/ - 3 errors
 > http://localhost:4321/guides/i18n/ - 3 errors
 > http://localhost:4321/guides/project-structure/ - 3 errors
 > http://localhost:4321/guides/sidebar/ - 3 errors

I can create a separate issue for this use case.

@HiDeoo
Copy link
Copy Markdown
Member

HiDeoo commented Sep 29, 2023

Thanks for the investigation.

Interesting. Even more interesting is that if I switch locally to this branch and run the tests on my machine, they pass. I guess it depends on the window size which depends on the screen resolution maybe? I assumed that the window size would be fixed, but it seems that it is not or something else is going on.

Nonetheless, I guess this is indeed something that should be fixed in another PR as when reverting your temporary long texts, the tests should pass again.

@julien-deramond julien-deramond force-pushed the main-jd-add-some-wrapping branch from 71cfbfe to 9df0e55 Compare October 3, 2023 10:40
@vercel
Copy link
Copy Markdown

vercel bot commented Oct 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
starlight ✅ Ready (Inspect) Visit Preview Oct 6, 2023 2:27pm

@julien-deramond
Copy link
Copy Markdown
Contributor Author

FYI I've updated the description so that it reflects the final changes, squashed the commits, and split them so that it is easier for you to remove the long text tests when the PR is considered ready to merge (you'll just have to drop 9df0e55).

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Thanks for tackling all of these edge cases @julien-deramond! Really nice work. Left a couple of notes to try to help get this PR over the line! 🙌

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Currently inside Starlight we only use (min-width: 50rem) and (min-width: 72rem). Would love to keep us to that consistency if possible.

This is definitely one of the trickier ones. I also think it’s one of the less crucial ones, so we could also decide to split out fixing this one into a follow-up PR if figuring it out will otherwise block this PR.

Comment on lines +68 to +70
.top-level > li {
overflow-wrap: anywhere;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this really only apply to the top-level list, or any li?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The components being black boxes, it's safe to apply the rule directly to li as defensive CSS. I've changed it to:

diff --git a/packages/starlight/components/SidebarSublist.astro b/packages/starlight/components/SidebarSublist.astro
index c4c5882..63308f6 100644
--- a/packages/starlight/components/SidebarSublist.astro
+++ b/packages/starlight/components/SidebarSublist.astro
@@ -53,6 +53,10 @@ interface Props {
                padding: 0;
        }
 
+       li {
+               overflow-wrap: anywhere;
+       }
+
        ul ul li {
                margin-inline-start: var(--sl-sidebar-item-padding-inline);
                border-inline-start: 1px solid var(--sl-color-hairline-light);
@@ -65,10 +69,6 @@ interface Props {
                color: var(--sl-color-white);
        }
 
-       .top-level > li {
-               overflow-wrap: anywhere;
-       }
-
        .top-level > li + li {
                margin-top: 0.75rem;
        }

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Thanks for splitting out that one fix to #814 @julien-deramond!

I think this is looking good if you’d like to go ahead and remove the text strings being used for testing.

@julien-deramond julien-deramond force-pushed the main-jd-add-some-wrapping branch from 55be354 to f1e175b Compare October 6, 2023 11:23
@github-actions github-actions bot removed the 📚 docs Documentation website changes label Oct 6, 2023
@julien-deramond
Copy link
Copy Markdown
Contributor Author

55be354 has been removed, and so the a11y tests are back to normal.

HiDeoo
HiDeoo previously approved these changes Oct 6, 2023
@HiDeoo HiDeoo dismissed their stale review October 6, 2023 11:31

Missing changeset

Copy link
Copy Markdown
Member

@HiDeoo HiDeoo left a comment

Choose a reason for hiding this comment

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

Just noticed we still need a changeset for the changes.

@julien-deramond
Copy link
Copy Markdown
Contributor Author

Added a changeset via 47526e2. I supposed it was a "patch" and used a really simple generic sentence. Don't hesitate to update it to change the type of changeset or to enhance the description with more detail if needed.

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Thanks again for all the hard work chasing down these issues and figuring out the fixes @julien-deramond — and for helping @HiDeoo!

@delucis delucis added the 🌟 patch Change that triggers a patch release label Oct 6, 2023
@delucis delucis merged commit f55a8f0 into withastro:main Oct 6, 2023
@astrobot-houston astrobot-houston mentioned this pull request Oct 6, 2023
Yoxnear pushed a commit to Yoxnear/starlight-custom that referenced this pull request Jul 23, 2025
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(minor): Sidebar Labels breaking / not properly displaying

3 participants