@uiwwsw/virtual-keyboard 0.5.3 → 0.5.5

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/index.js CHANGED
@@ -23,7 +23,7 @@ var ShadowWrapper = memo(({
23
23
  const Component = tagName;
24
24
  useEffect(() => {
25
25
  if (host && !shadowRoot) {
26
- const shadow = host.attachShadow({ mode: "closed" });
26
+ const shadow = host.attachShadow({ mode: "open" });
27
27
  setShadowRoot(shadow);
28
28
  }
29
29
  }, [host, shadowRoot]);
@@ -581,6 +581,7 @@ function VirtualInput({
581
581
  const [selection, setSelection] = useState2({ start: null, end: null });
582
582
  const [showCursor, setShowCursor] = useState2(true);
583
583
  const charPositionsRef = useRef([]);
584
+ const scrollXRef = useRef(0);
584
585
  useEffect2(() => {
585
586
  charPositionsRef.current = [];
586
587
  }, [internalValue]);
@@ -650,12 +651,18 @@ function VirtualInput({
650
651
  const height = rect.height;
651
652
  ctx.clearRect(0, 0, width, height);
652
653
  const computedStyle = window.getComputedStyle(container);
653
- const font = `${computedStyle.fontSize} ${computedStyle.fontFamily}`;
654
+ const font = `${computedStyle.fontStyle} ${computedStyle.fontVariant} ${computedStyle.fontWeight} ${computedStyle.fontSize} ${computedStyle.fontFamily}`;
654
655
  ctx.font = font;
655
656
  ctx.textBaseline = "middle";
656
657
  const fontSize = parseFloat(computedStyle.fontSize);
658
+ const paddingLeft = parseFloat(computedStyle.paddingLeft) || 0;
659
+ const paddingRight = parseFloat(computedStyle.paddingRight) || 0;
660
+ const borderLeft = parseFloat(computedStyle.borderLeftWidth) || 0;
661
+ const borderRight = parseFloat(computedStyle.borderRightWidth) || 0;
662
+ const color = computedStyle.color || "black";
663
+ const caretColor = (computedStyle.caretColor !== "auto" ? computedStyle.caretColor : color) || "black";
657
664
  const y = height / 2;
658
- const x = 0;
665
+ const x = borderLeft + paddingLeft;
659
666
  if (!internalValue && placeholder) {
660
667
  ctx.fillStyle = "#aaa";
661
668
  ctx.fillText(placeholder, x, y);
@@ -663,31 +670,52 @@ function VirtualInput({
663
670
  ctx.beginPath();
664
671
  ctx.moveTo(x, y - fontSize / 2);
665
672
  ctx.lineTo(x, y + fontSize / 2);
666
- ctx.strokeStyle = "black";
673
+ ctx.strokeStyle = caretColor;
667
674
  ctx.lineWidth = 1.5;
668
675
  ctx.stroke();
669
676
  }
670
677
  return;
671
678
  }
672
- ctx.fillStyle = "black";
679
+ ctx.fillStyle = color;
673
680
  if (charPositionsRef.current.length !== internalValue.length + 1) {
674
- let currentX = x;
675
681
  const positions = [x];
676
682
  for (let i = 0;i < internalValue.length; i++) {
677
- const w = ctx.measureText(internalValue[i]).width;
678
- currentX += w;
679
- positions.push(currentX);
683
+ const w = ctx.measureText(internalValue.slice(0, i + 1)).width;
684
+ positions.push(x + w);
680
685
  }
681
686
  charPositionsRef.current = positions;
682
687
  }
683
688
  const charX = charPositionsRef.current;
689
+ const caretPos = charX[caretIndex] ?? x;
690
+ const viewLeft = paddingLeft;
691
+ const viewRight = width - paddingRight - borderLeft - borderRight;
692
+ const safety = 2;
693
+ const totalTextWidth = (charX[charX.length - 1] ?? x) - x;
694
+ const availableWidth = viewRight - viewLeft;
695
+ if (totalTextWidth < availableWidth) {
696
+ scrollXRef.current = 0;
697
+ } else {
698
+ let s = scrollXRef.current;
699
+ if (caretPos - s > viewRight - safety) {
700
+ s = caretPos - viewRight + safety;
701
+ }
702
+ if (caretPos - s < viewLeft + safety) {
703
+ s = caretPos - viewLeft - safety;
704
+ }
705
+ const contentEnd = charX[charX.length - 1] ?? x;
706
+ const maxScroll = Math.max(0, contentEnd - viewRight + safety);
707
+ s = Math.max(0, Math.min(s, maxScroll));
708
+ scrollXRef.current = s;
709
+ }
710
+ ctx.save();
711
+ ctx.translate(-scrollXRef.current, 0);
684
712
  if (hasSelection && selection.start !== null && selection.end !== null) {
685
713
  const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
686
714
  const startX = charX[start] ?? x;
687
715
  const endX = charX[end] ?? charX[charX.length - 1];
688
716
  ctx.fillStyle = "#b4d5fe";
689
717
  ctx.fillRect(startX, 0, endX - startX, height);
690
- ctx.fillStyle = "black";
718
+ ctx.fillStyle = color;
691
719
  }
692
720
  ctx.fillText(internalValue, x, y);
693
721
  if (isFocused && showCursor && !hasSelection) {
@@ -695,10 +723,11 @@ function VirtualInput({
695
723
  ctx.beginPath();
696
724
  ctx.moveTo(caretX, y - fontSize / 2);
697
725
  ctx.lineTo(caretX, y + fontSize / 2);
698
- ctx.strokeStyle = "black";
726
+ ctx.strokeStyle = caretColor;
699
727
  ctx.lineWidth = 1.5;
700
728
  ctx.stroke();
701
729
  }
730
+ ctx.restore();
702
731
  }, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
703
732
  useEffect2(() => {
704
733
  let animationFrameId;
@@ -728,7 +757,7 @@ function VirtualInput({
728
757
  if (!canvas)
729
758
  return 0;
730
759
  const rect = canvas.getBoundingClientRect();
731
- const x = clientX - rect.left;
760
+ const x = clientX - rect.left + scrollXRef.current;
732
761
  if (charPositionsRef.current.length === internalValue.length + 1) {
733
762
  const charX = charPositionsRef.current;
734
763
  for (let i = 0;i < charX.length - 1; i++) {
@@ -865,7 +894,8 @@ function VirtualInput({
865
894
  outline: "none",
866
895
  font: "inherit",
867
896
  width: "100%",
868
- position: "relative"
897
+ position: "relative",
898
+ ...props.style
869
899
  },
870
900
  onFocus: () => onFocus(id),
871
901
  onBlur: (e) => onBlur(e),
@@ -1168,17 +1198,56 @@ function VirtualKeypad({
1168
1198
  getTransformedValue,
1169
1199
  keyBoundsRef
1170
1200
  });
1171
- const drawRoundedRect = (ctx, x, y, w, h, r) => {
1201
+ const drawKey = (ctx, key, isPressed, isActiveModifier, scale) => {
1202
+ const r = 8 / scale;
1203
+ const { x, y, w, h } = key;
1204
+ const colors = {
1205
+ bg: "#d1d5db",
1206
+ keyBg: isPressed ? "#E5E7EB" : "#FFFFFF",
1207
+ keyShadow: "#899499",
1208
+ text: "#111827",
1209
+ actionBg: isPressed ? "#cbd5e1" : "#e2e8f0",
1210
+ activeModBg: isPressed ? "#bfdbfe" : "#dbeafe",
1211
+ activeModText: "#1d4ed8"
1212
+ };
1213
+ if (key.isAction) {
1214
+ colors.keyBg = colors.actionBg;
1215
+ }
1216
+ if (isActiveModifier) {
1217
+ colors.keyBg = colors.activeModBg;
1218
+ }
1219
+ ctx.fillStyle = colors.keyShadow;
1220
+ roundRect(ctx, x, y + 1 / scale, w, h, r);
1221
+ ctx.fill();
1222
+ const pressOffset = isPressed ? 1 / scale : 0;
1223
+ const shadowOffset = isPressed ? 0 : 2 / scale;
1224
+ ctx.fillStyle = colors.keyBg;
1225
+ roundRect(ctx, x, y + pressOffset, w, h - shadowOffset, r);
1226
+ ctx.fill();
1227
+ ctx.textAlign = "center";
1228
+ ctx.textBaseline = "middle";
1229
+ let fontSize = 20 / scale;
1230
+ if (key.label.length > 1)
1231
+ fontSize = 16 / scale;
1232
+ ctx.font = `500 ${fontSize}px "Inter", "system-ui", sans-serif`;
1233
+ if (isActiveModifier) {
1234
+ ctx.fillStyle = colors.activeModText;
1235
+ } else {
1236
+ ctx.fillStyle = colors.text;
1237
+ }
1238
+ ctx.fillText(key.label, x + w / 2, y + (h - shadowOffset) / 2 + pressOffset + 1 / scale);
1239
+ };
1240
+ const roundRect = (ctx, x, y, w, h, r) => {
1241
+ if (w < 2 * r)
1242
+ r = w / 2;
1243
+ if (h < 2 * r)
1244
+ r = h / 2;
1172
1245
  ctx.beginPath();
1173
1246
  ctx.moveTo(x + r, y);
1174
- ctx.lineTo(x + w - r, y);
1175
- ctx.quadraticCurveTo(x + w, y, x + w, y + r);
1176
- ctx.lineTo(x + w, y + h - r);
1177
- ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
1178
- ctx.lineTo(x + r, y + h);
1179
- ctx.quadraticCurveTo(x, y + h, x, y + h - r);
1180
- ctx.lineTo(x, y + r);
1181
- ctx.quadraticCurveTo(x, y, x + r, y);
1247
+ ctx.arcTo(x + w, y, x + w, y + h, r);
1248
+ ctx.arcTo(x + w, y + h, x, y + h, r);
1249
+ ctx.arcTo(x, y + h, x, y, r);
1250
+ ctx.arcTo(x, y, x + w, y, r);
1182
1251
  ctx.closePath();
1183
1252
  };
1184
1253
  const draw = useCallback4(() => {
@@ -1201,7 +1270,7 @@ function VirtualKeypad({
1201
1270
  }
1202
1271
  const width = rect.width;
1203
1272
  const height = rect.height;
1204
- ctx.fillStyle = "#e8eaee";
1273
+ ctx.fillStyle = "#d1d5db";
1205
1274
  ctx.fillRect(0, 0, width, height);
1206
1275
  const scale = viewport.scale;
1207
1276
  keyBoundsRef.current.forEach((key) => {
@@ -1214,65 +1283,32 @@ function VirtualKeypad({
1214
1283
  }
1215
1284
  }
1216
1285
  const isActiveModifier = key.value === "Shift" && shift || key.value === "HangulMode" && hangulMode;
1217
- const radius = 12 / scale;
1218
- let drawX = key.x;
1219
- let drawY = key.y;
1220
- let drawW = key.w;
1221
- let drawH = key.h;
1222
- if (isPressed) {
1223
- const shrink = 2;
1224
- drawX += shrink;
1225
- drawY += shrink;
1226
- drawW -= shrink * 2;
1227
- drawH -= shrink * 2;
1228
- }
1229
- drawRoundedRect(ctx, drawX, drawY, drawW, drawH, radius);
1230
- if (isPressed) {
1231
- const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1232
- gradient.addColorStop(0, "rgba(248, 249, 251, 0.95)");
1233
- gradient.addColorStop(1, "#c9d2df");
1234
- ctx.fillStyle = gradient;
1235
- } else if (isActiveModifier) {
1236
- const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1237
- gradient.addColorStop(0, "#e0ecff");
1238
- gradient.addColorStop(1, "#99b7ff");
1239
- ctx.fillStyle = gradient;
1240
- } else if (key.isAction) {
1241
- const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1242
- gradient.addColorStop(0, "#f8fafc");
1243
- gradient.addColorStop(1, "#cbd5f5");
1244
- ctx.fillStyle = gradient;
1245
- } else {
1246
- const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1247
- gradient.addColorStop(0, "rgba(255, 255, 255, 0.95)");
1248
- gradient.addColorStop(1, "#dfe3eb");
1249
- ctx.fillStyle = gradient;
1250
- }
1251
- ctx.fill();
1252
- ctx.lineWidth = 1 / scale;
1253
- if (isActiveModifier) {
1254
- ctx.strokeStyle = "rgba(37, 99, 235, 0.25)";
1255
- } else if (key.isAction) {
1256
- ctx.strokeStyle = "rgba(59, 130, 246, 0.55)";
1257
- } else {
1258
- ctx.strokeStyle = "rgba(148, 163, 184, 0.7)";
1259
- }
1260
- ctx.stroke();
1261
- ctx.textAlign = "center";
1262
- ctx.textBaseline = "middle";
1263
- const fontSize = 18 / scale;
1264
- ctx.font = `600 ${fontSize}px sans-serif`;
1265
- if (isActiveModifier || key.isAction) {
1266
- ctx.fillStyle = "#1e3a8a";
1267
- } else {
1268
- ctx.fillStyle = "#1f2933";
1269
- }
1270
- ctx.fillText(key.label, drawX + drawW / 2, drawY + drawH / 2);
1286
+ drawKey(ctx, key, isPressed, isActiveModifier, scale);
1271
1287
  });
1272
1288
  }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef]);
1273
1289
  useEffect4(() => {
1274
1290
  keyBoundsRef.current = [];
1275
1291
  }, [hangulMode, shift, keyBoundsRef]);
1292
+ useEffect4(() => {
1293
+ const handleGlobalPointerDown = (e) => {
1294
+ if (!containerRef.current || !focusId)
1295
+ return;
1296
+ const path = e.composedPath();
1297
+ if (path.includes(containerRef.current)) {
1298
+ return;
1299
+ }
1300
+ const isInput = path.some((node) => {
1301
+ return node instanceof Element && node.getAttribute("data-virtual-input") === "true";
1302
+ });
1303
+ if (isInput)
1304
+ return;
1305
+ onBlur(true);
1306
+ };
1307
+ window.addEventListener("pointerdown", handleGlobalPointerDown, { capture: true });
1308
+ return () => {
1309
+ window.removeEventListener("pointerdown", handleGlobalPointerDown, { capture: true });
1310
+ };
1311
+ }, [focusId, onBlur]);
1276
1312
  useEffect4(() => {
1277
1313
  let handle;
1278
1314
  const loop = () => {
@@ -1289,7 +1325,7 @@ function VirtualKeypad({
1289
1325
  css: `
1290
1326
  .keypad-wrapper {
1291
1327
  position: fixed;
1292
- background-color: #e8eaee;
1328
+ background-color: #d1d5db;
1293
1329
  border-radius: calc(18px / var(--scale-factor));
1294
1330
  box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1295
1331
  user-select: none;
@@ -1529,7 +1565,12 @@ function VirtualInputProvider({
1529
1565
  setFocusId(id);
1530
1566
  };
1531
1567
  const onBlur = useCallback6((e) => {
1532
- if (e?.relatedTarget) {
1568
+ if (e === true) {
1569
+ setFocusId(undefined);
1570
+ isCompositionRef.current = false;
1571
+ return;
1572
+ }
1573
+ if (e && typeof e === "object" && "relatedTarget" in e && e.relatedTarget) {
1533
1574
  if (e.relatedTarget.getAttribute("data-virtual-input")) {
1534
1575
  return;
1535
1576
  }
@@ -4,7 +4,7 @@ interface VirtualInputContextValue {
4
4
  inputRef: RefObject<VirtualInputHandle | null>;
5
5
  isCompositionRef: RefObject<boolean | undefined>;
6
6
  onFocus: (id: string) => void;
7
- onBlur: (e?: React.FocusEvent) => void;
7
+ onBlur: (e?: React.FocusEvent | boolean) => void;
8
8
  focusId: string | undefined;
9
9
  setHangulMode: Dispatch<SetStateAction<boolean>>;
10
10
  hangulMode: boolean;
@@ -3,7 +3,7 @@ export interface VirtualInputHandle {
3
3
  handleKeyDown: (e: KeyboardEvent | React.KeyboardEvent) => void;
4
4
  scrollIntoView: () => void;
5
5
  }
6
- export interface VirtualInputProps {
6
+ export interface VirtualInputProps extends React.HTMLAttributes<HTMLDivElement> {
7
7
  value?: string;
8
8
  defaultValue?: string;
9
9
  placeholder?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/virtual-keyboard",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",