1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

colors for cards

This commit is contained in:
Fedor Katurov 2020-04-15 10:35:35 +07:00
parent d1d1848e07
commit 861fd8d9e3
3 changed files with 71 additions and 6 deletions

View file

@ -1,10 +1,10 @@
import React, { FC, memo, useCallback, useState } from 'react'; import React, { FC, memo, useCallback, useState, useMemo } from 'react';
import * as styles from './styles.scss'; import * as styles from './styles.scss';
import classNames from 'classnames'; import classNames from 'classnames';
import { INode } from '~/redux/types'; import { INode } from '~/redux/types';
import { URLS, PRESETS } from '~/constants/urls'; import { URLS, PRESETS } from '~/constants/urls';
import { RouteComponentProps, withRouter } from 'react-router'; import { RouteComponentProps, withRouter } from 'react-router';
import { getURL } from '~/utils/dom'; import { getURL, stringToColour } from '~/utils/dom';
type IProps = RouteComponentProps & { type IProps = RouteComponentProps & {
item: Partial<INode>; item: Partial<INode>;
@ -28,6 +28,15 @@ const NodeRelatedItemUnconnected: FC<IProps> = memo(({ item, history }) => {
const [is_loaded, setIsLoaded] = useState(false); const [is_loaded, setIsLoaded] = useState(false);
const onClick = useCallback(() => history.push(URLS.NODE_URL(item.id)), [item, history]); const onClick = useCallback(() => history.push(URLS.NODE_URL(item.id)), [item, history]);
const thumb = useMemo(
() => (item.thumbnail ? getURL({ url: item.thumbnail }, PRESETS.avatar) : ''),
[item]
);
const backgroundColor = useMemo(
() => (!thumb && item.title && stringToColour(item.title)) || '',
[]
);
return ( return (
<div <div
className={classNames(styles.item, { [styles.is_loaded]: is_loaded })} className={classNames(styles.item, { [styles.is_loaded]: is_loaded })}
@ -36,10 +45,16 @@ const NodeRelatedItemUnconnected: FC<IProps> = memo(({ item, history }) => {
> >
<div <div
className={styles.thumb} className={styles.thumb}
style={{ backgroundImage: `url("${getURL({ url: item.thumbnail }, PRESETS.avatar)}")` }} style={{
backgroundImage: `url("${thumb}")`,
}}
/> />
{!item.thumbnail && <div className={styles.letters}>{getTitleLetters(item.title)}</div>} {!item.thumbnail && (
<div className={styles.letters} style={{ backgroundColor }}>
{getTitleLetters(item.title)}
</div>
)}
<img <img
src={getURL({ url: item.thumbnail }, PRESETS.avatar)} src={getURL({ url: item.thumbnail }, PRESETS.avatar)}

View file

@ -40,6 +40,11 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
font: $font_24_semibold; font: $font_24_semibold;
// opacity: 0.2; color: white;
color: #444444; border-radius: $cell_radius;
background-image: linear-gradient(
180deg,
transparentize($content_bg, 0.4),
transparentize($content_bg, 0.4)
);
} }

View file

@ -170,3 +170,48 @@ export function plural(n: number, one: string, two: string, five: string) {
return `${n} ${five}`; return `${n} ${five}`;
} }
} }
export const stringToColour = (str: string) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let colour = '#';
for (let i = 0; i < 3; i++) {
let value = (hash >> (i * 8)) & 0xff;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
};
export const darken = (col: string, amt: number) => {
var usePound = false;
if (col[0] == '#') {
col = col.slice(1);
usePound = true;
}
var num = parseInt(col, 16);
var r = (num >> 16) + amt;
if (r > 255) r = 255;
else if (r < 0) r = 0;
var b = ((num >> 8) & 0x00ff) + amt;
if (b > 255) b = 255;
else if (b < 0) b = 0;
var g = (num & 0x0000ff) + amt;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16);
};