Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add identicon #158

Merged
merged 5 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const config = {
transformIgnorePatterns: ['node_modules/(?!minidenticons)'],
testEnvironment: 'jsdom',
};

module.exports = config;
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "0.0.3",
"type": "module",
"files": ["src", "presets"],
"jest": {
"testEnvironment": "jsdom"
},
"main": "src/index.ts",
"style": "src/global.css",
"scripts": {
Expand All @@ -22,6 +19,7 @@
"prepare": "husky install"
},
"dependencies": {
"minidenticons": "^3.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
Expand Down
14 changes: 14 additions & 0 deletions src/components/Identicon/Identicon.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Identicon } from './Identicon';

export default { title: 'Components/Identicon' };

export const _Identicon = {
render: () => (
<>
<Identicon size={32} username="test" />
<Identicon size={48} username="test2" saturation={70} />
<Identicon size={64} username="test3" lightness={20} />
<Identicon size={128} username="test4" saturation={95} lightness={30} />
</>
),
};
13 changes: 13 additions & 0 deletions src/components/Identicon/Identicon.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import { Identicon } from '.';

describe('Components | Fields | Identicon', () => {
test('it should render', () => {
render(<Identicon username="test" />);

let identicon = screen.getByTestId('identicon');

expect(identicon).toBeInTheDocument();
});
});
40 changes: 40 additions & 0 deletions src/components/Identicon/Identicon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import React from 'react';

import { ComponentPropsWithoutRef, useMemo } from 'react';

import { identicon } from 'minidenticons';

export interface IdenticonProps extends ComponentPropsWithoutRef<'img'> {
username: string;
size?: number;
saturation?: number;
lightness?: number;
mazmassa marked this conversation as resolved.
Show resolved Hide resolved
customClass?: string;
}

export function Identicon(props: IdenticonProps) {
const { username, size, saturation, lightness, customClass, ...rest } = props;
Thykof marked this conversation as resolved.
Show resolved Hide resolved

const computedSize = size?.toString() || '64';
Thykof marked this conversation as resolved.
Show resolved Hide resolved

const svgURI = useMemo(
() =>
'data:image/svg+xml;utf8,' +
encodeURIComponent(identicon(username, saturation, lightness)),
[username, saturation, lightness],
);
mazmassa marked this conversation as resolved.
Show resolved Hide resolved

return (
<img
data-testid="identicon"
className={`bg-neutral rounded-full ${customClass}`}
src={svgURI}
alt={username}
{...rest}
width={computedSize}
height={computedSize}
Thykof marked this conversation as resolved.
Show resolved Hide resolved
Thykof marked this conversation as resolved.
Show resolved Hide resolved
/>
);
}
1 change: 1 addition & 0 deletions src/components/Identicon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Identicon';