Files
backnotprop__plannotator/packages/ui/hooks/usePrintMode.ts
Stacey Haffner a0a6edd323 feat: print support with export menu integration and keyboard shortcut (#420)
* feat: add print stylesheet for white paper output

- Create packages/ui/styles/print.css with @media print rules
- White background and black text for paper output
- Monochrome code blocks for readability on white paper
- Hide UI chrome (annotations, toolbars, sidebars) during print
- Proper typography for headers, code, tables, lists, blockquotes
- Page break rules to avoid orphaned content
- Support for diagrams (Mermaid, Graphviz) in print
- Import print.css in packages/ui/theme.css

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* feat: add Print button to toolbar

- Add Print button next to Export button in plan editor toolbar
- Triggers window.print() for native browser print dialog
- Shows printer icon + 'Print' label (icon-only on mobile)
- Tooltip: 'Print plan (Ctrl+P)'
- Uses muted button styling consistent with other toolbar buttons

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: rewrite print.css selectors to match real DOM structure

Code review found the original selectors used class names that don't
exist in the actual component tree. Fixed:

- Target header.sticky / header.h-12 (not generic 'header')
- Target aside elements directly (SidebarContainer + AnnotationPanel)
- Target .annotation-toolbar (portalled floating toolbar)
- Target .fixed overlays (modals, export dialog)
- Target article element (Viewer renders <article>, not <main>)
- Target .bg-grid for background pattern removal
- Flatten .h-screen height for print flow
- Simplified hljs monochrome rule to [class*='hljs']

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: print stylesheet — hide all toolbar UI, fix code block colors

Issues fixed from user testing:
- Header toolbar (Export/Print/Settings) now hidden via 'header' selector
- AnnotationToolstrip (Select/Markup) hidden via '.flex-wrap'
- Action buttons (Images/Comment/Copy) hidden via '.float-right'
- Code block copy buttons hidden via '.group > button.absolute'
- Fenced code blocks: override github-dark.css hljs theme to monochrome
  with light background (#f5f5f5) and dark text (#1a1a1a)
- Inline code: solid light gray background with visible border
- All text forced to solid black (override theme muted/foreground vars)
- hljs span elements explicitly overridden for print

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: aggressive code block print overrides for dark theme

- Override CSS custom properties (--muted, --foreground, etc.) in print
- Use both background AND background-color on pre/code elements
- Target pre[class], code[class], code.hljs with higher specificity
- Explicitly list every hljs- class for monochrome override
- Ensures github-dark.css hljs theme is fully overridden in print

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: use JS beforeprint/afterprint class for code block print styling

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: add visibilitychange fallback for Firefox print cleanup

Firefox may not fire afterprint when print preview is closed without
printing, leaving the .plannotator-print class stuck on <html>. This
adds a visibilitychange listener that removes the class when the user
returns to the page, plus cleanup on unmount.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* refactor: extract usePrintMode hook, replace brittle CSS selectors with data attributes

- Extract print event listeners from App.tsx into packages/ui/hooks/usePrintMode.ts
- Add data-print-region attributes to layout elements (root, content, document, article)
- Add data-print-hide attribute to Viewer action buttons
- Replace .h-screen, .flex-1.flex.overflow-hidden, .bg-card, .bg-grid, .float-right,
  .flex-wrap selectors with stable data-attribute and semantic element selectors
- Follows existing hook patterns (useIsMobile, useDismissOnOutsideAndEscape, etc.)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: hide annotation toolstrip and repo badges in print output

Added data-print-hide to the AnnotationToolstrip wrapper and the
repo/branch badges div inside the article.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: collapse top whitespace gap in print output

Zero out article padding, add margin/padding reset to data-print-hide
elements, and collapse first h1 top margin for a tight print layout.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* feat: move print button into export menu and add Ctrl/Cmd+P shortcut

* chore: remove accidentally introduced annotate command files

Remove apps/hook/commands/annotate.md and apps/opencode-plugin/commands/annotate.md
that were unintentionally added in the print-styling PR.

For provenance purposes, this commit was AI assisted.

---------

Co-authored-by: Yecats <Yecats@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Michael Ramos <mdramos8@gmail.com>
2026-03-29 11:04:32 -07:00

28 lines
1.2 KiB
TypeScript

import { useEffect } from 'react';
/**
* Manages print mode by toggling 'plannotator-print' class on <html>.
* Includes visibilitychange fallback for Firefox, which may not fire
* afterprint when print preview is closed without printing.
*/
export function usePrintMode() {
useEffect(() => {
const onBeforePrint = () => document.documentElement.classList.add('plannotator-print');
const onAfterPrint = () => document.documentElement.classList.remove('plannotator-print');
const onVisibilityChange = () => {
if (!document.hidden && document.documentElement.classList.contains('plannotator-print')) {
document.documentElement.classList.remove('plannotator-print');
}
};
window.addEventListener('beforeprint', onBeforePrint);
window.addEventListener('afterprint', onAfterPrint);
document.addEventListener('visibilitychange', onVisibilityChange);
return () => {
window.removeEventListener('beforeprint', onBeforePrint);
window.removeEventListener('afterprint', onAfterPrint);
document.removeEventListener('visibilitychange', onVisibilityChange);
document.documentElement.classList.remove('plannotator-print');
};
}, []);
}