|
| 1 | +import React from "react"; |
| 2 | +import Draggable from "react-draggable"; |
| 3 | +import styled from "@emotion/styled"; |
| 4 | + |
| 5 | +interface DraggingBoxStyle { |
| 6 | + outline: string; |
| 7 | + outlineColor: string; |
| 8 | + outlineStyle: string; |
| 9 | + outlineWidth: number | string; |
| 10 | +} |
| 11 | + |
| 12 | +const unit = (u: string | number) => { |
| 13 | + if (typeof u === "number") { |
| 14 | + return `${u}px`; |
| 15 | + } |
| 16 | + return u; |
| 17 | +}; |
| 18 | + |
| 19 | +function PIP({ |
| 20 | + children, |
| 21 | + zIndex = 100, |
| 22 | + boxShadow = "1px 3px 3px 0 rgb(0 0 0 / 20%), 1px 3px 15px 2px rgb(0 0 0 / 20%)", |
| 23 | + borderRadius = "0.3rem", |
| 24 | + hoverCursor = "grab", |
| 25 | + draggingCursor = "grabbing", |
| 26 | + backgroundColor = "#242d36", |
| 27 | + draggingStyle = { |
| 28 | + outline: "-webkit-focus-ring-color auto 1px", |
| 29 | + outlineColor: "-webkit-focus-ring-color", |
| 30 | + outlineStyle: "auto", |
| 31 | + outlineWidth: 1, |
| 32 | + }, |
| 33 | +}: { |
| 34 | + children: React.ReactNode; |
| 35 | + /** |
| 36 | + * @default 100 |
| 37 | + */ |
| 38 | + zIndex?: number; |
| 39 | + /** |
| 40 | + * boxshadow css as string |
| 41 | + * @default "1px 3px 3px 0 rgb(0 0 0 / 20%), 1px 3px 15px 2px rgb(0 0 0 / 20%)" |
| 42 | + */ |
| 43 | + boxShadow?: string; |
| 44 | + /** |
| 45 | + * px in number or other unit (e.g. rem) as string |
| 46 | + * @default "0.3rem" |
| 47 | + */ |
| 48 | + borderRadius?: string | number; |
| 49 | + /** |
| 50 | + * @default grab |
| 51 | + */ |
| 52 | + hoverCursor?: string; |
| 53 | + /** |
| 54 | + * @default grabbing |
| 55 | + */ |
| 56 | + draggingCursor?: string; |
| 57 | + draggingStyle?: DraggingBoxStyle; |
| 58 | + /** |
| 59 | + * @default #242d36 |
| 60 | + */ |
| 61 | + backgroundColor?: string; |
| 62 | +}) { |
| 63 | + return ( |
| 64 | + <Draggable> |
| 65 | + <PipWindow |
| 66 | + borderRadius={borderRadius} |
| 67 | + boxShadow={boxShadow} |
| 68 | + zIndex={zIndex} |
| 69 | + hoverCursor={hoverCursor} |
| 70 | + draggingCursor={draggingCursor} |
| 71 | + draggingStyle={draggingStyle} |
| 72 | + backgroundColor={backgroundColor} |
| 73 | + > |
| 74 | + {children} |
| 75 | + </PipWindow> |
| 76 | + </Draggable> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +const PipWindow = styled.div<{ |
| 81 | + zIndex: number; |
| 82 | + boxShadow: string; |
| 83 | + borderRadius: string | number; |
| 84 | + hoverCursor: string; |
| 85 | + draggingCursor: string; |
| 86 | + draggingStyle: DraggingBoxStyle; |
| 87 | + backgroundColor: string; |
| 88 | +}>` |
| 89 | + background-color: ${(props) => props.backgroundColor}; |
| 90 | + z-index: ${(props) => props.zIndex}; |
| 91 | + position: fixed; |
| 92 | + color: rgb(248, 248, 249); |
| 93 | + box-sizing: content-box; |
| 94 | + box-shadow: ${(props) => props.boxShadow}; |
| 95 | + border-radius: ${(props) => unit(props.boxShadow)}; |
| 96 | +
|
| 97 | + :hover { |
| 98 | + cursor: ${(props) => props.hoverCursor}; |
| 99 | + } |
| 100 | +
|
| 101 | + :active { |
| 102 | + cursor: ${(props) => props.draggingCursor}; |
| 103 | + outline: ${(props) => props.draggingStyle.outline}; |
| 104 | + outline-color: ${(props) => props.draggingStyle.outlineColor}; |
| 105 | + outline-style: ${(props) => props.draggingStyle.outlineStyle}; |
| 106 | + outline-width: ${(props) => unit(props.draggingStyle.outlineWidth)}; |
| 107 | + } |
| 108 | +`; |
| 109 | + |
| 110 | +export default PIP; |
0 commit comments