Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds print styles for Starlight docs pages #157

Merged
merged 42 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
662ecd5
initial pass at print styles
Jun 5, 2023
b78fe96
refactor: using modifiers for display util classes
Jun 5, 2023
fc17193
make sure the header tools group is hidden
Jun 5, 2023
55eed5c
avoid page breaks in code blocks
Jun 5, 2023
5a8e7a1
a few layout fixes for Safari printing viewport
Jun 5, 2023
819ea02
fixed header was hiding print content
Jun 9, 2023
47b116c
Merge branch 'main' into feat/print-stylesheet
tony-sull Jun 20, 2023
9c46292
refactor: moving all print style selectors to the global CSS sheet
Jun 20, 2023
a302379
nit: @media print isn't needed in the global sheet
Jun 20, 2023
bc290f6
Merge branch 'main' into feat/print-stylesheet
tony-sull Jun 20, 2023
e3e2e18
Merge branch 'main' into feat/print-stylesheet
delucis Dec 16, 2024
6b6e4a5
Fix CSS import path
delucis Dec 16, 2024
94da2ac
Tweak class in `<Header>`
delucis Dec 16, 2024
8364940
Remove superfluous class names
delucis Dec 16, 2024
7c5ff3b
Update print styles
delucis Dec 16, 2024
ebf68ca
Fix code block break-inside style
delucis Dec 16, 2024
fe23fed
Better inline code styles
delucis Dec 17, 2024
4bd5784
Ignore print stylesheet in size limit
delucis Dec 17, 2024
7804db4
Fix inline code style for code in headings
delucis Dec 17, 2024
d58bc94
Adjust inline code size (again…)
delucis Dec 17, 2024
53988e6
Make FileTree icon color easier to override
delucis Dec 17, 2024
b5fa5ec
FileTree print styles
delucis Dec 17, 2024
7784c18
Add changeset
delucis Dec 17, 2024
080391b
Avoid page breaks inside tabs and asides
delucis Dec 17, 2024
20cf394
Fix sidebar preview rendering when printing in Starlight docs
delucis Dec 17, 2024
6870540
Merge branch 'main' into feat/print-stylesheet
delucis Jan 8, 2025
d0b8e58
Move print stylesheet link to bottom of head (it’s low priority)
delucis Jan 9, 2025
352a30e
Increase specificity of print variables
delucis Jan 9, 2025
b3977ed
Disable shadows in print styles
delucis Jan 9, 2025
9b9934f
Fix primary link button styles when printing
delucis Jan 9, 2025
8b368d9
Reorganise print.css slightly to group related styles and add a coupl…
delucis Jan 9, 2025
5e8d9d0
Clarify comment
delucis Jan 10, 2025
1342f86
Set banner colors in print styles
delucis Jan 10, 2025
1a36d9a
Ensure terminal frame dots are visible when printing
delucis Jan 10, 2025
20e47c9
Adjust Expressive Code title bar tab display
delucis Jan 10, 2025
bb2c313
Ensure steps guidelines are visible when background colors are disable
delucis Jan 10, 2025
71db973
Fix two outdated comments
delucis Jan 10, 2025
cd76d01
Fix text contrast in badge component
delucis Jan 10, 2025
104e853
Avoid breaks inside `<pre>` and `<figure>`
delucis Jan 10, 2025
43ed243
Hide EC copy button when printing from touch devices
delucis Jan 10, 2025
c142194
Merge branch 'main' into feat/print-stylesheet
delucis Jan 10, 2025
b7d6a24
Merge branch 'main' into feat/print-stylesheet
delucis Jan 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-gorillas-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/starlight": minor
---

Adds a print stylesheet to improve the appearance of Starlight docs pages when printed
3 changes: 2 additions & 1 deletion docs/src/components/sidebar-preview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function makeEntries(items: SidebarConfig): SidebarEntry[] {
background-color: var(--sl-color-bg-sidebar);
border: 1px solid var(--sl-color-gray-5);
padding: 1rem var(--sl-sidebar-pad-x);
max-width: var(--sl-sidebar-width);
/* Matches `var(--sl-sidebar-width)`, but hardcoded to avoid being overridden when printing the page. */
max-width: 18.75rem;
}
</style>
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
},
{
"name": "/_astro/*.css",
"path": "examples/basics/dist/_astro/*.css",
"path": [
"examples/basics/dist/_astro/*.css",
"!examples/basics/dist/_astro/print.*.css"
],
"limit": "14.5 kB",
"gzip": true
}
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight/components/EditLink.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { editUrl } = Astro.props;

{
editUrl && (
<a href={editUrl} class="sl-flex">
<a href={editUrl} class="sl-flex print:hidden">
<Icon name="pencil" size="1.2em" />
{Astro.locals.t('page.editLink')}
</a>
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const shouldRenderSearch =
<div class="title-wrapper sl-flex">
<SiteTitle {...Astro.props} />
</div>
<div class="sl-flex">
<div class="sl-flex print:hidden">
{shouldRenderSearch && <Search {...Astro.props} />}
</div>
<div class="sl-hidden md:sl-flex right-group">
<div class="sl-hidden md:sl-flex print:hidden right-group">
<div class="sl-flex social-icons">
<SocialIcons {...Astro.props} />
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight/components/MobileMenuToggle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Props } from '../props';
import Icon from '../user-components/Icon.astro';
---

<starlight-menu-button>
<starlight-menu-button class="print:hidden">
<button
aria-expanded="false"
aria-label={Astro.locals.t('menuButton.accessibleLabel')}
Expand Down
3 changes: 3 additions & 0 deletions packages/starlight/components/Page.astro
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import '../style/asides.css';
// Important that this is the last import so it can override built-in styles.
import 'virtual:starlight/user-css';

import printHref from '../style/print.css?url&no-inline';

const pagefindEnabled =
Astro.props.entry.slug !== '404' &&
!Astro.props.entry.slug.endsWith('/404') &&
Expand Down Expand Up @@ -75,6 +77,7 @@ if (pagefindEnabled) mainDataAttributes['data-pagefind-body'] = '';
}
</style>
<ThemeProvider {...Astro.props} />
<link rel="stylesheet" href={printHref} media="print" />
</head>
<body>
<SkipLink {...Astro.props} />
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight/components/PageFrame.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { hasSidebar } = Astro.props;
<header class="header"><slot name="header" /></header>
{
hasSidebar && (
<nav class="sidebar" aria-label={Astro.locals.t('sidebarNav.accessibleLabel')}>
<nav class="sidebar print:hidden" aria-label={Astro.locals.t('sidebarNav.accessibleLabel')}>
<MobileMenuToggle {...Astro.props} />
<div id="starlight__sidebar" class="sidebar-pane">
<div class="sidebar-content sl-flex">
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight/components/Pagination.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { prev, next } = pagination;
const isRtl = dir === 'rtl';
---

<div class="pagination-links" dir={dir}>
<div class="pagination-links print:hidden" dir={dir}>
{
prev && (
<a href={prev.href} rel="prev">
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/components/SiteTitle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { siteTitle, siteTitleHref } = Astro.props;
config.logo && logos.dark && (
<>
<img
class:list={{ 'light:sl-hidden': !('src' in config.logo) }}
class:list={{ 'light:sl-hidden print:hidden': !('src' in config.logo) }}
alt={config.logo.alt}
src={logos.dark.src}
width={logos.dark.width}
Expand All @@ -19,7 +19,7 @@ const { siteTitle, siteTitleHref } = Astro.props;
{/* Show light alternate if a user configure both light and dark logos. */}
{!('src' in config.logo) && (
<img
class="dark:sl-hidden"
class="dark:sl-hidden print:block"
alt={config.logo.alt}
src={logos.light?.src}
width={logos.light?.width}
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight/components/TwoColumnContent.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Props } from '../props';
<div class="lg:sl-flex">
{
Astro.props.toc && (
<aside class="right-sidebar-container">
<aside class="right-sidebar-container print:hidden">
<div class="right-sidebar">
<slot name="right-sidebar" />
</div>
Expand Down
140 changes: 140 additions & 0 deletions packages/starlight/style/print.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
@media print {
/**
This is increased in specificity so it works even when injected into pages before regular styles.
TODO: Just use `:root` once we move to @layer.
Same goes for the use of `!important` in this file.
*/
:root[data-theme]:is(:root) {
/* Colours (light mode) */
delucis marked this conversation as resolved.
Show resolved Hide resolved
--sl-color-white: hsl(224, 10%, 10%);
--sl-color-gray-1: hsl(224, 14%, 16%);
--sl-color-gray-2: hsl(224, 10%, 23%);
--sl-color-gray-3: hsl(224, 7%, 36%);
--sl-color-gray-4: hsl(224, 6%, 56%);
--sl-color-gray-5: hsl(224, 6%, 77%);
--sl-color-gray-6: hsl(224, 20%, 94%);
--sl-color-gray-7: hsl(224, 19%, 97%);
--sl-color-black: hsl(0, 0%, 100%);

--sl-color-orange-high: hsl(var(--sl-hue-orange), 80%, 25%);
--sl-color-orange: hsl(var(--sl-hue-orange), 90%, 60%);
--sl-color-orange-low: hsl(var(--sl-hue-orange), 90%, 88%);
--sl-color-green-high: hsl(var(--sl-hue-green), 80%, 22%);
--sl-color-green: hsl(var(--sl-hue-green), 90%, 46%);
--sl-color-green-low: hsl(var(--sl-hue-green), 85%, 90%);
--sl-color-blue-high: hsl(var(--sl-hue-blue), 80%, 30%);
--sl-color-blue: hsl(var(--sl-hue-blue), 90%, 60%);
--sl-color-blue-low: hsl(var(--sl-hue-blue), 88%, 90%);
--sl-color-purple-high: hsl(var(--sl-hue-purple), 90%, 30%);
--sl-color-purple: hsl(var(--sl-hue-purple), 90%, 60%);
--sl-color-purple-low: hsl(var(--sl-hue-purple), 80%, 90%);
--sl-color-red-high: hsl(var(--sl-hue-red), 80%, 30%);
--sl-color-red: hsl(var(--sl-hue-red), 90%, 60%);
--sl-color-red-low: hsl(var(--sl-hue-red), 80%, 90%);

--sl-color-accent-high: hsl(234, 80%, 30%);
--sl-color-accent: hsl(234, 90%, 60%);
--sl-color-accent-low: hsl(234, 88%, 90%);

--sl-color-text-accent: var(--sl-color-accent);
--sl-color-text-invert: var(--sl-color-black);
--sl-color-bg-nav: var(--sl-color-gray-7);
--sl-color-bg-sidebar: var(--sl-color-bg);
--sl-color-bg-inline-code: var(--sl-color-gray-6);
--sl-color-bg-accent: var(--sl-color-accent);
--sl-color-hairline-light: var(--sl-color-gray-6);
--sl-color-hairline-shade: var(--sl-color-gray-6);

--sl-color-backdrop-overlay: hsla(225, 9%, 36%, 0.66);

/* Disable shadows when printing. */
--sl-shadow-sm: none;
--sl-shadow-md: none;
--sl-shadow-lg: none;
}

/* Utility classes for controlling element visibility when printing. */
.print\:hidden {
display: none !important;
}
.print\:flex {
display: flex !important;
}
.print\:block {
display: block !important;
}

/* Page layout tweaks. */
main {
padding-bottom: 0 !important;
}
main > .content-panel {
padding-block-start: 0 !important;
}
.content-panel + .content-panel {
border: 0 !important;
}
/* layout/PageFrame.astro */
.page > header {
position: relative !important;
}
.page > .main-frame {
padding-top: 0;
padding-inline-start: 0;
}
/* layout/TwoColumnContent.astro */
.main-pane {
--sl-sidebar-width: 0px !important;
--sl-content-width: 100% !important;
}

/* components/MarkdownContent.astro */
.sl-markdown-content :is(h1, h2, h3, h4, h5, h6) {
break-after: avoid;
}
.sl-markdown-content :is(p, li) {
orphans: 2;
widows: 2;
}
.sl-markdown-content pre {
overflow-x: hidden !important;
white-space: pre-wrap !important;
}
.sl-markdown-content .expressive-code {
break-inside: avoid;
}
/* Inline code */
.sl-markdown-content code:not(:where(.not-content *)) {
background-color: transparent !important;
padding: 0 !important;
margin-block: unset !important;
font-size: 0.9375em !important;
}

/* user-components/FileTree.astro */
starlight-file-tree {
break-inside: avoid;
}
starlight-file-tree .highlight {
outline: 3px solid var(--sl-color-accent-low);
color: var(--sl-color-text) !important;
background-color: transparent !important;
}

/* user-components/Aside.astro */
.starlight-aside {
break-inside: avoid;
}

/* user-components/LinkButton.astro */
.sl-link-button.primary {
background: transparent !important;
border-color: var(--sl-color-white) !important;
color: var(--sl-color-white) !important;
}

/* user-components/Tabs.astro */
starlight-tabs {
break-inside: avoid;
}
}
6 changes: 3 additions & 3 deletions packages/starlight/user-components/FileTree.astro
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ const html = processFileTree(fileTreeHtml, Astro.locals.t('fileTree.directory'))
starlight-file-tree :global(.directory > details > summary:hover .tree-icon) {
cursor: pointer;
color: var(--sl-color-text-accent);
fill: var(--sl-color-text-accent);
fill: currentColor;
}

starlight-file-tree :global(.directory > details > summary:hover ~ ul) {
border-color: var(--sl-color-gray-4);
}

starlight-file-tree :global(.directory > details > summary:hover .highlight .tree-icon) {
fill: var(--sl-color-text-invert);
fill: currentColor;
}

starlight-file-tree :global(ul) {
Expand Down Expand Up @@ -129,6 +129,6 @@ const html = processFileTree(fileTreeHtml, Astro.locals.t('fileTree.directory'))
}

starlight-file-tree :global(.highlight svg.tree-icon) {
fill: var(--sl-color-text-invert);
fill: currentColor;
}
</style>
Loading