react-battery-level 0.1.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 HyeonGyu OH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # React Battery Level
2
+
3
+ ## Simple customizable battery component!
4
+
5
+ ## Features
6
+
7
+ - **SVG-Based**: Crisp and clean rendering at any size or resolution.
8
+ - **Dynamic Gauge**: Visual battery level represented by a customizable SVG fill.
9
+ - **Charging State**: Optional SVG lightning bolt icon for charging indication.
10
+ - **Customizable Appearance**: Configure the dimensions, color, and styles of the SVG elements.
11
+ - **Percentage Display**: Option to display the battery percentage text within the SVG.
12
+
13
+ <div align="center">
14
+ <img width="300px" src="/demo/demo1.gif">
15
+ <img width="300px" src="/demo/demo2.gif">
16
+ <img width="300px" src="/demo/demo3.gif">
17
+ </div>
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install react-battery-level --save
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```jsx
28
+ import { useState } from "react";
29
+ import BatteryLevel from "./BatteryLevel";
30
+
31
+ function App() {
32
+ const [gauge, setGauge] = useState(75);
33
+ return (
34
+ //Adjust only width to maintain aspect ratio.
35
+ <BatteryLevel
36
+ width="10vw"
37
+ gauge={gauge}
38
+ gaugeColor={gauge <= 20 ? "#FF5713" : "#6EF47A"}
39
+ isCharging={true}
40
+ isShowGaugePercentage={false}
41
+ lightningBoltStyles={{
42
+ fill: gauge <= 20 ? "#FF5713" : "#6EF47A",
43
+ stroke: "white",
44
+ strokeWidth: 0.5,
45
+ }}
46
+ />
47
+ );
48
+ }
49
+ export default App;
50
+ ```
51
+
52
+ ## Props
53
+
54
+ | Prop | Type | Default | Description |
55
+ | ----------------------- | -------------------- | ----------- | ------------------------------------------------------ |
56
+ | `width` | `number` \| `string` | `"100%"` | Optional. The width of the SVG battery icon. |
57
+ | `height` | `number` \| `string` | `"auto"` | Optional. The height of the SVG battery icon. |
58
+ | `gauge` | `number` | None | Required. The charge level of the battery (0-100). |
59
+ | `gaugeColor` | `string` | `"#6EF47A"` | Optional. The color of the battery fill. |
60
+ | `isCharging` | `boolean` | `false` | Required. Indicates if the battery is charging. |
61
+ | `isShowGaugePercentage` | `boolean` | `true` | Optional. Shows the battery percentage inside the SVG. |
62
+ | `lightningBoltStyles` | `object` | `{}` | Optional. Styling for the SVG lightning bolt. |
63
+ | `gaugePercentageStyles` | `object` | `{}` | Optional. Styling for the battery percentage text. |
64
+
65
+ <br>
66
+
67
+ ### `lightningBoltStyles` Object
68
+
69
+ | Key | Type | Default | Description |
70
+ | ------------- | -------------------- | --------- | ---------------------------------------- |
71
+ | `fill` | `string` | `"white"` | SVG fill color for the lightning bolt. |
72
+ | `stroke` | `string` | `"black"` | SVG stroke color for the lightning bolt. |
73
+ | `strokeWidth` | `number` \| `string` | `0.5` | SVG stroke width for the lightning bolt. |
74
+
75
+ ### `gaugePercentageStyles` Object
76
+
77
+ | Key | Type | Default | Description |
78
+ | ---------- | -------------------- | --------- | ------------------------------------------ |
79
+ | `fontSize` | `number` \| `string` | `7` | Font size for the battery percentage text. |
80
+ | `color` | `string` | `"black"` | Color for the battery percentage text. |
81
+
82
+ ## License
83
+
84
+ MIT
package/demo/demo1.gif ADDED
Binary file
package/demo/demo2.gif ADDED
Binary file
package/demo/demo3.gif ADDED
Binary file
@@ -0,0 +1,19 @@
1
+ type Props = {
2
+ width?: number | string;
3
+ height?: number | string;
4
+ gauge: number;
5
+ gaugeColor?: string;
6
+ isCharging: boolean;
7
+ isShowGaugePercentage: boolean;
8
+ lightningBoltStyles?: {
9
+ fill?: string;
10
+ stroke?: string;
11
+ strokeWidth?: string | number;
12
+ };
13
+ gaugePercentageStyles?: {
14
+ fontSize: string | number;
15
+ color: string;
16
+ };
17
+ };
18
+ declare const BatteryLevel: ({ gauge, width, height, isCharging, isShowGaugePercentage, gaugeColor, lightningBoltStyles, gaugePercentageStyles, }: Props) => import("react/jsx-runtime").JSX.Element;
19
+ export default BatteryLevel;
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
+ var BatteryLevel = function (_a) {
14
+ var gauge = _a.gauge, _b = _a.width, width = _b === void 0 ? "100%" : _b, _c = _a.height, height = _c === void 0 ? "100%" : _c, _d = _a.isCharging, isCharging = _d === void 0 ? false : _d, _e = _a.isShowGaugePercentage, isShowGaugePercentage = _e === void 0 ? true : _e, _f = _a.gaugeColor, gaugeColor = _f === void 0 ? "#6EF47A" : _f, _g = _a.lightningBoltStyles, lightningBoltStyles = _g === void 0 ? {
15
+ fill: "white",
16
+ stroke: "black",
17
+ strokeWidth: 0.5,
18
+ } : _g, _h = _a.gaugePercentageStyles, gaugePercentageStyles = _h === void 0 ? {
19
+ fontSize: 7,
20
+ color: "black",
21
+ } : _h;
22
+ var maxGaugeWidth = 73;
23
+ var batteryHeight = 73;
24
+ var gaugeWidth = gauge * (maxGaugeWidth / 100);
25
+ return (_jsxs("svg", __assign({ width: width, height: height, viewBox: "0 0 45 23", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, { children: [_jsx("path", { d: "M44.2102 11.4C44.2102 9.96999 43.2702 8.76999 41.9702 8.35999V14.43C43.2702 14.02 44.2102 12.82 44.2102 11.39V11.4Z", fill: "black" }), _jsx("path", { d: "M41.9702 14.93C41.8702 14.93 41.7602 14.9 41.6702 14.83C41.5402 14.74 41.4702 14.59 41.4702 14.43V8.35999C41.4702 8.19999 41.5502 8.04999 41.6702 7.95999C41.8002 7.86999 41.9702 7.83999 42.1202 7.88999C43.6702 8.37999 44.7102 9.78999 44.7102 11.4C44.7102 13.01 43.6702 14.42 42.1202 14.91C42.0702 14.93 42.0202 14.93 41.9702 14.93ZM42.4702 9.13999V13.65C43.2302 13.16 43.7102 12.32 43.7102 11.39C43.7102 10.46 43.2302 9.61999 42.4702 9.12999V9.13999Z", fill: "black" }), _jsx("path", { d: "M33.5 22.74H7.60995C3.94995 22.74 0.959961 19.76 0.959961 16.09V6.7C0.959961 3.04 3.93995 0.0500031 7.60995 0.0500031H33.5C37.16 0.0500031 40.1499 3.03 40.1499 6.7V16.09C40.1499 19.75 37.17 22.74 33.5 22.74ZM7.60995 2.56C5.31995 2.56 3.45996 4.42 3.45996 6.71V16.1C3.45996 18.39 5.31995 20.25 7.60995 20.25H33.5C35.79 20.25 37.6499 18.39 37.6499 16.1V6.71C37.6499 4.42 35.79 2.56 33.5 2.56H7.60995Z", fill: "black" }), _jsx("rect", { id: "4", x: "4.1", y: "3", width: gaugeWidth + "%", height: batteryHeight + "%", rx: "3", ry: "3", fill: gaugeColor }), isShowGaugePercentage && (_jsxs("text", __assign({ x: "20", y: "11", fontSize: gaugePercentageStyles.fontSize, color: gaugePercentageStyles.color, textAnchor: "middle", alignmentBaseline: "central", fill: "black" }, { children: [gauge, "%"] }))), isCharging && (_jsx("path", { d: "M27.6495 12.15L18.7595 7.23C18.7595 7.23 18.6696 7.21 18.6396 7.23C18.5996 7.25 18.5796 7.29 18.5796 7.34V10.3H11.5996C11.5396 10.3 11.4896 10.34 11.4796 10.39C11.4696 10.44 11.4895 10.5 11.5395 10.53L20.4295 15.45C20.4295 15.45 20.4696 15.47 20.4896 15.47C20.5096 15.47 20.5296 15.47 20.5496 15.45C20.5896 15.43 20.6096 15.39 20.6096 15.34V12.38H27.5896C27.6496 12.38 27.6995 12.34 27.7095 12.29C27.7195 12.24 27.6995 12.18 27.6495 12.15Z", fill: lightningBoltStyles.fill, stroke: lightningBoltStyles.stroke, strokeWidth: lightningBoltStyles.strokeWidth }))] })));
26
+ };
27
+ export default BatteryLevel;
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "react-battery-level",
3
+ "version": "0.1.3",
4
+ "private": false,
5
+ "description": "Simple customizable react battery component",
6
+ "main": "dist/index.js",
7
+ "typings": "dist/index.d.ts",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/rbrbrb7290/react-battery-level"
11
+ },
12
+ "dependencies": {
13
+ "@testing-library/jest-dom": "^5.17.0",
14
+ "@testing-library/react": "^13.4.0",
15
+ "@testing-library/user-event": "^13.5.0",
16
+ "@types/jest": "^27.5.2",
17
+ "@types/node": "^16.18.86",
18
+ "@types/react": "^18.2.63",
19
+ "@types/react-dom": "^18.2.20",
20
+ "react": "^18.2.0",
21
+ "react-dom": "^18.2.0",
22
+ "react-scripts": "5.0.1",
23
+ "typescript": "^4.9.5",
24
+ "web-vitals": "^2.1.4"
25
+ },
26
+ "license": "MIT",
27
+ "keywords": [
28
+ "react",
29
+ "battery",
30
+ "battery svg",
31
+ "battery indicator",
32
+ "battery level"
33
+ ],
34
+ "scripts": {
35
+ "start": "react-scripts start",
36
+ "build": "react-scripts build",
37
+ "test": "react-scripts test",
38
+ "eject": "react-scripts eject",
39
+ "build:npm": "tsc -p tsconfig.json"
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "demo/*.gif"
44
+ ],
45
+ "eslintConfig": {
46
+ "extends": [
47
+ "react-app",
48
+ "react-app/jest"
49
+ ]
50
+ },
51
+ "browserslist": {
52
+ "production": [
53
+ ">0.2%",
54
+ "not dead",
55
+ "not op_mini all"
56
+ ],
57
+ "development": [
58
+ "last 1 chrome version",
59
+ "last 1 firefox version",
60
+ "last 1 safari version"
61
+ ]
62
+ }
63
+ }