react-ink-scripter 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1,29 +1,58 @@
1
1
  'use strict';
2
2
 
3
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
3
  var react = require('react');
5
4
  var reactDom = require('react-dom');
6
- var clsx = require('clsx');
7
5
  var jsxRuntime = require('react/jsx-runtime');
6
+ var clsx = require('clsx');
8
7
  var _extends = require('@babel/runtime/helpers/extends');
9
8
 
9
+ var IFrame = react.forwardRef(function (props, ref) {
10
+ var _iframeRef$current, _iframeRef$current$co, _iframeRef$current$co2, _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2;
11
+ var styleCss = props.styleCss,
12
+ className = props.className,
13
+ children = props.children;
14
+ var iframeRef = react.useRef(null);
15
+ var attachBody = iframeRef === null || iframeRef === void 0 ? void 0 : (_iframeRef$current = iframeRef.current) === null || _iframeRef$current === void 0 ? void 0 : (_iframeRef$current$co = _iframeRef$current.contentWindow) === null || _iframeRef$current$co === void 0 ? void 0 : (_iframeRef$current$co2 = _iframeRef$current$co.document) === null || _iframeRef$current$co2 === void 0 ? void 0 : _iframeRef$current$co2.body;
16
+ var attachHead = iframeRef === null || iframeRef === void 0 ? void 0 : (_iframeRef$current2 = iframeRef.current) === null || _iframeRef$current2 === void 0 ? void 0 : (_iframeRef$current2$c = _iframeRef$current2.contentWindow) === null || _iframeRef$current2$c === void 0 ? void 0 : (_iframeRef$current2$c2 = _iframeRef$current2$c.document) === null || _iframeRef$current2$c2 === void 0 ? void 0 : _iframeRef$current2$c2.head;
17
+ react.useImperativeHandle(ref, function () {
18
+ return iframeRef === null || iframeRef === void 0 ? void 0 : iframeRef.current;
19
+ }, []);
20
+ return jsxRuntime.jsxs("iframe", {
21
+ ref: iframeRef,
22
+ className: className,
23
+ children: [attachBody && reactDom.createPortal(children, attachBody), attachHead && reactDom.createPortal(jsxRuntime.jsx("style", {
24
+ children: styleCss
25
+ }), attachHead)]
26
+ });
27
+ });
28
+
10
29
  var PrinterFrame = function PrinterFrame(props) {
11
- var _node$contentWindow, _node$contentWindow$d, _node$contentWindow2, _node$contentWindow2$;
30
+ var _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2, _iframeRef$current3, _iframeRef$current3$c, _iframeRef$current3$c2;
12
31
  var styleCss = props.styleCss,
13
32
  open = props.open,
14
33
  className = props.className,
15
- children = props.children;
16
- var _useState = react.useState(),
17
- _useState2 = _slicedToArray(_useState, 2),
18
- node = _useState2[0],
19
- setNode = _useState2[1];
20
- var attachBody = node === null || node === void 0 ? void 0 : (_node$contentWindow = node.contentWindow) === null || _node$contentWindow === void 0 ? void 0 : (_node$contentWindow$d = _node$contentWindow.document) === null || _node$contentWindow$d === void 0 ? void 0 : _node$contentWindow$d.body;
21
- var attachHead = node === null || node === void 0 ? void 0 : (_node$contentWindow2 = node.contentWindow) === null || _node$contentWindow2 === void 0 ? void 0 : (_node$contentWindow2$ = _node$contentWindow2.document) === null || _node$contentWindow2$ === void 0 ? void 0 : _node$contentWindow2$.head;
34
+ children = props.children,
35
+ actionRef = props.actionRef;
36
+ var iframeRef = react.useRef(null);
37
+ react.useImperativeHandle(actionRef, function () {
38
+ return {
39
+ print: function print() {
40
+ var _iframeRef$current;
41
+ var cw = iframeRef === null || iframeRef === void 0 ? void 0 : (_iframeRef$current = iframeRef.current) === null || _iframeRef$current === void 0 ? void 0 : _iframeRef$current.contentWindow;
42
+ if (cw) {
43
+ cw.focus();
44
+ cw.print();
45
+ }
46
+ }
47
+ };
48
+ }, []);
49
+ var attachBody = iframeRef === null || iframeRef === void 0 ? void 0 : (_iframeRef$current2 = iframeRef.current) === null || _iframeRef$current2 === void 0 ? void 0 : (_iframeRef$current2$c = _iframeRef$current2.contentWindow) === null || _iframeRef$current2$c === void 0 ? void 0 : (_iframeRef$current2$c2 = _iframeRef$current2$c.document) === null || _iframeRef$current2$c2 === void 0 ? void 0 : _iframeRef$current2$c2.body;
50
+ var attachHead = iframeRef === null || iframeRef === void 0 ? void 0 : (_iframeRef$current3 = iframeRef.current) === null || _iframeRef$current3 === void 0 ? void 0 : (_iframeRef$current3$c = _iframeRef$current3.contentWindow) === null || _iframeRef$current3$c === void 0 ? void 0 : (_iframeRef$current3$c2 = _iframeRef$current3$c.document) === null || _iframeRef$current3$c2 === void 0 ? void 0 : _iframeRef$current3$c2.head;
22
51
  return jsxRuntime.jsxs("iframe", {
23
- ref: setNode,
52
+ ref: iframeRef,
24
53
  className: clsx("inks-iframe-root", !!open && "inks-iframe-root-open", className),
25
- children: [attachBody && reactDom.createPortal(children, attachBody), attachHead && reactDom.createPortal(jsxRuntime.jsxs("style", {
26
- children: ["@media print {\n .hidden-print {\n display: none !important;\n }\n }", styleCss]
54
+ children: [!!attachBody && reactDom.createPortal(children, attachBody), !!attachHead && reactDom.createPortal(jsxRuntime.jsxs("style", {
55
+ children: ["\n@media print {\n .hidden-print {\n display: none !important;\n }\n}\n ", styleCss]
27
56
  }), attachHead)]
28
57
  });
29
58
  };
@@ -227,5 +256,6 @@ var InkScripter = function InkScripter(props) {
227
256
  });
228
257
  };
229
258
 
259
+ exports.IFrame = IFrame;
230
260
  exports.InkScripter = InkScripter;
231
261
  exports.PrinterFrame = PrinterFrame;
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from "react";
2
+ export interface IFrameProps {
3
+ className?: string;
4
+ styleCss?: string;
5
+ children?: ReactNode;
6
+ }
7
+ export declare const IFrame: import("react").ForwardRefExoticComponent<IFrameProps & import("react").RefAttributes<HTMLIFrameElement>>;
@@ -1,9 +1,13 @@
1
- import { ReactNode } from "react";
1
+ import { ReactNode, Ref } from "react";
2
+ export interface PrinterActions {
3
+ print: () => void;
4
+ }
2
5
  export interface PrinterFrameProps {
3
6
  styleCss?: string;
4
7
  open?: boolean;
5
8
  className?: string;
6
9
  children?: ReactNode;
10
+ actionRef?: Ref<PrinterActions>;
7
11
  }
8
12
  export declare const PrinterFrame: (props: PrinterFrameProps) => JSX.Element;
9
13
  export default PrinterFrame;
@@ -1,2 +1,3 @@
1
+ export * from "./IFrame";
1
2
  export * from "./PrinterFrame";
2
3
  export * from "./InkScripter";
package/dist/esm/index.js CHANGED
@@ -1,19 +1,43 @@
1
1
  import { jsxs, jsx } from 'react/jsx-runtime';
2
- import { useState } from 'react';
2
+ import { forwardRef, useRef, useImperativeHandle } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
4
  import clsx from 'clsx';
5
5
 
6
+ const IFrame = forwardRef((props, ref) => {
7
+ const { styleCss, className, children } = props;
8
+ const iframeRef = useRef(null);
9
+ const attachBody = iframeRef?.current?.contentWindow?.document?.body;
10
+ const attachHead = iframeRef?.current?.contentWindow?.document?.head;
11
+ useImperativeHandle(ref, () => {
12
+ return iframeRef?.current;
13
+ }, []);
14
+ return (jsxs("iframe", { ref: iframeRef, className: className, children: [attachBody && createPortal(children, attachBody), attachHead && createPortal(jsx("style", { children: styleCss }), attachHead)] }));
15
+ });
16
+
6
17
  const PrinterFrame = (props) => {
7
- const { styleCss, open, className, children } = props;
8
- const [node, setNode] = useState();
9
- const attachBody = node?.contentWindow?.document?.body;
10
- const attachHead = node?.contentWindow?.document?.head;
11
- return (jsxs("iframe", { ref: setNode, className: clsx("inks-iframe-root", !!open && "inks-iframe-root-open", className), children: [attachBody && createPortal(children, attachBody), attachHead &&
12
- createPortal(jsxs("style", { children: [`@media print {
13
- .hidden-print {
14
- display: none !important;
15
- }
16
- }`, styleCss] }), attachHead)] }));
18
+ const { styleCss, open, className, children, actionRef } = props;
19
+ const iframeRef = useRef(null);
20
+ useImperativeHandle(actionRef, () => {
21
+ return {
22
+ print() {
23
+ const cw = iframeRef?.current?.contentWindow;
24
+ if (cw) {
25
+ cw.focus();
26
+ cw.print();
27
+ }
28
+ },
29
+ };
30
+ }, []);
31
+ const attachBody = iframeRef?.current?.contentWindow?.document?.body;
32
+ const attachHead = iframeRef?.current?.contentWindow?.document?.head;
33
+ return (jsxs("iframe", { ref: iframeRef, className: clsx("inks-iframe-root", !!open && "inks-iframe-root-open", className), children: [!!attachBody && createPortal(children, attachBody), !!attachHead &&
34
+ createPortal(jsxs("style", { children: [`
35
+ @media print {
36
+ .hidden-print {
37
+ display: none !important;
38
+ }
39
+ }
40
+ `, styleCss] }), attachHead)] }));
17
41
  };
18
42
 
19
43
  const Title = ({ value, id }) => {
@@ -105,4 +129,4 @@ const InkScripter = (props) => {
105
129
  }) }));
106
130
  };
107
131
 
108
- export { InkScripter, PrinterFrame };
132
+ export { IFrame, InkScripter, PrinterFrame };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-ink-scripter",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "a react component for generate page to print",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -12,7 +12,9 @@
12
12
  "keywords": [
13
13
  "react",
14
14
  "component",
15
- "printer"
15
+ "print",
16
+ "printer",
17
+ "iframe"
16
18
  ],
17
19
  "author": "lawrsp",
18
20
  "license": "MIT",
@@ -21,16 +23,14 @@
21
23
  },
22
24
  "homepage": "https://github.com/lawrsp/react-ink-scripter#readme",
23
25
  "dependencies": {
24
- "@babel/runtime": "^7.21.0",
25
- "clsx": "^1.2.1",
26
- "react": "^18.2.0",
27
- "react-dom": "^18.2.0"
26
+ "clsx": "^1.2.1"
28
27
  },
29
28
  "peerDependencies": {
30
29
  "@types/react": "^17.0.0 || ^18.0.0",
31
30
  "@types/react-dom": "^17.0.0 || ^18.0.0",
32
31
  "react": "^17.0.0 || ^18.0.0",
33
- "react-dom": "^17.0.0 || ^18.0.0"
32
+ "react-dom": "^17.0.0 || ^18.0.0",
33
+ "@babel/runtime": "^7.21.0"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@types/react": {