react-led-digit 0.0.16 → 0.0.17

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Switches intrnal state periodically (visible), calling subscribers each time.
2
+ * Switches internal state periodically (visible), calling subscribers each time.
3
3
  */
4
4
  type BlinkerSubscriber = (state: BlinkerState['visible']) => void;
5
5
  type BlinkerState = {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /**
3
- * Switches intrnal state periodically (visible), calling subscribers each time.
3
+ * Switches internal state periodically (visible), calling subscribers each time.
4
4
  */
5
5
  var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
6
6
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
@@ -81,7 +81,7 @@ class Blinker {
81
81
  }
82
82
  start(visible = __classPrivateFieldGet(this, _Blinker_state, "f").visible) {
83
83
  if (__classPrivateFieldGet(this, _Blinker_state, "f").timeout)
84
- return; // already runnning
84
+ this.stop(); // restart with new params
85
85
  visible ? __classPrivateFieldGet(this, _Blinker_instances, "m", _Blinker_on).call(this) : __classPrivateFieldGet(this, _Blinker_instances, "m", _Blinker_off).call(this); // start blinking
86
86
  }
87
87
  stop() {
@@ -97,6 +97,8 @@ class Blinker {
97
97
  }
98
98
  unsubscribe(fn) {
99
99
  __classPrivateFieldGet(this, _Blinker_state, "f").subscribers = __classPrivateFieldGet(this, _Blinker_state, "f").subscribers.filter(i => i !== fn);
100
+ if (__classPrivateFieldGet(this, _Blinker_state, "f").subscribers.length === 0)
101
+ this.stop();
100
102
  }
101
103
  }
102
104
  exports.Blinker = Blinker;
@@ -24,7 +24,7 @@ const Digit = (_a) => {
24
24
  const scopeAttribute = (0, useStyleInjector_1.default)(digit_css_generated_js_1.default.content, [], {
25
25
  scopeID: digit_css_generated_js_1.default.hash,
26
26
  });
27
- if (Object.entries(scopeAttribute)[0].includes(true))
27
+ if (Object.values(scopeAttribute).some(v => v === true))
28
28
  return ((0, jsx_runtime_1.jsx)("div", Object.assign({}, scopeAttribute, { style: { display: 'contents' }, children: (0, jsx_runtime_1.jsx)(UnstyledDigit_1.Digit, Object.assign({}, rest)) })));
29
29
  return (0, jsx_runtime_1.jsx)(UnstyledDigit_1.Digit, Object.assign({}, rest));
30
30
  };
@@ -61,13 +61,14 @@ const charset = new Set(Object.keys(_1.charToSevenSegments));
61
61
  function setCharAt(str, index, chr) {
62
62
  if (index > str.length - 1)
63
63
  return str;
64
- return str.substr(0, index) + chr + str.substr(index + 1);
64
+ return str.substring(0, index) + chr + str.substring(index + 1);
65
65
  }
66
66
  const BlinkingDigitTest = () => {
67
67
  const input = (0, react_1.useRef)(null);
68
68
  const [dgts, setDgts] = (0, react_1.useState)('');
69
69
  const addDigit = () => {
70
- if (input.current.value) {
70
+ var _a;
71
+ if ((_a = input.current) === null || _a === void 0 ? void 0 : _a.value) {
71
72
  setDgts(prev => prev + input.current.value);
72
73
  }
73
74
  };
@@ -76,7 +77,7 @@ const BlinkingDigitTest = () => {
76
77
  setDgts(next);
77
78
  };
78
79
  const handleChange = () => {
79
- if (!charset.has(input.current.value)) {
80
+ if (input.current && !charset.has(input.current.value)) {
80
81
  input.current.value = '';
81
82
  }
82
83
  };
@@ -142,11 +143,11 @@ const Display = (_a) => {
142
143
  const getTime = () => new Date().toLocaleTimeString();
143
144
  const SimpleClock = () => {
144
145
  const [time, setTime] = (0, react_1.useState)(getTime());
145
- const blinker = new __1.Blinker();
146
- blinker.period = 2000;
147
- blinker.ratio = 1;
148
- const handleBlinkerChange = () => setTime(getTime());
149
146
  (0, react_1.useEffect)(() => {
147
+ const blinker = new __1.Blinker();
148
+ blinker.period = 2000;
149
+ blinker.ratio = 1;
150
+ const handleBlinkerChange = () => setTime(getTime());
150
151
  blinker.subscribe(handleBlinkerChange);
151
152
  return () => blinker.unsubscribe(handleBlinkerChange);
152
153
  }, []);
@@ -33,7 +33,7 @@ const Digit = (_a) => {
33
33
  return (0, jsx_runtime_1.jsx)(components_1.AmpmSegments, Object.assign({ "aria-label": v }, rest, segments, { style: sx }));
34
34
  if (type === 'dot')
35
35
  return (0, jsx_runtime_1.jsx)(components_1.DotSegments, Object.assign({ "aria-label": v }, rest, segments, { style: sx }));
36
- console.warn(`(at Digit.tsx) incompatible value: ${value.toString()}`);
36
+ console.warn(`(at UnstyledDigit.tsx) incompatible value: ${value.toString()}`);
37
37
  return ((0, jsx_runtime_1.jsx)("div", Object.assign({ "aria-label": v }, rest, { className: (0, clsx_1.default)('digit unknown', rest.className) })));
38
38
  };
39
39
  exports.Digit = Digit;
@@ -51,7 +51,8 @@ function valueToType(v) {
51
51
  return undefined;
52
52
  }
53
53
  function valueToSegments(v) {
54
- const str = v === null || v === void 0 ? void 0 : v.toString();
54
+ var _a;
55
+ const str = (_a = v === null || v === void 0 ? void 0 : v.toString()) !== null && _a !== void 0 ? _a : '';
55
56
  if (str.toLowerCase() === 'am')
56
57
  return { AM: true };
57
58
  if (str.toLowerCase() === 'pm')
@@ -41,7 +41,7 @@ class StyleInjector {
41
41
  return StyleInjector.instance; // singleton
42
42
  }
43
43
  increase(s) {
44
- if (this.count(s) === 0)
44
+ if (this.count(s) === 0 && typeof document !== 'undefined')
45
45
  document.head.appendChild(createStyleElement(s));
46
46
  if (this.count(s) >= 0)
47
47
  __classPrivateFieldGet(this, _StyleInjector_styles, "f").set(s.id, this.count(s) + 1);
@@ -50,7 +50,7 @@ class StyleInjector {
50
50
  var _a;
51
51
  if (this.count(s) > 0)
52
52
  __classPrivateFieldGet(this, _StyleInjector_styles, "f").set(s.id, this.count(s) - 1);
53
- if (this.count(s) === 0)
53
+ if (this.count(s) === 0 && typeof document !== 'undefined')
54
54
  (_a = document.head.querySelector(`#${s.id}`)) === null || _a === void 0 ? void 0 : _a.remove();
55
55
  }
56
56
  count(s) {
@@ -69,16 +69,10 @@ const createStyleElement = (style) => {
69
69
  return el;
70
70
  };
71
71
  const wrapWithScope = (css, scopeAttribute) => {
72
- const scoped = // unused bc @scope at-rule does not work in Firefox
73
- `@scope ([${scopeAttribute}]) {` +
74
- `:scope { display: contents; }` +
75
- `${css}` +
76
- `}`;
77
- const isolated = `[${scopeAttribute}] { display: contents; }` +
72
+ return (`[${scopeAttribute}] { display: contents; }` +
78
73
  `[${scopeAttribute}] {` +
79
74
  `${css}` +
80
- `}`;
81
- return isolated;
75
+ `}`);
82
76
  };
83
77
  // https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
84
78
  const cyrb53string = (str, seed = 0) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-led-digit",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "react component for 7-segment display (digit), includes dot, colon and am-pm digits",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -36,6 +36,10 @@
36
36
  "engines": {
37
37
  "node": ">=12.0"
38
38
  },
39
+ "peerDependencies": {
40
+ "react": ">=16.8.0",
41
+ "react-dom": ">=16.8.0"
42
+ },
39
43
  "dependencies": {
40
44
  "clsx": "^2.1.1"
41
45
  },