preact-missing-hooks
Version:
A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.
40 lines (39 loc) • 1.65 kB
TypeScript
import type { RefObject } from "preact";
export interface UseRefPrintOptions {
/**
* When true, the print flow is triggered so the user can choose
* "Save as PDF" in the native print dialog. Uses the same window.print() path.
*/
downloadAsPdf?: boolean;
/** Title for the print document (e.g. used when saving as PDF). */
documentTitle?: string;
}
export interface UseRefPrintReturn {
/** Triggers native print for the section bound to the ref (opens print dialog; only that section is printed via @media print). */
print: () => void;
}
/**
* A Preact hook that binds a ref to a printable section and provides a function
* to print only that section using the native window.print() and @media print CSS.
* When print() is called (or user presses Ctrl+P after focusing that section), only
* the ref section is visible in the print layout. User can then print or save as PDF.
*
* @param printRef - Ref to the DOM element (e.g. a div) that should be printed.
* @param options - Optional: downloadAsPdf hint, documentTitle for the print document.
* @returns Object with a print() function.
*
* @example
* ```tsx
* function Report() {
* const printRef = useRef<HTMLDivElement>(null);
* const { print } = useRefPrint(printRef, { documentTitle: 'Report', downloadAsPdf: true });
* return (
* <div>
* <div ref={printRef}>Content to print or save as PDF</div>
* <button onClick={print}>Print / Save as PDF</button>
* </div>
* );
* }
* ```
*/
export declare function useRefPrint(printRef: RefObject<HTMLElement | null>, options?: UseRefPrintOptions): UseRefPrintReturn;