@uiwwsw/virtual-keyboard 0.5.5 → 0.5.7

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
@@ -13,22 +13,23 @@ import {
13
13
  import { useEffect, useState, memo } from "react";
14
14
  import ReactDOM from "react-dom";
15
15
  import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
16
- var ShadowWrapper = memo(({
17
- children,
18
- css,
19
- tagName
20
- }) => {
16
+ var ShadowWrapper = memo((props) => {
21
17
  const [host, setHost] = useState(null);
22
18
  const [shadowRoot, setShadowRoot] = useState(null);
19
+ const { tagName, css, children, hostRef, ...rest } = props;
23
20
  const Component = tagName;
24
21
  useEffect(() => {
25
22
  if (host && !shadowRoot) {
26
23
  const shadow = host.attachShadow({ mode: "open" });
27
24
  setShadowRoot(shadow);
28
25
  }
29
- }, [host, shadowRoot]);
26
+ if (hostRef) {
27
+ hostRef.current = host;
28
+ }
29
+ }, [host, shadowRoot, hostRef]);
30
30
  return /* @__PURE__ */ jsxDEV(Component, {
31
31
  ref: setHost,
32
+ ...rest,
32
33
  children: shadowRoot && ReactDOM.createPortal(/* @__PURE__ */ jsxDEV(Fragment, {
33
34
  children: [
34
35
  css && /* @__PURE__ */ jsxDEV("style", {
@@ -875,53 +876,56 @@ function VirtualInput({
875
876
  }, [isFocused, handleKeyDown, inputRef]);
876
877
  return /* @__PURE__ */ jsxDEV2(ShadowWrapper, {
877
878
  tagName: "virtual-input-canvas",
879
+ hostRef: containerRef,
880
+ id,
881
+ className: props.className,
882
+ role: "textbox",
883
+ tabIndex: 0,
884
+ ...props,
885
+ style: {
886
+ display: "inline-block",
887
+ minHeight: "1.5em",
888
+ height: "1.5em",
889
+ outline: "none",
890
+ font: "inherit",
891
+ width: "100%",
892
+ position: "relative",
893
+ cursor: "text",
894
+ ...props.style
895
+ },
896
+ onFocus: () => onFocus(id, containerRef.current),
897
+ onBlur: (e) => onBlur(e),
898
+ onKeyDown: handleKeyDown,
899
+ onClick: handleCanvasClick,
900
+ "data-virtual-input": "true",
878
901
  css: `
902
+ :host {
903
+ display: inline-block;
904
+ position: relative;
905
+ }
879
906
  canvas {
880
907
  width: 100%;
881
908
  height: 100%;
882
- cursor: text;
883
909
  display: block;
884
910
  }
885
911
  `,
886
- children: /* @__PURE__ */ jsxDEV2("div", {
887
- ref: containerRef,
888
- ...props,
889
- role: "textbox",
890
- tabIndex: 0,
891
- style: {
892
- minHeight: "1.5em",
893
- height: "1.5em",
894
- outline: "none",
895
- font: "inherit",
896
- width: "100%",
897
- position: "relative",
898
- ...props.style
899
- },
900
- onFocus: () => onFocus(id),
901
- onBlur: (e) => onBlur(e),
902
- onKeyDown: handleKeyDown,
903
- onClick: handleCanvasClick,
904
- "data-virtual-input": "true",
905
- children: /* @__PURE__ */ jsxDEV2("canvas", {
906
- ref: canvasRef
907
- }, undefined, false, undefined, this)
912
+ children: /* @__PURE__ */ jsxDEV2("canvas", {
913
+ ref: canvasRef,
914
+ style: { width: "100%", height: "100%", display: "block" }
908
915
  }, undefined, false, undefined, this)
909
916
  }, undefined, false, undefined, this);
910
917
  }
911
918
  // src/components/Provider.tsx
912
919
  import {
913
- useState as useState5,
920
+ useState as useState6,
914
921
  useRef as useRef5,
915
922
  useCallback as useCallback6,
916
- useEffect as useEffect7
923
+ useEffect as useEffect8
917
924
  } from "react";
918
925
 
919
926
  // src/components/Keypad.tsx
920
- import {
921
- useCallback as useCallback4,
922
- useEffect as useEffect4,
923
- useRef as useRef4
924
- } from "react";
927
+ import { useRef as useRef4, useEffect as useEffect4, useCallback as useCallback4 } from "react";
928
+ import { createPortal } from "react-dom";
925
929
 
926
930
  // src/utils/isMobileAgent.ts
927
931
  function isMobileAgent(userAgent = navigator.userAgent) {
@@ -1170,7 +1174,8 @@ function VirtualKeypad({
1170
1174
  shift,
1171
1175
  hangulMode,
1172
1176
  toggleShift,
1173
- toggleKorean
1177
+ toggleKorean,
1178
+ theme
1174
1179
  } = useVirtualInputContext();
1175
1180
  const canvasRef = useRef4(null);
1176
1181
  const containerRef = useRef4(null);
@@ -1198,44 +1203,50 @@ function VirtualKeypad({
1198
1203
  getTransformedValue,
1199
1204
  keyBoundsRef
1200
1205
  });
1201
- const drawKey = (ctx, key, isPressed, isActiveModifier, scale) => {
1202
- const r = 8 / scale;
1206
+ const drawKey = (ctx, key, isPressed, isActiveModifier, scale, currentTheme) => {
1207
+ const r = 10 / scale;
1203
1208
  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",
1209
+ const colors = currentTheme === "dark" ? {
1210
+ shadow: "#0f172a",
1211
+ keyFace: isPressed ? "#334155" : "#1e293b",
1212
+ text: "#f8fafc",
1213
+ actionFace: isPressed ? "#475569" : "#334155",
1214
+ activeModFace: isPressed ? "#f8fafc" : "#e2e8f0",
1215
+ activeModText: "#0f172a"
1216
+ } : {
1217
+ shadow: "#cbd5e1",
1218
+ keyFace: isPressed ? "#f1f5f9" : "#ffffff",
1219
+ text: "#1e293b",
1220
+ actionFace: isPressed ? "#e2e8f0" : "#f1f5f9",
1221
+ activeModFace: isPressed ? "#bfdbfe" : "#dbeafe",
1211
1222
  activeModText: "#1d4ed8"
1212
1223
  };
1224
+ let faceColor = colors.keyFace;
1225
+ let textColor = colors.text;
1213
1226
  if (key.isAction) {
1214
- colors.keyBg = colors.actionBg;
1227
+ faceColor = colors.actionFace;
1215
1228
  }
1216
1229
  if (isActiveModifier) {
1217
- colors.keyBg = colors.activeModBg;
1230
+ faceColor = colors.activeModFace;
1231
+ textColor = colors.activeModText;
1218
1232
  }
1219
- ctx.fillStyle = colors.keyShadow;
1220
- roundRect(ctx, x, y + 1 / scale, w, h, r);
1233
+ const depth = 4 / scale;
1234
+ const pressOffset = isPressed ? depth * 0.6 : 0;
1235
+ ctx.fillStyle = colors.shadow;
1236
+ roundRect(ctx, x, y + depth, w, h, r);
1221
1237
  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);
1238
+ ctx.fillStyle = faceColor;
1239
+ roundRect(ctx, x, y + pressOffset, w, h, r);
1226
1240
  ctx.fill();
1227
1241
  ctx.textAlign = "center";
1228
1242
  ctx.textBaseline = "middle";
1229
1243
  let fontSize = 20 / scale;
1230
1244
  if (key.label.length > 1)
1231
1245
  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);
1246
+ const fontFamily = currentTheme === "dark" ? "Inter, system-ui, sans-serif" : "Inter, system-ui, sans-serif";
1247
+ ctx.font = `500 ${fontSize}px "${fontFamily}"`;
1248
+ ctx.fillStyle = textColor;
1249
+ ctx.fillText(key.label, x + w / 2, y + h / 2 + pressOffset);
1239
1250
  };
1240
1251
  const roundRect = (ctx, x, y, w, h, r) => {
1241
1252
  if (w < 2 * r)
@@ -1270,7 +1281,11 @@ function VirtualKeypad({
1270
1281
  }
1271
1282
  const width = rect.width;
1272
1283
  const height = rect.height;
1273
- ctx.fillStyle = "#d1d5db";
1284
+ const bgColors = {
1285
+ dark: "#0f172a",
1286
+ light: "#e2e8f0"
1287
+ };
1288
+ ctx.fillStyle = bgColors[theme] || bgColors.light;
1274
1289
  ctx.fillRect(0, 0, width, height);
1275
1290
  const scale = viewport.scale;
1276
1291
  keyBoundsRef.current.forEach((key) => {
@@ -1283,9 +1298,9 @@ function VirtualKeypad({
1283
1298
  }
1284
1299
  }
1285
1300
  const isActiveModifier = key.value === "Shift" && shift || key.value === "HangulMode" && hangulMode;
1286
- drawKey(ctx, key, isPressed, isActiveModifier, scale);
1301
+ drawKey(ctx, key, isPressed, isActiveModifier, scale, theme);
1287
1302
  });
1288
- }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef]);
1303
+ }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef, theme]);
1289
1304
  useEffect4(() => {
1290
1305
  keyBoundsRef.current = [];
1291
1306
  }, [hangulMode, shift, keyBoundsRef]);
@@ -1320,60 +1335,59 @@ function VirtualKeypad({
1320
1335
  }, [draw]);
1321
1336
  if (!focusId || !isMobileAgent())
1322
1337
  return null;
1323
- return /* @__PURE__ */ jsxDEV3(ShadowWrapper, {
1338
+ return createPortal(/* @__PURE__ */ jsxDEV3(ShadowWrapper, {
1324
1339
  tagName: "virtual-keypad-canvas",
1340
+ hostRef: containerRef,
1341
+ style: {
1342
+ position: "fixed",
1343
+ bottom: 0,
1344
+ left: 0,
1345
+ right: 0,
1346
+ margin: "0 auto",
1347
+ width: viewport.width,
1348
+ height: Math.round(200 / viewport.scale),
1349
+ backgroundColor: theme === "dark" ? "#0f172a" : "#e2e8f0",
1350
+ borderRadius: `calc(18px / ${viewport.scale}) calc(18px / ${viewport.scale}) 0 0`,
1351
+ boxShadow: `0 calc(-6px / ${viewport.scale}) calc(30px / ${viewport.scale}) rgba(15, 23, 42, 0.2)`,
1352
+ zIndex: 9999,
1353
+ overflow: "hidden",
1354
+ userSelect: "none",
1355
+ touchAction: "none",
1356
+ "--scale-factor": viewport.scale,
1357
+ "--keypad-bg": theme === "dark" ? "#0f172a" : "#e2e8f0"
1358
+ },
1359
+ onFocus: () => {
1360
+ if (focusId)
1361
+ onFocus(focusId);
1362
+ },
1363
+ onBlur,
1364
+ onContextMenu: (e) => e.preventDefault(),
1365
+ onPointerDown: (e) => e.preventDefault(),
1366
+ onClickCapture: (e) => {
1367
+ e.preventDefault();
1368
+ e.stopPropagation();
1369
+ },
1325
1370
  css: `
1326
- .keypad-wrapper {
1327
- position: fixed;
1328
- background-color: #d1d5db;
1329
- border-radius: calc(18px / var(--scale-factor));
1330
- box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1331
- user-select: none;
1332
- -webkit-user-select: none;
1333
- touch-action: none; /* Critical for iOS */
1334
- -webkit-touch-callout: none; /* Disable magnifier/menu */
1335
- -webkit-tap-highlight-color: transparent; /* No gray tap box */
1336
- z-index: 9999;
1337
- overflow: hidden; /* Canvas containment */
1338
- }
1339
- canvas {
1340
- display: block;
1341
- width: 100%;
1342
- height: 100%;
1343
- touch-action: none; /* Extra safety */
1344
- }
1345
- `,
1346
- children: /* @__PURE__ */ jsxDEV3("div", {
1347
- ref: containerRef,
1348
- className: "keypad-wrapper",
1349
- onFocus: () => {
1350
- if (focusId)
1351
- onFocus(focusId);
1352
- },
1353
- onBlur,
1354
- onContextMenu: (e) => e.preventDefault(),
1355
- onPointerDown: (e) => e.preventDefault(),
1356
- onClickCapture: (e) => {
1357
- e.preventDefault();
1358
- e.stopPropagation();
1359
- },
1360
- style: {
1361
- left: viewport.offsetLeft,
1362
- width: viewport.width,
1363
- top: Math.round(viewport.offsetTop + viewport.height - 200 / viewport.scale),
1364
- height: Math.round(200 / viewport.scale),
1365
- "--scale-factor": viewport.scale
1366
- },
1367
- children: /* @__PURE__ */ jsxDEV3("canvas", {
1368
- ref: canvasRef,
1369
- onPointerDown: handlePointerDown,
1370
- onPointerMove: handlePointerMove,
1371
- onPointerUp: handlePointerUp,
1372
- onPointerLeave: handlePointerUp,
1373
- onPointerCancel: handlePointerCancel
1374
- }, undefined, false, undefined, this)
1371
+ :host {
1372
+ display: block;
1373
+ }
1374
+ canvas {
1375
+ display: block;
1376
+ width: 100%;
1377
+ height: 100%;
1378
+ touch-action: none;
1379
+ }
1380
+ `,
1381
+ children: /* @__PURE__ */ jsxDEV3("canvas", {
1382
+ ref: canvasRef,
1383
+ onPointerDown: handlePointerDown,
1384
+ onPointerMove: handlePointerMove,
1385
+ onPointerUp: handlePointerUp,
1386
+ onPointerLeave: handlePointerUp,
1387
+ onPointerCancel: handlePointerCancel,
1388
+ style: { width: "100%", height: "100%", display: "block" }
1375
1389
  }, undefined, false, undefined, this)
1376
- }, undefined, false, undefined, this);
1390
+ }, undefined, false, undefined, this), document.body);
1377
1391
  }
1378
1392
 
1379
1393
  // src/hooks/useStorage.ts
@@ -1536,32 +1550,75 @@ var useVisualViewport = () => {
1536
1550
  return viewport;
1537
1551
  };
1538
1552
 
1553
+ // src/hooks/useSystemTheme.ts
1554
+ import { useState as useState5, useEffect as useEffect7 } from "react";
1555
+ function useSystemTheme() {
1556
+ const [theme, setTheme] = useState5(() => {
1557
+ if (typeof window !== "undefined" && window.matchMedia) {
1558
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
1559
+ }
1560
+ return "light";
1561
+ });
1562
+ useEffect7(() => {
1563
+ if (typeof window === "undefined" || !window.matchMedia)
1564
+ return;
1565
+ const mq = window.matchMedia("(prefers-color-scheme: dark)");
1566
+ const handler = (e) => {
1567
+ setTheme(e.matches ? "dark" : "light");
1568
+ };
1569
+ mq.addEventListener("change", handler);
1570
+ return () => mq.removeEventListener("change", handler);
1571
+ }, []);
1572
+ return theme;
1573
+ }
1574
+
1539
1575
  // src/components/Provider.tsx
1540
1576
  import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
1541
1577
  function VirtualInputProvider({
1542
1578
  children,
1543
1579
  layout = qwerty_default,
1544
- defaultHangulMode = true
1580
+ defaultHangulMode = true,
1581
+ theme
1545
1582
  }) {
1546
1583
  const inputRef = useRef5(null);
1547
1584
  const sti = useRef5(setTimeout(() => null, 0));
1548
1585
  const isCompositionRef = useRef5(undefined);
1549
- const [focusId, setFocusId] = useState5();
1550
- const [shift, setShift] = useState5(false);
1586
+ const [focusId, setFocusId] = useState6();
1587
+ const [shift, setShift] = useState6(false);
1551
1588
  const [hangulMode, setHangulMode] = useStorage("virtual-keyboard-hangul-mode", defaultHangulMode);
1552
1589
  const viewport = useVisualViewport();
1553
- useEffect7(() => {
1554
- if (focusId) {
1555
- document.body.style.paddingBottom = `${Math.round(200 / viewport.scale)}px`;
1590
+ const systemTheme = useSystemTheme();
1591
+ const effectiveTheme = theme ?? systemTheme;
1592
+ const focusedElementRef = useRef5(null);
1593
+ useEffect8(() => {
1594
+ if (focusId && focusedElementRef.current) {
1595
+ const paddingBottom = Math.round(200 / viewport.scale);
1596
+ document.body.style.paddingBottom = `${paddingBottom}px`;
1597
+ setTimeout(() => {
1598
+ const el = focusedElementRef.current;
1599
+ if (el) {
1600
+ const rect = el.getBoundingClientRect();
1601
+ const scrollTop = window.scrollY || document.documentElement.scrollTop;
1602
+ const elementTop = rect.top + scrollTop;
1603
+ const availableHeight = viewport.height - paddingBottom;
1604
+ const targetScroll = elementTop - availableHeight / 2 + rect.height / 2;
1605
+ window.scrollTo({
1606
+ top: targetScroll,
1607
+ behavior: "smooth"
1608
+ });
1609
+ }
1610
+ }, 100);
1556
1611
  } else {
1557
1612
  document.body.style.paddingBottom = "0px";
1558
1613
  }
1559
1614
  return () => {
1560
1615
  document.body.style.paddingBottom = "0px";
1561
1616
  };
1562
- }, [focusId, viewport.scale]);
1563
- const onFocus = (id) => {
1617
+ }, [focusId, viewport.scale, viewport.height, viewport.offsetTop]);
1618
+ const onFocus = (id, target) => {
1564
1619
  clearTimeout(sti.current);
1620
+ if (target)
1621
+ focusedElementRef.current = target;
1565
1622
  setFocusId(id);
1566
1623
  };
1567
1624
  const onBlur = useCallback6((e) => {
@@ -1589,16 +1646,17 @@ function VirtualInputProvider({
1589
1646
  }, [setHangulMode]);
1590
1647
  return /* @__PURE__ */ jsxDEV4(VirtualInputContext.Provider, {
1591
1648
  value: {
1592
- inputRef,
1593
- isCompositionRef,
1594
- onFocus,
1595
- onBlur,
1596
1649
  focusId,
1597
- setHangulMode,
1598
1650
  hangulMode,
1599
1651
  shift,
1652
+ theme: effectiveTheme,
1653
+ onFocus,
1654
+ onBlur,
1655
+ setHangulMode,
1600
1656
  toggleShift,
1601
- toggleKorean
1657
+ toggleKorean,
1658
+ isCompositionRef,
1659
+ inputRef
1602
1660
  },
1603
1661
  children: [
1604
1662
  children,
@@ -3,12 +3,13 @@ import type { VirtualInputHandle } from "./Input";
3
3
  interface VirtualInputContextValue {
4
4
  inputRef: RefObject<VirtualInputHandle | null>;
5
5
  isCompositionRef: RefObject<boolean | undefined>;
6
- onFocus: (id: string) => void;
6
+ onFocus: (id: string, target?: HTMLElement | null) => void;
7
7
  onBlur: (e?: React.FocusEvent | boolean) => void;
8
8
  focusId: string | undefined;
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
  }
@@ -3,4 +3,4 @@ export type { KeypadLayout, Viewport };
3
3
  export declare function VirtualKeypad({ layout, viewport, }: {
4
4
  layout: KeypadLayout;
5
5
  viewport: Viewport;
6
- }): import("react/jsx-runtime").JSX.Element;
6
+ }): import("react").ReactPortal;
@@ -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;
@@ -4,8 +4,9 @@
4
4
  * @param css 스타일을 문자열로 받아서 <style>로 삽입
5
5
  * @returns Shadow DOM 안에 children과 style을 렌더링
6
6
  */
7
- export declare const ShadowWrapper: import("react").MemoExoticComponent<({ children, css, tagName, }: {
7
+ export declare const ShadowWrapper: import("react").MemoExoticComponent<(props: {
8
8
  children: React.ReactNode;
9
9
  css?: string;
10
- tagName: "input" | "div";
11
- }) => import("react/jsx-runtime").JSX.Element>;
10
+ tagName: string;
11
+ hostRef?: React.MutableRefObject<HTMLElement | null>;
12
+ } & React.HTMLAttributes<HTMLElement>) => 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.5",
3
+ "version": "0.5.7",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",