@xylabs/react-identicon 6.0.1 → 6.0.2

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.
@@ -14,7 +14,7 @@ var updateCanvas = /* @__PURE__ */ __name((canvas, props) => {
14
14
  const hash = md5(value);
15
15
  const block = Math.floor(size / count);
16
16
  const hashColor = hash.slice(0, 6);
17
- const current = canvas.current;
17
+ const current = canvas?.current;
18
18
  if (!current) {
19
19
  return;
20
20
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/components/Identicon.tsx"],"sourcesContent":["import type { FlexBoxProps } from '@xylabs/react-flexbox'\nimport { FlexRow } from '@xylabs/react-flexbox'\n// eslint-disable-next-line depend/ban-dependencies\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 {\n value = '', size = 400, bg = 'transparent', count = 5, palette, iconPadding = 0,\n } = 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, {\n bg, className, count, fg, iconPadding, palette, size, value,\n })\n })\n\n return (\n <FlexRow {...props}>\n {value\n ? (\n <canvas\n className={className}\n ref={canvas}\n style={{ height: size, width: size }}\n />\n )\n : null}\n </FlexRow>\n )\n}\n"],"mappings":";;;;AACA,SAASA,eAAe;AAExB,OAAOC,SAAS;AAChB,OAAOC,SAASC,WAAWC,cAAc;AAEzC,IAAMC,QAAQ,wBAACC,GAAWC,QAAgBC,QAAgBC,SAAiBC,YAAAA;AACzE,UAASJ,IAAIC,WAAWG,UAAUD,YAAaD,SAASD,UAAUE;AACpE,GAFc;AAed,IAAME,eAAe,wBAACC,QAA4CC,UAAAA;AAChE,QAAM,EACJC,QAAQ,IAAIC,OAAO,KAAKC,KAAK,eAAeC,QAAQ,GAAGC,SAASC,cAAc,EAAC,IAC7EN;AACJ,MAAI,EAAEO,GAAE,IAAKP;AACb,QAAMQ,OAAOC,IAAIR,KAAAA;AACjB,QAAMS,QAAQC,KAAKC,MAAMV,OAAOE,KAAAA;AAChC,QAAMS,YAAYL,KAAKM,MAAM,GAAG,CAAA;AAEhC,QAAMC,UAAUhB,OAAOgB;AAEvB,MAAI,CAACA,SAAS;AACZ;EACF;AAEA,MAAIV,WAAWA,QAAQW,SAAS,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BAAC,EAClDlC,OAAO,KACPmC,YAAY,aACZlC,IACAC,OACAG,IACAD,aACAD,SACAJ,OACA,GAAGD,MAAAA,MACJ;AACC,QAAMD,SAASuC,OAA0B,IAAA;AAEzCC,YAAU,MAAA;AACRzC,iBAAaC,QAAQ;MACnBI;MAAIkC;MAAWjC;MAAOG;MAAID;MAAaD;MAASH;MAAMD;IACxD,CAAA;EACF,CAAA;AAEA,SACE,sBAAA,cAACuC,SAAYxC,OACVC,QAEK,sBAAA,cAACF,UAAAA;IACCsC;IACAI,KAAK1C;IACL2C,OAAO;MAAErB,QAAQnB;MAAMkB,OAAOlB;IAAK;OAGvC,IAAA;AAGV,GAhCmD;","names":["FlexRow","md5","React","useEffect","useRef","range","n","in_min","in_max","out_min","out_max","updateCanvas","canvas","props","value","size","bg","count","palette","iconPadding","fg","hash","md5","block","Math","floor","hashColor","slice","current","length","index","Number","parseInt","width","height","arr","map","el","parsedEl","ctx","getContext","imageSmoothingEnabled","clearRect","i","row","entries","j","fillStyle","fillRect","Identicon","className","useRef","useEffect","FlexRow","ref","style"]}
1
+ {"version":3,"sources":["../../src/components/Identicon.tsx"],"sourcesContent":["import type { FlexBoxProps } from '@xylabs/react-flexbox'\nimport { FlexRow } from '@xylabs/react-flexbox'\n// eslint-disable-next-line depend/ban-dependencies\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 | null>, props: IdenticonProps) => {\n const {\n value = '', size = 400, bg = 'transparent', count = 5, palette, iconPadding = 0,\n } = 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, {\n bg, className, count, fg, iconPadding, palette, size, value,\n })\n })\n\n return (\n <FlexRow {...props}>\n {value\n ? (\n <canvas\n className={className}\n ref={canvas}\n style={{ height: size, width: size }}\n />\n )\n : null}\n </FlexRow>\n )\n}\n"],"mappings":";;;;AACA,SAASA,eAAe;AAExB,OAAOC,SAAS;AAChB,OAAOC,SAASC,WAAWC,cAAc;AAEzC,IAAMC,QAAQ,wBAACC,GAAWC,QAAgBC,QAAgBC,SAAiBC,YAAAA;AACzE,UAASJ,IAAIC,WAAWG,UAAUD,YAAaD,SAASD,UAAUE;AACpE,GAFc;AAed,IAAME,eAAe,wBAACC,QAAmDC,UAAAA;AACvE,QAA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,YAAIR,IAAI;AACNE,cAAIQ,YAAY3B,MAAM,MAAMM;AAC5Ba,cAAIS,SAASzB,QAAQoB,IAAIxB,aAAaI,QAAQuB,IAAI3B,aAAaI,QAAQJ,aAAaI,QAAQJ,WAAAA;QAC9F,OAAO;AACLoB,cAAIQ,YAAY/B;AAChBuB,cAAIS,SAASzB,QAAQoB,IAAIxB,aAAaI,QAAQuB,IAAI3B,aAAaI,QAAQJ,aAAaI,QAAQJ,WAAAA;QAC9F;MACF;IACF;EACF;AACF,GAlDqB;AAoDd,IAAM8B,YAAsC,wBAAC,EAClDlC,OAAO,KACPmC,YAAY,aACZlC,IACAC,OACAG,IACAD,aACAD,SACAJ,OACA,GAAGD,MAAAA,MACJ;AACC,QAAMD,SAASuC,OAA0B,IAAA;AAEzCC,YAAU,MAAA;AACRzC,iBAAaC,QAAQ;MACnBI;MAAIkC;MAAWjC;MAAOG;MAAID;MAAaD;MAASH;MAAMD;IACxD,CAAA;EACF,CAAA;AAEA,SACE,sBAAA,cAACuC,SAAYxC,OACVC,QAEK,sBAAA,cAACF,UAAAA;IACCsC;IACAI,KAAK1C;IACL2C,OAAO;MAAErB,QAAQnB;MAAMkB,OAAOlB;IAAK;OAGvC,IAAA;AAGV,GAhCmD;","names":["FlexRow","md5","React","useEffect","useRef","range","n","in_min","in_max","out_min","out_max","updateCanvas","canvas","props","value","size","bg","count","palette","iconPadding","fg","hash","md5","block","Math","floor","hashColor","slice","current","length","index","Number","parseInt","width","height","arr","map","el","parsedEl","ctx","getContext","imageSmoothingEnabled","clearRect","i","row","entries","j","fillStyle","fillRect","Identicon","className","useRef","useEffect","FlexRow","ref","style"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/react-identicon",
3
- "version": "6.0.1",
3
+ "version": "6.0.2",
4
4
  "description": "Common React library for all XY Labs projects that use React",
5
5
  "keywords": [
6
6
  "utility",
@@ -32,20 +32,20 @@
32
32
  "./package.json": "./package.json"
33
33
  },
34
34
  "module": "./dist/browser/index.mjs",
35
- "types": "dist/browser/index.d.ts",
35
+ "types": "dist/types/index.d.ts",
36
36
  "workspaces": [
37
37
  "packages/*"
38
38
  ],
39
39
  "dependencies": {
40
- "@xylabs/react-flexbox": "^6.0.1",
40
+ "@xylabs/react-flexbox": "^6.0.2",
41
41
  "md5": "^2.3.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@mui/material": "^6.4.7",
45
45
  "@types/md5": "^2.3.5",
46
46
  "@types/react": "^19.0.10",
47
- "@xylabs/ts-scripts-yarn3": "^6.0.5",
48
- "@xylabs/tsconfig-react": "^6.0.5",
47
+ "@xylabs/ts-scripts-yarn3": "^6.0.8",
48
+ "@xylabs/tsconfig-react": "^6.0.8",
49
49
  "react": "^19.0.0",
50
50
  "react-dom": "^19.0.0",
51
51
  "typescript": "^5.8.2"
@@ -19,7 +19,7 @@ export interface IdenticonProps extends FlexBoxProps {
19
19
  value?: string
20
20
  }
21
21
 
22
- const updateCanvas = (canvas: React.RefObject<HTMLCanvasElement>, props: IdenticonProps) => {
22
+ const updateCanvas = (canvas: React.RefObject<HTMLCanvasElement | null>, props: IdenticonProps) => {
23
23
  const {
24
24
  value = '', size = 400, bg = 'transparent', count = 5, palette, iconPadding = 0,
25
25
  } = props
@@ -28,7 +28,7 @@ const updateCanvas = (canvas: React.RefObject<HTMLCanvasElement>, props: Identic
28
28
  const block = Math.floor(size / count)
29
29
  const hashColor = hash.slice(0, 6)
30
30
 
31
- const current = canvas.current
31
+ const current = canvas?.current
32
32
 
33
33
  if (!current) {
34
34
  return