@wwwares/keycaps 0.0.0 → 0.1.0
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 +42 -18
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, forwardRef, useContext,
|
|
1
|
+
import { createContext, forwardRef, useContext, useLayoutEffect, useRef } from "react";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
// src/config.ts
|
|
4
4
|
const PROFILES = [
|
|
@@ -830,6 +830,8 @@ var Renderer = class {
|
|
|
830
830
|
loc = {};
|
|
831
831
|
instances = /* @__PURE__ */ new Set();
|
|
832
832
|
byEl = /* @__PURE__ */ new Map();
|
|
833
|
+
/** Caps added since the last microtask draw. */
|
|
834
|
+
fresh = /* @__PURE__ */ new Set();
|
|
833
835
|
raf = 0;
|
|
834
836
|
lost = false;
|
|
835
837
|
legends = new LegendAtlas(() => this.invalidate());
|
|
@@ -860,8 +862,10 @@ var Renderer = class {
|
|
|
860
862
|
for (const e of entries) {
|
|
861
863
|
const inst = this.byEl.get(e.target);
|
|
862
864
|
if (!inst) continue;
|
|
863
|
-
|
|
864
|
-
inst.
|
|
865
|
+
const { inlineSize, blockSize } = e.borderBoxSize[0];
|
|
866
|
+
if (inlineSize === inst.width && blockSize === inst.height) continue;
|
|
867
|
+
inst.width = inlineSize;
|
|
868
|
+
inst.height = blockSize;
|
|
865
869
|
inst.dirty = true;
|
|
866
870
|
}
|
|
867
871
|
this.kick();
|
|
@@ -873,9 +877,12 @@ var Renderer = class {
|
|
|
873
877
|
this.io.observe(inst.el);
|
|
874
878
|
this.ro.observe(inst.el);
|
|
875
879
|
this.kick();
|
|
880
|
+
if (this.lost) inst.onReady(false);
|
|
881
|
+
else if (this.fresh.add(inst).size === 1) queueMicrotask(this.drawFresh);
|
|
876
882
|
}
|
|
877
883
|
remove(inst) {
|
|
878
884
|
this.instances.delete(inst);
|
|
885
|
+
this.fresh.delete(inst);
|
|
879
886
|
this.byEl.delete(inst.el);
|
|
880
887
|
this.io.unobserve(inst.el);
|
|
881
888
|
this.ro.unobserve(inst.el);
|
|
@@ -929,12 +936,22 @@ var Renderer = class {
|
|
|
929
936
|
}
|
|
930
937
|
frame = (now) => {
|
|
931
938
|
this.raf = 0;
|
|
939
|
+
if (this.draw(this.instances, now)) this.kick();
|
|
940
|
+
};
|
|
941
|
+
/** Draws new caps before the browser paints. The frame loop started by `add` keeps animated ones going. */
|
|
942
|
+
drawFresh = () => {
|
|
943
|
+
const insts = [...this.fresh];
|
|
944
|
+
this.fresh.clear();
|
|
945
|
+
this.draw(insts, performance.now());
|
|
946
|
+
};
|
|
947
|
+
/** Draws the caps in `insts` that need it. Returns whether any of them need another frame. */
|
|
948
|
+
draw(insts, now) {
|
|
932
949
|
const gl = this.gl;
|
|
933
|
-
if (!gl || this.lost || gl.isContextLost()) return;
|
|
950
|
+
if (!gl || this.lost || gl.isContextLost()) return false;
|
|
934
951
|
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
935
952
|
let again = false;
|
|
936
953
|
const jobs = [];
|
|
937
|
-
for (const inst of
|
|
954
|
+
for (const inst of insts) {
|
|
938
955
|
if (!inst.visible || inst.width === 0) continue;
|
|
939
956
|
const spinning = isSpinning(inst.spin);
|
|
940
957
|
if (inst.params.animated || spinning) again = true;
|
|
@@ -1007,8 +1024,8 @@ var Renderer = class {
|
|
|
1007
1024
|
}
|
|
1008
1025
|
}
|
|
1009
1026
|
}
|
|
1010
|
-
|
|
1011
|
-
}
|
|
1027
|
+
return again;
|
|
1028
|
+
}
|
|
1012
1029
|
drawTile(gl, job, x, y, age) {
|
|
1013
1030
|
const { loc } = this;
|
|
1014
1031
|
const p = job.inst.params;
|
|
@@ -1075,26 +1092,33 @@ function getRenderer() {
|
|
|
1075
1092
|
}
|
|
1076
1093
|
const isWebGLAvailable = () => getRenderer() !== null;
|
|
1077
1094
|
// src/mount.ts
|
|
1078
|
-
/** Shown
|
|
1095
|
+
/** Shown while the WebGL context is lost and when WebGL is unavailable. */
|
|
1079
1096
|
const FALLBACK = "var(--keycap-fallback, #d4d4d4)";
|
|
1080
|
-
/**
|
|
1097
|
+
/**
|
|
1098
|
+
* Renders a keycap into `host`, filling its box. The host needs a non-static position. The first
|
|
1099
|
+
* draw happens in a microtask, so a cap mounted before paint appears in that same paint.
|
|
1100
|
+
*/
|
|
1081
1101
|
function mountKeycap(host, options, { spinnable = false } = {}) {
|
|
1082
1102
|
const canvas = document.createElement("canvas");
|
|
1083
|
-
canvas.style.cssText =
|
|
1103
|
+
canvas.style.cssText = "position:absolute;inset:0;width:100%;height:100%;border-radius:18%";
|
|
1084
1104
|
host.append(canvas);
|
|
1085
1105
|
const renderer = getRenderer();
|
|
1086
|
-
if (!renderer)
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1106
|
+
if (!renderer) {
|
|
1107
|
+
canvas.style.background = FALLBACK;
|
|
1108
|
+
return {
|
|
1109
|
+
update() {},
|
|
1110
|
+
destroy: () => canvas.remove()
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1090
1113
|
const ctx = canvas.getContext("2d");
|
|
1114
|
+
const { width, height } = host.getBoundingClientRect();
|
|
1091
1115
|
const inst = {
|
|
1092
1116
|
el: host,
|
|
1093
1117
|
canvas,
|
|
1094
1118
|
ctx,
|
|
1095
1119
|
params: prepare(options),
|
|
1096
|
-
width
|
|
1097
|
-
height
|
|
1120
|
+
width,
|
|
1121
|
+
height,
|
|
1098
1122
|
visible: true,
|
|
1099
1123
|
dirty: true,
|
|
1100
1124
|
drawn: false,
|
|
@@ -1171,7 +1195,7 @@ const Keycap = forwardRef(function Keycap({ size = 48, preset, label, frozen = f
|
|
|
1171
1195
|
const key = JSON.stringify(options);
|
|
1172
1196
|
const host = useRef(null);
|
|
1173
1197
|
const handle = useRef(null);
|
|
1174
|
-
|
|
1198
|
+
useLayoutEffect(() => {
|
|
1175
1199
|
const h = mountKeycap(host.current, {}, { spinnable });
|
|
1176
1200
|
handle.current = h;
|
|
1177
1201
|
return () => {
|
|
@@ -1179,7 +1203,7 @@ const Keycap = forwardRef(function Keycap({ size = 48, preset, label, frozen = f
|
|
|
1179
1203
|
handle.current = null;
|
|
1180
1204
|
};
|
|
1181
1205
|
}, [spinnable]);
|
|
1182
|
-
|
|
1206
|
+
useLayoutEffect(() => handle.current?.update(JSON.parse(key)), [key, spinnable]);
|
|
1183
1207
|
const setHost = (node) => {
|
|
1184
1208
|
host.current = node;
|
|
1185
1209
|
if (typeof ref === "function") ref(node);
|