@xylabs/react-identicon 3.1.7 → 3.2.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/neutral/components/Identicon.d.cts +14 -0
- package/dist/neutral/components/Identicon.d.cts.map +1 -0
- package/dist/neutral/components/Identicon.d.mts +14 -0
- package/dist/neutral/components/Identicon.d.mts.map +1 -0
- package/dist/neutral/components/Identicon.d.ts +14 -0
- package/dist/neutral/components/Identicon.d.ts.map +1 -0
- package/dist/neutral/components/index.d.cts +2 -0
- package/dist/neutral/components/index.d.cts.map +1 -0
- package/dist/neutral/components/index.d.mts +2 -0
- package/dist/neutral/components/index.d.mts.map +1 -0
- package/dist/neutral/components/index.d.ts +2 -0
- package/dist/neutral/components/index.d.ts.map +1 -0
- package/dist/neutral/index.cjs +103 -0
- package/dist/neutral/index.cjs.map +1 -0
- package/dist/neutral/index.d.cts +2 -0
- package/dist/neutral/index.d.cts.map +1 -0
- package/dist/neutral/index.d.mts +2 -0
- package/dist/neutral/index.d.mts.map +1 -0
- package/dist/neutral/index.d.ts +2 -0
- package/dist/neutral/index.d.ts.map +1 -0
- package/dist/neutral/index.js +70 -0
- package/dist/neutral/index.js.map +1 -0
- package/package.json +7 -7
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FlexBoxProps } from '@xylabs/react-flexbox';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export interface IdenticonProps extends FlexBoxProps {
|
|
4
|
+
bg?: string;
|
|
5
|
+
className?: string;
|
|
6
|
+
count?: number;
|
|
7
|
+
fg?: string;
|
|
8
|
+
iconPadding?: number;
|
|
9
|
+
palette?: string[];
|
|
10
|
+
size?: number;
|
|
11
|
+
value?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const Identicon: React.FC<IdenticonProps>;
|
|
14
|
+
//# sourceMappingURL=Identicon.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Identicon.d.ts","sourceRoot":"","sources":["../../../src/components/Identicon.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAW,MAAM,uBAAuB,CAAA;AAE7D,OAAO,KAA4B,MAAM,OAAO,CAAA;AAMhD,MAAM,WAAW,cAAe,SAAQ,YAAY;IAClD,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAoDD,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CAwB9C,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FlexBoxProps } from '@xylabs/react-flexbox';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export interface IdenticonProps extends FlexBoxProps {
|
|
4
|
+
bg?: string;
|
|
5
|
+
className?: string;
|
|
6
|
+
count?: number;
|
|
7
|
+
fg?: string;
|
|
8
|
+
iconPadding?: number;
|
|
9
|
+
palette?: string[];
|
|
10
|
+
size?: number;
|
|
11
|
+
value?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const Identicon: React.FC<IdenticonProps>;
|
|
14
|
+
//# sourceMappingURL=Identicon.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Identicon.d.ts","sourceRoot":"","sources":["../../../src/components/Identicon.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAW,MAAM,uBAAuB,CAAA;AAE7D,OAAO,KAA4B,MAAM,OAAO,CAAA;AAMhD,MAAM,WAAW,cAAe,SAAQ,YAAY;IAClD,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAoDD,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CAwB9C,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FlexBoxProps } from '@xylabs/react-flexbox';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export interface IdenticonProps extends FlexBoxProps {
|
|
4
|
+
bg?: string;
|
|
5
|
+
className?: string;
|
|
6
|
+
count?: number;
|
|
7
|
+
fg?: string;
|
|
8
|
+
iconPadding?: number;
|
|
9
|
+
palette?: string[];
|
|
10
|
+
size?: number;
|
|
11
|
+
value?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const Identicon: React.FC<IdenticonProps>;
|
|
14
|
+
//# sourceMappingURL=Identicon.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Identicon.d.ts","sourceRoot":"","sources":["../../../src/components/Identicon.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAW,MAAM,uBAAuB,CAAA;AAE7D,OAAO,KAA4B,MAAM,OAAO,CAAA;AAMhD,MAAM,WAAW,cAAe,SAAQ,YAAY;IAClD,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAoDD,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CAwB9C,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
Identicon: () => Identicon
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/components/Identicon.tsx
|
|
38
|
+
var import_react_flexbox = require("@xylabs/react-flexbox");
|
|
39
|
+
var import_md5 = __toESM(require("md5"), 1);
|
|
40
|
+
var import_react = require("react");
|
|
41
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
42
|
+
var range = (n, in_min, in_max, out_min, out_max) => {
|
|
43
|
+
return (n - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
44
|
+
};
|
|
45
|
+
var updateCanvas = (canvas, props) => {
|
|
46
|
+
const { value = "", size = 400, bg = "transparent", count = 5, palette, iconPadding = 0 } = props;
|
|
47
|
+
let { fg } = props;
|
|
48
|
+
const hash = (0, import_md5.default)(value);
|
|
49
|
+
const block = Math.floor(size / count);
|
|
50
|
+
const hashColor = hash.slice(0, 6);
|
|
51
|
+
const current = canvas.current;
|
|
52
|
+
if (!current) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (palette && palette.length > 0) {
|
|
56
|
+
const index = Math.floor(range(Number.parseInt(hash.slice(-3), 16), 0, 4095, 0, palette.length));
|
|
57
|
+
fg = palette[index];
|
|
58
|
+
}
|
|
59
|
+
current.width = block * count + iconPadding;
|
|
60
|
+
current.height = block * count + iconPadding;
|
|
61
|
+
const arr = [...hash].map((el) => {
|
|
62
|
+
const parsedEl = Number.parseInt(el, 16);
|
|
63
|
+
return parsedEl < 8 ? 0 : 1;
|
|
64
|
+
});
|
|
65
|
+
const map = [];
|
|
66
|
+
map[0] = map[4] = arr.slice(0, 5);
|
|
67
|
+
map[1] = map[3] = arr.slice(5, 10);
|
|
68
|
+
map[2] = arr.slice(10, 15);
|
|
69
|
+
const ctx = current.getContext("2d");
|
|
70
|
+
if (ctx) {
|
|
71
|
+
ctx.imageSmoothingEnabled = false;
|
|
72
|
+
ctx.clearRect(0, 0, current.width, current.height);
|
|
73
|
+
for (const [i, row] of map.entries()) {
|
|
74
|
+
for (const [j, el] of row.entries()) {
|
|
75
|
+
if (el) {
|
|
76
|
+
ctx.fillStyle = fg ?? "#" + hashColor;
|
|
77
|
+
ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding);
|
|
78
|
+
} else {
|
|
79
|
+
ctx.fillStyle = bg;
|
|
80
|
+
ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var Identicon = ({
|
|
87
|
+
size = 400,
|
|
88
|
+
className = "identicon",
|
|
89
|
+
bg,
|
|
90
|
+
count,
|
|
91
|
+
fg,
|
|
92
|
+
iconPadding,
|
|
93
|
+
palette,
|
|
94
|
+
value,
|
|
95
|
+
...props
|
|
96
|
+
}) => {
|
|
97
|
+
const canvas = (0, import_react.useRef)(null);
|
|
98
|
+
(0, import_react.useEffect)(() => {
|
|
99
|
+
updateCanvas(canvas, { bg, className, count, fg, iconPadding, palette, size, value });
|
|
100
|
+
});
|
|
101
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_flexbox.FlexRow, { ...props, children: value ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("canvas", { className, ref: canvas, style: { height: size, width: size } }) : null });
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/components/Identicon.tsx"],"sourcesContent":["export * from './components'\n","import { FlexBoxProps, FlexRow } from '@xylabs/react-flexbox'\nimport md5 from 'md5'\nimport React, { useEffect, useRef } from 'react'\n\nconst range = (n: number, in_min: number, in_max: number, out_min: number, out_max: number) => {\n return ((n - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min\n}\n\nexport interface IdenticonProps extends FlexBoxProps {\n bg?: string\n className?: string\n count?: number\n fg?: string\n iconPadding?: number\n palette?: string[]\n size?: number\n value?: string\n}\n\nconst updateCanvas = (canvas: React.RefObject<HTMLCanvasElement>, props: IdenticonProps) => {\n const { value = '', size = 400, bg = 'transparent', count = 5, palette, iconPadding = 0 } = props\n let { fg } = props\n const hash = md5(value)\n const block = Math.floor(size / count)\n const hashColor = hash.slice(0, 6)\n\n const current = canvas.current\n\n if (!current) {\n return\n }\n\n if (palette && palette.length > 0) {\n const index = Math.floor(range(Number.parseInt(hash.slice(-3), 16), 0, 4095, 0, palette.length))\n fg = palette[index]\n }\n\n current.width = block * count + iconPadding\n current.height = block * count + iconPadding\n const arr = [...hash].map((el) => {\n const parsedEl = Number.parseInt(el, 16)\n return parsedEl < 8 ? 0 : 1\n })\n\n const map = []\n\n map[0] = map[4] = arr.slice(0, 5)\n map[1] = map[3] = arr.slice(5, 10)\n map[2] = arr.slice(10, 15)\n\n const ctx = current.getContext('2d')\n if (ctx) {\n ctx.imageSmoothingEnabled = false\n ctx.clearRect(0, 0, current.width, current.height)\n\n for (const [i, row] of map.entries()) {\n for (const [j, el] of row.entries()) {\n if (el) {\n ctx.fillStyle = fg ?? '#' + hashColor\n ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding)\n } else {\n ctx.fillStyle = bg\n ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding)\n }\n }\n }\n }\n}\n\nexport const Identicon: React.FC<IdenticonProps> = ({\n size = 400,\n className = 'identicon',\n bg,\n count,\n fg,\n iconPadding,\n palette,\n value,\n ...props\n}) => {\n const canvas = useRef<HTMLCanvasElement>(null)\n\n useEffect(() => {\n updateCanvas(canvas, { bg, className, count, fg, iconPadding, palette, size, value })\n })\n\n return (\n <FlexRow {...props}>\n {value ?\n <canvas className={className} ref={canvas} style={{ height: size, width: size }} />\n : null}\n </FlexRow>\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,2BAAsC;AACtC,iBAAgB;AAChB,mBAAyC;AAuFjC;AArFR,IAAM,QAAQ,CAAC,GAAW,QAAgB,QAAgB,SAAiB,YAAoB;AAC7F,UAAS,IAAI,WAAW,UAAU,YAAa,SAAS,UAAU;AACpE;AAaA,IAAM,eAAe,CAAC,QAA4C,UAA0B;AAC1F,QAAM,EAAE,QAAQ,IAAI,OAAO,KAAK,KAAK,eAAe,QAAQ,GAAG,SAAS,cAAc,EAAE,IAAI;AAC5F,MAAI,EAAE,GAAG,IAAI;AACb,QAAM,WAAO,WAAAA,SAAI,KAAK;AACtB,QAAM,QAAQ,KAAK,MAAM,OAAO,KAAK;AACrC,QAAM,YAAY,KAAK,MAAM,GAAG,CAAC;AAEjC,QAAM,UAAU,OAAO;AAEvB,MAAI,CAAC,SAAS;AACZ;AAAA,EACF;AAEA,MAAI,WAAW,QAAQ,SAAS,GAAG;AACjC,UAAM,QAAQ,KAAK,MAAM,MAAM,OAAO,SAAS,KAAK,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,GAAG,QAAQ,MAAM,CAAC;AAC/F,SAAK,QAAQ,KAAK;AAAA,EACpB;AAEA,UAAQ,QAAQ,QAAQ,QAAQ;AAChC,UAAQ,SAAS,QAAQ,QAAQ;AACjC,QAAM,MAAM,CAAC,GAAG,IAAI,EAAE,IAAI,C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AO,EAAE,QAAQ,MAAM,OAAO,KAAK,GAAG,IACjF,MACJ;AAEJ;","names":["md5"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// src/components/Identicon.tsx
|
|
2
|
+
import { FlexRow } from "@xylabs/react-flexbox";
|
|
3
|
+
import md5 from "md5";
|
|
4
|
+
import { useEffect, useRef } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
var range = (n, in_min, in_max, out_min, out_max) => {
|
|
7
|
+
return (n - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
8
|
+
};
|
|
9
|
+
var updateCanvas = (canvas, props) => {
|
|
10
|
+
const { value = "", size = 400, bg = "transparent", count = 5, palette, iconPadding = 0 } = props;
|
|
11
|
+
let { fg } = props;
|
|
12
|
+
const hash = md5(value);
|
|
13
|
+
const block = Math.floor(size / count);
|
|
14
|
+
const hashColor = hash.slice(0, 6);
|
|
15
|
+
const current = canvas.current;
|
|
16
|
+
if (!current) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (palette && palette.length > 0) {
|
|
20
|
+
const index = Math.floor(range(Number.parseInt(hash.slice(-3), 16), 0, 4095, 0, palette.length));
|
|
21
|
+
fg = palette[index];
|
|
22
|
+
}
|
|
23
|
+
current.width = block * count + iconPadding;
|
|
24
|
+
current.height = block * count + iconPadding;
|
|
25
|
+
const arr = [...hash].map((el) => {
|
|
26
|
+
const parsedEl = Number.parseInt(el, 16);
|
|
27
|
+
return parsedEl < 8 ? 0 : 1;
|
|
28
|
+
});
|
|
29
|
+
const map = [];
|
|
30
|
+
map[0] = map[4] = arr.slice(0, 5);
|
|
31
|
+
map[1] = map[3] = arr.slice(5, 10);
|
|
32
|
+
map[2] = arr.slice(10, 15);
|
|
33
|
+
const ctx = current.getContext("2d");
|
|
34
|
+
if (ctx) {
|
|
35
|
+
ctx.imageSmoothingEnabled = false;
|
|
36
|
+
ctx.clearRect(0, 0, current.width, current.height);
|
|
37
|
+
for (const [i, row] of map.entries()) {
|
|
38
|
+
for (const [j, el] of row.entries()) {
|
|
39
|
+
if (el) {
|
|
40
|
+
ctx.fillStyle = fg ?? "#" + hashColor;
|
|
41
|
+
ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding);
|
|
42
|
+
} else {
|
|
43
|
+
ctx.fillStyle = bg;
|
|
44
|
+
ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var Identicon = ({
|
|
51
|
+
size = 400,
|
|
52
|
+
className = "identicon",
|
|
53
|
+
bg,
|
|
54
|
+
count,
|
|
55
|
+
fg,
|
|
56
|
+
iconPadding,
|
|
57
|
+
palette,
|
|
58
|
+
value,
|
|
59
|
+
...props
|
|
60
|
+
}) => {
|
|
61
|
+
const canvas = useRef(null);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
updateCanvas(canvas, { bg, className, count, fg, iconPadding, palette, size, value });
|
|
64
|
+
});
|
|
65
|
+
return /* @__PURE__ */ jsx(FlexRow, { ...props, children: value ? /* @__PURE__ */ jsx("canvas", { className, ref: canvas, style: { height: size, width: size } }) : null });
|
|
66
|
+
};
|
|
67
|
+
export {
|
|
68
|
+
Identicon
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/Identicon.tsx"],"sourcesContent":["import { FlexBoxProps, FlexRow } from '@xylabs/react-flexbox'\nimport md5 from 'md5'\nimport React, { useEffect, useRef } from 'react'\n\nconst range = (n: number, in_min: number, in_max: number, out_min: number, out_max: number) => {\n return ((n - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min\n}\n\nexport interface IdenticonProps extends FlexBoxProps {\n bg?: string\n className?: string\n count?: number\n fg?: string\n iconPadding?: number\n palette?: string[]\n size?: number\n value?: string\n}\n\nconst updateCanvas = (canvas: React.RefObject<HTMLCanvasElement>, props: IdenticonProps) => {\n const { value = '', size = 400, bg = 'transparent', count = 5, palette, iconPadding = 0 } = props\n let { fg } = props\n const hash = md5(value)\n const block = Math.floor(size / count)\n const hashColor = hash.slice(0, 6)\n\n const current = canvas.current\n\n if (!current) {\n return\n }\n\n if (palette && palette.length > 0) {\n const index = Math.floor(range(Number.parseInt(hash.slice(-3), 16), 0, 4095, 0, palette.length))\n fg = palette[index]\n }\n\n current.width = block * count + iconPadding\n current.height = block * count + iconPadding\n const arr = [...hash].map((el) => {\n const parsedEl = Number.parseInt(el, 16)\n return parsedEl < 8 ? 0 : 1\n })\n\n const map = []\n\n map[0] = map[4] = arr.slice(0, 5)\n map[1] = map[3] = arr.slice(5, 10)\n map[2] = arr.slice(10, 15)\n\n const ctx = current.getContext('2d')\n if (ctx) {\n ctx.imageSmoothingEnabled = false\n ctx.clearRect(0, 0, current.width, current.height)\n\n for (const [i, row] of map.entries()) {\n for (const [j, el] of row.entries()) {\n if (el) {\n ctx.fillStyle = fg ?? '#' + hashColor\n ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding)\n } else {\n ctx.fillStyle = bg\n ctx.fillRect(block * i + iconPadding, block * j + iconPadding, block - iconPadding, block - iconPadding)\n }\n }\n }\n }\n}\n\nexport const Identicon: React.FC<IdenticonProps> = ({\n size = 400,\n className = 'identicon',\n bg,\n count,\n fg,\n iconPadding,\n palette,\n value,\n ...props\n}) => {\n const canvas = useRef<HTMLCanvasElement>(null)\n\n useEffect(() => {\n updateCanvas(canvas, { bg, className, count, fg, iconPadding, palette, size, value })\n })\n\n return (\n <FlexRow {...props}>\n {value ?\n <canvas className={className} ref={canvas} style={{ height: size, width: size }} />\n : null}\n </FlexRow>\n )\n}\n"],"mappings":";AAAA,SAAuB,eAAe;AACtC,OAAO,SAAS;AAChB,SAAgB,WAAW,cAAc;AAuFjC;AArFR,IAAM,QAAQ,CAAC,GAAW,QAAgB,QAAgB,SAAiB,YAAoB;AAC7F,UAAS,IAAI,WAAW,UAAU,YAAa,SAAS,UAAU;AACpE;AAaA,IAAM,eAAe,CAAC,QAA4C,UAA0B;AAC1F,QAAM,EAAE,QAAQ,IAAI,OAAO,KAAK,KAAK,eAAe,QAAQ,GAAG,SAAS,cAAc,EAAE,IAAI;AAC5F,MAAI,EAAE,GAAG,IAAI;AACb,QAAM,OAAO,IAAI,KAAK;AACtB,QAAM,QAAQ,KAAK,MAAM,OAAO,KAAK;AACrC,QAAM,YAAY,KAAK,MAAM,GAAG,CAAC;AAEjC,QAAM,UAAU,OAAO;AAEvB,MAAI,CAAC,SAAS;AACZ;AAAA,EACF;AAEA,MAAI,WAAW,QAAQ,SAAS,GAAG;AACjC,UAAM,QAAQ,KAAK,MAAM,MAAM,OAAO,SAAS,KAAK,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,GAAG,QAAQ,MAAM,CAAC;AAC/F,SAAK,QAAQ,KAAK;AAAA,EACpB;AAEA,UAAQ,QAAQ,QAAQ,QAAQ;AAChC,UAAQ,SAAS,QAAQ,QAAQ;AACjC,QAAM,MAAM,CAAC,GAAG,IAAI,EAAE,IAAI,CAAC,OAAO;AAChC,UAAM,WAAW,OAAO,SAAS,IAAI,EAAE;AACvC,WAAO,WAAW,IAAI,IAAI;AAAA,EAC5B,CAAC;AAED,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
|
13
13
|
"packages/*"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@xylabs/react-flexbox": "
|
|
16
|
+
"@xylabs/react-flexbox": "^3.2.0",
|
|
17
17
|
"md5": "^2.3.0"
|
|
18
18
|
},
|
|
19
19
|
"description": "Common React library for all XY Labs projects that use React",
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/md5": "^2.3.5",
|
|
22
|
-
"@types/react": "^18.
|
|
23
|
-
"@xylabs/ts-scripts-yarn3": "^3.
|
|
24
|
-
"@xylabs/tsconfig-react": "^3.
|
|
25
|
-
"react": "^18.
|
|
26
|
-
"typescript": "^5.
|
|
22
|
+
"@types/react": "^18.3.3",
|
|
23
|
+
"@xylabs/ts-scripts-yarn3": "^3.11.12",
|
|
24
|
+
"@xylabs/tsconfig-react": "^3.11.12",
|
|
25
|
+
"react": "^18.3.1",
|
|
26
|
+
"typescript": "^5.5.3"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@mui/material": "^5",
|
|
@@ -73,6 +73,6 @@
|
|
|
73
73
|
"url": "https://github.com/xylabs/sdk-react.git"
|
|
74
74
|
},
|
|
75
75
|
"sideEffects": false,
|
|
76
|
-
"version": "3.
|
|
76
|
+
"version": "3.2.0",
|
|
77
77
|
"type": "module"
|
|
78
78
|
}
|