mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
made colorfull lab nodes
This commit is contained in:
parent
ba4bf28834
commit
779012a418
6 changed files with 43 additions and 15 deletions
|
@ -5,6 +5,7 @@ import styles from './styles.module.scss';
|
||||||
import { LabBottomPanel } from '~/components/lab/LabBottomPanel';
|
import { LabBottomPanel } from '~/components/lab/LabBottomPanel';
|
||||||
import { isAfter, parseISO } from 'date-fns';
|
import { isAfter, parseISO } from 'date-fns';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
import { useColorGradientFromString } from '~/utils/hooks/useColorGradientFromString';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
node: INode;
|
node: INode;
|
||||||
|
@ -22,8 +23,10 @@ const LabNode: FC<IProps> = ({ node, isLoading, lastSeen, commentCount }) => {
|
||||||
[node.commented_at, lastSeen]
|
[node.commented_at, lastSeen]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const background = useColorGradientFromString(node.is_heroic ? node.title : '', 3, 2);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(styles.wrap, { [styles.heroic]: node.is_heroic })}>
|
<div className={classNames(styles.wrap)} style={{ background }}>
|
||||||
{lab}
|
{lab}
|
||||||
<LabBottomPanel
|
<LabBottomPanel
|
||||||
node={node}
|
node={node}
|
||||||
|
|
|
@ -6,6 +6,9 @@ import { PRESETS, URLS } from '~/constants/urls';
|
||||||
import { RouteComponentProps, withRouter } from 'react-router';
|
import { RouteComponentProps, withRouter } from 'react-router';
|
||||||
import { getURL, stringToColour } from '~/utils/dom';
|
import { getURL, stringToColour } from '~/utils/dom';
|
||||||
import { Avatar } from '~/components/common/Avatar';
|
import { Avatar } from '~/components/common/Avatar';
|
||||||
|
import { normalizeBrightColor } from '~/utils/color';
|
||||||
|
import { adjustHue } from 'color2k';
|
||||||
|
import { useColorGradientFromString } from '~/utils/hooks/useColorGradientFromString';
|
||||||
|
|
||||||
type IProps = RouteComponentProps & {
|
type IProps = RouteComponentProps & {
|
||||||
item: Partial<INode>;
|
item: Partial<INode>;
|
||||||
|
@ -37,10 +40,8 @@ const NodeRelatedItemUnconnected: FC<IProps> = memo(({ item, history }) => {
|
||||||
() => (item.thumbnail ? getURL({ url: item.thumbnail }, PRESETS.avatar) : ''),
|
() => (item.thumbnail ? getURL({ url: item.thumbnail }, PRESETS.avatar) : ''),
|
||||||
[item]
|
[item]
|
||||||
);
|
);
|
||||||
const backgroundColor = useMemo(
|
|
||||||
() => (!thumb && item.title && stringToColour(item.title)) || '',
|
const background = useColorGradientFromString(!thumb ? item.title : '');
|
||||||
[]
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ref.current) return;
|
if (!ref.current) return;
|
||||||
|
@ -76,13 +77,13 @@ const NodeRelatedItemUnconnected: FC<IProps> = memo(({ item, history }) => {
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{!item.thumbnail && size === 'small' && (
|
{!item.thumbnail && size === 'small' && (
|
||||||
<div className={styles.letters} style={{ backgroundColor }}>
|
<div className={styles.letters} style={{ background }}>
|
||||||
{getTitleLetters(item.title)}
|
{getTitleLetters(item.title)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!item.thumbnail && size !== 'small' && (
|
{!item.thumbnail && size !== 'small' && (
|
||||||
<div className={styles.title} style={{ backgroundColor }}>
|
<div className={styles.title} style={{ background }}>
|
||||||
{item.title}
|
{item.title}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -43,11 +43,6 @@ div.thumb {
|
||||||
font: $font_24_semibold;
|
font: $font_24_semibold;
|
||||||
color: transparentize(white, 0.5);
|
color: transparentize(white, 0.5);
|
||||||
border-radius: $cell_radius;
|
border-radius: $cell_radius;
|
||||||
background-image: linear-gradient(
|
|
||||||
180deg,
|
|
||||||
transparentize($content_bg, 0.4),
|
|
||||||
transparentize($content_bg, 0.4)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.title {
|
.title {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { darken, desaturate, parseToHsla } from 'color2k';
|
import { darken, desaturate, parseToHsla } from 'color2k';
|
||||||
import { DEFAULT_DOMINANT_COLOR } from '~/constants/node';
|
import { DEFAULT_DOMINANT_COLOR } from '~/constants/node';
|
||||||
|
|
||||||
export const normalizeBrightColor = (color?: string) => {
|
export const normalizeBrightColor = (color?: string, saturationExp = 3, lightnessExp = 3) => {
|
||||||
if (!color) {
|
if (!color) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,6 @@ export const normalizeBrightColor = (color?: string) => {
|
||||||
const saturation = hsla[1];
|
const saturation = hsla[1];
|
||||||
const lightness = hsla[2];
|
const lightness = hsla[2];
|
||||||
|
|
||||||
const desaturated = desaturate(color, saturation ** 3);
|
const desaturated = desaturate(color, saturation ** saturationExp);
|
||||||
return darken(desaturated, lightness ** 3);
|
return darken(desaturated, lightness ** lightnessExp);
|
||||||
};
|
};
|
||||||
|
|
10
src/utils/hooks/useColorFromString.ts
Normal file
10
src/utils/hooks/useColorFromString.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
import { normalizeBrightColor } from '~/utils/color';
|
||||||
|
import { stringToColour } from '~/utils/dom';
|
||||||
|
|
||||||
|
export const useColorFromString = (val?: string, saturation = 3, lightness = 3) => {
|
||||||
|
return useMemo(
|
||||||
|
() => (val && normalizeBrightColor(stringToColour(val), saturation, lightness)) || '',
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
};
|
19
src/utils/hooks/useColorGradientFromString.ts
Normal file
19
src/utils/hooks/useColorGradientFromString.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
import { adjustHue } from 'color2k';
|
||||||
|
import { useColorFromString } from '~/utils/hooks/useColorFromString';
|
||||||
|
import { normalizeBrightColor } from '~/utils/color';
|
||||||
|
|
||||||
|
export const useColorGradientFromString = (val?: string, saturation = 3, lightness = 3) => {
|
||||||
|
const color = useColorFromString(val, saturation, lightness);
|
||||||
|
return useMemo(
|
||||||
|
() =>
|
||||||
|
val
|
||||||
|
? `linear-gradient(155deg, ${color}, ${normalizeBrightColor(
|
||||||
|
adjustHue(color, 80),
|
||||||
|
saturation,
|
||||||
|
lightness
|
||||||
|
)})`
|
||||||
|
: '',
|
||||||
|
[color]
|
||||||
|
);
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue