|
| 1 | +import React from "react"; |
| 2 | +import styled from "@emotion/styled"; |
| 3 | +import RefreshSharpIcon from "@material-ui/icons/RefreshSharp"; |
| 4 | + |
| 5 | +export function ZoomControl({ |
| 6 | + scale, |
| 7 | + stepper, |
| 8 | + onChange, |
| 9 | +}: { |
| 10 | + onChange: (scale: number) => void; |
| 11 | + resetControl?: boolean; |
| 12 | + select?: boolean; |
| 13 | + stepper?: boolean; |
| 14 | + scale: number; |
| 15 | +}) { |
| 16 | + const [isEditing, setIsEditing] = React.useState(false); |
| 17 | + const displayScale = (scale * 100).toFixed(0); |
| 18 | + const mincontrol = 0; |
| 19 | + return ( |
| 20 | + <Wrapper> |
| 21 | + <Controls> |
| 22 | + <ControlsContainer> |
| 23 | + <Valuedisplay |
| 24 | + onMouseEnter={() => setIsEditing(true)} |
| 25 | + onClick={() => setIsEditing(true)} |
| 26 | + > |
| 27 | + {isEditing ? ( |
| 28 | + <StyledInput |
| 29 | + autoFocus |
| 30 | + min={mincontrol * 100} |
| 31 | + onChange={(e) => { |
| 32 | + const num = Number(e.target.value.replace(/[^0-9]/g, "")); |
| 33 | + const scae = num / 100; |
| 34 | + if (scae >= mincontrol) { |
| 35 | + onChange(scae); |
| 36 | + } |
| 37 | + }} |
| 38 | + onKeyDown={(e) => { |
| 39 | + if (e.key === "Enter") { |
| 40 | + setIsEditing(false); |
| 41 | + } |
| 42 | + }} |
| 43 | + onMouseLeave={() => setIsEditing(false)} |
| 44 | + onBlur={() => setIsEditing(false)} |
| 45 | + value={displayScale} |
| 46 | + defaultValue={displayScale} |
| 47 | + type="number" |
| 48 | + /> |
| 49 | + ) : ( |
| 50 | + <> |
| 51 | + <ReadonlyValue>{displayScale}</ReadonlyValue> |
| 52 | + <PercentText>%</PercentText> |
| 53 | + </> |
| 54 | + )} |
| 55 | + </Valuedisplay> |
| 56 | + {scale !== 1 && ( |
| 57 | + <RefreshSharpIcon |
| 58 | + onClick={() => onChange(1)} |
| 59 | + style={{ color: "white", fontSize: 18 }} |
| 60 | + /> |
| 61 | + )} |
| 62 | + </ControlsContainer> |
| 63 | + </Controls> |
| 64 | + </Wrapper> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +const Wrapper = styled.div` |
| 69 | + display: flex; |
| 70 | + justify-content: center; |
| 71 | + flex-direction: column; |
| 72 | + align-items: center; |
| 73 | + flex: none; |
| 74 | + gap: 10px; |
| 75 | + box-sizing: border-box; |
| 76 | + padding: 10px 24px; |
| 77 | +`; |
| 78 | + |
| 79 | +const Controls = styled.div` |
| 80 | + display: flex; |
| 81 | + justify-content: center; |
| 82 | + flex-direction: row; |
| 83 | + align-items: center; |
| 84 | + flex: none; |
| 85 | + gap: 10px; |
| 86 | + height: 24px; |
| 87 | + box-sizing: border-box; |
| 88 | + border-radius: 4px; |
| 89 | + padding: 4px; |
| 90 | + background-color: #252526; |
| 91 | + box-shadow: #25252650 0px 0px 0px 16px inset; |
| 92 | +`; |
| 93 | + |
| 94 | +const ControlsContainer = styled.div` |
| 95 | + display: flex; |
| 96 | + justify-content: center; |
| 97 | + flex-direction: row; |
| 98 | + align-items: center; |
| 99 | + flex: none; |
| 100 | + gap: 4px; |
| 101 | + height: 16px; |
| 102 | + box-sizing: border-box; |
| 103 | +`; |
| 104 | + |
| 105 | +const Valuedisplay = styled.div` |
| 106 | + display: flex; |
| 107 | + justify-content: center; |
| 108 | + flex-direction: row; |
| 109 | + align-items: center; |
| 110 | + flex: none; |
| 111 | + gap: 1px; |
| 112 | + height: 16px; |
| 113 | + box-sizing: border-box; |
| 114 | +`; |
| 115 | + |
| 116 | +const ReadonlyValue = styled.span` |
| 117 | + color: rgba(124, 124, 124, 1); |
| 118 | + text-overflow: ellipsis; |
| 119 | + font-size: 14px; |
| 120 | + font-family: Roboto, sans-serif; |
| 121 | + font-weight: 400; |
| 122 | + text-align: left; |
| 123 | +`; |
| 124 | + |
| 125 | +const PercentText = styled.span` |
| 126 | + color: rgba(124, 124, 124, 1); |
| 127 | + text-overflow: ellipsis; |
| 128 | + font-size: 14px; |
| 129 | + font-family: Roboto, sans-serif; |
| 130 | + font-weight: 400; |
| 131 | + text-align: left; |
| 132 | +`; |
| 133 | + |
| 134 | +const StyledInput = styled.input` |
| 135 | + border: none; |
| 136 | + outline: none; |
| 137 | + border-radius: 4px; |
| 138 | + color: white; |
| 139 | + background-color: transparent; |
| 140 | + width: 32px; |
| 141 | +
|
| 142 | + /* hide number arrow */ |
| 143 | + ::-webkit-inner-spin-button, |
| 144 | + ::-webkit-outer-spin-button { |
| 145 | + -webkit-appearance: none; |
| 146 | + margin: 0; |
| 147 | + } |
| 148 | +`; |
0 commit comments