|
| 1 | +# useDocumentPIP |
| 2 | +Hook to use Document PIP [(Document-Picture-in-Picture API)](https://developer.mozilla.org/en-US/docs/Web/API/Document_Picture-in-Picture_API). |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +```tsx |
| 7 | +export const UseDocumentPIP = () => { |
| 8 | + const [show, setShow] = useState(true); |
| 9 | + const [c, setC] = useState(0); |
| 10 | + const { isSupported, openPIP, PipWindow, closePIP } = useDocumentPIP({ |
| 11 | + onOpened: useCallback(() => setShow(false), []), |
| 12 | + onClose: useCallback(() => setShow(true), []) |
| 13 | + }); |
| 14 | + |
| 15 | + return <div> |
| 16 | + <p>Window Counter</p> |
| 17 | + <button onClick={() => setC(c => c + 1)}>{c}</button> |
| 18 | + <p>Supported: {isSupported ? 'Yes' : 'No'}</p> |
| 19 | + { |
| 20 | + show && |
| 21 | + <> |
| 22 | + <div> |
| 23 | + <button |
| 24 | + onClick={() => openPIP({ |
| 25 | + inheritCSS: true |
| 26 | + })} |
| 27 | + > |
| 28 | + Open PIP |
| 29 | + </button> |
| 30 | + </div> |
| 31 | + </> |
| 32 | + } |
| 33 | + <PipWindow> |
| 34 | + <div style={{display: 'flex', flexDirection: "column", alignItems: "center", width: "100%", gap: 20}}> |
| 35 | + <h4 style={{textAlign: "center"}}>PIP Window Counter</h4> |
| 36 | + <button onClick={() => setC(c => c + 1)}>{c}</button> |
| 37 | + <button onClick={closePIP}>Close</button> |
| 38 | + </div> |
| 39 | + </PipWindow> |
| 40 | + </div> |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +> The component uses _useDocumentPIP_ hook to show in a separate window the counter showed renderes by the component. |
| 45 | +
|
| 46 | + |
| 47 | +## API |
| 48 | + |
| 49 | +```tsx |
| 50 | +useDocumentPIP({ options: { inheritCSS, window: wind } = { window: { height: 300, width: 450 } }, onOpen, onOpened, onClose, onError }: UseDocumentPIPProps): UseDocumentPIPResult |
| 51 | +``` |
| 52 | + |
| 53 | +> ### Params |
| 54 | +> |
| 55 | +> - __param__: _UseDocumentPIPProps_ |
| 56 | +object |
| 57 | +> - __param.options?__: _DocumentPIPOptions_ |
| 58 | +object |
| 59 | +> - __param.options.inheritCSS?__: _boolean_ |
| 60 | +boolean that indicates if PIP window will inherit CSS from main window. |
| 61 | +> - __param.options.window?__: _Object_ |
| 62 | +object |
| 63 | +> - __param.options.width=450?__: _number_ |
| 64 | +number that indicates PIP window width. Default value is 450. |
| 65 | +> - __param.options.height=300?__: _number_ |
| 66 | +number that indicates PIP window height. Default value is 300. |
| 67 | +> - __param.onOpen?__: _()=>void_ |
| 68 | +function that will be executed on PIP opening. |
| 69 | +> - __param.onOpened?__: _(evt: DocumentPictureInPictureEvent)=>void_ |
| 70 | +function that will be executed when PIP is opened. |
| 71 | +> - __param.onClose?__: _(evt: PageTransitionEvent)=>void_ |
| 72 | +function that will be executed on PIP closing. |
| 73 | +> - __param.onError?__: _(err: unknown)=>void_ |
| 74 | +function that will be executed when error is throwing. |
| 75 | +> |
| 76 | +
|
| 77 | +> ### Returns |
| 78 | +> |
| 79 | +> __result__: _UseDocumentPIPResult_ |
| 80 | +> Object with four properties: |
| 81 | +> - __isSupported__: boolean that indicates if PIP is supported or not. |
| 82 | +> - __openPIP__: function to open PIP. |
| 83 | +> - __closePIP__: function to close PIP. |
| 84 | +> - __PipWindow__: Component that wraps the element to render in Document Picture in Picture. |
| 85 | +> |
0 commit comments