@uiwwsw/virtual-keyboard 0.5.3 → 0.5.4

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,53 @@ 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
681
  let currentX = x;
675
682
  const positions = [x];
676
683
  for (let i = 0;i < internalValue.length; i++) {
677
- const w = ctx.measureText(internalValue[i]).width;
678
- currentX += w;
679
- positions.push(currentX);
684
+ const w = ctx.measureText(internalValue.slice(0, i + 1)).width;
685
+ positions.push(x + w);
680
686
  }
681
687
  charPositionsRef.current = positions;
682
688
  }
683
689
  const charX = charPositionsRef.current;
690
+ const caretPos = charX[caretIndex] ?? x;
691
+ const viewLeft = paddingLeft;
692
+ const viewRight = width - paddingRight - borderLeft - borderRight;
693
+ const safety = 2;
694
+ const totalTextWidth = (charX[charX.length - 1] ?? x) - x;
695
+ const availableWidth = viewRight - viewLeft;
696
+ if (totalTextWidth < availableWidth) {
697
+ scrollXRef.current = 0;
698
+ } else {
699
+ let s = scrollXRef.current;
700
+ if (caretPos - s > viewRight - safety) {
701
+ s = caretPos - viewRight + safety;
702
+ }
703
+ if (caretPos - s < viewLeft + safety) {
704
+ s = caretPos - viewLeft - safety;
705
+ }
706
+ const contentEnd = charX[charX.length - 1] ?? x;
707
+ const maxScroll = Math.max(0, contentEnd - viewRight + safety);
708
+ s = Math.max(0, Math.min(s, maxScroll));
709
+ scrollXRef.current = s;
710
+ }
711
+ ctx.save();
712
+ ctx.translate(-scrollXRef.current, 0);
684
713
  if (hasSelection && selection.start !== null && selection.end !== null) {
685
714
  const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
686
715
  const startX = charX[start] ?? x;
687
716
  const endX = charX[end] ?? charX[charX.length - 1];
688
717
  ctx.fillStyle = "#b4d5fe";
689
718
  ctx.fillRect(startX, 0, endX - startX, height);
690
- ctx.fillStyle = "black";
719
+ ctx.fillStyle = color;
691
720
  }
692
721
  ctx.fillText(internalValue, x, y);
693
722
  if (isFocused && showCursor && !hasSelection) {
@@ -695,10 +724,11 @@ function VirtualInput({
695
724
  ctx.beginPath();
696
725
  ctx.moveTo(caretX, y - fontSize / 2);
697
726
  ctx.lineTo(caretX, y + fontSize / 2);
698
- ctx.strokeStyle = "black";
727
+ ctx.strokeStyle = caretColor;
699
728
  ctx.lineWidth = 1.5;
700
729
  ctx.stroke();
701
730
  }
731
+ ctx.restore();
702
732
  }, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
703
733
  useEffect2(() => {
704
734
  let animationFrameId;
@@ -728,7 +758,7 @@ function VirtualInput({
728
758
  if (!canvas)
729
759
  return 0;
730
760
  const rect = canvas.getBoundingClientRect();
731
- const x = clientX - rect.left;
761
+ const x = clientX - rect.left + scrollXRef.current;
732
762
  if (charPositionsRef.current.length === internalValue.length + 1) {
733
763
  const charX = charPositionsRef.current;
734
764
  for (let i = 0;i < charX.length - 1; i++) {
@@ -865,7 +895,8 @@ function VirtualInput({
865
895
  outline: "none",
866
896
  font: "inherit",
867
897
  width: "100%",
868
- position: "relative"
898
+ position: "relative",
899
+ ...props.style
869
900
  },
870
901
  onFocus: () => onFocus(id),
871
902
  onBlur: (e) => onBlur(e),
@@ -1168,17 +1199,56 @@ function VirtualKeypad({
1168
1199
  getTransformedValue,
1169
1200
  keyBoundsRef
1170
1201
  });
1171
- const drawRoundedRect = (ctx, x, y, w, h, r) => {
1202
+ const drawKey = (ctx, key, isPressed, isActiveModifier, scale) => {
1203
+ const r = 8 / scale;
1204
+ const { x, y, w, h } = key;
1205
+ const colors = {
1206
+ bg: "#d1d5db",
1207
+ keyBg: isPressed ? "#E5E7EB" : "#FFFFFF",
1208
+ keyShadow: "#899499",
1209
+ text: "#111827",
1210
+ actionBg: isPressed ? "#cbd5e1" : "#e2e8f0",
1211
+ activeModBg: isPressed ? "#bfdbfe" : "#dbeafe",
1212
+ activeModText: "#1d4ed8"
1213
+ };
1214
+ if (key.isAction) {
1215
+ colors.keyBg = colors.actionBg;
1216
+ }
1217
+ if (isActiveModifier) {
1218
+ colors.keyBg = colors.activeModBg;
1219
+ }
1220
+ ctx.fillStyle = colors.keyShadow;
1221
+ roundRect(ctx, x, y + 1 / scale, w, h, r);
1222
+ ctx.fill();
1223
+ const pressOffset = isPressed ? 1 / scale : 0;
1224
+ const shadowOffset = isPressed ? 0 : 2 / scale;
1225
+ ctx.fillStyle = colors.keyBg;
1226
+ roundRect(ctx, x, y + pressOffset, w, h - shadowOffset, r);
1227
+ ctx.fill();
1228
+ ctx.textAlign = "center";
1229
+ ctx.textBaseline = "middle";
1230
+ let fontSize = 20 / scale;
1231
+ if (key.label.length > 1)
1232
+ fontSize = 16 / scale;
1233
+ ctx.font = `500 ${fontSize}px "Inter", "system-ui", sans-serif`;
1234
+ if (isActiveModifier) {
1235
+ ctx.fillStyle = colors.activeModText;
1236
+ } else {
1237
+ ctx.fillStyle = colors.text;
1238
+ }
1239
+ ctx.fillText(key.label, x + w / 2, y + (h - shadowOffset) / 2 + pressOffset + 1 / scale);
1240
+ };
1241
+ const roundRect = (ctx, x, y, w, h, r) => {
1242
+ if (w < 2 * r)
1243
+ r = w / 2;
1244
+ if (h < 2 * r)
1245
+ r = h / 2;
1172
1246
  ctx.beginPath();
1173
1247
  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);
1248
+ ctx.arcTo(x + w, y, x + w, y + h, r);
1249
+ ctx.arcTo(x + w, y + h, x, y + h, r);
1250
+ ctx.arcTo(x, y + h, x, y, r);
1251
+ ctx.arcTo(x, y, x + w, y, r);
1182
1252
  ctx.closePath();
1183
1253
  };
1184
1254
  const draw = useCallback4(() => {
@@ -1201,7 +1271,7 @@ function VirtualKeypad({
1201
1271
  }
1202
1272
  const width = rect.width;
1203
1273
  const height = rect.height;
1204
- ctx.fillStyle = "#e8eaee";
1274
+ ctx.fillStyle = "#d1d5db";
1205
1275
  ctx.fillRect(0, 0, width, height);
1206
1276
  const scale = viewport.scale;
1207
1277
  keyBoundsRef.current.forEach((key) => {
@@ -1214,65 +1284,32 @@ function VirtualKeypad({
1214
1284
  }
1215
1285
  }
1216
1286
  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);
1287
+ drawKey(ctx, key, isPressed, isActiveModifier, scale);
1271
1288
  });
1272
1289
  }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef]);
1273
1290
  useEffect4(() => {
1274
1291
  keyBoundsRef.current = [];
1275
1292
  }, [hangulMode, shift, keyBoundsRef]);
1293
+ useEffect4(() => {
1294
+ const handleGlobalPointerDown = (e) => {
1295
+ if (!containerRef.current || !focusId)
1296
+ return;
1297
+ const path = e.composedPath();
1298
+ if (path.includes(containerRef.current)) {
1299
+ return;
1300
+ }
1301
+ const isInput = path.some((node) => {
1302
+ return node instanceof Element && node.getAttribute("data-virtual-input") === "true";
1303
+ });
1304
+ if (isInput)
1305
+ return;
1306
+ onBlur(true);
1307
+ };
1308
+ window.addEventListener("pointerdown", handleGlobalPointerDown, { capture: true });
1309
+ return () => {
1310
+ window.removeEventListener("pointerdown", handleGlobalPointerDown, { capture: true });
1311
+ };
1312
+ }, [focusId, onBlur]);
1276
1313
  useEffect4(() => {
1277
1314
  let handle;
1278
1315
  const loop = () => {
@@ -1289,7 +1326,7 @@ function VirtualKeypad({
1289
1326
  css: `
1290
1327
  .keypad-wrapper {
1291
1328
  position: fixed;
1292
- background-color: #e8eaee;
1329
+ background-color: #d1d5db;
1293
1330
  border-radius: calc(18px / var(--scale-factor));
1294
1331
  box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1295
1332
  user-select: none;
@@ -1529,7 +1566,12 @@ function VirtualInputProvider({
1529
1566
  setFocusId(id);
1530
1567
  };
1531
1568
  const onBlur = useCallback6((e) => {
1532
- if (e?.relatedTarget) {
1569
+ if (e === true) {
1570
+ setFocusId(undefined);
1571
+ isCompositionRef.current = false;
1572
+ return;
1573
+ }
1574
+ if (e && typeof e === "object" && "relatedTarget" in e && e.relatedTarget) {
1533
1575
  if (e.relatedTarget.getAttribute("data-virtual-input")) {
1534
1576
  return;
1535
1577
  }
@@ -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.4",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",