@uiwwsw/virtual-keyboard 0.5.4 → 0.5.6

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
@@ -678,7 +678,6 @@ function VirtualInput({
678
678
  }
679
679
  ctx.fillStyle = color;
680
680
  if (charPositionsRef.current.length !== internalValue.length + 1) {
681
- let currentX = x;
682
681
  const positions = [x];
683
682
  for (let i = 0;i < internalValue.length; i++) {
684
683
  const w = ctx.measureText(internalValue.slice(0, i + 1)).width;
@@ -911,10 +910,10 @@ function VirtualInput({
911
910
  }
912
911
  // src/components/Provider.tsx
913
912
  import {
914
- useState as useState5,
913
+ useState as useState6,
915
914
  useRef as useRef5,
916
915
  useCallback as useCallback6,
917
- useEffect as useEffect7
916
+ useEffect as useEffect8
918
917
  } from "react";
919
918
 
920
919
  // src/components/Keypad.tsx
@@ -1171,7 +1170,8 @@ function VirtualKeypad({
1171
1170
  shift,
1172
1171
  hangulMode,
1173
1172
  toggleShift,
1174
- toggleKorean
1173
+ toggleKorean,
1174
+ theme
1175
1175
  } = useVirtualInputContext();
1176
1176
  const canvasRef = useRef4(null);
1177
1177
  const containerRef = useRef4(null);
@@ -1199,44 +1199,50 @@ function VirtualKeypad({
1199
1199
  getTransformedValue,
1200
1200
  keyBoundsRef
1201
1201
  });
1202
- const drawKey = (ctx, key, isPressed, isActiveModifier, scale) => {
1203
- const r = 8 / scale;
1202
+ const drawKey = (ctx, key, isPressed, isActiveModifier, scale, currentTheme) => {
1203
+ const r = 10 / scale;
1204
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",
1205
+ const colors = currentTheme === "dark" ? {
1206
+ shadow: "#0f172a",
1207
+ keyFace: isPressed ? "#334155" : "#1e293b",
1208
+ text: "#f8fafc",
1209
+ actionFace: isPressed ? "#475569" : "#334155",
1210
+ activeModFace: isPressed ? "#f8fafc" : "#e2e8f0",
1211
+ activeModText: "#0f172a"
1212
+ } : {
1213
+ shadow: "#cbd5e1",
1214
+ keyFace: isPressed ? "#f1f5f9" : "#ffffff",
1215
+ text: "#1e293b",
1216
+ actionFace: isPressed ? "#e2e8f0" : "#f1f5f9",
1217
+ activeModFace: isPressed ? "#bfdbfe" : "#dbeafe",
1212
1218
  activeModText: "#1d4ed8"
1213
1219
  };
1220
+ let faceColor = colors.keyFace;
1221
+ let textColor = colors.text;
1214
1222
  if (key.isAction) {
1215
- colors.keyBg = colors.actionBg;
1223
+ faceColor = colors.actionFace;
1216
1224
  }
1217
1225
  if (isActiveModifier) {
1218
- colors.keyBg = colors.activeModBg;
1226
+ faceColor = colors.activeModFace;
1227
+ textColor = colors.activeModText;
1219
1228
  }
1220
- ctx.fillStyle = colors.keyShadow;
1221
- roundRect(ctx, x, y + 1 / scale, w, h, r);
1229
+ const depth = 4 / scale;
1230
+ const pressOffset = isPressed ? depth * 0.6 : 0;
1231
+ ctx.fillStyle = colors.shadow;
1232
+ roundRect(ctx, x, y + depth, w, h, r);
1222
1233
  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);
1234
+ ctx.fillStyle = faceColor;
1235
+ roundRect(ctx, x, y + pressOffset, w, h, r);
1227
1236
  ctx.fill();
1228
1237
  ctx.textAlign = "center";
1229
1238
  ctx.textBaseline = "middle";
1230
1239
  let fontSize = 20 / scale;
1231
1240
  if (key.label.length > 1)
1232
1241
  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);
1242
+ const fontFamily = currentTheme === "dark" ? "Inter, system-ui, sans-serif" : "Inter, system-ui, sans-serif";
1243
+ ctx.font = `500 ${fontSize}px "${fontFamily}"`;
1244
+ ctx.fillStyle = textColor;
1245
+ ctx.fillText(key.label, x + w / 2, y + h / 2 + pressOffset);
1240
1246
  };
1241
1247
  const roundRect = (ctx, x, y, w, h, r) => {
1242
1248
  if (w < 2 * r)
@@ -1271,7 +1277,11 @@ function VirtualKeypad({
1271
1277
  }
1272
1278
  const width = rect.width;
1273
1279
  const height = rect.height;
1274
- ctx.fillStyle = "#d1d5db";
1280
+ const bgColors = {
1281
+ dark: "#0f172a",
1282
+ light: "#e2e8f0"
1283
+ };
1284
+ ctx.fillStyle = bgColors[theme] || bgColors.light;
1275
1285
  ctx.fillRect(0, 0, width, height);
1276
1286
  const scale = viewport.scale;
1277
1287
  keyBoundsRef.current.forEach((key) => {
@@ -1284,9 +1294,9 @@ function VirtualKeypad({
1284
1294
  }
1285
1295
  }
1286
1296
  const isActiveModifier = key.value === "Shift" && shift || key.value === "HangulMode" && hangulMode;
1287
- drawKey(ctx, key, isPressed, isActiveModifier, scale);
1297
+ drawKey(ctx, key, isPressed, isActiveModifier, scale, theme);
1288
1298
  });
1289
- }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef]);
1299
+ }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef, theme]);
1290
1300
  useEffect4(() => {
1291
1301
  keyBoundsRef.current = [];
1292
1302
  }, [hangulMode, shift, keyBoundsRef]);
@@ -1326,7 +1336,7 @@ function VirtualKeypad({
1326
1336
  css: `
1327
1337
  .keypad-wrapper {
1328
1338
  position: fixed;
1329
- background-color: #d1d5db;
1339
+ background-color: var(--keypad-bg);
1330
1340
  border-radius: calc(18px / var(--scale-factor));
1331
1341
  box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1332
1342
  user-select: none;
@@ -1363,7 +1373,8 @@ function VirtualKeypad({
1363
1373
  width: viewport.width,
1364
1374
  top: Math.round(viewport.offsetTop + viewport.height - 200 / viewport.scale),
1365
1375
  height: Math.round(200 / viewport.scale),
1366
- "--scale-factor": viewport.scale
1376
+ "--scale-factor": viewport.scale,
1377
+ "--keypad-bg": theme === "dark" ? "#0f172a" : "#e2e8f0"
1367
1378
  },
1368
1379
  children: /* @__PURE__ */ jsxDEV3("canvas", {
1369
1380
  ref: canvasRef,
@@ -1537,21 +1548,49 @@ var useVisualViewport = () => {
1537
1548
  return viewport;
1538
1549
  };
1539
1550
 
1551
+ // src/hooks/useSystemTheme.ts
1552
+ import { useState as useState5, useEffect as useEffect7 } from "react";
1553
+ function useSystemTheme() {
1554
+ const [theme, setTheme] = useState5(() => {
1555
+ if (typeof window !== "undefined" && window.matchMedia) {
1556
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
1557
+ }
1558
+ return "light";
1559
+ });
1560
+ useEffect7(() => {
1561
+ if (typeof window === "undefined" || !window.matchMedia)
1562
+ return;
1563
+ const mq = window.matchMedia("(prefers-color-scheme: dark)");
1564
+ const handler = (e) => {
1565
+ setTheme(e.matches ? "dark" : "light");
1566
+ };
1567
+ mq.addEventListener("change", handler);
1568
+ return () => mq.removeEventListener("change", handler);
1569
+ }, []);
1570
+ return theme;
1571
+ }
1572
+
1540
1573
  // src/components/Provider.tsx
1541
1574
  import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
1542
1575
  function VirtualInputProvider({
1543
1576
  children,
1544
1577
  layout = qwerty_default,
1545
- defaultHangulMode = true
1578
+ defaultHangulMode = true,
1579
+ theme
1546
1580
  }) {
1547
1581
  const inputRef = useRef5(null);
1548
1582
  const sti = useRef5(setTimeout(() => null, 0));
1549
1583
  const isCompositionRef = useRef5(undefined);
1550
- const [focusId, setFocusId] = useState5();
1551
- const [shift, setShift] = useState5(false);
1584
+ const [focusId, setFocusId] = useState6();
1585
+ const [shift, setShift] = useState6(false);
1552
1586
  const [hangulMode, setHangulMode] = useStorage("virtual-keyboard-hangul-mode", defaultHangulMode);
1553
1587
  const viewport = useVisualViewport();
1554
- useEffect7(() => {
1588
+ const systemTheme = useSystemTheme();
1589
+ const effectiveTheme = theme ?? systemTheme;
1590
+ useEffect8(() => {
1591
+ console.log("[Provider] Theme Debug:", { prop: theme, system: systemTheme, effective: effectiveTheme });
1592
+ }, [theme, systemTheme, effectiveTheme]);
1593
+ useEffect8(() => {
1555
1594
  if (focusId) {
1556
1595
  document.body.style.paddingBottom = `${Math.round(200 / viewport.scale)}px`;
1557
1596
  } else {
@@ -1590,16 +1629,17 @@ function VirtualInputProvider({
1590
1629
  }, [setHangulMode]);
1591
1630
  return /* @__PURE__ */ jsxDEV4(VirtualInputContext.Provider, {
1592
1631
  value: {
1593
- inputRef,
1594
- isCompositionRef,
1595
- onFocus,
1596
- onBlur,
1597
1632
  focusId,
1598
- setHangulMode,
1599
1633
  hangulMode,
1600
1634
  shift,
1635
+ theme: effectiveTheme,
1636
+ onFocus,
1637
+ onBlur,
1638
+ setHangulMode,
1601
1639
  toggleShift,
1602
- toggleKorean
1640
+ toggleKorean,
1641
+ isCompositionRef,
1642
+ inputRef
1603
1643
  },
1604
1644
  children: [
1605
1645
  children,
@@ -9,6 +9,7 @@ interface VirtualInputContextValue {
9
9
  setHangulMode: Dispatch<SetStateAction<boolean>>;
10
10
  hangulMode: boolean;
11
11
  shift: boolean;
12
+ theme: "light" | "dark";
12
13
  toggleShift: () => void;
13
14
  toggleKorean: () => void;
14
15
  }
@@ -1,7 +1,8 @@
1
1
  import { type ReactNode } from "react";
2
2
  import { type KeypadLayout } from "./Keypad";
3
- export declare function VirtualInputProvider({ children, layout, defaultHangulMode, }: {
3
+ export declare function VirtualInputProvider({ children, layout, defaultHangulMode, theme, }: {
4
4
  layout?: KeypadLayout;
5
5
  children: ReactNode;
6
6
  defaultHangulMode?: boolean;
7
+ theme?: "light" | "dark";
7
8
  }): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1 @@
1
+ export declare function useSystemTheme(): "dark" | "light";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/virtual-keyboard",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",