-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
German Shteinardt
committed
May 9, 2024
1 parent
45aba44
commit 9982f63
Showing
25 changed files
with
286 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!--GITHUB_BLOCK--> | ||
|
||
# useColorGenerator | ||
|
||
<!--/GITHUB_BLOCK--> | ||
|
||
```tsx | ||
import {useColorGenerator} from '@gravity-ui/uikit'; | ||
``` | ||
|
||
The `useColorGenerator` a hook that generates a unique (but consistent) background color based on some unique attribute (e.g., name, id, email). The background color remains unchanged with each update. | ||
|
||
## Properties | ||
|
||
| Name | Description | Type | Default | | | | ||
| :-------- | :----------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------- | :---------: | --- | --- | | ||
| mode | Value to control color saturation | `saturated` `unsaturated` `bright` | `saturated` | | ||
| token | Unique attribute of the entity (e.g., name, id, email) | `string` | | | | | ||
| colorKeys | If an array of colors is passed, an index is generated from the token passed, and the value from the color array at that index is returned | `string[]` | | | | | ||
|
||
## Result | ||
|
||
`useColorGenerator` returns an object with exactly two values: | ||
|
||
1. color - unique color from a token. | ||
2. textColor - text color (dark or light), ensurring higher contrast on generated color. |
12 changes: 12 additions & 0 deletions
12
src/hooks/useColorGenerator/__stories__/ColorGenerator.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@use '../../../components/variables.scss'; | ||
|
||
$block: '.#{variables.$ns}color-generator'; | ||
|
||
#{$block} { | ||
&__color-items { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 10px; | ||
margin-block-start: 20px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
|
||
import {Avatar} from '../../../components/Avatar'; | ||
import type {AvatarProps} from '../../../components/Avatar'; | ||
import type {UseColorGeneratorProps} from '../types'; | ||
import {useColorGenerator} from '../useColorGenerator'; | ||
|
||
type ColoredAvatarProps = AvatarProps & { | ||
withText: boolean; | ||
mode: UseColorGeneratorProps['mode']; | ||
token: UseColorGeneratorProps['token']; | ||
}; | ||
|
||
export const ColoredAvatar = ({ | ||
mode, | ||
theme, | ||
token, | ||
withText, | ||
...avatarProps | ||
}: ColoredAvatarProps) => { | ||
const {color, textColor} = useColorGenerator({ | ||
token, | ||
mode, | ||
}); | ||
|
||
return ( | ||
<Avatar | ||
{...avatarProps} | ||
theme={theme} | ||
text={withText ? token : undefined} | ||
color={withText ? textColor : undefined} | ||
title={color} | ||
backgroundColor={color} | ||
size="l" | ||
/> | ||
); | ||
}; |
69 changes: 69 additions & 0 deletions
69
src/hooks/useColorGenerator/__stories__/useColorGenerator.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react'; | ||
|
||
import type {Meta, StoryObj} from '@storybook/react'; | ||
|
||
import {Button} from '../../../components/Button'; | ||
import {block} from '../../../components/utils/cn'; | ||
import {randomString} from '../__tests__/utils/randomString'; | ||
|
||
import {ColoredAvatar} from './ColoredAvatar'; | ||
|
||
import './ColorGenerator.scss'; | ||
|
||
const b = block('color-generator'); | ||
|
||
const meta: Meta = { | ||
title: 'Hooks/useColorGenerator', | ||
argTypes: { | ||
mode: { | ||
options: ['unsaturated', 'saturated', 'bright'], | ||
control: {type: 'radio'}, | ||
}, | ||
withText: { | ||
control: 'boolean', | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof ColoredAvatar>; | ||
|
||
const initValues = () => { | ||
const result = Array.from({length: 10}, () => randomString(16)); | ||
|
||
return result; | ||
}; | ||
|
||
const Template = (args: React.ComponentProps<typeof ColoredAvatar>) => { | ||
const {mode, withText} = args; | ||
const [tokens, setTokens] = React.useState<string[]>(initValues); | ||
|
||
const onClick = React.useCallback(() => { | ||
const newToken = randomString(16); | ||
setTokens((prev) => [newToken, ...prev]); | ||
}, []); | ||
|
||
return ( | ||
<React.Fragment> | ||
<Button title="generate color" onClick={onClick}> | ||
Generate color | ||
</Button> | ||
<div className={b('color-items')}> | ||
{tokens.map((token) => ( | ||
<ColoredAvatar key={token} token={token} mode={mode} withText={withText} /> | ||
))} | ||
</div> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export const Default: Story = { | ||
render: (args) => { | ||
return <Template {...args} />; | ||
}, | ||
args: { | ||
mode: 'unsaturated', | ||
withText: false, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {getHue} from '../utils'; // Подставьте правильное имя файла | ||
|
||
describe('getHue', () => { | ||
test('returns values within the range [0, 360)', () => { | ||
const MIN_HASH = -Math.pow(2, 31); | ||
const MAX_HASH = Math.pow(2, 31); | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
const hash = Math.random() * (MAX_HASH - MIN_HASH) + MIN_HASH; | ||
const hue = getHue(hash); | ||
|
||
expect(hue).toBeGreaterThanOrEqual(0); | ||
expect(hue).toBeLessThan(360); | ||
} | ||
|
||
const maxHue = getHue(MAX_HASH); | ||
const minHue = getHue(MIN_HASH); | ||
|
||
expect(maxHue).toBeGreaterThanOrEqual(0); | ||
expect(maxHue).toBeLessThan(360); | ||
expect(minHue).toBeGreaterThanOrEqual(0); | ||
expect(minHue).toBeLessThan(360); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {randomIndex} from '../utils'; | ||
|
||
import {randomString} from './utils/randomString'; | ||
|
||
describe('randomIndex', () => { | ||
test('returns numbers within the range [0, max)', () => { | ||
const MAX_VALUE = 500; | ||
|
||
for (let i = 0; i <= 1000; i++) { | ||
const token = randomString(16); | ||
const index = randomIndex(token, MAX_VALUE); | ||
|
||
expect(index).toBeGreaterThanOrEqual(0); | ||
expect(index).toBeLessThan(MAX_VALUE); | ||
} | ||
|
||
const zeroIndex = randomIndex('test', 0); | ||
expect(zeroIndex).toBe(0); | ||
|
||
const oneIndex = randomIndex('test', 1); | ||
expect(oneIndex).toBe(0); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ooks/useGeneratorColor/utils/constants.ts → src/hooks/useColorGenerator/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {useColorGenerator} from './useColorGenerator'; | ||
export type {UseColorGeneratorProps} from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.