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

Demo: add print.css (and related things) #41

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Thumbs.db
.sass-cache
.env
.cache
.vscode

# Node modules and output
node_modules
Expand Down
25 changes: 14 additions & 11 deletions src/_includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
<link rel="icon" type="image/png" sizes="48x48" href="/images/logo-48.png">
<link rel="icon" type="image/png" sizes="192x192" href="/images/logo-192.png">

{# Add facility for pages to delare an array of non-critical javascript #}
{% if pageNonCriticalScripts %}
{% for item in pageNonCriticalScripts %}
<script src="{{ item }}?{{ assetHash }}" defer></script>
{% endfor %}
{% endif %}
<link rel="stylesheet" href="/css/vars.css" />
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/queries.css" />
<link rel="stylesheet" href="/css/print.css" media="print" />

{# Add facility for pages to delare an array of critical styles #}
{% if pageCriticalStyles %}
Expand All @@ -32,17 +30,22 @@
{% endfor %}
{% endif %}

{# Add facility for pages to declare an array of stylesheet paths #}
{# Add facility for pages to declare an array of non-critical stylesheet paths #}
{% if pageStylesheets %}
{% for item in pageStylesheets %}
<link rel="stylesheet" media="print" href="{{ item }}?{{ assetHash }}" onload="this.media='all'"/>
{% endfor %}
{% endif %}
<link rel="stylesheet" href="/css/vars.css" />
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/queries.css" />

{# Add facility for pages to delare an array of non-critical javascript #}
{% if pageNonCriticalScripts %}
{% for item in pageNonCriticalScripts %}
<script src="{{ item }}?{{ assetHash }}" defer></script>
{% endfor %}
{% endif %}

</head>
<body>
<body class="page--{{ page.fileSlug }} {{ pageBodyClasses | join(" ") if pageBodyClasses }}">
{% if isHome %}
{% include "partials/home-header.njk" %}
{% else %}
Expand Down
8 changes: 7 additions & 1 deletion src/_includes/layouts/paper.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---
pageNonCriticalScripts: [ '/js/papers/collapser.js' ]
pageBodyClasses: [
'page--is-paper'
]
pageNonCriticalScripts: [
'/js/papers/collapser.js',
'/js/papers/print.js'
]
---

{% extends "layouts/base.njk" %}
Expand Down
46 changes: 46 additions & 0 deletions src/css/print.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

@page {
siz: 210mm 297mm;
margin: 2cm;
}

/* ------------------------------ Papers ---------------------------- */

body.page--is-paper {
font-family: sans-serif;
}

body.page--is-paper header,
body.page--is-paper .download {
display: none;
}

body.page--is-paper details {
padding: 0;
background: none;
}

body.page--is-paper summary::marker {
content: none;
}
body.page--is-paper summary h2 {
font-size: calc(1.5rem + 1vw);
}

body.page--is-paper section {
page-break-inside : avoid;
}

body.page--is-paper section h2 {
page-break-before: always;
}

/* ------------------------------ Misc ---------------------------- */

/* Expand all collapsers */
.collapser > button:first-child {
display: none;
}
.collapser > .collapsed {
display: block;
}
37 changes: 37 additions & 0 deletions src/js/papers/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

(() => {

const details = [...document.querySelectorAll('details')];

window.addEventListener('beforeprint', event => {
details.forEach( element => element.setAttribute('open', '') );
});

window.addEventListener('afterprint', event => {
details.forEach( element => element.removeAttribute('open') );
});

// @TODO: Move this into eleventy plugin so we perform
// this at build time, not on the client
const content = document.querySelector('.content')
const { children } = content;
let section;
[ ...children ].forEach( child => {

if (child.tagName === 'H2') {
if (section)
content.append( section );
section = document.createElement('section');
section.classList.add('flow');
}

section.append( child );

});

if (section)
content.append( section );

content.classList.remove('flow');

})();